2023-09-12 06:50:01 +00:00
|
|
|
# cython: infer_types=True
|
2022-09-08 08:38:07 +00:00
|
|
|
|
|
|
|
from typing import Iterable
|
2023-06-14 15:48:41 +00:00
|
|
|
|
2022-09-08 08:38:07 +00:00
|
|
|
from .kb cimport KnowledgeBase
|
2023-06-14 15:48:41 +00:00
|
|
|
|
2022-09-08 08:38:07 +00:00
|
|
|
from ..tokens import Span
|
|
|
|
|
2023-06-14 15:48:41 +00:00
|
|
|
|
2022-09-08 08:38:07 +00:00
|
|
|
cdef class Candidate:
|
2023-07-19 10:03:31 +00:00
|
|
|
"""A `Candidate` object refers to a textual mention (`alias`) that may or
|
|
|
|
may not be resolved to a specific `entity` from a Knowledge Base. This
|
|
|
|
will be used as input for the entity linking algorithm which will
|
|
|
|
disambiguate the various candidates to the correct one.
|
2022-09-08 08:38:07 +00:00
|
|
|
Each candidate (alias, entity) pair is assigned a certain prior probability.
|
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/kb/#candidate-init
|
|
|
|
"""
|
|
|
|
|
2023-07-19 10:03:31 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
KnowledgeBase kb,
|
|
|
|
entity_hash,
|
|
|
|
entity_freq,
|
|
|
|
entity_vector,
|
|
|
|
alias_hash,
|
|
|
|
prior_prob
|
|
|
|
):
|
2022-09-08 08:38:07 +00:00
|
|
|
self.kb = kb
|
|
|
|
self.entity_hash = entity_hash
|
|
|
|
self.entity_freq = entity_freq
|
|
|
|
self.entity_vector = entity_vector
|
|
|
|
self.alias_hash = alias_hash
|
|
|
|
self.prior_prob = prior_prob
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity(self) -> int:
|
|
|
|
"""RETURNS (uint64): hash of the entity's KB ID/name"""
|
|
|
|
return self.entity_hash
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_(self) -> str:
|
|
|
|
"""RETURNS (str): ID/name of this entity in the KB"""
|
|
|
|
return self.kb.vocab.strings[self.entity_hash]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def alias(self) -> int:
|
|
|
|
"""RETURNS (uint64): hash of the alias"""
|
|
|
|
return self.alias_hash
|
|
|
|
|
|
|
|
@property
|
|
|
|
def alias_(self) -> str:
|
|
|
|
"""RETURNS (str): ID of the original alias"""
|
|
|
|
return self.kb.vocab.strings[self.alias_hash]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_freq(self) -> float:
|
|
|
|
return self.entity_freq
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_vector(self) -> Iterable[float]:
|
|
|
|
return self.entity_vector
|
|
|
|
|
|
|
|
@property
|
|
|
|
def prior_prob(self) -> float:
|
|
|
|
return self.prior_prob
|
|
|
|
|
|
|
|
|
|
|
|
def get_candidates(kb: KnowledgeBase, mention: Span) -> Iterable[Candidate]:
|
|
|
|
"""
|
2023-07-19 10:03:31 +00:00
|
|
|
Return candidate entities for a given mention and fetching appropriate
|
|
|
|
entries from the index.
|
2022-09-08 08:38:07 +00:00
|
|
|
kb (KnowledgeBase): Knowledge base to query.
|
|
|
|
mention (Span): Entity mention for which to identify candidates.
|
|
|
|
RETURNS (Iterable[Candidate]): Identified candidates.
|
|
|
|
"""
|
|
|
|
return kb.get_candidates(mention)
|
|
|
|
|
|
|
|
|
2023-07-19 10:03:31 +00:00
|
|
|
def get_candidates_batch(
|
|
|
|
kb: KnowledgeBase, mentions: Iterable[Span]
|
|
|
|
) -> Iterable[Iterable[Candidate]]:
|
2022-09-08 08:38:07 +00:00
|
|
|
"""
|
2023-07-19 10:03:31 +00:00
|
|
|
Return candidate entities for the given mentions and fetching appropriate entries
|
|
|
|
from the index.
|
2022-09-08 08:38:07 +00:00
|
|
|
kb (KnowledgeBase): Knowledge base to query.
|
|
|
|
mention (Iterable[Span]): Entity mentions for which to identify candidates.
|
|
|
|
RETURNS (Iterable[Iterable[Candidate]]): Identified candidates.
|
|
|
|
"""
|
|
|
|
return kb.get_candidates_batch(mentions)
|