spaCy/spacy/kb.pyx

35 lines
1.3 KiB
Cython
Raw Normal View History

from .strings cimport hash_string
cdef class KnowledgeBase:
def __len__(self):
return self._entries.size()
2019-03-18 11:38:40 +00:00
def add_entity(self, entity_id: str, float prob, vectors=None, features=None):
# TODO: more friendly check for non-unique name
2019-03-18 11:38:40 +00:00
if entity_id in self:
return
2019-03-18 11:38:40 +00:00
cdef hash_t id_hash = hash_string(entity_id)
2019-03-18 09:31:01 +00:00
cdef int32_t dummy_value = 342
2019-03-18 11:38:40 +00:00
self.c_add_entity(entity_key=id_hash, prob=prob, vector_rows=&dummy_value, feats_row=dummy_value)
2019-03-18 09:31:01 +00:00
# TODO self._vectors_table.get_pointer(vectors),
2019-03-18 11:38:40 +00:00
# self._features_table.get(features))
def add_alias(self, alias, entities, probabilities):
"""For a given alias, add its potential entities and prior probabilies to the KB."""
cdef hash_t alias_hash = hash_string(alias)
2019-03-18 09:31:01 +00:00
cdef hash_t entity_hash = 0
cdef int64_t entity_index = 0
2019-03-18 11:38:40 +00:00
cdef vector[int64_t] entry_indices = [self._entry_index[hash_string(entity)] for entity in entities]
self.c_add_aliases(alias_key=alias_hash, entry_indices=entry_indices, probs=probabilities)
# TODO: check that alias hadn't been defined before
# TODO: check that entity is already in this KB (entity_index is OK)
# TODO: check sum(probabilities) <= 1
# TODO: check len(entities) == len(probabilities)
2019-03-18 11:38:40 +00:00