Implemented SheerkaOntology
This commit is contained in:
@@ -6,6 +6,7 @@ from typing import Set
|
||||
import core.utils
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import VARIABLE_PREFIX, Concept, DEFINITION_TYPE_BNF, ConceptParts
|
||||
from core.global_symbols import NotFound
|
||||
from core.rule import Rule
|
||||
from core.tokenizer import TokenKind, Token
|
||||
from parsers.BaseParser import Node, BaseParser, ParsingError
|
||||
@@ -817,12 +818,6 @@ class BaseNodeParser(BaseParser):
|
||||
|
||||
def __init__(self, name, priority, **kwargs):
|
||||
super().__init__(name, priority, yield_eof=True)
|
||||
if 'sheerka' in kwargs:
|
||||
sheerka = kwargs.get("sheerka")
|
||||
self.concepts_by_first_keyword = sheerka.resolved_concepts_by_first_keyword
|
||||
|
||||
else:
|
||||
self.concepts_by_first_keyword = None
|
||||
|
||||
def init_from_concepts(self, context, concepts, **kwargs):
|
||||
"""
|
||||
@@ -832,8 +827,12 @@ class BaseNodeParser(BaseParser):
|
||||
:param concepts
|
||||
:return:
|
||||
"""
|
||||
concepts_by_first_keyword = self.get_concepts_by_first_token(context, concepts).body
|
||||
self.concepts_by_first_keyword = self.resolve_concepts_by_first_keyword(context, concepts_by_first_keyword).body
|
||||
concepts_by_first_keyword = self.compute_concepts_by_first_token(context, concepts).body
|
||||
resolved = self.resolve_concepts_by_first_keyword(context, concepts_by_first_keyword).body
|
||||
|
||||
context.sheerka.om.put(context.sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY,
|
||||
False,
|
||||
resolved)
|
||||
|
||||
def get_concepts(self, token, to_keep, custom=None, to_map=None, strip_quotes=False):
|
||||
"""
|
||||
@@ -858,24 +857,25 @@ class BaseNodeParser(BaseParser):
|
||||
custom_concepts = custom(name) if custom else [] # to get extra concepts using an alternative method
|
||||
|
||||
result = []
|
||||
if name in self.concepts_by_first_keyword:
|
||||
for concept_id in self.concepts_by_first_keyword.get(name):
|
||||
concepts_ids = self.sheerka.om.get(self.sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY, name)
|
||||
if concepts_ids is NotFound:
|
||||
return custom_concepts if custom else None
|
||||
|
||||
concept = self.sheerka.get_by_id(concept_id)
|
||||
for concept_id in concepts_ids:
|
||||
|
||||
if not to_keep(concept):
|
||||
continue
|
||||
concept = self.sheerka.get_by_id(concept_id)
|
||||
|
||||
concept = to_map(concept, self, self.sheerka) if to_map else concept
|
||||
result.append(concept)
|
||||
if not to_keep(concept):
|
||||
continue
|
||||
|
||||
return core.utils.make_unique(result + custom_concepts,
|
||||
lambda c: c.concept.id if hasattr(c, "concept") else c.id)
|
||||
concept = to_map(concept, self, self.sheerka) if to_map else concept
|
||||
result.append(concept)
|
||||
|
||||
return custom_concepts if custom else None
|
||||
return core.utils.make_unique(result + custom_concepts,
|
||||
lambda c: c.concept.id if hasattr(c, "concept") else c.id)
|
||||
|
||||
@staticmethod
|
||||
def get_concepts_by_first_token(context, concepts, use_sheerka=False, previous_entries=None):
|
||||
def compute_concepts_by_first_token(context, concepts, use_sheerka=False, previous_entries=None):
|
||||
"""
|
||||
Create the map describing the first token expected by a concept
|
||||
:param context:
|
||||
@@ -885,7 +885,7 @@ class BaseNodeParser(BaseParser):
|
||||
:return:
|
||||
"""
|
||||
sheerka = context.sheerka
|
||||
res = sheerka.cache_manager.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) if use_sheerka else (previous_entries or {})
|
||||
res = sheerka.om.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) if use_sheerka else (previous_entries or {})
|
||||
for concept in concepts:
|
||||
keywords = BaseNodeParser.get_first_tokens(sheerka, concept)
|
||||
|
||||
@@ -966,7 +966,7 @@ class BaseNodeParser(BaseParser):
|
||||
for concept_id in concepts_in_recursion:
|
||||
# make sure we keep the longest chain
|
||||
old = sheerka.chicken_and_eggs.get(concept_id)
|
||||
if old is None or len(old) < len(ex.concepts):
|
||||
if old is NotFound or len(old) < len(ex.concepts):
|
||||
sheerka.chicken_and_eggs.put(concept_id, concepts_in_recursion)
|
||||
else:
|
||||
res.setdefault(k, []).extend(v)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.global_symbols import NotFound
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from parsers.BaseParser import BaseParser
|
||||
|
||||
@@ -34,11 +35,10 @@ class ShortTermMemoryParser(BaseParser):
|
||||
concept_name = parser_input.as_text()
|
||||
concept = sheerka.get_from_short_term_memory(context, concept_name)
|
||||
|
||||
if concept:
|
||||
if concept is NotFound:
|
||||
body = sheerka.new(BuiltinConcepts.NOT_FOUND, body=concept_name)
|
||||
return sheerka.ret(self.name, False, body)
|
||||
else:
|
||||
# Unlike what is usually done, we directly return the concept, not a ParsingResult of the concept
|
||||
# This is to save the evaluation time cost
|
||||
return sheerka.ret(self.name, True, concept)
|
||||
|
||||
else:
|
||||
body = sheerka.new(BuiltinConcepts.NOT_FOUND, body=concept_name)
|
||||
return sheerka.ret(self.name, False, body)
|
||||
|
||||
@@ -126,15 +126,15 @@ class SyaConceptDef:
|
||||
|
||||
# first, try to look in the parser
|
||||
# it is where to find the data during the unit tests
|
||||
if parser and concept.id in parser.sya_definitions:
|
||||
if parser and concept.id in parser.test_only_sya_definitions:
|
||||
# Manage when precedence and associativity are given in the unit tests
|
||||
sya_def = parser.sya_definitions.get(concept.id)
|
||||
sya_def = parser.test_only_sya_definitions.get(concept.id)
|
||||
if sya_def[0] is not None:
|
||||
sya_concept_def.precedence = sya_def[0]
|
||||
if sya_def[1] is not None:
|
||||
sya_concept_def.associativity = sya_def[1]
|
||||
|
||||
# otherwise, use sheerka
|
||||
# otherwise, use sheerka # KSI 20210109 otherwise or override ??
|
||||
if sheerka:
|
||||
concept_weight = parser.sheerka.get_concepts_weights(BuiltinConcepts.PRECEDENCE, CONCEPT_COMPARISON_CONTEXT)
|
||||
if concept.str_id in concept_weight:
|
||||
@@ -332,7 +332,7 @@ class InFixToPostFix:
|
||||
def _add_debug(self, debug_info: DebugInfo):
|
||||
if debug_info.level is None or (self.enabled_debug_levels and
|
||||
(f"#{self.id}.{debug_info.level}" in self.enabled_debug_levels or
|
||||
"*" in self.enabled_debug_levels)):
|
||||
"*" in self.enabled_debug_levels)):
|
||||
self.debug.append(debug_info)
|
||||
|
||||
def _is_lpar(self, token):
|
||||
@@ -1134,20 +1134,14 @@ class SyaNodeParser(BaseNodeParser):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(SyaNodeParser.NAME, 50, **kwargs)
|
||||
if 'sheerka' in kwargs:
|
||||
sheerka = kwargs.get("sheerka")
|
||||
self.sya_definitions = sheerka.resolved_sya_def
|
||||
|
||||
else:
|
||||
self.concepts_by_first_keyword = {}
|
||||
self.sya_definitions = {}
|
||||
self.test_only_sya_definitions = {}
|
||||
|
||||
def init_from_concepts(self, context, concepts, **kwargs):
|
||||
super().init_from_concepts(context, concepts)
|
||||
|
||||
sya_definitions = kwargs.get("sya", None)
|
||||
if sya_definitions:
|
||||
self.sya_definitions = sya_definitions
|
||||
self.test_only_sya_definitions = sya_definitions
|
||||
|
||||
@staticmethod
|
||||
def _is_eligible(concept):
|
||||
@@ -1431,10 +1425,3 @@ class SyaNodeParser(BaseNodeParser):
|
||||
result.append(infix_to_postfix)
|
||||
|
||||
return result
|
||||
|
||||
# @staticmethod
|
||||
# def init_sheerka(self, sheerka):
|
||||
# if hasattr(BaseNodeParser, "init_sheerka"):
|
||||
# BaseNodeParser.init_sheerka(sheerka)
|
||||
#
|
||||
# # init syadefinitins
|
||||
|
||||
Reference in New Issue
Block a user