mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-26 18:06:29 +03:00
get candidates by alias
This commit is contained in:
parent
151b855cc8
commit
c4ba942765
|
@ -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)
|
|
||||||
|
|
||||||
|
|
11
spacy/kb.pyx
11
spacy/kb.pyx
|
@ -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]
|
||||||
|
|
||||||
|
|
|
@ -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():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user