51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
from core.builtin_concepts import BuiltinConcepts
|
|
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
|
|
|
|
|
class PrepareEvalQuestionEvaluator(OneReturnValueEvaluator):
|
|
"""
|
|
To recognize when the user input is a question
|
|
"""
|
|
|
|
NAME = "PrepareEvalQuestion"
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(self.NAME, [BuiltinConcepts.BEFORE_PARSING], 90)
|
|
self.question = None
|
|
|
|
def reset(self):
|
|
self.question = None
|
|
|
|
def matches(self, context, return_value):
|
|
if not (return_value.status and
|
|
context.sheerka.isinstance(return_value.body, BuiltinConcepts.USER_INPUT) and
|
|
isinstance(return_value.body.body, str)):
|
|
return False
|
|
|
|
text = return_value.body.body.strip()
|
|
if not (text.startswith("question(") and text.endswith(")")):
|
|
return False
|
|
|
|
self.question = text[9:-1].strip()
|
|
if self.question == "":
|
|
return False
|
|
|
|
return True
|
|
|
|
def eval(self, context, return_value):
|
|
sheerka = context.sheerka
|
|
|
|
new_text_to_parse = sheerka.ret(
|
|
self.name,
|
|
True, sheerka.new(BuiltinConcepts.USER_INPUT, body=self.question, user_name=context.event.user_id))
|
|
|
|
root = context.get_parents(lambda ec: ec.action in (BuiltinConcepts.EVALUATING_CONCEPT,
|
|
BuiltinConcepts.PROCESS_INPUT))
|
|
root = root[0] if root else context
|
|
root.add_to_protected_hints(BuiltinConcepts.EVAL_QUESTION_REQUESTED)
|
|
root.add_to_protected_hints(BuiltinConcepts.EVAL_UNTIL_SUCCESS_REQUESTED)
|
|
root.add_to_protected_hints(BuiltinConcepts.EVAL_BODY_REQUESTED)
|
|
root.add_to_protected_hints(BuiltinConcepts.RETURN_BODY_REQUESTED)
|
|
|
|
return new_text_to_parse
|