spaCy/spacy/kb.pyx

77 lines
2.7 KiB
Cython
Raw Normal View History

2019-03-18 19:27:51 +03:00
# cython: profile=True
# coding: utf8
from spacy.errors import user_warning
cdef class KnowledgeBase:
2019-03-18 19:27:51 +03:00
def __init__(self):
self._entry_index = PreshMap()
self._alias_index = PreshMap()
self.mem = Pool()
2019-03-19 18:43:23 +03:00
self.strings = StringStore()
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()
2019-03-19 17:51:56 +03:00
def get_size_aliases(self):
return self._aliases_table.size()
2019-03-18 19:27:51 +03:00
def add_entity(self, unicode entity_id, float prob, vectors=None, features=None):
2019-03-19 18:43:23 +03:00
cdef hash_t id_hash = self.strings.add(entity_id)
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:
user_warning("Entity " + entity_id + " already exists in the KB")
return
2019-03-18 12:31:01 +03:00
cdef int32_t dummy_value = 342
2019-03-18 14:38:40 +03:00
self.c_add_entity(entity_key=id_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))
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."""
# Throw an error if the probabilities sum up to more than 1
prob_sum = sum(probabilities)
if prob_sum > 1:
raise ValueError("The sum of prior probabilities for alias '" + alias + "' should not exceed 1, "
"but found " + str(prob_sum))
2019-03-19 18:43:23 +03:00
cdef hash_t alias_hash = self.strings.add(alias)
# Return if this alias was added before
if alias_hash in self._alias_index:
user_warning("Alias " + alias + " already exists in the KB")
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):
entity_hash = self.strings[entity]
if not entity_hash in self._entry_index:
raise ValueError("Alias '" + alias + "' defined for unknown entity '" + entity + "'")
2019-03-19 18:15:38 +03:00
entry_index = <int64_t>self._entry_index.get(entity_hash)
entry_indices.push_back(int(entry_index))
probs.push_back(float(prob))
2019-03-18 14:38:40 +03:00
# TODO: check sum(probabilities) <= 1
# TODO: check len(entities) == len(probabilities)
2019-03-18 14:38:40 +03:00
2019-03-19 18:15:38 +03:00
self.c_add_aliases(alias_key=alias_hash, entry_indices=entry_indices, probs=probs)
2019-03-18 19:27:51 +03:00
2019-03-18 19:50:01 +03:00
def get_candidates(self, unicode alias):
2019-03-19 18:43:23 +03:00
cdef hash_t alias_hash = self.strings.add(alias)
2019-03-19 17:51:56 +03:00
alias_index = <int64_t>self._alias_index.get(alias_hash)
return self._aliases_table[alias_index]