Source code for sugaroid.brain.dis

import logging
from chatterbot.logic import LogicAdapter
from nltk.sentiment import SentimentIntensityAnalyzer
from pyinflect import getInflection
from sugaroid.brain.postprocessor import any_in, random_response
from sugaroid.brain.constants import (
    DIS_RESPONSES_YOU,
    CONSOLATION,
    DIS_RESPONSES_I,
    DIS_RESPONSES_HIM,
)
from sugaroid.brain.ooo import Emotion
from sugaroid.brain.preprocessors import normalize, spac_token
from sugaroid.core.statement import SugaroidStatement


[docs]class DisAdapter(LogicAdapter): """ A complex algorithm sorting the words beginning with negative based on the probability. and achieving a similar confidence ratio of the word percentage. The DisAdapter keeps the confidence below 0.5 so that the BestAdapter may find some other answer similar to """ def __init__(self, chatbot, **kwargs): super().__init__(chatbot, **kwargs) self.normalized = None self.dis = None
[docs] def can_process(self, statement): self.normalized = normalize(str(statement)) self.dis = None for i in self.normalized: if i.startswith("dis"): self.dis = i return True else: return False
[docs] def process(self, statement, additional_response_selection_parameters=None): confidence = 0 dis_word = False if any_in( [ "distinguish", "disfigure", "distinct", "distinction", "distant", "distance", "distribution", "distilled", ], self.normalized, ): confidence = 0 else: logging.info( "DisAdapter: Starting Advanced scan. dis_word == {}".format(self.dis)[0] ) dis_word = self.dis[3:] logging.info("DisAdapter: Distilled word == {}".format(dis_word)) sia = SentimentIntensityAnalyzer().polarity_scores(dis_word) if dis_word[0] in ["a", "e", "i", "o", "u", "g", "m", "p"]: confidence += 0.4 if "infect" in dis_word: confidence -= 0.3 if "spirit" in dis_word: confidence += 0.2 if any_in( [ "play", "pensary", "pense", "patch", "port", "persal", "perse", "persion", "praise", ], dis_word, ): confidence -= 0.2 confidence += sia["neg"] inflection = getInflection(self.chatbot.lp.tokenize(self.dis)[0].lemma_, "VBD") if inflection is None: past_participle_form_of_verb = self.dis else: past_participle_form_of_verb = inflection[0] if "you" in self.normalized: response = random_response(DIS_RESPONSES_YOU).format( past_participle_form_of_verb ) emotion = Emotion.angry_non_expressive elif "I" in self.normalized: response = "{} {}".format( random_response(DIS_RESPONSES_I), random_response(CONSOLATION) ) emotion = Emotion.angel else: nn = None pn = None tokenized = spac_token(statement, chatbot=self.chatbot) for i in tokenized: if (i.pos_ == "NOUN") or (i.pos_ == "PROPN"): nn = i.text elif i.pos_ == "PRON": pn = i.text if not (nn or pn): response = "Lol. What?" emotion = Emotion.seriously else: response = random_response(DIS_RESPONSES_HIM).format(nn or pn) emotion = Emotion.cry_overflow selected_statement = SugaroidStatement(response, chatbot=True) selected_statement.confidence = confidence selected_statement.emotion = emotion selected_statement.adapter = None return selected_statement