ExactConceptParser can now recognize concepts by their names

This commit is contained in:
2020-05-21 16:27:18 +02:00
parent d357329f51
commit 37d3d16e21
17 changed files with 347 additions and 112 deletions
+6 -6
View File
@@ -163,10 +163,10 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
def test_concepts_are_defined_once(self):
sheerka = self.get_sheerka()
context = self.get_context(sheerka)
good = self.create_concept_lite(sheerka, "good")
foo = self.create_concept_lite(sheerka, "foo", bnf=ConceptExpression("good"))
bar = self.create_concept_lite(sheerka, "bar", bnf=ConceptExpression("good"))
baz = self.create_concept_lite(sheerka, "baz", bnf=OrderedChoice(
good = self.create_and_add_in_cache_concept(sheerka, "good")
foo = self.create_and_add_in_cache_concept(sheerka, "foo", bnf=ConceptExpression("good"))
bar = self.create_and_add_in_cache_concept(sheerka, "bar", bnf=ConceptExpression("good"))
baz = self.create_and_add_in_cache_concept(sheerka, "baz", bnf=OrderedChoice(
ConceptExpression("foo"),
ConceptExpression("bar")))
@@ -183,8 +183,8 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
sheerka = self.get_sheerka()
context = self.get_context(sheerka)
a = self.create_concept_lite(sheerka, "a", bnf=Sequence("one", "two"))
b = self.create_concept_lite(sheerka, "b", bnf=Sequence(ConceptExpression("a"), "two"))
a = self.create_and_add_in_cache_concept(sheerka, "a", bnf=Sequence("one", "two"))
b = self.create_and_add_in_cache_concept(sheerka, "b", bnf=Sequence(ConceptExpression("a"), "two"))
concepts_by_first_keywords = BaseNodeParser.get_concepts_by_first_keyword(
context, [a, b]).body
+59 -16
View File
@@ -1,5 +1,5 @@
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.concept import Concept, CMV
from core.tokenizer import Tokenizer
from parsers.ExactConceptParser import ExactConceptParser
@@ -49,10 +49,10 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
('__var__1', '__var__0', '__var__1')}
# TODO: the last tuple is not possible, so the algo can be improved
def test_i_can_recognize_a_simple_concept(self):
def test_i_can_parse_a_simple_concept(self):
sheerka = self.get_sheerka(singleton=True)
context = self.get_context(sheerka)
concept = self.create_concept_lite(sheerka, "hello world")
concept = self.create_and_add_in_cache_concept(sheerka, "hello world")
source = "hello world"
results = ExactConceptParser().parse(context, source)
@@ -62,12 +62,13 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
assert results[0].status
assert concept_found == concept
assert not concept_found.metadata.need_validation
assert not concept_found.metadata.is_evaluated
def test_i_can_recognize_concepts_defined_several_times(self):
def test_i_can_parse_concepts_defined_several_times(self):
sheerka = self.get_sheerka(singleton=True)
context = self.get_context(sheerka)
self.create_concept_lite(sheerka, "hello world")
self.create_concept_lite(sheerka, "hello a", variables=["a"])
self.create_and_add_in_cache_concept(sheerka, "hello world")
self.create_and_add_in_cache_concept(sheerka, "hello a", variables=["a"])
source = "hello world"
results = ExactConceptParser().parse(context, source)
@@ -84,10 +85,10 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
assert results[1].value.value.name == "hello world"
assert not results[1].value.value.metadata.need_validation
def test_i_can_recognize_a_concept_with_variables(self):
def test_i_can_parse_a_concept_with_variables(self):
sheerka = self.get_sheerka(singleton=True)
context = self.get_context(sheerka)
concept = self.create_concept_lite(sheerka, "a + b", ["a", "b"])
concept = self.create_and_add_in_cache_concept(sheerka, "a + b", ["a", "b"])
source = "10 + 5"
results = ExactConceptParser().parse(context, source)
@@ -96,15 +97,14 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
assert results[0].status
concept_found = results[0].value.value
assert concept_found.key == concept.key
assert variable_def(concept_found, "a") == "10"
assert variable_def(concept_found, "b") == "5"
assert concept_found == CMV(concept, a="10", b="5")
assert concept_found.metadata.need_validation
assert not concept_found.metadata.is_evaluated
def test_i_can_recognize_a_concept_with_duplicate_variables(self):
def test_i_can_parse_a_concept_with_duplicate_variables(self):
sheerka = self.get_sheerka(singleton=True)
context = self.get_context(sheerka)
concept = self.create_concept_lite(sheerka, "a + b + a", ["a", "b"])
concept = self.create_and_add_in_cache_concept(sheerka, "a + b + a", ["a", "b"])
source = "10 + 5 + 10"
results = ExactConceptParser(max_word_size=5).parse(context, source)
@@ -113,11 +113,54 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
assert results[0].status
concept_found = results[0].value.value
assert concept_found.key == concept.key
assert variable_def(concept_found, "a") == "10"
assert variable_def(concept_found, "b") == "5"
assert concept_found == CMV(concept, a="10", b="5")
assert concept_found.metadata.need_validation
def test_i_can_parse_concept_when_defined_using_from_def(self):
sheerka, context, plus = self.init_concepts(
self.from_def_concept("+", "a + b", ["a", "b"])
)
source = "10 + 5"
results = ExactConceptParser().parse(context, source)
concept_found = results[0].value.value
assert len(results) == 1
assert results[0].status
assert concept_found == CMV(plus, a="10", b="5")
assert concept_found.metadata.need_validation
assert not concept_found.metadata.is_evaluated
def test_i_can_parse_concept_token(self):
sheerka, context, foo = self.init_concepts("foo")
source = "c:foo:"
results = ExactConceptParser().parse(context, source)
concept_found = results[0].value.value
assert len(results) == 1
assert results[0].status
assert concept_found == foo
assert not concept_found.metadata.need_validation
assert concept_found.metadata.is_evaluated
def test_i_can_parse_concept_with_concept_tokens(self):
sheerka, context, one, two, plus = self.init_concepts(
"one",
"two",
self.from_def_concept("plus", "a plus b", ["a", "b"])
)
source = "c:one: plus c:two:"
results = ExactConceptParser().parse(context, source)
concept_found = results[0].value.value
assert len(results) == 1
assert results[0].status
assert concept_found == CMV(plus, a="c:one:", b="c:two:")
assert concept_found.metadata.need_validation
assert not concept_found.metadata.is_evaluated
def test_i_can_manage_unknown_concept(self):
context = self.get_context(self.get_sheerka(singleton=True))
source = "def concept hello" # this is not a concept by itself