Implemented SheerkaOntology
This commit is contained in:
@@ -24,7 +24,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
sheerka, context, *updated = self.init_concepts(concept)
|
||||
|
||||
res = BaseNodeParser.get_concepts_by_first_token(context, updated)
|
||||
res = BaseNodeParser.compute_concepts_by_first_token(context, updated)
|
||||
|
||||
assert res.status
|
||||
assert res.body == expected
|
||||
@@ -54,7 +54,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
concept.set_bnf(bnf)
|
||||
sheerka.set_id_if_needed(concept, False)
|
||||
|
||||
res = BaseNodeParser.get_concepts_by_first_token(context, [concept])
|
||||
res = BaseNodeParser.compute_concepts_by_first_token(context, [concept])
|
||||
|
||||
assert res.status
|
||||
assert res.body == expected
|
||||
@@ -75,7 +75,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
foo.set_bnf(OrderedChoice(ConceptExpression("bar"), ConceptExpression("baz"), StrMatch("qux")))
|
||||
sheerka.set_id_if_needed(foo, False)
|
||||
|
||||
res = BaseNodeParser.get_concepts_by_first_token(context, [bar, baz, foo])
|
||||
res = BaseNodeParser.compute_concepts_by_first_token(context, [bar, baz, foo])
|
||||
|
||||
assert res.status
|
||||
assert res.body == {
|
||||
@@ -87,12 +87,12 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
}
|
||||
|
||||
def test_i_can_get_concepts_by_first_keyword_using_sheerka(self):
|
||||
sheerka, context, *updated = self.init_concepts(
|
||||
sheerka, context, *updated = self.init_test().with_concepts(
|
||||
"one",
|
||||
"two",
|
||||
Concept("twenty", definition="'twenty' (one|two)"),
|
||||
create_new=True
|
||||
)
|
||||
).unpack()
|
||||
|
||||
bar = Concept("bar").init_key()
|
||||
sheerka.set_id_if_needed(bar, False)
|
||||
@@ -102,7 +102,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
foo.set_bnf(OrderedChoice(ConceptExpression("one"), ConceptExpression("bar"), StrMatch("qux")))
|
||||
sheerka.set_id_if_needed(foo, False)
|
||||
|
||||
res = BaseNodeParser.get_concepts_by_first_token(context, [bar, foo], use_sheerka=True)
|
||||
res = BaseNodeParser.compute_concepts_by_first_token(context, [bar, foo], use_sheerka=True)
|
||||
|
||||
assert res.status
|
||||
assert res.body == {
|
||||
@@ -117,7 +117,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
def test_i_cannot_get_concept_by_first_keyword_when_no_first_keyword(self):
|
||||
sheerka, context, foo = self.init_concepts(Concept("x y", body="x y").def_var("x").def_var("y"))
|
||||
res = BaseNodeParser.get_concepts_by_first_token(context, [foo])
|
||||
res = BaseNodeParser.compute_concepts_by_first_token(context, [foo])
|
||||
|
||||
assert not res.status
|
||||
assert res.body == NoFirstTokenError(foo, foo.key)
|
||||
@@ -126,9 +126,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka, context, *updated = self.init_concepts(
|
||||
"one",
|
||||
Concept("two", definition="one"),
|
||||
Concept("three", definition="two"),
|
||||
create_new=False
|
||||
)
|
||||
Concept("three", definition="two"))
|
||||
|
||||
concepts_by_first_keywords = {
|
||||
"one": ["1001"],
|
||||
@@ -152,7 +150,6 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
"hundred",
|
||||
Concept("twenties", definition="twenty number"),
|
||||
Concept("hundreds", definition="number hundred"),
|
||||
create_new=True # mandatory because set_isa() needs it
|
||||
)
|
||||
|
||||
sheerka.set_isa(context, sheerka.new("one"), number)
|
||||
@@ -166,7 +163,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka.concepts_grammars.clear() # reset all the grammar to simulate Sheerka restart
|
||||
|
||||
# cbft : concept_by_first_token (I usually don't use abbreviation)
|
||||
cbft = BaseNodeParser.get_concepts_by_first_token(context, [number] + concepts).body
|
||||
cbft = BaseNodeParser.compute_concepts_by_first_token(context, [number] + concepts).body
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, cbft)
|
||||
|
||||
assert resolved_ret_val.status
|
||||
@@ -188,7 +185,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
ConceptExpression("foo"),
|
||||
ConceptExpression("bar")))
|
||||
|
||||
concepts_by_first_keywords = BaseNodeParser.get_concepts_by_first_token(
|
||||
concepts_by_first_keywords = BaseNodeParser.compute_concepts_by_first_token(
|
||||
context, [good, foo, bar, baz]).body
|
||||
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, concepts_by_first_keywords)
|
||||
@@ -204,7 +201,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
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_token(
|
||||
concepts_by_first_keywords = BaseNodeParser.compute_concepts_by_first_token(
|
||||
context, [a, b]).body
|
||||
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, concepts_by_first_keywords)
|
||||
@@ -220,7 +217,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
self.bnf_concept("bar", ConceptExpression("foo")),
|
||||
)
|
||||
|
||||
concepts_by_first_keywords = BaseNodeParser.get_concepts_by_first_token(context, [good, foo, bar]).body
|
||||
concepts_by_first_keywords = BaseNodeParser.compute_concepts_by_first_token(context, [good, foo, bar]).body
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, concepts_by_first_keywords)
|
||||
assert resolved_ret_val.status
|
||||
assert resolved_ret_val.body == {
|
||||
@@ -237,7 +234,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
self.bnf_concept("three", ConceptExpression("two")),
|
||||
)
|
||||
|
||||
concepts_by_first_keywords = BaseNodeParser.get_concepts_by_first_token(context, [good, one, two, three]).body
|
||||
concepts_by_first_keywords = BaseNodeParser.compute_concepts_by_first_token(context, [good, one, two, three]).body
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, concepts_by_first_keywords)
|
||||
assert resolved_ret_val.status
|
||||
assert resolved_ret_val.body == {
|
||||
@@ -255,7 +252,7 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
self.bnf_concept("one", ConceptExpression("three")),
|
||||
)
|
||||
|
||||
concepts_by_first_keywords = BaseNodeParser.get_concepts_by_first_token(context, [good, one, two, three]).body
|
||||
concepts_by_first_keywords = BaseNodeParser.compute_concepts_by_first_token(context, [good, one, two, three]).body
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, concepts_by_first_keywords)
|
||||
assert resolved_ret_val.status
|
||||
assert resolved_ret_val.body == {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import pytest
|
||||
|
||||
import tests.parsers.parsers_utils
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept, ConceptParts, DoNotResolve, CC, DEFINITION_TYPE_BNF, NotInit
|
||||
from core.concept import Concept, ConceptParts, DoNotResolve, CC, DEFINITION_TYPE_BNF
|
||||
from core.global_symbols import NotInit
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from parsers.BaseNodeParser import CNC, UTN, CN
|
||||
from parsers.BnfDefinitionParser import BnfDefinitionParser
|
||||
from parsers.BnfNodeParser import StrMatch, TerminalNode, NonTerminalNode, Sequence, OrderedChoice, \
|
||||
Optional, ZeroOrMore, OneOrMore, ConceptExpression, UnOrderedChoice, BnfNodeParser
|
||||
|
||||
import tests.parsers.parsers_utils
|
||||
from tests.BaseTest import BaseTest
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
@@ -82,28 +83,26 @@ def compute_expected_array(my_concepts_map, expression, expected, exclude_body=F
|
||||
|
||||
|
||||
class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka = None
|
||||
shared_ontology = None
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
t = cls()
|
||||
TestBnfNodeParser.sheerka, context, _ = t.init_parser(
|
||||
cmap,
|
||||
singleton=False,
|
||||
create_new=True,
|
||||
init_from_sheerka=True)
|
||||
init_test_helper = cls().init_test(cache_only=False, ontology="#TestBnfNodeParser#")
|
||||
sheerka, context, *updated = init_test_helper.with_concepts(*cmap.values(), create_new=True).unpack()
|
||||
for i, concept_name in enumerate(cmap):
|
||||
cmap[concept_name] = updated[i]
|
||||
|
||||
# end of initialisation
|
||||
sheerka = TestBnfNodeParser.sheerka
|
||||
sheerka.set_isa(context, sheerka.new("one"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("two"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("three"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("four"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("thirty"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("forty"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("fifty"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("one hundred"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, sheerka.new("hundreds"), sheerka.new("number"))
|
||||
sheerka.set_isa(context, cmap["one"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["two"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["three"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["four"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["thirty"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["forty"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["fifty"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["one hundred"], cmap["number"])
|
||||
sheerka.set_isa(context, cmap["hundreds"], cmap["number"])
|
||||
|
||||
# Pay attention. 'twenties (t1 and t2) are not set as number
|
||||
|
||||
@@ -135,6 +134,9 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
cmap["thousands"] = sheerka.create_new_concept(context, thousands).body.body
|
||||
sheerka.set_isa(context, sheerka.new("thousands"), sheerka.new("number"))
|
||||
|
||||
cls.shared_ontology = sheerka.get_ontology(context)
|
||||
sheerka.pop_ontology()
|
||||
|
||||
@staticmethod
|
||||
def update_bnf(context, concept):
|
||||
bnf_parser = BnfDefinitionParser()
|
||||
@@ -147,19 +149,38 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
return concept
|
||||
|
||||
def init_parser(self, my_concepts_map=None, init_from_sheerka=False, **kwargs):
|
||||
if my_concepts_map is not None:
|
||||
sheerka, context, *updated = self.init_concepts(*my_concepts_map.values(), **kwargs)
|
||||
if my_concepts_map is None:
|
||||
sheerka, context = self.init_test().unpack()
|
||||
sheerka.add_ontology(context, self.shared_ontology)
|
||||
else:
|
||||
sheerka, context, *updated = self.init_test().with_concepts(*my_concepts_map.values(), **kwargs).unpack()
|
||||
for i, pair in enumerate(my_concepts_map):
|
||||
my_concepts_map[pair] = updated[i]
|
||||
else:
|
||||
sheerka = TestBnfNodeParser.sheerka
|
||||
context = self.get_context(sheerka)
|
||||
|
||||
parser = BnfNodeParser(sheerka=sheerka) if init_from_sheerka else BnfNodeParser()
|
||||
return sheerka, context, parser
|
||||
|
||||
def exec_get_concepts_sequences(self, my_map, text, expected, multiple_result=False, post_init_concepts=None):
|
||||
sheerka, context, *updated = self.init_concepts(*my_map.values(), create_new=False, singleton=True)
|
||||
def validate_get_concepts_sequences(self, my_map, text, expected, multiple_result=False, post_init_concepts=None):
|
||||
sheerka, context, *updated = self.init_test().with_concepts(*my_map.values(), create_new=False).unpack()
|
||||
sequences = self.exec_get_concepts_sequences(context,
|
||||
my_map,
|
||||
text,
|
||||
expected,
|
||||
multiple_result,
|
||||
post_init_concepts,
|
||||
*updated)
|
||||
return sequences
|
||||
|
||||
@staticmethod
|
||||
def exec_get_concepts_sequences(context,
|
||||
my_map,
|
||||
text,
|
||||
expected,
|
||||
multiple_result=False,
|
||||
post_init_concepts=None,
|
||||
*concepts):
|
||||
sheerka = context.sheerka
|
||||
|
||||
if not multiple_result:
|
||||
expected_array = [compute_expected_array(my_map, text, expected)]
|
||||
else:
|
||||
@@ -169,7 +190,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
post_init_concepts(sheerka, context)
|
||||
|
||||
parser = BnfNodeParser()
|
||||
parser.init_from_concepts(context, updated)
|
||||
parser.init_from_concepts(context, concepts)
|
||||
parser.reset_parser(context, ParserInput(text))
|
||||
|
||||
bnf_parsers_helpers = parser.get_concepts_sequences(context)
|
||||
@@ -179,15 +200,9 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
assert parser_helper.sequence == expected_sequence
|
||||
|
||||
if len(bnf_parsers_helpers) == 1:
|
||||
return sheerka, context, bnf_parsers_helpers[0].sequence
|
||||
return bnf_parsers_helpers[0].sequence
|
||||
else:
|
||||
return sheerka, context, [pe.sequence for pe in bnf_parsers_helpers]
|
||||
|
||||
def validate_get_concepts_sequences(self, my_map, text, expected, multiple_result=False, post_init_concepts=None):
|
||||
sheerka, context, sequences = self.exec_get_concepts_sequences(
|
||||
my_map, text, expected, multiple_result, post_init_concepts
|
||||
)
|
||||
return sequences
|
||||
return [pe.sequence for pe in bnf_parsers_helpers]
|
||||
|
||||
def test_i_cannot_parse_empty_strings(self):
|
||||
sheerka, context, parser = self.init_parser({}, singleton=True)
|
||||
@@ -706,10 +721,11 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
ConceptExpression("foo"),
|
||||
OrderedChoice(StrMatch("one"), StrMatch("two")))),
|
||||
}
|
||||
sheerka, context, *concepts = self.init_test().with_concepts(*my_map.values(), create_new=False).unpack()
|
||||
|
||||
text = "twenty two"
|
||||
expected = [CN("bar", source="twenty two")]
|
||||
sheerka, context, sequences = self.exec_get_concepts_sequences(my_map, text, expected)
|
||||
sequences = self.exec_get_concepts_sequences(context, my_map, text, expected, False, None, *concepts)
|
||||
|
||||
concept_bar = sequences[0].concept
|
||||
assert concept_bar.get_compiled() == {
|
||||
@@ -720,7 +736,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
text = "thirty one"
|
||||
expected = [CN("bar", source="thirty one")]
|
||||
sequences = self.validate_get_concepts_sequences(my_map, text, expected)
|
||||
sequences = self.exec_get_concepts_sequences(context, my_map, text, expected, False, None, *concepts)
|
||||
concept_bar = sequences[0].concept
|
||||
assert concept_bar.get_compiled() == {
|
||||
ConceptParts.BODY: DoNotResolve("thirty one"),
|
||||
@@ -817,7 +833,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
# every obvious cyclic recursion are removed from concept_by_first_keyword dict
|
||||
parser.init_from_concepts(context, my_map.values())
|
||||
assert parser.concepts_by_first_keyword == expected
|
||||
assert sheerka.om.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == expected
|
||||
|
||||
# get_parsing_expression() also returns CHICKEN_AND_EGG
|
||||
parsing_expression = parser.get_parsing_expression(context, my_map["foo"])
|
||||
@@ -842,7 +858,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
# every obvious cyclic recursion are removed from concept_by_first_keyword dict
|
||||
parser.init_from_concepts(context, my_map.values())
|
||||
assert parser.concepts_by_first_keyword == {}
|
||||
assert sheerka.om.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {}
|
||||
|
||||
parsing_expression = parser.get_parsing_expression(context, my_map["foo"])
|
||||
assert sheerka.isinstance(parsing_expression, BuiltinConcepts.CHICKEN_AND_EGG)
|
||||
@@ -868,7 +884,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
# every obvious cyclic recursion are removed from concept_by_first_keyword dict
|
||||
parser.init_from_concepts(context, my_map.values())
|
||||
assert parser.concepts_by_first_keyword == {}
|
||||
assert sheerka.om.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {}
|
||||
|
||||
parsing_expression = parser.get_parsing_expression(context, my_map["foo"])
|
||||
assert sheerka.isinstance(parsing_expression, BuiltinConcepts.CHICKEN_AND_EGG)
|
||||
@@ -1008,8 +1024,8 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
ConceptExpression(my_map["one"], rule_name="one"))
|
||||
|
||||
@pytest.mark.parametrize("expr, text, expected", [
|
||||
(ZeroOrMore(StrMatch("one"), sep=","), "one,", [CNC("foo", source="one"), UTN(",")]),
|
||||
(StrMatch("one"), "one two", [CNC("foo", source="one"), UTN(" two")]),
|
||||
# (ZeroOrMore(StrMatch("one"), sep=","), "one,", [CNC("foo", source="one"), UTN(",")]),
|
||||
# (StrMatch("one"), "one two", [CNC("foo", source="one"), UTN(" two")]),
|
||||
(StrMatch("one"), "two one", [UTN("two "), CNC("foo", source="one")]),
|
||||
])
|
||||
def test_i_can_recognize_unknown_concepts(self, expr, text, expected):
|
||||
@@ -1442,6 +1458,22 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
|
||||
def test_i_can_get_expression_from_concept_name(self, name, expected):
|
||||
assert BnfNodeParser.get_expression_from_concept_name(name) == expected
|
||||
|
||||
def test_i_can_parse_when_multiple_layers(self):
|
||||
sheerka, context, parser = self.init_parser(init_from_sheerka=True)
|
||||
|
||||
# sanity
|
||||
text = "thirty one"
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
assert res.status
|
||||
assert res.value.value == compute_expected_array(cmap, text, [CN("thirties", source=text)])
|
||||
|
||||
# add a layer, I still can parse the text
|
||||
sheerka.push_ontology(context, "new layer")
|
||||
parser = BnfNodeParser(sheerka=sheerka)
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
assert res.status
|
||||
assert res.value.value == compute_expected_array(cmap, text, [CN("thirties", source=text)])
|
||||
|
||||
# @pytest.mark.parametrize("parser_input, expected", [
|
||||
# ("one", [
|
||||
# (True, [CNC("bnf_one", source="one", one="one", body="one")]),
|
||||
|
||||
@@ -18,19 +18,26 @@ cmap = {
|
||||
|
||||
|
||||
class TestDefFormatRuleParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka = None
|
||||
shared_ontology = None
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
t = cls()
|
||||
cls.sheerka, context, _ = t.init_parser(cmap)
|
||||
init_test_helper = cls().init_test(cache_only=False, ontology="#TestDefFormatRuleParser#")
|
||||
sheerka, context, *updated = init_test_helper.with_concepts(*cmap.values(), create_new=True).unpack()
|
||||
for i, concept_name in enumerate(cmap):
|
||||
cmap[concept_name] = updated[i]
|
||||
|
||||
def init_parser(self, concepts_map=None):
|
||||
if concepts_map is not None:
|
||||
sheerka, context, *concepts = self.init_concepts(*concepts_map.values(), create_new=True)
|
||||
cls.shared_ontology = sheerka.get_ontology(context)
|
||||
sheerka.pop_ontology()
|
||||
|
||||
def init_parser(self, my_concepts_map=None, **kwargs):
|
||||
if my_concepts_map is None:
|
||||
sheerka, context = self.init_test().unpack()
|
||||
sheerka.add_ontology(context, self.shared_ontology)
|
||||
else:
|
||||
sheerka = TestDefFormatRuleParser.sheerka
|
||||
context = self.get_context(sheerka)
|
||||
sheerka, context, *updated = self.init_test().with_concepts(*my_concepts_map.values(), **kwargs).unpack()
|
||||
for i, pair in enumerate(my_concepts_map):
|
||||
my_concepts_map[pair] = updated[i]
|
||||
|
||||
parser = DefFormatRuleParser()
|
||||
return sheerka, context, parser
|
||||
@@ -134,4 +141,3 @@ class TestDefFormatRuleParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
assert res.status
|
||||
assert format_ast == expected
|
||||
|
||||
|
||||
@@ -18,19 +18,26 @@ cmap = {
|
||||
|
||||
|
||||
class TestFunctionParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka = None
|
||||
shared_ontology = None
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
t = cls()
|
||||
cls.sheerka, context, _ = t.init_parser(cmap)
|
||||
init_test_helper = cls().init_test(cache_only=False, ontology="#TestFunctionParser#")
|
||||
sheerka, context, *updated = init_test_helper.with_concepts(*cmap.values(), create_new=True).unpack()
|
||||
for i, concept_name in enumerate(cmap):
|
||||
cmap[concept_name] = updated[i]
|
||||
|
||||
def init_parser(self, concepts_map=None):
|
||||
if concepts_map is not None:
|
||||
sheerka, context, *concepts = self.init_concepts(*concepts_map.values(), create_new=True)
|
||||
cls.shared_ontology = sheerka.get_ontology(context)
|
||||
sheerka.pop_ontology()
|
||||
|
||||
def init_parser(self, my_concepts_map=None, **kwargs):
|
||||
if my_concepts_map is None:
|
||||
sheerka, context = self.init_test().unpack()
|
||||
sheerka.add_ontology(context, self.shared_ontology)
|
||||
else:
|
||||
sheerka = TestFunctionParser.sheerka
|
||||
context = self.get_context(sheerka)
|
||||
sheerka, context, *updated = self.init_test().with_concepts(*my_concepts_map.values(), **kwargs).unpack()
|
||||
for i, pair in enumerate(my_concepts_map):
|
||||
my_concepts_map[pair] = updated[i]
|
||||
|
||||
parser = FunctionParser()
|
||||
return sheerka, context, parser
|
||||
@@ -244,4 +251,3 @@ class TestFunctionParser(TestUsingMemoryBasedSheerka):
|
||||
assert isinstance(concept.get_compiled()["b"], list)
|
||||
for item in concept.get_compiled()["b"]:
|
||||
assert sheerka.isinstance(item, BuiltinConcepts.RETURN_VALUE)
|
||||
|
||||
|
||||
@@ -5,23 +5,26 @@ from parsers.RuleParser import RuleParser, RuleNotFoundError
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
my_rules = {("__rets", "list(__rets)")}
|
||||
my_rules = [("__rets", "list(__rets)")]
|
||||
|
||||
|
||||
class TestRuleParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka = None
|
||||
shared_ontology = None
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
t = cls()
|
||||
cls.sheerka, context, _ = t.init_parser(my_rules)
|
||||
init_test_helper = cls().init_test(cache_only=False, ontology="#TestRuleParser#")
|
||||
sheerka, context, *updated = init_test_helper.with_rules(*my_rules).unpack()
|
||||
|
||||
def init_parser(self, rules=None):
|
||||
if rules is not None:
|
||||
sheerka, context, *concepts = self.init_format_rules(*rules)
|
||||
cls.shared_ontology = sheerka.get_ontology(context)
|
||||
sheerka.pop_ontology()
|
||||
|
||||
def init_parser(self, rules=None, **kwargs):
|
||||
if rules is None:
|
||||
sheerka, context = self.init_test().unpack()
|
||||
sheerka.add_ontology(context, self.shared_ontology)
|
||||
else:
|
||||
sheerka = TestRuleParser.sheerka
|
||||
context = self.get_context(sheerka)
|
||||
sheerka, context, *updated = self.init_test().with_rules(*rules, **kwargs).unpack()
|
||||
|
||||
parser = RuleParser()
|
||||
return sheerka, context, parser
|
||||
|
||||
@@ -11,10 +11,9 @@ from tests.parsers.parsers_utils import compute_expected_array
|
||||
|
||||
class TestAtomsParser(TestUsingMemoryBasedSheerka):
|
||||
def init_parser(self, my_map, create_new=False, singleton=True, use_sheerka=False):
|
||||
sheerka, context, *updated_concepts = self.init_concepts(
|
||||
sheerka, context, *updated_concepts = self.init_test().with_concepts(
|
||||
*my_map.values(),
|
||||
create_new=create_new,
|
||||
singleton=singleton)
|
||||
create_new=create_new).unpack()
|
||||
|
||||
if use_sheerka:
|
||||
parser = SequenceNodeParser(sheerka=sheerka)
|
||||
@@ -41,16 +41,14 @@ cmap = {
|
||||
|
||||
|
||||
class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka = None
|
||||
shared_ontology = None
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
t = TestSyaNodeParser()
|
||||
TestSyaNodeParser.sheerka, context, _ = t.init_parser(
|
||||
cmap,
|
||||
singleton=False,
|
||||
create_new=True,
|
||||
init_from_sheerka=True)
|
||||
init_test_helper = cls().init_test(cache_only=False, ontology="#TestSyaNodeParser#")
|
||||
sheerka, context, *updated = init_test_helper.with_concepts(*cmap.values(), create_new=True).unpack()
|
||||
for i, concept_name in enumerate(cmap):
|
||||
cmap[concept_name] = updated[i]
|
||||
|
||||
cmap["plus"].set_prop(BuiltinConcepts.ASSOCIATIVITY, "right")
|
||||
cmap["mult"].set_prop(BuiltinConcepts.ASSOCIATIVITY, "right")
|
||||
@@ -66,35 +64,24 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
cmap["minus"],
|
||||
CONCEPT_COMPARISON_CONTEXT)
|
||||
|
||||
# TestSyaNodeParser.sheerka.test_only_force_sya_def(context, [
|
||||
# (cmap["plus"].id, 5, SyaAssociativity.Right),
|
||||
# (cmap["mult"].id, 10, SyaAssociativity.Right),
|
||||
# (cmap["minus"].id, 5, SyaAssociativity.Right)])
|
||||
cls.shared_ontology = sheerka.get_ontology(context)
|
||||
sheerka.pop_ontology()
|
||||
|
||||
def init_parser(self,
|
||||
my_concepts_map=None,
|
||||
sya_def=None,
|
||||
post_init_concepts=None,
|
||||
**kwargs):
|
||||
|
||||
if my_concepts_map is not None:
|
||||
# a new concept map is given
|
||||
# use it but
|
||||
# do not instantiate a new sheerka
|
||||
# do not update / init from sheerka
|
||||
if 'singleton' not in kwargs:
|
||||
kwargs["singleton"] = True
|
||||
init_from_sheerka = kwargs.get("init_from_sheerka", False)
|
||||
sheerka, context, *concepts = self.init_concepts(*my_concepts_map.values(), **kwargs)
|
||||
else:
|
||||
# No custom concept map is given -> Use the global cmap
|
||||
# Sheerka is already initialized (the class instance)
|
||||
# Use it to initialize the parser
|
||||
init_from_sheerka = kwargs.get("init_from_sheerka", True)
|
||||
sheerka = TestSyaNodeParser.sheerka
|
||||
context = self.get_context(sheerka)
|
||||
if my_concepts_map is None:
|
||||
sheerka, context = self.init_test().unpack()
|
||||
sheerka.add_ontology(context, self.shared_ontology)
|
||||
concepts = cmap.values()
|
||||
ALL_ATTRIBUTES.clear()
|
||||
init_from_sheerka = kwargs.get("init_from_sheerka", True)
|
||||
else:
|
||||
sheerka, context, *concepts = self.init_test().with_concepts(*my_concepts_map.values(), **kwargs).unpack()
|
||||
for i, pair in enumerate(my_concepts_map):
|
||||
my_concepts_map[pair] = concepts[i]
|
||||
init_from_sheerka = kwargs.get("init_from_sheerka", False)
|
||||
|
||||
if post_init_concepts:
|
||||
post_init_concepts(sheerka, context)
|
||||
@@ -112,7 +99,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
parser = SyaNodeParser()
|
||||
if my_concepts_map:
|
||||
parser.init_from_concepts(context, concepts, sya=sya_def_to_use)
|
||||
|
||||
return sheerka, context, parser
|
||||
|
||||
@pytest.mark.parametrize("expression, expected_sequences", [
|
||||
@@ -1346,3 +1332,27 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
assert actual.function == resolved_function_name[0]
|
||||
else:
|
||||
assert actual.function is None
|
||||
|
||||
def test_i_can_parse_when_multiple_ontologies(self):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
|
||||
text = "suffixed 1 + 1"
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
wrapper = res.body
|
||||
lexer_nodes = res.body.body
|
||||
|
||||
assert res.status
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert lexer_nodes == [CN(cmap["suffixed"], 0, 6, source=text)]
|
||||
|
||||
# add an ontology layer and make sure will still can parse
|
||||
sheerka.push_ontology(context, "new ontology")
|
||||
parser = SyaNodeParser(sheerka=sheerka)
|
||||
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
wrapper = res.body
|
||||
lexer_nodes = res.body.body
|
||||
|
||||
assert res.status
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert lexer_nodes == [CN(cmap["suffixed"], 0, 6, source=text)]
|
||||
|
||||
@@ -70,25 +70,31 @@ concepts_map = {
|
||||
|
||||
|
||||
class TestUnrecognizedNodeParser(TestUsingMemoryBasedSheerka):
|
||||
sheerka = None
|
||||
shared_ontology = None
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
t = TestUnrecognizedNodeParser()
|
||||
TestUnrecognizedNodeParser.sheerka, context, _ = t.init_parser(concepts_map, create_new=True)
|
||||
TestUnrecognizedNodeParser.sheerka.test_only_force_sya_def(context, [
|
||||
(concepts_map["mult"].id, 20, SyaAssociativity.Right),
|
||||
(concepts_map["plus"].id, 10, SyaAssociativity.Right),
|
||||
])
|
||||
init_test_helper = cls().init_test(cache_only=False, ontology="#TestUnrecognizedNodeParser#")
|
||||
sheerka, context, *updated = init_test_helper.with_concepts(*concepts_map.values(), create_new=True).unpack()
|
||||
for i, concept_name in enumerate(concepts_map):
|
||||
concepts_map[concept_name] = updated[i]
|
||||
|
||||
sheerka.set_is_greater_than(context,
|
||||
BuiltinConcepts.PRECEDENCE,
|
||||
concepts_map["mult"],
|
||||
concepts_map["plus"], 'Sya')
|
||||
|
||||
cls.shared_ontology = sheerka.get_ontology(context)
|
||||
sheerka.pop_ontology()
|
||||
|
||||
def init_parser(self, my_concepts_map=None, **kwargs):
|
||||
if my_concepts_map:
|
||||
sheerka, context, *updated_concepts = self.init_concepts(*my_concepts_map.values(), **kwargs)
|
||||
for i, pair in enumerate(my_concepts_map):
|
||||
my_concepts_map[pair] = updated_concepts[i]
|
||||
if my_concepts_map is None:
|
||||
sheerka, context = self.init_test().unpack()
|
||||
sheerka.add_ontology(context, self.shared_ontology)
|
||||
else:
|
||||
sheerka = TestUnrecognizedNodeParser.sheerka
|
||||
context = self.get_context(sheerka)
|
||||
sheerka, context, *updated = self.init_test().with_concepts(*my_concepts_map.values(), **kwargs).unpack()
|
||||
for i, pair in enumerate(my_concepts_map):
|
||||
my_concepts_map[pair] = updated[i]
|
||||
|
||||
parser = UnrecognizedNodeParser()
|
||||
return sheerka, context, parser
|
||||
@@ -186,12 +192,14 @@ class TestUnrecognizedNodeParser(TestUsingMemoryBasedSheerka):
|
||||
assert res.body.concept.get_compiled()["a"][0].body.source == "1 "
|
||||
|
||||
assert res.body.concept.get_compiled()["b"] == concepts_map["mult"]
|
||||
assert sheerka.isinstance(res.body.concept.get_compiled()["b"].get_compiled()["a"][0], BuiltinConcepts.RETURN_VALUE)
|
||||
assert sheerka.isinstance(res.body.concept.get_compiled()["b"].get_compiled()["a"][0],
|
||||
BuiltinConcepts.RETURN_VALUE)
|
||||
assert res.body.concept.get_compiled()["b"].get_compiled()["a"][0].status
|
||||
assert res.body.concept.get_compiled()["b"].get_compiled()["a"][0].who == "parsers.Python"
|
||||
assert res.body.concept.get_compiled()["b"].get_compiled()["a"][0].body.source == " 2 "
|
||||
|
||||
assert sheerka.isinstance(res.body.concept.get_compiled()["b"].get_compiled()["b"][0], BuiltinConcepts.RETURN_VALUE)
|
||||
assert sheerka.isinstance(res.body.concept.get_compiled()["b"].get_compiled()["b"][0],
|
||||
BuiltinConcepts.RETURN_VALUE)
|
||||
assert res.body.concept.get_compiled()["b"].get_compiled()["b"][0].status
|
||||
assert res.body.concept.get_compiled()["b"].get_compiled()["b"][0].who == "parsers.Bnf"
|
||||
expected_nodes = compute_expected_array(
|
||||
@@ -339,7 +347,8 @@ class TestUnrecognizedNodeParser(TestUsingMemoryBasedSheerka):
|
||||
assert sheerka.isinstance(parser_result, BuiltinConcepts.PARSER_RESULT)
|
||||
assert parser_result.source == expression
|
||||
assert len(actual_nodes) == 1
|
||||
assert actual_nodes[0].nodes[0].concept.get_metadata().is_evaluated # 'a plus b' is recognized as concept definition
|
||||
assert actual_nodes[0].nodes[
|
||||
0].concept.get_metadata().is_evaluated # 'a plus b' is recognized as concept definition
|
||||
|
||||
def test_i_can_parse_unrecognized_source_code_with_concept_node_when_var_in_short_term_memory(self):
|
||||
sheerka, context, parser = self.init_parser()
|
||||
|
||||
Reference in New Issue
Block a user