Source code for sugaroid.brain.wiki

import nltk
import wikipediaapi
from chatterbot.logic import LogicAdapter
from nltk import word_tokenize
from sugaroid.brain.brain import Neuron
from sugaroid.brain.ooo import Emotion
from sugaroid.core.statement import SugaroidStatement
from sugaroid.brain.constants import WHATSUP
from sugaroid.brain.postprocessor import random_response, any_in

try:
    from mediawikiapi import MediaWikiAPI

    media_wiki_found = True
except ModuleNotFoundError:
    media_wiki_found = False


[docs]class WikiAdapter(LogicAdapter): """ Handles Wikipedia based questions """
[docs] def can_process(self, statement) -> bool: self.text = word_tokenize(str(statement)) if "$wolf" in self.text: return False tagged = nltk.pos_tag(self.text) q = False pr = False for i in tagged: if i[1] == "WP" or i[1] == "WRB": q = True elif (i[1].startswith("PRP")) and (not i[0] == "we"): pr = True if q and (not pr): return True else: return False
[docs] def process(self, statement, additional_response_selection_parameters=None): # FIXME: This may printout unrelated data for phrase searches self.text = word_tokenize(str(statement)) response = "I don't know" confidence = 0 emotion = Emotion.neutral count = 0 for i in self.text: if i.lower() == "what": count += 1 if count > 1: selected_statement = SugaroidStatement("Nothing " * count, chatbot=True) selected_statement.confidence = 0.99 emotion = Emotion.seriously selected_statement.emotion = emotion return selected_statement elif ("Srevin" in self.text) or ("srevin" in self.text): response = "Srevin Saju is the creator of Sugaroid bot" selected_statement = SugaroidStatement(response, chatbot=True) selected_statement.confidence = 1.0 selected_statement.emotion = emotion return selected_statement elif "sugaroid" in self.text or "Sugaroid" in self.text: response = "I am Sugaroid. 😎" selected_statement = SugaroidStatement(response, chatbot=True) selected_statement.confidence = 1.0 selected_statement.emotion = emotion return selected_statement elif "up" in self.text or "Up" in self.text: # FIXME will introduce a regression in future response = random_response(WHATSUP) selected_statement = SugaroidStatement(response, chatbot=True) selected_statement.confidence = 1.0 selected_statement.emotion = emotion return selected_statement else: nlp = self.chatbot.lp what = False norm = nlp.tokenize(str(statement)) for i in range(len(norm) - 2): if norm[i].tag_ == "WP": if ( norm[i].lower_ == "what" or norm[i].lower_ == "who" or norm[i].lower_ == "where" ): what = True elif norm[i].lower_ == "how": what = False how = True break else: what = True if what: for j in range(i + 1, len(norm)): if (norm[j].tag_ == "VBZ") or (norm[j].tag_ == "DT"): continue else: question = norm[j:] if question[0].lower_ == "time": response = Neuron.gen_time() confidence = 1.0 stat = True break else: if media_wiki_found: response, confidence, stat = wikipedia_search( self, question ) break else: response = "Seems like MediaWikiAPI is not installed on your PC" confidence = 0.9 break else: response = "I am not sure what you are asking for." confidence = 0.4 emotion = Emotion.cry break if response: break else: response = ( "I can't give a reason for that at the moment. Maybe you might want to search the " "internet." ) confidence = 0.4 emotion = Emotion.cry_overflow break else: response = "Well. maybe I do not know" confidence = 0 selected_statement = SugaroidStatement(str(response), chatbot=True) selected_statement.confidence = confidence selected_statement.emotion = emotion return selected_statement