Fixed #43 : BnfNodeParser: I can recognize when multiple level of ISA
Fixed #44 : BnfNodeParser: I must simplify results when multiple levels of ISA Fixed #45 : Dynamic variables cannot be parsed at restart Fixed #46 : Concepts variables values are transformed into list by default Fixed #47 : SheerkaAdmin. Add min, max, mean time when restoring files
This commit is contained in:
@@ -115,6 +115,12 @@ class NonTerminalNode(ParseTreeNode):
|
||||
res = f"{self.parsing_expression.concept}=>" if isinstance(self.parsing_expression, ConceptExpression) else ""
|
||||
return res + ".".join([c.get_debug() for c in self.children])
|
||||
|
||||
def get_depth(self):
|
||||
if isinstance(self.parsing_expression, ConceptExpression):
|
||||
return 1 + max([c.get_depth() for c in self.children])
|
||||
else:
|
||||
return max([c.get_depth() for c in self.children])
|
||||
|
||||
|
||||
class TerminalNode(ParseTreeNode):
|
||||
"""
|
||||
@@ -150,6 +156,9 @@ class TerminalNode(ParseTreeNode):
|
||||
def get_debug(self):
|
||||
return str(self.value)
|
||||
|
||||
def get_depth(self):
|
||||
return 0
|
||||
|
||||
|
||||
class MultiNode:
|
||||
""""
|
||||
@@ -246,6 +255,12 @@ class ParsingContext:
|
||||
res = f"ParsingContext('{self.node.get_debug()}', pos={self.pos})"
|
||||
return res
|
||||
|
||||
def get_depth(self):
|
||||
if isinstance(self.node, list):
|
||||
return max([n.get_depth() for n in self.node])
|
||||
else:
|
||||
return self.node.get_depth()
|
||||
|
||||
|
||||
class ParsingExpression:
|
||||
log_sink = []
|
||||
@@ -738,8 +753,9 @@ class UnOrderedChoice(ParsingExpression):
|
||||
|
||||
if parser_helper.debugger.is_enabled():
|
||||
debug_prefix = self.debug_prefix("UnOrderedChoice", parser_helper)
|
||||
debug_text = {"pos": parser_helper.pos, "text": self.debug_remaining_text(parser_helper)}
|
||||
parser_helper.debug_concept(debug_prefix, raw=f"{CCM['green']}{debug_text}{CCM['reset']}")
|
||||
debug_vars = {"pos": parser_helper.pos, "text": self.debug_remaining_text(parser_helper)}
|
||||
debug_text = self.debug_to_raw(debug_vars)
|
||||
parser_helper.debug_concept(debug_prefix, color="cyan", raw=debug_text)
|
||||
|
||||
debug_text = ""
|
||||
for e in self.nodes:
|
||||
@@ -772,16 +788,52 @@ class UnOrderedChoice(ParsingExpression):
|
||||
|
||||
parser_helper.seek(parsing_contexts[0].pos)
|
||||
|
||||
if len(parsing_contexts) == 1:
|
||||
return parsing_contexts[0].node
|
||||
# Try to simplify the parsing_context
|
||||
simplified_parsing_contexts = self.simplify(parsing_contexts)
|
||||
|
||||
if parser_helper.debugger.is_enabled() and len(simplified_parsing_contexts) != len(parsing_contexts):
|
||||
parser_helper.debug_concept(debug_prefix, simplified=simplified_parsing_contexts)
|
||||
|
||||
if len(simplified_parsing_contexts) == 1:
|
||||
return simplified_parsing_contexts[0].node
|
||||
else:
|
||||
parsing_contexts.sort(key=attrgetter("pos"), reverse=True)
|
||||
return MultiNode(parsing_contexts)
|
||||
simplified_parsing_contexts.sort(key=attrgetter("pos"), reverse=True)
|
||||
return MultiNode(simplified_parsing_contexts)
|
||||
|
||||
def __repr__(self):
|
||||
to_str = "# ".join(repr(n) for n in self.elements)
|
||||
return self.add_rule_name_if_needed(f"({to_str})")
|
||||
|
||||
@staticmethod
|
||||
def simplify(parsing_contexts: List[ParsingContext]):
|
||||
"""
|
||||
Try to remove redundant parsing context
|
||||
for example, if
|
||||
color is an adjective
|
||||
red is an adjective
|
||||
red is a color
|
||||
when parsing 'red' we will receive two parsing context
|
||||
one for 'red'
|
||||
one for 'color' -> 'red'
|
||||
|
||||
The second one should be discarded
|
||||
:param parsing_contexts:
|
||||
:return:
|
||||
"""
|
||||
if len(parsing_contexts) == 1:
|
||||
return parsing_contexts
|
||||
|
||||
by_target = {}
|
||||
for pc in parsing_contexts:
|
||||
by_target.setdefault(pc.node.source, []).append((pc, pc.get_depth()))
|
||||
|
||||
res = []
|
||||
for k, tuple_pc_pc_depth in by_target.items():
|
||||
min_depth = min([pc_depth for pc, pc_depth in tuple_pc_pc_depth])
|
||||
res.extend([pc for pc, pc_depth in tuple_pc_pc_depth if pc_depth == min_depth])
|
||||
|
||||
return res
|
||||
|
||||
|
||||
class Optional(ParsingExpression):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user