mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-27 10:26:35 +03:00
raising error when adding alias for unknown entity + unit test
This commit is contained in:
parent
8843f9279c
commit
20a7b7b1c0
|
@ -39,13 +39,15 @@ cdef class KnowledgeBase:
|
||||||
cdef vector[float] probs
|
cdef vector[float] probs
|
||||||
|
|
||||||
for entity, prob in zip(entities, probabilities):
|
for entity, prob in zip(entities, probabilities):
|
||||||
entity_hash = self.strings.add(entity)
|
entity_hash = self.strings[entity]
|
||||||
|
if not entity_hash in self._entry_index:
|
||||||
|
raise ValueError("Alias '" + alias + "' defined for unknown entity '" + entity + "'")
|
||||||
|
|
||||||
entry_index = <int64_t>self._entry_index.get(entity_hash)
|
entry_index = <int64_t>self._entry_index.get(entity_hash)
|
||||||
entry_indices.push_back(int(entry_index))
|
entry_indices.push_back(int(entry_index))
|
||||||
probs.push_back(float(prob))
|
probs.push_back(float(prob))
|
||||||
|
|
||||||
# TODO: check that alias hadn't been defined before
|
# TODO: check that alias hadn't been defined before
|
||||||
# TODO: check that entity is already in this KB (entity_index is OK)
|
|
||||||
# TODO: check sum(probabilities) <= 1
|
# TODO: check sum(probabilities) <= 1
|
||||||
# TODO: check len(entities) == len(probabilities)
|
# TODO: check len(entities) == len(probabilities)
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,10 @@ def create_kb():
|
||||||
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 entities
|
# adding entities
|
||||||
|
entity_0 = "Q0" # douglas adams
|
||||||
|
mykb.add_entity(entity_id=entity_0, prob=0.5)
|
||||||
|
print(" adding entity", entity_0)
|
||||||
|
|
||||||
entity_42 = "Q42" # douglas adams
|
entity_42 = "Q42" # douglas adams
|
||||||
mykb.add_entity(entity_id=entity_42, prob=0.5)
|
mykb.add_entity(entity_id=entity_42, prob=0.5)
|
||||||
print(" adding entity", entity_42)
|
print(" adding entity", entity_42)
|
||||||
|
@ -18,7 +22,7 @@ def create_kb():
|
||||||
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 = "douglas"
|
alias = "douglassss"
|
||||||
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])
|
||||||
|
|
||||||
|
|
29
spacy/tests/pipeline/test_el.py
Normal file
29
spacy/tests/pipeline/test_el.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from spacy.kb import KnowledgeBase
|
||||||
|
|
||||||
|
|
||||||
|
def test_kb_valid_entities():
|
||||||
|
mykb = KnowledgeBase()
|
||||||
|
|
||||||
|
# adding entities
|
||||||
|
mykb.add_entity(entity_id="Q1", prob=0.5)
|
||||||
|
mykb.add_entity(entity_id="Q2", prob=0.5)
|
||||||
|
mykb.add_entity(entity_id="Q3", prob=0.5)
|
||||||
|
|
||||||
|
# adding aliases
|
||||||
|
mykb.add_alias(alias="douglassss", entities=["Q2", "Q3"], probabilities=[0.8, 0.2])
|
||||||
|
|
||||||
|
|
||||||
|
def test_kb_invalid_entities():
|
||||||
|
mykb = KnowledgeBase()
|
||||||
|
|
||||||
|
# adding entities
|
||||||
|
mykb.add_entity(entity_id="Q1", prob=0.5)
|
||||||
|
mykb.add_entity(entity_id="Q2", prob=0.5)
|
||||||
|
mykb.add_entity(entity_id="Q3", prob=0.5)
|
||||||
|
|
||||||
|
# adding aliases - should fail because one of the given IDs is not valid
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
mykb.add_alias(alias="douglassss", entities=["Q2", "Q342"], probabilities=[0.8, 0.2])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user