-
Notifications
You must be signed in to change notification settings - Fork 0
/
assistant.py
78 lines (69 loc) · 2.96 KB
/
assistant.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
import webbrowser
import pyttsx3
import speech_recognition as sr
engine = pyttsx3.init()
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()
# Specify the keyword to start the bot
keyword = 'start'
print("Say the keyword to start the bot!")
# Initial loop that waits for the keyword
while True:
with sr.Microphone() as source:
try:
input_speech = r.listen(source)
query = r.recognize_google(input_speech, language='en_gb')
print(f'The query was: {query}')
# If keyword is detected, break the loop
if query.lower() == keyword:
print("Keyword detected, starting the bot...")
break
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
print("Talk to the bot!")
# The main loop of bot
while True:
with sr.Microphone() as source:
input_speech = r.listen(source)
# Exception handling added to manage errors in speech recognition
try:
query = r.recognize_google(input_speech, language='en_gb')
print("Recognizing...")
print(f'The query was: {query}')
if query.lower() == 'hey':
engine.say('Sup')
engine.runAndWait()
elif query.lower() == 'hello':
engine.say('Hello Nikolas')
engine.runAndWait()
elif query.lower() == 'hello my friend':
engine.say('Hello boiiii')
engine.runAndWait()
elif query.lower() == 'how are you doing today':
engine.say('Fantastic')
engine.runAndWait()
elif query.lower() == 'tell me a joke':
engine.say('Roses are Red , Violettes are Blue , input a joke whenever you are ready too')
engine.runAndWait()
elif query.lower() == 'tell me another joke':
engine.say("I'm out")
engine.runAndWait()
elif query.lower() == 'search':
engine.say('What do you want to search for?')
engine.runAndWait()
input_speech = r.listen(source)
search_query = r.recognize_google(input_speech, language='en_gb')
print(f'Searching for: {search_query}')
engine.say('Hold on let me check')
engine.runAndWait()
url = f"https://www.google.com.tr/search?q={search_query}"
webbrowser.open_new_tab(url)
else:
print('Goodbye')
break
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))