知识库
KnowledgeBase对象是一个抽象类,提供了一种生成Candidate对象的方法,这些对象是在给定特定文本提及时的合理外部标识符。每个这样的Candidate包含来自相关知识库实体的信息,例如其在文本中的出现频率和可能的别名。知识库中的每个实体还具有固定大小的预训练实体向量。
除此之外,KnowledgeBase类还需要实现一些由EntityLinker组件调用的实用函数。
KnowledgeBase.__init__ 方法
KnowledgeBase 是一个抽象类,无法直接实例化。其子类应调用 __init__() 方法来设置一些必要的属性。
| 名称 | 描述 |
|---|---|
vocab | The shared vocabulary. Vocab |
entity_vector_length | Length of the fixed-size entity vectors. int |
KnowledgeBase.entity_vector_length 属性
知识库中固定大小实体向量的长度。
| 名称 | 描述 |
|---|---|
| 返回值 | 固定大小实体向量的长度。int |
KnowledgeBase.get_candidates 方法
给定某个文本提及作为输入,检索类型为Candidate的候选实体列表。
| 名称 | 描述 |
|---|---|
mention | The textual mention or alias. Span |
| RETURNS | An iterable of relevant Candidate objects. Iterable[Candidate] |
KnowledgeBase.get_candidates_batch 方法
与get_candidates()相同,但适用于任意数量的提及。EntityLinker组件将调用get_candidates_batch()而非get_candidates(),前提是配置参数candidates_batch_size的值大于或等于1。
get_candidates_batch()的默认实现会循环执行get_candidates()。如果性能对您很重要,我们建议实现一种更高效的方式来一次性检索多个提及的候选对象。
| 名称 | 描述 |
|---|---|
mentions | The textual mention or alias. Iterable[Span] |
| RETURNS | An iterable of iterable with relevant Candidate objects. Iterable[Iterable[Candidate]] |
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,检索其预训练的实体向量。
| 名称 | 描述 |
|---|---|
entity | The entity ID. str |
| 返回值 | 实体向量。 可迭代对象[浮点数] |
KnowledgeBase.get_vectors 方法
与get_vector()相同,但适用于任意数量的实体ID。
get_vectors()的默认实现是通过循环执行get_vector()。
如果您关注性能,我们建议实现一种更高效的方式来一次性检索多个实体的向量。
| 名称 | 描述 |
|---|---|
entities | The entity IDs. Iterable[str] |
| 返回值 | 实体向量。 可迭代[可迭代[numpy.ndarray]] |
KnowledgeBase.to_disk 方法
将知识库的当前状态保存到一个目录中。
| 名称 | 描述 |
|---|---|
path | A 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] |
exclude | List of components to exclude. Iterable[str] |
KnowledgeBase.from_disk 方法
从给定目录恢复知识库的状态。请注意,Vocab也应与创建知识库时使用的相同。
| 名称 | 描述 |
|---|---|
loc | A path to a directory. Paths may be either strings or Path-like objects. Union[str,Path] |
exclude | List of components to exclude. Iterable[str] |
| RETURNS | The modified KnowledgeBase object. KnowledgeBase |
候选 类
一个Candidate对象指的是可能(也可能不)解析为KnowledgeBase中特定实体的文本提及(别名)。这将作为实体链接算法的输入,该算法会将各种候选对象消歧为正确的那个。每个候选(alias, entity)对被分配了特定的先验概率。
Candidate.__init__ 方法
构造一个Candidate对象。通常不会直接调用此构造函数,而是通过entity_linker管道的get_candidates方法返回这些对象。
| 名称 | 描述 |
|---|---|
kb | The knowledge base that defined this candidate. KnowledgeBase |
entity_hash | The hash of the entity’s KB ID. int |
entity_freq | The entity frequency as recorded in the KB. float |
alias_hash | The hash of the textual mention or alias. int |
prior_prob | The prior probability of the alias referring to the entity. float |
候选属性
| 名称 | 描述 |
|---|---|
entity | The entity’s unique KB identifier. int |
entity_ | The entity’s unique KB identifier. str |
alias | The alias or textual mention. int |
alias_ | The alias or textual mention. str |
prior_prob | The prior probability of the alias referring to the entity. long |
entity_freq | The frequency of the entity in a typical corpus. long |
entity_vector | The pretrained vector of the entity. numpy.ndarray |