import logging
from chatterbot.logic import LogicAdapter
from sugaroid.brain.postprocessor import random_response
from sugaroid.brain.constants import IMITATE
from sugaroid.brain.ooo import Emotion
from sugaroid.brain.preprocessors import normalize
from sugaroid.core.statement import SugaroidStatement
[docs]class ImitateAdapter(LogicAdapter):
"""
Handles statements involving imitations of some sentences
"""
def __init__(self, chatbot, **kwargs):
super().__init__(chatbot, **kwargs)
self.normalized = None
[docs] def can_process(self, statement):
self.normalized = normalize(str(statement))
more_words = len(self.normalized) > 3
logging.info(
"ImitatorSensei: userhistory {}, history: {}".format(
self.chatbot.globals["history"]["user"],
self.chatbot.globals["history"]["total"],
)
)
if (
self.chatbot.globals["history"]["user"][-1]
and self.chatbot.globals["history"]["total"][-1]
and more_words
):
return True
else:
return False
[docs] def process(
self,
statement: SugaroidStatement,
additional_response_selection_parameters=None,
) -> SugaroidStatement:
emotion = Emotion.lol
sim = self.chatbot.lp.similarity(
str(statement), str(self.chatbot.globals["history"]["total"][-1])
)
logging.info(
"ImitatorSensei compared {} and {}. Sim: {}".format(
str(statement), self.chatbot.globals["history"]["user"][-1], sim
)
)
if sim > 0.8:
response = random_response(IMITATE)
confidence = sim
else:
response = "Ok!"
confidence = 0
selected_statement = SugaroidStatement(response, chatbot=True)
selected_statement.confidence = confidence
selected_statement.emotion = emotion
return selected_statement