get candidates by alias

This commit is contained in:
svlandeg 2019-03-19 15:51:56 +01:00
parent 151b855cc8
commit c4ba942765
3 changed files with 18 additions and 15 deletions

View File

@ -118,8 +118,4 @@ cdef class KnowledgeBase:
self._alias_index[alias_key] = alias_index self._alias_index[alias_key] = alias_index
return alias_index return alias_index
cdef inline c_get_candidates(self, hash_t alias_key):
cdef int64_t alias_index = self._alias_index[alias_key]
cdef _AliasC candidates = self._aliases_table[alias_index]
print("candidates", candidates)

View File

@ -10,10 +10,15 @@ cdef class KnowledgeBase:
self._alias_index = PreshMap() self._alias_index = PreshMap()
self.mem = Pool() self.mem = Pool()
def __len__(self): def __len__(self):
return self.get_size_entities()
def get_size_entities(self):
return self._entries.size() return self._entries.size()
def get_size_aliases(self):
return self._aliases_table.size()
def add_entity(self, unicode entity_id, float prob, vectors=None, features=None): def add_entity(self, unicode entity_id, float prob, vectors=None, features=None):
cdef hash_t id_hash = hash_string(entity_id) cdef hash_t id_hash = hash_string(entity_id)
@ -40,6 +45,6 @@ cdef class KnowledgeBase:
def get_candidates(self, unicode alias): def get_candidates(self, unicode alias):
cdef hash_t alias_hash = hash_string(alias) cdef hash_t alias_hash = hash_string(alias)
cdef _AliasC candidates = self.c_get_candidates(alias_key=alias_hash) alias_index = <int64_t>self._alias_index.get(alias_hash)
return candidates return self._aliases_table[alias_index]

View File

@ -4,7 +4,7 @@ from spacy.kb import KnowledgeBase
def create_kb(): def create_kb():
mykb = KnowledgeBase() mykb = KnowledgeBase()
print("kb size", len(mykb)) print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases())
# adding entities # adding entities
entity_42 = "Q42" # douglas adams entity_42 = "Q42" # douglas adams
@ -15,16 +15,18 @@ def create_kb():
mykb.add_entity(entity_id=entity_5301561, prob=0.5) mykb.add_entity(entity_id=entity_5301561, prob=0.5)
print(" adding entity", entity_5301561) print(" adding entity", entity_5301561)
print("kb size", len(mykb)) print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases())
# adding aliases # adding aliases
alias = "douglas" alias = "douglas"
print(" adding alias", alias) print(" adding alias", alias)
mykb.add_alias(alias=alias, entities=["Q42", "Q5301561"], probabilities=[0.8, 0.2]) mykb.add_alias(alias=alias, entities=["Q42", "Q5301561"], probabilities=[0.8, 0.2])
print("kb size", len(mykb))
print("aliases for", alias) print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases())
mykb.get_candidates(alias)
print("candidates for", alias)
candidates = mykb.get_candidates(alias)
print(" ", candidates)
def add_el(): def add_el():