-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassistente.py
260 lines (178 loc) · 8.91 KB
/
assistente.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import pyttsx3
import speech_recognition as sr
import webbrowser
import datetime
import wikipedia
import PyDictionary
import setuptools
import string
from weather import Weather
dictionary = PyDictionary.PyDictionary()
# Comandos:
# Pesquisar artigo na Wikipédia = "Pesquisar na Wikipedia (sua_palavra)"
# Pesquisar no Youtube = "Youtube (suas_termos_de_pesquisa)"
# Qual é o dia de hoje? = Contanto que você tenha as palavras-chave
# "qual" e "dia" ou "que" e "dia", isso acionará a função tellDay()
# Que horas são? = Contanto que você tenha as palavras-chave "que" e "horas", ou
# "qual" e "horas", isso acionará a função tellTime()
# Qual é o seu nome? = Contanto que você tenha as palavras-chave "qual", "seu" e "nome",
# isso acionará a função greet()
# Saia do programa = "Tchau"
# A programa vai continuar rodando até que você diga "Tchau".
# Traduzir para português = "Significa (sua_palavra)"
# Para instalar as dependências do projeto, execute os seguintes comandos no terminal:
# pip install -r requirements.txt
# Para atualizar o projeto, execute os seguintes comandos no terminal:
# git fetch --all
# git reset --hard origin/master
class Assistant:
def __init__(self):
# voices[2] appears to be the Brazilian Portuguese voice from my localization files
# muito bem
self.engine = pyttsx3.init()
self.voices = self.engine.getProperty('voices')
self.engine.setProperty('voice', self.voices[2].id)
def speak(self, audio):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)
self.engine.say(audio)
self.engine.runAndWait()
def take_command(self):
r = sr.Recognizer()
with sr.Microphone() as source:
print('Escutando...')
r.pause_threshold = 0.7
audio = r.listen(source)
try:
print("Analisando...")
Query = r.recognize_whisper(audio, language='portuguese')
print("Utilizador disse: ", Query.lower())
except Exception as error:
print(error)
print("Como?")
return "None"
return Query
def greet(self):
# only get the hour from tuple returned by tellTime() with [0]
hour = self.tellTime()[0]
if hour > 6 and hour < 12:
print("Bom dia! O pássaro madrugador pega a minhoca.")
self.speak("Bom dia! O pássaro madrugador pega a minhoca.")
elif hour >= 12 and hour < 18:
print("Boa tarde! O dia está passando rápido.")
self.speak("Boa tarde! O dia está passando rápido.")
elif hour >= 18 and hour < 24:
print("Boa noite! Talvez você durma cedo de uma vez.")
self.speak("Boa noite! Talvez você durma cedo de uma vez.")
else:
print("Estou cansado, chefe.")
self.speak("Estou cansado, chefe.")
def getReady(self):
print("Pois nao?")
self.speak("Pois nao?")
def tellDay(self):
day = datetime.datetime.today().weekday() + 1
day_dict = {1: 'segunda-feira', 2: 'terca-feira',
3: 'quarta-feira', 4: 'quinta-feira',
5: 'sexta-feira', 6: 'sabado',
7: 'domingo'}
day_of_the_week = day_dict.get(day)
if day_of_the_week:
print(f"Hoje e {day_of_the_week}.")
self.speak(f"Hoje e {day_of_the_week}.")
def tellTime(self):
current_time = datetime.datetime.now()
print(str(current_time))
hour = current_time.hour
min = current_time.minute
return hour, min
def take_query(self):
self.greet()
start = input("Voce quer falar comigo? (sim/nao): ")
if start.lower() == "sim":
self.getReady()
while True:
query = self.take_command().lower()
if "pesquisar" and "wikipedia" in query:
self.search_fav_sites(query)
continue
elif "qual" and "dia" in query or "que" and "dia" in query:
self.tellDay()
continue
elif "que" and "horas" in query or "qual" and "horas" in query:
hour, min = self.tellTime()
print((f"A hora é {hour} e {min} minutos."))
self.speak(f"A hora é {hour} e {min} minutos.")
continue
elif "qual" and "seu" and "nome" in query:
self.speak("Eu sou Jose Rizal. O seu assistente.")
continue
elif "youtube" in query:
self.search_fav_sites(query)
continue
elif "significa" in query or "Significa" in query:
query_list = query.lower().split()
print(f"Query list: {query_list}")
trigger_word = query_list[0]
print(f"Trigger word: {trigger_word}")
word = query_list[1]
self.translate_to_portuguese(word)
elif "clima" in query or "Clima" in query:
# clean up the string and send the keywords to the weather class
# translate(str.maketrans) makes translation table that translates punctuation to None
query_no_punctuation = query.translate(str.maketrans('', '', string.punctuation))
query_list = query_no_punctuation.lower().split()
trigger_word = query_list.index("clima")
city_name = query_list[trigger_word + 1:]
print(city_name)
weather_instance = Weather()
weather_instance.get_weather(city_name)
elif "tchau" in query:
self.speak("Tchau, tchau!")
break
else:
self.speak("Desculpe, não entendi o que você quis dizer")
continue
elif start.lower() == "nao":
print("Que tristeza :(")
return
else:
print("Por favor, responda com sim ou nao.")
return
def search_fav_sites(self, sitename):
if "youtube" in sitename:
# Index of youtube is found in the string.
# Grab the list items that comes after "youtube"
# search_terms are then joined to the youtube search URL
# OpenAI will sometimes put in punctuation in the string which was messing up
# the trigger_word variable. It wasn't finding "youtube" because it was "youtube," in the string.
# translate(str.maketrans('', '', string.punctuation)) removes punctuation from the string
sitename_no_punctuation = sitename.translate(str.maketrans('', '', string.punctuation))
sitename_list = sitename_no_punctuation.lower().split()
trigger_word = sitename_list.index("youtube")
search_terms = sitename_list[trigger_word + 1:]
self.speak(f"Pesquisando o Youtube para {"".join(search_terms)}.")
webbrowser.open(f"http://www.youtube.com/results?search_query={'+'.join(search_terms)}")
return
if "pesquisar" and "wikipedia" in sitename:
# Trigger words "pesquisar" and "wikipedia" are removed from the string
# Leaving search term only, then the string is passed to the portuguese wikipedia
# search URL
sitename = sitename.replace(",", "")
wikipedia.set_lang("pt")
sitename_list = sitename.lower().split()
index_of_wikipedia = sitename_list.index("wikipedia")
search_terms = sitename_list[index_of_wikipedia + 1:]
self.speak(f"Pesquisando por wikipedia {"".join(search_terms)} ")
print(f"Pesquisando por wikipedia {"".join(search_terms)}.")
webbrowser.open(f"https://pt.wikipedia.org/wiki/Special:Search?search={"_".join(search_terms)}")
return
def translate_to_portuguese(self, word):
dictionary = PyDictionary.PyDictionary()
translation = dictionary.translate(word, 'pt-BR')
print(translation.replace(".", ""))
self.speak(f"{word} significa {translation}")
if __name__ == '__main__':
assistant = Assistant()
assistant.take_query()