Refactored to allow ConceptEvaluator
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
from core.sheerka import ReturnValue
|
||||
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
|
||||
from parsers.BaseParser import BaseParser
|
||||
from core.tokenizer import Tokenizer, Keywords, TokenKind
|
||||
from core.concept import Concept
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ExactConceptParser(BaseParser):
|
||||
@@ -15,11 +18,17 @@ class ExactConceptParser(BaseParser):
|
||||
BaseParser.__init__(self, "ConceptParser")
|
||||
|
||||
def parse(self, context, text):
|
||||
"""
|
||||
text can be string, but text can also be an list of tokens
|
||||
:param context:
|
||||
:param text:
|
||||
:return:
|
||||
"""
|
||||
res = []
|
||||
sheerka = context.sheerka
|
||||
words = self.get_words(text)
|
||||
if len(words) > self.MAX_WORDS_SIZE:
|
||||
return ReturnValue(self.name, False, sheerka.new(sheerka.CONCEPT_TOO_LONG_CONCEPT_NAME))
|
||||
return ReturnValueConcept(self.name, False, sheerka.new(BuiltinConcepts.CONCEPT_TOO_LONG, obj=text))
|
||||
|
||||
recognized = False
|
||||
for combination in self.combinations(words):
|
||||
@@ -30,24 +39,27 @@ class ExactConceptParser(BaseParser):
|
||||
# That will depend on the context
|
||||
# Let's return a new one for now and see if it works
|
||||
concept = sheerka.new(concept_key)
|
||||
if not sheerka.isinstance(concept, sheerka.UNKNOWN_CONCEPT_NAME):
|
||||
if not sheerka.isinstance(concept, BuiltinConcepts.UNKNOWN_CONCEPT):
|
||||
# update the properties if needed
|
||||
for i, token in enumerate(combination):
|
||||
if token.startswith(Concept.PROPERTY_PREFIX):
|
||||
index = int(token[len(Concept.PROPERTY_PREFIX):])
|
||||
concept.set_prop_by_index(index, words[i])
|
||||
res.append(ReturnValue(self.name, True, concept))
|
||||
res.append(ReturnValueConcept(self.name, True, concept))
|
||||
log.debug(f"Recognized '{text}' as '{concept}'")
|
||||
recognized = True
|
||||
|
||||
if recognized:
|
||||
return res
|
||||
|
||||
return ReturnValue(self.name, False, sheerka.new(sheerka.UNKNOWN_CONCEPT_NAME, body=text))
|
||||
log.debug(f"Failed to recognize {words}")
|
||||
return ReturnValueConcept(self.name, False, sheerka.new(BuiltinConcepts.UNKNOWN_CONCEPT, obj=text))
|
||||
|
||||
@staticmethod
|
||||
def get_words(text):
|
||||
tokens = iter(Tokenizer(text)) if isinstance(text, str) else text
|
||||
res = []
|
||||
for t in iter(Tokenizer(text)):
|
||||
for t in tokens:
|
||||
if t.type == TokenKind.EOF:
|
||||
break
|
||||
if t.type == TokenKind.NEWLINE or t.type == TokenKind.WHITESPACE:
|
||||
|
||||
Reference in New Issue
Block a user