spaCy/spacy/kb.pyx

32 lines
1.2 KiB
Cython
Raw Normal View History

from .strings cimport hash_string
cdef class KnowledgeBase:
def __len__(self):
return self._entries.size()
def add_entity(self, name, float prob, vectors=None, features=None, aliases=None):
# TODO: more friendly check for non-unique name
if name in self:
return
cdef hash_t name_hash = hash_string(name)
2019-03-18 09:31:01 +00:00
cdef int32_t dummy_value = 342
self.c_add_entity(name_hash, prob, &dummy_value, dummy_value)
# TODO self._vectors_table.get_pointer(vectors),
# 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
# TODO: check len(entities) == len(probabilities)
for entity, prob in zip(entities, probabilities):
2019-03-18 09:31:01 +00:00
entity_hash = hash_string(entity)
entity_index = self._index[entity_hash]
# TODO: check that entity is already in this KB (entity_index is OK)
self._aliases_table.add(alias_hash, entity_index, prob)