def searchIntent(self, session: DialogSession): if 'UserRandomAnswer' in session.intentName: search = session.payload['input'] else: search = self._extractSearchWord(session) if not search: self._whatToSearch(session, 'whatToSearch') return mediawikiapi = MediaWikiAPI() # set language of the results mediawikiapi.config.language = self.LanguageManager.activeLanguage # Debug code for dev if self._devDebug: self.logInfo(f'User request = {search}') # Store the top 5 titles of the requested search (5 reduces chances of index error) self._top5Results = mediawikiapi.search(search, results=5) # set a index value for iterating through a list in the dialogs index = 0 if not self._top5Results: self.logWarning('No match') self._whatToSearch(session, 'noMatch') return # remove known ambiguous results self.removeKnowenAmbiguousResults() # Check for exceptions and return any good result self._resultSummary = self.sortThroughResults(wikiInstance=mediawikiapi, index=index) # If there are bo good answers then log a warning and inform user if not self._resultSummary: self.logWarning('No match') self._whatToSearch(session, 'noMatch') # If search result found say the result and end else: if self._devDebug: self.logInfo(f'result Summary is {self._resultSummary}') if not self._alternatveResultUsed: self.sayResult(session=session, index=index) else: self.sayAlternatives(alternatives=self._top5Results[index + 1]) self.sayResult(session=session, index=index)
def wikipedia_search(self, question): """ :param self: self object where chatbot object is also located :param question: :return: A tuple of values: (response, confidence, stat) stat = Found or not found """ mw = MediaWikiAPI() wiki = wikipediaapi.Wikipedia("en") a = mw.search(str(question)) question = str(question) if len(a) >= 1: cos = self.chatbot.lp.similarity(question.lower(), a[0].lower()) else: return "Oops, the item you wanted to know is not on wikipedia.", 0.9, False if cos > 0.9: self.chatbot.globals["reversei"]["enabled"] = False self.chatbot.globals["reversei"]["uid"] = False response = wiki.page(a[0]).summary confidence = cos stat = True else: self.chatbot.globals["reversei"]["enabled"] = True self.chatbot.globals["reversei"]["uid"] = 30000000002 self.chatbot.globals["reversei"]["type"] = int self.chatbot.globals["temp_data"] = a def bracketize(x): return "\n[{}] {}".format(x[0] + 1, str(x[1])) response = "Did you mean any of these {}".format(" ".join( [bracketize(x) for x in enumerate(a)])) confidence = 1.0 stat = False return response, confidence, stat