其他

知识库

class
用于存储特定知识库(本体)中实体和别名的存储类

KnowledgeBase对象是一个抽象类,提供了一种生成Candidate对象的方法,这些对象是在给定特定文本提及时的合理外部标识符。每个这样的Candidate包含来自相关知识库实体的信息,例如其在文本中的出现频率和可能的别名。知识库中的每个实体还具有固定大小的预训练实体向量。

除此之外,KnowledgeBase类还需要实现一些由EntityLinker组件调用的实用函数。

KnowledgeBase.__init__ 方法

KnowledgeBase 是一个抽象类,无法直接实例化。其子类应调用 __init__() 方法来设置一些必要的属性。

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

KnowledgeBase.entity_vector_length 属性

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

名称描述

KnowledgeBase.get_candidates 方法

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

名称描述
mentionThe textual mention or alias. Span

KnowledgeBase.get_candidates_batch 方法

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

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

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

KnowledgeBase.get_alias_candidates 方法

从spaCy 3.5开始,KnowledgeBase成为一个抽象类(其中 InMemoryLookupKB作为直接替代品),以便在自定义知识库时提供更大的灵活性。在此重构过程中,它的一些方法被移至InMemoryLookupKB,其中包括get_alias_candidates()方法。该方法现在可通过 InMemoryLookupKB.get_alias_candidates()访问。 注意: InMemoryLookupKB.get_candidates() 默认调用 InMemoryLookupKB.get_alias_candidates()

KnowledgeBase.get_vector 方法

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

名称描述
entityThe entity ID. str

KnowledgeBase.get_vectors 方法

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

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

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

KnowledgeBase.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]

KnowledgeBase.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]

候选

一个Candidate对象指的是可能(也可能不)解析为KnowledgeBase中特定实体的文本提及(别名)。这将作为实体链接算法的输入,该算法会将各种候选对象消歧为正确的那个。每个候选(alias, entity)对被分配了特定的先验概率。

Candidate.__init__ 方法

构造一个Candidate对象。通常不会直接调用此构造函数,而是通过entity_linker管道的get_candidates方法返回这些对象。

名称描述
kbThe knowledge base that defined this candidate. KnowledgeBase
entity_hashThe hash of the entity’s KB ID. int
entity_freqThe entity frequency as recorded in the KB. float
alias_hashThe hash of the textual mention or alias. int
prior_probThe prior probability of the alias referring to the entity. float

候选属性

名称描述
entityThe entity’s unique KB identifier. int
entity_The entity’s unique KB identifier. str
aliasThe alias or textual mention. int
alias_The alias or textual mention. str
prior_probThe prior probability of the alias referring to the entity. long
entity_freqThe frequency of the entity in a typical corpus. long
entity_vectorThe pretrained vector of the entity. numpy.ndarray