e69745adc8
Fixed #99 : SheerkaQueryManager: I can manage contains predicate when filtering objects Fixed #97 : ERROR: list indices must be integers or slices, not Concept Fixed #96 : SequenceNodeParser: SequenceNodeParser must correctly handle concept definition Fixed #95 : ResolveAmbiguity must not remove concepts that do not require evaluation Fixed #94 : Concepts with the same key are lost when new ontology Fixed #93 : Introduce BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED Fixed #92 : ExpressionParser: Implement compile_disjunctions() Fixed #91 : Implement get_concepts_complexity(context, concepts, concept_parts) Fixed #90 : ResolveAmbiguity : where predicate is not used to resolve ambiguity Fixed #89 : ResolveAmbiguityEvaluator: Concepts embedded in ConceptNode are not resolved Fixed #88: SyaNodeParser: Parse multiple parameters when some of the are not recognized Fixed #87: SyaNodeParser : Parse the multiple parameters
180 lines
7.4 KiB
Python
180 lines
7.4 KiB
Python
from core.builtin_concepts import BuiltinConcepts
|
|
from core.concept import Concept
|
|
from core.sheerka.services.SheerkaConceptsAlgebra import ConceptScore
|
|
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
|
|
def test_i_can_add_concepts(self):
|
|
sheerka, context, man, human, male, driver, licence, car = self.init_test(global_truth=True).with_concepts(
|
|
"man", "human", "male",
|
|
"driver", "licence", "car").unpack()
|
|
|
|
sheerka.set_isa(context, sheerka.new("man"), human)
|
|
sheerka.set_isa(context, sheerka.new("man"), male)
|
|
sheerka.set_hasa(context, sheerka.new("driver"), licence)
|
|
sheerka.set_hasa(context, sheerka.new("driver"), car)
|
|
|
|
res = sheerka.cadd(context, sheerka.new("man"), sheerka.new("driver"))
|
|
|
|
assert isinstance(res, Concept)
|
|
assert res.get_metadata().props == {BuiltinConcepts.ISA: {male, human},
|
|
BuiltinConcepts.HASA: {car, licence}, }
|
|
|
|
def test_can_add_concepts_when_property_already_exist(self):
|
|
sheerka, context, man, human, king, male = self.init_test(global_truth=True).with_concepts(
|
|
"man", "human", "king", "male").unpack()
|
|
|
|
sheerka.set_isa(context, sheerka.new("man"), human)
|
|
sheerka.set_isa(context, sheerka.new("king"), male)
|
|
|
|
res = sheerka.cadd(context, sheerka.new("man"), sheerka.new("king"))
|
|
|
|
assert isinstance(res, Concept)
|
|
assert res.get_metadata().props == {BuiltinConcepts.ISA: {male, human}}
|
|
|
|
def test_i_can_subtract_concepts(self):
|
|
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_test(global_truth=True).with_concepts(
|
|
"foo", "bar",
|
|
"isa1", "isa2", "has1", "has2").unpack()
|
|
|
|
new_foo = sheerka.new("foo")
|
|
sheerka.set_isa(context, new_foo, isa1)
|
|
sheerka.set_isa(context, new_foo, isa2)
|
|
sheerka.set_hasa(context, new_foo, hasa1)
|
|
sheerka.set_hasa(context, new_foo, hasa2)
|
|
|
|
new_bar = sheerka.new("bar")
|
|
sheerka.set_isa(context, new_bar, isa1)
|
|
sheerka.set_hasa(context, new_bar, hasa1)
|
|
|
|
assert sheerka.csub(context) == Concept()
|
|
|
|
res = sheerka.csub(context, new_foo)
|
|
assert isinstance(res, Concept)
|
|
assert res.get_metadata().props == res.get_metadata().props
|
|
|
|
res = sheerka.csub(context, new_foo, new_bar)
|
|
assert isinstance(res, Concept)
|
|
assert res.get_metadata().props == {BuiltinConcepts.ISA: {isa2},
|
|
BuiltinConcepts.HASA: {hasa2}, }
|
|
|
|
def test_i_can_recognize_myself_when_using_sdp_repository(self):
|
|
sheerka, context, foo, isa1, hasa1, = self.init_test(cache_only=False, global_truth=True).with_concepts(
|
|
"foo",
|
|
"isa1",
|
|
"has1", create_new=True).unpack()
|
|
sheerka.om.commit(context)
|
|
|
|
new_foo = sheerka.new("foo")
|
|
sheerka.set_isa(context, new_foo, isa1)
|
|
sheerka.set_hasa(context, new_foo, hasa1)
|
|
sheerka.om.commit(context)
|
|
|
|
assert sheerka.recognize(new_foo, all_scores=True) == [ConceptScore(1, new_foo, new_foo)]
|
|
|
|
def test_i_can_recognize_myself_when_not_using_sdp_repository(self):
|
|
sheerka, context, foo, isa1, hasa1, = self.init_test(global_truth=True).with_concepts(
|
|
"foo",
|
|
"isa1",
|
|
"has1").unpack()
|
|
|
|
new_foo = sheerka.new("foo")
|
|
sheerka.set_isa(context, new_foo, isa1)
|
|
sheerka.set_hasa(context, new_foo, hasa1)
|
|
|
|
assert sheerka.recognize(new_foo, all_scores=True) == [ConceptScore(1, new_foo, new_foo)]
|
|
|
|
def test_unknown_prop_is_returned_when_nothing_is_recognized(self):
|
|
sheerka, context, isa1, hasa1, = self.init_concepts("isa1", "has1")
|
|
|
|
foo = Concept()
|
|
foo.add_prop(BuiltinConcepts.ISA, isa1)
|
|
foo.add_prop(BuiltinConcepts.HASA, hasa1)
|
|
|
|
res = sheerka.recognize(foo, all_scores=True)
|
|
|
|
assert sheerka.isinstance(res, BuiltinConcepts.UNKNOWN_CONCEPT)
|
|
assert res.body == [(BuiltinConcepts.ISA, {isa1}), (BuiltinConcepts.HASA, {hasa1})]
|
|
|
|
def test_empty_list_is_return_when_there_is_noting_to_recognize(self):
|
|
sheerka, context = self.init_concepts()
|
|
|
|
assert sheerka.recognize(Concept(), all_scores=True) == []
|
|
|
|
def test_i_can_recognize_multiple_concepts_with_the_proper_score(self):
|
|
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_test(global_truth=True).with_concepts(
|
|
"foo", "bar",
|
|
"isa1", "isa2", "has1", "has2").unpack()
|
|
|
|
new_foo = sheerka.new("foo")
|
|
sheerka.set_isa(context, new_foo, isa1)
|
|
sheerka.set_isa(context, new_foo, isa2)
|
|
sheerka.set_hasa(context, new_foo, hasa1)
|
|
sheerka.set_hasa(context, new_foo, hasa2)
|
|
|
|
new_bar = sheerka.new("bar")
|
|
sheerka.set_isa(context, new_bar, isa1)
|
|
sheerka.set_hasa(context, new_bar, hasa1)
|
|
|
|
to_recognize = Concept()
|
|
to_recognize.add_prop(BuiltinConcepts.ISA, isa1)
|
|
to_recognize.add_prop(BuiltinConcepts.HASA, hasa1)
|
|
|
|
res = sheerka.recognize(to_recognize, all_scores=True)
|
|
assert res == [ConceptScore(1.0, sheerka.new("bar"), to_recognize),
|
|
ConceptScore(0.5, sheerka.new("foo"), to_recognize)]
|
|
|
|
# add the other properties, to match foo
|
|
to_recognize.add_prop(BuiltinConcepts.ISA, isa2)
|
|
to_recognize.add_prop(BuiltinConcepts.HASA, hasa2)
|
|
|
|
res = sheerka.recognize(to_recognize, all_scores=True)
|
|
assert res == [ConceptScore(1.0, sheerka.new("foo"), to_recognize),
|
|
ConceptScore(0.5, sheerka.new("bar"), to_recognize)]
|
|
|
|
def test_i_can_recognize_if_all_scores_is_disabled(self):
|
|
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_test(global_truth=True).with_concepts(
|
|
"foo", "bar",
|
|
"isa1", "isa2", "has1", "has2").unpack()
|
|
|
|
new_foo = sheerka.new("foo")
|
|
sheerka.set_isa(context, new_foo, isa1)
|
|
sheerka.set_isa(context, new_foo, isa2)
|
|
sheerka.set_hasa(context, new_foo, hasa1)
|
|
sheerka.set_hasa(context, new_foo, hasa2)
|
|
|
|
new_bar = sheerka.new("bar")
|
|
sheerka.set_isa(context, new_bar, isa1)
|
|
sheerka.set_hasa(context, new_bar, hasa1)
|
|
|
|
to_recognize = Concept()
|
|
to_recognize.add_prop(BuiltinConcepts.ISA, isa1)
|
|
to_recognize.add_prop(BuiltinConcepts.HASA, hasa1)
|
|
|
|
res = sheerka.recognize(to_recognize, all_scores=False)
|
|
assert res == ConceptScore(1.0, sheerka.new("bar"), to_recognize)
|
|
|
|
def test_i_can_recognize_if_all_scores_is_disabled_but_multiple_high_scores(self):
|
|
sheerka, context, foo, bar, isa1, hasa1 = self.init_test(global_truth=True).with_concepts(
|
|
"foo", "bar",
|
|
"isa1", "has1").unpack()
|
|
|
|
new_foo = sheerka.new("foo")
|
|
sheerka.set_isa(context, new_foo, isa1)
|
|
sheerka.set_hasa(context, new_foo, hasa1)
|
|
|
|
new_bar = sheerka.new("bar")
|
|
sheerka.set_isa(context, new_bar, isa1)
|
|
sheerka.set_hasa(context, new_bar, hasa1)
|
|
|
|
to_recognize = Concept()
|
|
to_recognize.add_prop(BuiltinConcepts.ISA, isa1)
|
|
to_recognize.add_prop(BuiltinConcepts.HASA, hasa1)
|
|
|
|
res = sheerka.recognize(to_recognize, all_scores=False)
|
|
assert len(res) == 2
|
|
assert ConceptScore(1.0, sheerka.new("foo"), to_recognize) in res
|
|
assert ConceptScore(1.0, sheerka.new("bar"), to_recognize) in res
|