Fixed BnfNodeParser to allow expressions like 'number hundred' when number is a group
This commit is contained in:
@@ -137,28 +137,38 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
}
|
||||
|
||||
def test_i_can_resolve_when_concepts_are_sets(self):
|
||||
sheerka, context, one, two, three, number, foo = self.init_concepts(
|
||||
sheerka, context, number, *concepts = self.init_concepts(
|
||||
"number",
|
||||
"one",
|
||||
"two",
|
||||
"three",
|
||||
"number",
|
||||
Concept("foo", definition="number three"),
|
||||
"twenty",
|
||||
"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)
|
||||
sheerka.set_isa(context, sheerka.new("two"), number)
|
||||
sheerka.set_isa(context, sheerka.new("twenty"), number)
|
||||
sheerka.set_isa(context, sheerka.new("thirty"), number)
|
||||
sheerka.set_isa(context, sheerka.new("hundred"), number)
|
||||
sheerka.set_isa(context, sheerka.new("twenties"), number)
|
||||
sheerka.set_isa(context, sheerka.new("hundreds"), number)
|
||||
|
||||
cbfk = BaseNodeParser.get_concepts_by_first_token(context, [one, two, three, number, foo]).body
|
||||
sheerka.concepts_grammars.clear() # reset all the grammar to simulate Sheerka restart
|
||||
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, cbfk)
|
||||
# cbft : concept_by_first_token (I usually don't use abbreviation)
|
||||
cbft = BaseNodeParser.get_concepts_by_first_token(context, [number] + concepts).body
|
||||
resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(context, cbft)
|
||||
|
||||
assert resolved_ret_val.status
|
||||
assert resolved_ret_val.body == {
|
||||
"one": ["1001", "1005"],
|
||||
"two": ["1002", "1005"],
|
||||
"three": ["1003"],
|
||||
"number": ["1004"],
|
||||
'number': ['1001'],
|
||||
'one': ['1002', '1007'],
|
||||
'two': ['1003', '1007'],
|
||||
'twenty': ['1004', '1006', '1007'],
|
||||
'hundred': ['1005', '1007'],
|
||||
}
|
||||
|
||||
def test_concepts_are_defined_once(self):
|
||||
@@ -196,36 +206,58 @@ class TestBaseNodeParser(TestUsingMemoryBasedSheerka):
|
||||
"one": ["1001", "1002"],
|
||||
}
|
||||
|
||||
# def tests_i_can_detect_direct_recursion(self):
|
||||
# sheerka = self.get_sheerka()
|
||||
# good = self.get_concept(sheerka, "good")
|
||||
# foo = self.get_concept(sheerka, "foo", ConceptExpression("bar"))
|
||||
# bar = self.get_concept(sheerka, "bar", ConceptExpression("foo"))
|
||||
#
|
||||
# concepts_by_first_keywords = BaseNodeParser.get_concepts_by_first_token(sheerka, [good, foo, bar]).body
|
||||
#
|
||||
# resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(sheerka, concepts_by_first_keywords)
|
||||
# assert resolved_ret_val.status
|
||||
# assert resolved_ret_val.body == {
|
||||
# "good": ["1001"],
|
||||
# BuiltinConcepts.CHICKEN_AND_EGG: ["1002", "1003"]
|
||||
# }
|
||||
#
|
||||
# def test_i_can_detect_indirect_infinite_recursion(self):
|
||||
# sheerka = self.get_sheerka()
|
||||
# good = self.get_concept(sheerka, "good")
|
||||
# one = self.get_concept(sheerka, "one", ConceptExpression("two"))
|
||||
# two = self.get_concept(sheerka, "two", ConceptExpression("three"))
|
||||
# three = self.get_concept(sheerka, "three", ConceptExpression("two"))
|
||||
#
|
||||
# concepts_by_first_keywords = BaseNodeParser.get_concepts_by_first_token(sheerka, [good, one, two, three]).body
|
||||
#
|
||||
# resolved_ret_val = BaseNodeParser.resolve_concepts_by_first_keyword(sheerka, concepts_by_first_keywords)
|
||||
# assert resolved_ret_val.status
|
||||
# assert resolved_ret_val.body == {
|
||||
# "good": ["1001"],
|
||||
# BuiltinConcepts.CHICKEN_AND_EGG: ["1002", "1004", "1003"]
|
||||
# }
|
||||
def tests_i_can_detect_direct_recursion(self):
|
||||
sheerka, context, good, foo, bar = self.init_concepts(
|
||||
"good",
|
||||
self.bnf_concept("foo", ConceptExpression("bar")),
|
||||
self.bnf_concept("bar", ConceptExpression("foo")),
|
||||
)
|
||||
|
||||
concepts_by_first_keywords = BaseNodeParser.get_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 == {
|
||||
"good": ["1001"],
|
||||
}
|
||||
assert sheerka.chicken_and_eggs.get(foo.id) == {foo.id, bar.id}
|
||||
assert sheerka.chicken_and_eggs.get(bar.id) == {foo.id, bar.id}
|
||||
|
||||
def test_i_can_detect_indirect_infinite_recursion(self):
|
||||
sheerka, context, good, one, two, three = self.init_concepts(
|
||||
"good",
|
||||
self.bnf_concept("one", ConceptExpression("two")),
|
||||
self.bnf_concept("two", ConceptExpression("three")),
|
||||
self.bnf_concept("three", ConceptExpression("two")),
|
||||
)
|
||||
|
||||
concepts_by_first_keywords = BaseNodeParser.get_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 == {
|
||||
"good": ["1001"],
|
||||
}
|
||||
assert sheerka.chicken_and_eggs.get(one.id) == {one.id, two.id, three.id}
|
||||
assert sheerka.chicken_and_eggs.get(two.id) == {one.id, two.id, three.id}
|
||||
assert sheerka.chicken_and_eggs.get(three.id) == {one.id, two.id, three.id}
|
||||
|
||||
def test_i_can_detect_the_longest_infinite_recursion_chain(self):
|
||||
sheerka, context, good, one, two, three = self.init_concepts(
|
||||
"good",
|
||||
self.bnf_concept("two", ConceptExpression("three")),
|
||||
self.bnf_concept("three", ConceptExpression("two")),
|
||||
self.bnf_concept("one", ConceptExpression("three")),
|
||||
)
|
||||
|
||||
concepts_by_first_keywords = BaseNodeParser.get_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 == {
|
||||
"good": ["1001"],
|
||||
}
|
||||
assert sheerka.chicken_and_eggs.get(one.id) == {one.id, two.id, three.id}
|
||||
assert sheerka.chicken_and_eggs.get(two.id) == {one.id, two.id, three.id}
|
||||
assert sheerka.chicken_and_eggs.get(three.id) == {one.id, two.id, three.id}
|
||||
|
||||
#
|
||||
# def test_i_can_detect_infinite_recursion_from_ordered_choice(self):
|
||||
# sheerka = self.get_sheerka()
|
||||
|
||||
Reference in New Issue
Block a user