其他

InMemoryLookupKB

classv3.5
KnowledgeBase接口的默认实现。将所有信息存储在内存中。

InMemoryLookupKB类继承自KnowledgeBase并实现了其所有方法。它将所有知识库数据存储在内存中,并通过精确匹配提及与实体名称来生成Candidate对象。该实现高度优化,兼具低内存占用和快速检索的特点。

InMemoryLookupKB.__init__ 方法

创建知识库。

名称描述
vocabThe shared vocabulary. Vocab
entity_vector_lengthLength of the fixed-size entity vectors. int

InMemoryLookupKB.entity_vector_length 属性

知识库中固定大小实体向量的长度。

名称描述

InMemoryLookupKB.add_entity 方法

向知识库添加一个实体,指定其语料频率和实体向量,该向量的长度应为entity_vector_length

名称描述
entityThe unique entity identifier. str
freqThe frequency of the entity in a typical corpus. float
entity_vectorThe pretrained vector of the entity. numpy.ndarray

InMemoryLookupKB.set_entities 方法

定义知识库中的完整实体列表,为每个实体指定语料库频率和实体向量。

名称描述
entity_listList of unique entity identifiers. Iterable[Union[str, int]]
freq_listList of entity frequencies. Iterable[int]
vector_listList of entity vectors. Iterable[numpy.ndarray]

InMemoryLookupKB.add_alias 方法

向知识库添加别名或提及,指定其潜在的知识库标识符及其先验概率。实体标识符应引用先前通过add_entityset_entities添加的实体。先验概率的总和不应超过1。请注意,空字符串不能用作别名。

名称描述
aliasThe textual mention or alias. Can not be the empty string. str
entitiesThe potential entities that the alias may refer to. Iterable[Union[str, int]]
probabilitiesThe prior probabilities of each entity. Iterable[float]

InMemoryLookupKB.__len__ 方法

获取知识库中的实体总数。

名称描述

InMemoryLookupKB.get_entity_strings 方法

获取知识库中所有实体ID的列表。

名称描述

InMemoryLookupKB.get_size_aliases 方法

获取知识库中的别名总数。

名称描述

InMemoryLookupKB.get_alias_strings 方法

获取知识库中所有别名的列表。

名称描述

InMemoryLookupKB.get_candidates 方法

给定某个文本提及作为输入,检索类型为Candidate的候选实体列表。封装了get_alias_candidates()函数。

名称描述
mentionThe textual mention or alias. Span

InMemoryLookupKB.get_candidates_batch 方法

get_candidates()相同,但适用于任意数量的提及。当配置参数candidates_batch_size大于或等于1时,EntityLinker组件将调用get_candidates_batch()而非get_candidates()

get_candidates_batch()的默认实现会循环执行get_candidates()。如果性能对您很重要,我们建议实现一种更高效的方式来一次性检索多个提及的候选对象。

名称描述
mentionsThe textual mention or alias. Iterable[Span]

InMemoryLookupKB.get_alias_candidates 方法

给定某个文本提及作为输入,检索类型为Candidate的候选实体列表。

名称描述
aliasThe textual mention or alias. str

InMemoryLookupKB.get_vector 方法

给定某个实体ID,检索其预训练的实体向量。

名称描述
entityThe entity ID. str

InMemoryLookupKB.get_vectors 方法

get_vector()相同,但适用于任意数量的实体ID。

get_vectors()的默认实现是通过循环执行get_vector()。 如果您关注性能,我们建议实现一种更高效的方式来一次性检索多个实体的向量。

名称描述
entitiesThe entity IDs. Iterable[str]

InMemoryLookupKB.get_prior_prob 方法

给定某个实体ID和特定的文本提及,检索该提及链接到实体ID的先验概率。

名称描述
entityThe entity ID. str
aliasThe textual mention or alias. str

InMemoryLookupKB.to_disk 方法

将知识库的当前状态保存到一个目录中。

名称描述
pathA path to a directory, which will be created if it doesn’t exist. Paths may be either strings or Path-like objects. Union[str,Path]
excludeList of components to exclude. Iterable[str]

InMemoryLookupKB.from_disk 方法

从给定目录恢复知识库的状态。请注意,Vocab也应与创建知识库时使用的相同。

名称描述
locA path to a directory. Paths may be either strings or Path-like objects. Union[str,Path]
excludeList of components to exclude. Iterable[str]