get candidates by alias

This commit is contained in:
svlandeg 2019-03-19 15:51:56 +01:00
parent a4d876d471
commit c62cca3368
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
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.mem = Pool()
def __len__(self):
return self.get_size_entities()
def get_size_entities(self):
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):
cdef hash_t id_hash = hash_string(entity_id)
@ -40,6 +45,6 @@ cdef class KnowledgeBase:
def get_candidates(self, unicode alias):
cdef hash_t alias_hash = hash_string(alias)
cdef _AliasC candidates = self.c_get_candidates(alias_key=alias_hash)
return candidates
alias_index = <int64_t>self._alias_index.get(alias_hash)
return self._aliases_table[alias_index]

View File

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