-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_bot.py
More file actions
49 lines (41 loc) · 1.31 KB
/
Copy pathchat_bot.py
File metadata and controls
49 lines (41 loc) · 1.31 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
import speech_recognition as sr
import pyttsx3
r = sr.Recognizer()
mic = sr.Microphone()
engine = pyttsx3.init()
engine.setProperty('rate', 120)
while True:
with mic as source:
print('say something')
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
print("recognizing it...")
try:
# engine.say("You said " + r.recognize_google(audio))
# engine.runAndWait()
say = r.recognize_google(audio)
print(say)
if 'google' in say.lower():
engine.say('What can I do for you ?')
engine.runAndWait()
with mic as source:
print('What can I do for you?')
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
print("recognizing it...")
try:
cmd = r.recognize_google(audio)
engine.say('You said ' + cmd)
engine.runAndWait()
except Exception as e:
engine.say('error')
engine.runAndWait()
else:
print('You said ' + say)
except sr.RequestError:
engine.say("api unavailable")
engine.runAndWait()
except sr.UnknownValueError:
# engine.say("unable to recognize speech")
# engine.runAndWait()
pass