-
Notifications
You must be signed in to change notification settings - Fork 6
/
answer_of_everything.py
144 lines (116 loc) · 5.14 KB
/
answer_of_everything.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: iso-8859-1 -*-
from kalliope.core.NeuronModule import NeuronModule, MissingParameterException
from kalliope import Utils
from google_trans_new import google_translator
import re
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from engines.WolframAlpha.wolfram_alpha import WolframAlpha
from engines.GoogleAnswers.google_answer import GoogleAnswer
from engines.DuckDuckGo.duckduckgo import DuckDuckGo
class Answer_of_everything(NeuronModule):
def __init__(self, **kwargs):
super(Answer_of_everything, self).__init__(**kwargs)
# the args from the neuron configuration
self.question = kwargs.get('question', None)
self.engines = kwargs.get('engines', None)
self.language = kwargs.get('language', None)
if self.language:
self.translator = google_translator()
if self._is_parameters_ok():
sorted_engines = self.engines
answer = None
result = None
for engine in sorted_engines: #Python 3 preserves the order of a dictionary
if engine == "wolfram_alpha":
result = self.wa_engine(self.question)
if result:
answer = ({"wolfram_answer": result})
break
if engine == "google":
result = self.google_engine(self.question)
if result:
answer = ({"google_answer": result})
break
if engine == "duckduckgo":
result = self.ddg_engine(self.question)
if result:
answer = ({"duckduckgo_answer": result})
break
if not answer:
answer = ({"NoAnswerFound": self.question})
self.say(answer)
def google_engine(self, question):
result = None
g_a = GoogleAnswer(question)
result = g_a.get_answer()
if result:
Utils.print_info('Found answer on Google')
result = self.format_result(result[0])
else:
Utils.print_info('No answer found on Google')
return result
def wa_engine(self, question):
result = None
try:
key = self.engines['wolfram_alpha']['key']
except KeyError:
raise MissingParameterException("API key is missing or is incorrect. Please set a valid API key.")
try:
option = self.engines['wolfram_alpha']['option']
if option not in ["spoken_answer", "short_answer"]:
raise MissingParameterException("%s is not a valid option. Valid options are short_answer or spoken_answer." % option)
except KeyError:
option = "spoken_answer"
try:
unit = self.engines['wolfram_alpha']['unit']
if unit.lower() not in ["metric", "imperial"]:
raise MissingParameterException("%s is not a valid unit. Valid units are metric or imperial." % unit)
except KeyError:
unit = "metric"
question = self.translate_text(question, is_question=True)
w_a = WolframAlpha(question, key, unit.lower())
if option == "spoken_answer":
result = w_a.spoken_answer()
if result is None:
result = w_a.short_answer()
if option == "short_answer":
result = w_a.short_answer()
if result:
result = self.format_result(self.translate_text(result))
Utils.print_info('Found answer on Wolfram Alpha')
else:
Utils.print_info('No answer found on Wolfram Alpha')
return result
def ddg_engine(self, question):
result = None
ddg = DuckDuckGo(self.translate_text(question, is_question=True))
result = ddg.get_answer()
if result:
result = self.format_result(self.translate_text(result))
Utils.print_info('Found answer on DuckDuckGo')
else:
Utils.print_info('No answer found on DuckDuckGo')
return result
def format_result(self, result):
result = re.sub(r'\([^)]*\)|/[^/]*/', '', result)
result = re.sub(r" \s+", r" ", result)
return result
def translate_text(self, text, is_question=False):
if self.language is None:
return text
if is_question:
return self.translator.translate(text, lang_src=self.language, lang_tgt='en')
return self.translator.translate(text, lang_tgt=self.language, lang_src='en')
def _is_parameters_ok(self):
"""
Check if received parameters are ok to perform operations in the neuron
:return: true if parameters are ok, raise an exception otherwise
.. raises:: MissingParameterException
"""
if not self.question:
raise MissingParameterException("Question parameter is missing.")
if self.engines is None:
raise MissingParameterException("Engines parameter is missing. Please define the search engine you want to use.")
return True