import logging
from chatterbot.logic import LogicAdapter
from chatterbot.trainers import ListTrainer
from sugaroid.brain.postprocessor import random_response, any_in
from sugaroid.brain.constants import (
ASK_AND_YOU_SHALL_RECEIVE,
SEEK_AND_YOU_SHALL_FIND,
THANK,
)
from sugaroid.brain.ooo import Emotion
from sugaroid.core.statement import SugaroidStatement
[docs]class InterruptAdapter(LogicAdapter):
__adapter__ = "interr"
def __init__(self, chatbot, **kwargs):
super().__init__(chatbot, **kwargs)
self.tokenized = None
self.nn = False
[docs] def can_process(self, statement):
if self.chatbot.interrupt == 2:
logging.info("InterruptAdapter: Found Discord")
self.tokenized = self.chatbot.lp.nlp(str(statement))
if 6 > len(self.tokenized) > 2:
for i in self.tokenized:
if str(i.tag_).startswith("NNP"):
self.nn = i.lemma_
if self.nn in self.chatbot.globals["learned"]:
return False
return True
else:
self.chatbot.interrupt = False
return False
elif self.chatbot.interrupt == 1:
return True
else:
return False
[docs] def process(
self,
statement: SugaroidStatement,
additional_response_selection_parameters=None,
username=None,
) -> SugaroidStatement:
if self.chatbot.interrupt == 2:
if self.nn:
response = "{} {} what is {}".format(
random_response(ASK_AND_YOU_SHALL_RECEIVE),
random_response(SEEK_AND_YOU_SHALL_FIND),
self.nn,
)
self.chatbot.interrupt = self.nn
else:
if username:
response = "{} {} what is actually meant in {}'s message?".format(
random_response(ASK_AND_YOU_SHALL_RECEIVE),
random_response(SEEK_AND_YOU_SHALL_FIND),
username,
)
else:
response = (
"{} {} what is actually meant in the previous message?".format(
random_response(ASK_AND_YOU_SHALL_RECEIVE),
random_response(SEEK_AND_YOU_SHALL_FIND),
)
)
self.chatbot.interrupt = str(statement)
else:
if any_in(["no", "not", "later", "busy", "nah"], self.tokenized) or (
("next" in self.tokenized or "another" in self.tokenized)
and "time" in self.tokenized
):
response = "Ok."
self.chatbot.interrupt = False
else:
response = random_response(THANK)
learner = ListTrainer(self.chatbot)
learner.train(
["What is {} ?".format(self.chatbot.interrupt), str(statement)]
)
self.chatbot.globals["learned"].append(self.chatbot.interrupt)
self.chatbot.interrupt = False
selected_statement = SugaroidStatement(response, chatbot=True)
selected_statement.confidence = 9
emotion = Emotion.lol
selected_statement.emotion = emotion
return selected_statement