-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinry.py
More file actions
134 lines (112 loc) · 3.97 KB
/
winry.py
File metadata and controls
134 lines (112 loc) · 3.97 KB
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
import os
import sys
import speech_recognition as sr
import wolframalpha
import playsound
from gtts import gTTS
from selenium import webdriver
wolfram_id = "3X5UQU-TK9ULVQTVE"
num = 1
shutdown = ["bye","nothing","sleep","shut up","exit"]
def winry_speaks(output):
global num
num += 1
print("Winry : ",output)
toSpeak = gTTS(text=output,lang="en-us",slow=False)
file = str(num)+".mp3"
toSpeak.save(file)
playsound.playsound(file)
os.remove(file)
def speech_to_text():
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
#winry_speaks("What would you like me to do ? ")
print("Speak....")
r.adjust_for_ambient_noise(source)
audio = r.listen(source,phrase_time_limit=5)
#winry_speaks("Okay, thats enough now.")
try:
winry_speaks("Gotcha ! ")
text = r.recognize_google(audio,language="en-us")
print("You :",text)
print("Processing....")
return text
except:
winry_speaks("I didn't quite get you, so i'll call you 0. ")
return "0"
def get_name():
name = speech_to_text()
if(name != "0"):
winry_speaks("Hello " + name )
return name
else:
get_name()
def process_request(request,name):
try:
if("search" in request or "play" in request):
search_web(request)
return
elif("calculate" in request.lower()):
app_id = wolfram_id
client = wolframalpha.Client(app_id)
indx = request.lower().split().index('calculate')
query = request.split()[indx + 1:]
res = client.query(' '.join(query))
answer = next(res.results).text
winry_speaks("The answer is " + answer)
return
else:
winry_speaks("I can search the web for you, Do you want to continue?")
ans = speech_to_text()
if 'yes' in str(ans) or 'yeah' in str(ans):
search_web(request)
else:
return
except :
winry_speaks("I don't understand, I can search the web for you, Do you want to continue?")
ans = speech_to_text()
if 'yes' in str(ans) or 'yeah' in str(ans):
search_web(request)
def search_web(request):
driver = webdriver.Firefox()
driver.implicitly_wait(1)
driver.maximize_window()
if 'youtube' in request.lower():
winry_speaks("Opening in youtube")
indx = request.lower().split().index('youtube')
query = request.split()[indx + 1:]
driver.get("http://www.youtube.com/results?search_query=" + '+'.join(query))
return
elif 'wikipedia' in request.lower():
winry_speaks("Opening Wikipedia")
indx = request.lower().split().index('wikipedia')
query = request.split()[indx + 1:]
driver.get("https://en.wikipedia.org/wiki/" + '_'.join(query))
return
else:
if 'google' in request:
indx = request.lower().split().index('google')
query = request.split()[indx + 1:]
driver.get("https://www.google.com/search?q=" + '+'.join(query))
elif 'search' in request:
indx = request.lower().split().index('google')
query = request.split()[indx + 1:]
driver.get("https://www.google.com/search?q=" + '+'.join(query))
else:
driver.get("https://www.google.com/search?q=" + '+'.join(request.split()))
return
if __name__=="__main__":
winry_speaks("Hi my name is Winry, what is yours ? ")
name = get_name()
while(1):
winry_speaks("What can i do for you today, "+name)
request = speech_to_text()
if(request == "0"):
winry_speaks("I didn't quite get your name, please repeat. ")
continue
elif(str(request.lower()) in shutdown):
winry_speaks("Goodbye, "+name)
sys.exit()
process_request(request,name)
#mass