correct size, not counting dummy elements in the vector

This commit is contained in:
svlandeg 2019-03-19 21:50:32 +01:00
parent f0decf98f1
commit 7402bb4c06
3 changed files with 24 additions and 25 deletions

View File

@ -15,10 +15,10 @@ cdef class KnowledgeBase:
return self.get_size_entities() return self.get_size_entities()
def get_size_entities(self): def get_size_entities(self):
return self._entries.size() return self._entries.size() - 1 # not counting dummy element on index 0
def get_size_aliases(self): def get_size_aliases(self):
return self._aliases_table.size() return self._aliases_table.size() - 1 # not counting dummy element on index 0
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 = self.strings.add(entity_id) cdef hash_t id_hash = self.strings.add(entity_id)

View File

@ -21,35 +21,29 @@ def create_kb():
print(" adding entity", entity_5301561) print(" adding entity", entity_5301561)
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)
mykb.add_entity(entity_id=entity_5301561, prob=0.5)
print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases()) print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases())
# adding aliases # adding aliases
alias = "douglassss" alias1 = "douglassss"
print(" adding alias", alias) print(" adding alias", alias1)
mykb.add_alias(alias=alias, entities=["Q42", "Q5301561"], probabilities=[0.8, 0.2]) mykb.add_alias(alias=alias1, entities=["Q42", "Q5301561"], probabilities=[0.8, 0.2])
print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases())
print("candidates for", alias)
candidates = mykb.get_candidates(alias)
print(" ", candidates)
print(" adding alias", alias)
mykb.add_alias(alias=alias, entities=["Q42"], probabilities=[0.9])
print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases())
alias2 = "johny" alias2 = "johny"
print(" adding alias2", alias2) print(" adding alias", alias2)
mykb.add_alias(alias=alias2, entities=["Q0", "Q42"], probabilities=[0.3, 1.1]) mykb.add_alias(alias=alias2, entities=["Q0", "Q42", "Q5301561"], probabilities=[0.3, 0.1, 0.4])
alias3 = "adam"
print(" adding alias", alias3)
mykb.add_alias(alias=alias3, entities=["Q42"], probabilities=[1.0])
print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases()) print("kb size", len(mykb), mykb.get_size_entities(), mykb.get_size_aliases())
print("candidates for", alias) print("candidates for", alias1)
candidates = mykb.get_candidates(alias) candidates = mykb.get_candidates(alias1)
print(" ", candidates)
print("candidates for", alias3)
candidates = mykb.get_candidates(alias3)
print(" ", candidates) print(" ", candidates)

View File

@ -14,7 +14,12 @@ def test_kb_valid_entities():
mykb.add_entity(entity_id="Q3", prob=0.5) mykb.add_entity(entity_id="Q3", prob=0.5)
# adding aliases # adding aliases
mykb.add_alias(alias="douglassss", entities=["Q2", "Q3"], probabilities=[0.8, 0.2]) mykb.add_alias(alias="douglas", entities=["Q2", "Q3"], probabilities=[0.8, 0.2])
mykb.add_alias(alias="adam", entities=["Q2"], probabilities=[0.9])
# test the size of the corresponding KB
assert(mykb.get_size_entities() == 3)
assert(mykb.get_size_aliases() == 2)
def test_kb_invalid_entities(): def test_kb_invalid_entities():
@ -28,7 +33,7 @@ def test_kb_invalid_entities():
# adding aliases - should fail because one of the given IDs is not valid # adding aliases - should fail because one of the given IDs is not valid
with pytest.raises(ValueError): with pytest.raises(ValueError):
mykb.add_alias(alias="douglassss", entities=["Q2", "Q342"], probabilities=[0.8, 0.2]) mykb.add_alias(alias="douglas", entities=["Q2", "Q342"], probabilities=[0.8, 0.2])
def test_kb_invalid_probabilities(): def test_kb_invalid_probabilities():