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