From a4d876d47101523a4b4d7591dddfe2fd780b2601 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 18 Mar 2019 17:50:01 +0100 Subject: [PATCH] adding and retrieving aliases --- spacy/kb.pxd | 8 +++++++- spacy/kb.pyx | 5 ++++- spacy/sandbox_test_sofie/testing_el.py | 20 +++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/spacy/kb.pxd b/spacy/kb.pxd index 43f3e83e8..7ee7f38be 100644 --- a/spacy/kb.pxd +++ b/spacy/kb.pxd @@ -116,4 +116,10 @@ cdef class KnowledgeBase: probs=probs )) self._alias_index[alias_key] = alias_index - return alias_index \ No newline at end of file + 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) + diff --git a/spacy/kb.pyx b/spacy/kb.pyx index d2b8fffe1..f420e0b73 100644 --- a/spacy/kb.pyx +++ b/spacy/kb.pyx @@ -38,5 +38,8 @@ cdef class KnowledgeBase: self.c_add_aliases(alias_key=alias_hash, entities=entities, probabilities=probabilities) - + 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 diff --git a/spacy/sandbox_test_sofie/testing_el.py b/spacy/sandbox_test_sofie/testing_el.py index 840d890b5..9a5ab638d 100644 --- a/spacy/sandbox_test_sofie/testing_el.py +++ b/spacy/sandbox_test_sofie/testing_el.py @@ -6,12 +6,26 @@ def create_kb(): mykb = KnowledgeBase() print("kb size", len(mykb)) - entity_id = "Q42" - mykb.add_entity(entity_id=entity_id, prob=0.5) - print("adding entity", entity_id) + # adding entities + entity_42 = "Q42" # douglas adams + mykb.add_entity(entity_id=entity_42, prob=0.5) + print("adding entity", entity_42) + + entity_5301561 = "Q5301561" + mykb.add_entity(entity_id=entity_5301561, prob=0.5) + print("adding entity", entity_5301561) print("kb size", len(mykb)) + # adding aliases + alias = "douglas" + 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) + def add_el(): nlp = spacy.load('en_core_web_sm')