Refactored Caching, Refactored BnfNodeParser, Introduced Sphinx

This commit is contained in:
2020-05-12 17:21:10 +02:00
parent 7d3a490bc5
commit 6e343ba996
110 changed files with 13865 additions and 7540 deletions
+32 -29
View File
@@ -6,19 +6,19 @@ from parsers.ExactConceptParser import ExactConceptParser
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
def metadata_prop(concept, prop_name):
for name, value in concept.metadata.props:
def variable_def(concept, prop_name):
for name, value in concept.metadata.variables:
if name == prop_name:
return value
return None
def get_concept(name, variables):
def get_concept(name, variables=None):
c = Concept(name=name)
if variables:
for v in variables:
c.def_prop(v)
c.def_var(v)
c.init_key()
return c
@@ -50,9 +50,9 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
# TODO: the last tuple is not possible, so the algo can be improved
def test_i_can_recognize_a_simple_concept(self):
context = self.get_context()
concept = get_concept("hello world", [])
context.sheerka.add_in_cache(concept)
sheerka = self.get_sheerka(singleton=True)
context = self.get_context(sheerka)
concept = self.create_concept_lite(sheerka, "hello world")
source = "hello world"
results = ExactConceptParser().parse(context, source)
@@ -64,9 +64,10 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
assert not concept_found.metadata.need_validation
def test_i_can_recognize_concepts_defined_several_times(self):
context = self.get_context()
context.sheerka.add_in_cache(get_concept("hello world", []))
context.sheerka.add_in_cache(get_concept("hello a", ["a"]))
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"])
source = "hello world"
results = ExactConceptParser().parse(context, source)
@@ -76,7 +77,7 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
assert results[0].status
assert results[0].value.value.name == "hello a"
assert metadata_prop(results[0].value.value, "a") == "world"
assert variable_def(results[0].value.value, "a") == "world"
assert results[0].value.value.metadata.need_validation
assert results[1].status
@@ -84,9 +85,10 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
assert not results[1].value.value.metadata.need_validation
def test_i_can_recognize_a_concept_with_variables(self):
context = self.get_context()
concept = get_concept("a + b", ["a", "b"])
context.sheerka.add_in_cache(concept)
sheerka = self.get_sheerka(singleton=True)
context = self.get_context(sheerka)
concept = self.create_concept_lite(sheerka, "a + b", ["a", "b"])
source = "10 + 5"
results = ExactConceptParser().parse(context, source)
@@ -95,46 +97,47 @@ class TestExactConceptParser(TestUsingMemoryBasedSheerka):
concept_found = results[0].value.value
assert concept_found.key == concept.key
assert metadata_prop(concept_found, "a") == "10"
assert metadata_prop(concept_found, "b") == "5"
assert variable_def(concept_found, "a") == "10"
assert variable_def(concept_found, "b") == "5"
assert concept_found.metadata.need_validation
def test_i_can_recognize_a_concept_with_duplicate_variables(self):
context = self.get_context()
concept = get_concept("a + b + a", ["a", "b"])
context.sheerka.cache_by_key[concept.key] = concept
sheerka = self.get_sheerka(singleton=True)
context = self.get_context(sheerka)
concept = self.create_concept_lite(sheerka, "a + b + a", ["a", "b"])
source = "10 + 5 + 10"
results = ExactConceptParser().parse(context, source)
results = ExactConceptParser(max_word_size=5).parse(context, source)
assert len(results) == 1
assert results[0].status
concept_found = results[0].value.value
assert concept_found.key == concept.key
assert metadata_prop(concept_found, "a") == "10"
assert metadata_prop(concept_found, "b") == "5"
assert variable_def(concept_found, "a") == "10"
assert variable_def(concept_found, "b") == "5"
assert concept_found.metadata.need_validation
def test_i_can_manage_unknown_concept(self):
context = self.get_context()
source = "def concept hello world" # this is not a concept by itself
context = self.get_context(self.get_sheerka(singleton=True))
source = "def concept hello" # this is not a concept by itself
res = ExactConceptParser().parse(context, source)
assert not res.status
assert context.sheerka.isinstance(res.value, BuiltinConcepts.UNKNOWN_CONCEPT)
assert res.value.body == "def concept hello world"
assert res.value.body == "def concept hello"
def test_i_can_detect_concepts_too_long(self):
context = self.get_context()
source = "a very very long concept that cannot be an unique one"
context = self.get_context(self.get_sheerka(singleton=True))
source = "a very very long concept"
res = ExactConceptParser().parse(context, source)
assert not res.status
assert context.sheerka.isinstance(res.value, BuiltinConcepts.CONCEPT_TOO_LONG)
assert res.value.body == "a very very long concept that cannot be an unique one"
assert res.value.body == "a very very long concept"
def test_i_can_detect_concept_from_tokens(self):
context = self.get_context()
context = self.get_context(self.get_sheerka(singleton=True))
concept = get_concept("hello world", [])
context.sheerka.add_in_cache(concept)