Пример #1
0
class Bot:
    """
    The main bot class
    """

    APIs = {
        "What is the definition of": Wikipedia(),
        "Calculate": Wolfram(),
    }

    def __init__(self):
        self.cleaner = Cleaner
        self.es = ElasticSearch
        self.translator = Translator()

    def get_response(self, raw_input_string: str):
        for api_key, api in Bot.APIs.items():
            if raw_input_string.startswith(api_key):
                text = raw_input_string.replace(api_key, "")
                return api.search(text)

        query = self.cleaner.clean(raw_input_string)
        results = self.es.search(query)

        if len(results) > 0:
            return results[0]["_source"]['response']

        from random import randint

        return DEFAULT[randint(0, len(DEFAULT) - 1)]

    def ask(self, raw_input_string: str):
        """
        :param raw_input_string: Users question as raw string
        :return: Bots response as string
        """

        lang = self.translator.detect(raw_input_string).lang
        translated = self.translator.translate(raw_input_string, 'en', lang).text
        english_response = self.get_response(translated)
        return self.translator.translate(english_response, lang, 'en').text
 def test_error_finding_article(self, mock_response):
     mock_response.return_value = MockResponse()
     with self.assertRaises(MissingArticleError):
         article = Wikipedia.get_article("blah")
Пример #3
0
 def article(cls, title):
     return Wikipedia.get_article(title)
Пример #4
0
 def article(cls, title):
     response = Wikipedia.article(title)
     return response
Пример #5
0
 def article(cls, title):
     return Wikipedia.article(title)