spaCy/spacy/kb.pyx

146 lines
5.0 KiB
Cython
Raw Normal View History

2019-03-18 19:27:51 +03:00
# cython: profile=True
# coding: utf8
2019-03-22 18:55:05 +03:00
from spacy.errors import Errors, Warnings, user_warning
cdef class Candidate:
2019-03-21 20:20:57 +03:00
def __init__(self, KnowledgeBase kb, entity_id_hash, alias_hash, prior_prob):
self.kb = kb
2019-03-21 20:20:57 +03:00
self.entity_id_hash = entity_id_hash
2019-03-21 14:31:02 +03:00
self.alias_hash = alias_hash
self.prior_prob = prior_prob
@property
def entity_id(self):
2019-03-21 20:20:57 +03:00
"""RETURNS (uint64): hash of the entity's KB ID"""
return self.entity_id_hash
2019-03-21 20:20:57 +03:00
@property
def entity_id_(self):
"""RETURNS (unicode): ID of this entity in the KB"""
return self.kb.vocab.strings[self.entity_id]
@property
def entity_name(self):
2019-03-21 20:20:57 +03:00
"""RETURNS (uint64): hash of the entity's KB name"""
entry_index = <int64_t>self.kb._entry_index.get(self.entity_id)
return self.kb._entries[entry_index].entity_name_hash
@property
def entity_name_(self):
2019-03-21 20:20:57 +03:00
"""RETURNS (unicode): name of this entity in the KB"""
return self.kb.vocab.strings[self.entity_name]
@property
def alias(self):
"""RETURNS (uint64): hash of the alias"""
return self.alias_hash
@property
def alias_(self):
2019-03-21 20:20:57 +03:00
"""RETURNS (unicode): ID of the original alias"""
return self.kb.vocab.strings[self.alias]
2019-03-21 20:20:57 +03:00
@property
def prior_prob(self):
return self.prior_prob
cdef class KnowledgeBase:
2019-03-18 19:27:51 +03:00
2019-03-22 01:17:25 +03:00
def __init__(self, Vocab vocab):
self.vocab = vocab
2019-03-18 19:27:51 +03:00
self._entry_index = PreshMap()
self._alias_index = PreshMap()
self.mem = Pool()
2019-03-21 19:33:25 +03:00
self._create_empty_vectors()
2019-03-18 19:27:51 +03:00
def __len__(self):
2019-03-19 17:51:56 +03:00
return self.get_size_entities()
def get_size_entities(self):
return self._entries.size() - 1 # not counting dummy element on index 0
2019-03-19 17:51:56 +03:00
def get_size_aliases(self):
return self._aliases_table.size() - 1 # not counting dummy element on index 0
2019-03-19 17:51:56 +03:00
2019-03-21 20:20:57 +03:00
def add_entity(self, unicode entity_id, unicode entity_name=None, float prob=0.5, vectors=None, features=None):
"""
Add an entity to the KB.
Return the hash of the entity ID at the end
"""
2019-03-21 20:20:57 +03:00
if not entity_name:
entity_name = entity_id
2019-03-22 01:17:25 +03:00
cdef hash_t id_hash = self.vocab.strings.add(entity_id)
cdef hash_t name_hash = self.vocab.strings.add(entity_name)
2019-03-18 19:27:51 +03:00
# Return if this entity was added before
2019-03-18 19:27:51 +03:00
if id_hash in self._entry_index:
2019-03-22 18:55:05 +03:00
user_warning(Warnings.W018.format(entity=entity_id))
return
2019-03-18 12:31:01 +03:00
cdef int32_t dummy_value = 342
self.c_add_entity(entity_id_hash=id_hash, entity_name_hash=name_hash, prob=prob,
vector_rows=&dummy_value, feats_row=dummy_value)
2019-03-18 12:31:01 +03:00
# TODO self._vectors_table.get_pointer(vectors),
2019-03-18 14:38:40 +03:00
# self._features_table.get(features))
return id_hash
2019-03-18 19:27:51 +03:00
def add_alias(self, unicode alias, entities, probabilities):
"""
For a given alias, add its potential entities and prior probabilies to the KB.
Return the alias_hash at the end
"""
# Throw an error if the length of entities and probabilities are not the same
if not len(entities) == len(probabilities):
2019-03-22 18:55:05 +03:00
raise ValueError(Errors.E132.format(alias=alias,
entities_length=len(entities),
probabilities_length=len(probabilities)))
# Throw an error if the probabilities sum up to more than 1
prob_sum = sum(probabilities)
if prob_sum > 1:
2019-03-22 18:55:05 +03:00
raise ValueError(Errors.E133.format(alias=alias, sum=prob_sum))
2019-03-22 01:17:25 +03:00
cdef hash_t alias_hash = self.vocab.strings.add(alias)
# Return if this alias was added before
if alias_hash in self._alias_index:
2019-03-22 18:55:05 +03:00
user_warning(Warnings.W017.format(alias=alias))
return
2019-03-19 18:15:38 +03:00
cdef hash_t entity_hash
cdef vector[int64_t] entry_indices
cdef vector[float] probs
for entity, prob in zip(entities, probabilities):
2019-03-22 01:17:25 +03:00
entity_id_hash = self.vocab.strings[entity]
2019-03-21 20:20:57 +03:00
if not entity_id_hash in self._entry_index:
2019-03-22 18:55:05 +03:00
raise ValueError(Errors.E134.format(alias=alias, entity=entity))
2019-03-21 20:20:57 +03:00
entry_index = <int64_t>self._entry_index.get(entity_id_hash)
2019-03-19 18:15:38 +03:00
entry_indices.push_back(int(entry_index))
probs.push_back(float(prob))
2019-03-18 14:38:40 +03:00
2019-03-21 14:31:02 +03:00
self.c_add_aliases(alias_hash=alias_hash, entry_indices=entry_indices, probs=probs)
2019-03-18 19:27:51 +03:00
return alias_hash
2019-03-18 19:50:01 +03:00
def get_candidates(self, unicode alias):
""" TODO: where to put this functionality ?"""
2019-03-22 01:17:25 +03:00
cdef hash_t alias_hash = self.vocab.strings[alias]
2019-03-19 17:51:56 +03:00
alias_index = <int64_t>self._alias_index.get(alias_hash)
alias_entry = self._aliases_table[alias_index]
return [Candidate(kb=self,
2019-03-21 20:20:57 +03:00
entity_id_hash=self._entries[entry_index].entity_id_hash,
2019-03-21 14:31:02 +03:00
alias_hash=alias_hash,
prior_prob=prob)
for (entry_index, prob) in zip(alias_entry.entry_indices, alias_entry.probs)
if entry_index != 0]