-
Notifications
You must be signed in to change notification settings - Fork 1
/
virtual_assistant.py
170 lines (128 loc) · 5.22 KB
/
virtual_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
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
# Description: tHis is a virtual assistant program that gives date,currenttime, responds back with random greeting and returns information
import speech_recognition as sr
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import wikipedia
import random
#Ignore any warning msgs
warnings.filterwarnings('ignore')
#record audio and return audio as string
def recordAudio():
#Record the audio
r = sr.Recognizer()#creating a recognizer object
#Open the microphone and start recording
with sr.Microphone() as source:
print("Listening")
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
#USe google's speech recognition
data = ''
try:
data = r.recognize_google(audio)
print("You said: " +data)
except sr.UnknownValueError: #check for unknown error
print("Google Speech recognition could not understand the audio")
except sr.RequestError as e:
print("Request results from Google SPeech Recognition service error" +e)
return data
#recordAudio()
#A function to get the virtual assistant response
def assistantResponse(text):
print(text)
#Convert the text to speech
myobj = gTTS(text = text, lang='en', slow = False) #text =text passed as param to this funct
#Save the converted audio to a file
myobj.save('assistant_response.mp3')
#play the converted file
os.system('start assistant_response.mp3')
#text = 'This is a test'
#assistantResponse(text)
#A function for wake words for phrase
def wakeWord(text):
WAKE_WORDS = ['hey computer','okay computer','hi computer'] #A list of wake words
text = text.lower() #Coverting the text to all lower case words
#Check if users command/text contains a wake word/phrase
for phrase in WAKE_WORDS:
if phrase in text:
#print("T")
return True
#IF the wake words isn't found in the text from the loop, spo it returns false
#print("F")
return False
#text = "hey"
#wakeWord(text)
#A funtion to get the current date
def getDate():
now = datetime.datetime.now()
myDate = datetime.datetime.today()
weekday = calendar.day_name[myDate.weekday()] #e.g. Monday
monthNum = now.month
dayNum = now.day
#A list of months
monthNames = ['January','February','March',
'April','May','June','July','August',
'September','October','November','December']
#List of ordinal numbers
ordinalNumbers = ['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th',
'11th','12th','13th','14th','15th','16th','17th','18th','19th','20th',
'21st','22nd','23rd','24th','25th','26th','27th','28th','29th','30th']
return 'Today is '+weekday+ ' '+monthNames[monthNum -1]+' the '+ ordinalNumbers[dayNum-1]+'.'
#print(getDate())
#a function to return a random response
def greeting(text):
#greeting input
greetingInput = ['hi','hey','hello','ola','wassup']
#greeting responses
greetingResponse = ['howdy!','whatsgood?','hello!','hey there!']
#if the user's input is greeting, then return a randomly chosen response
for word in text.split():
if word.lower() in greetingInput:
return random.choice(greetingResponse) + '. '
#if no greetings are detected, return an empty string.
return ''
#function to get a person's first and last name from text
def getPerson(text):
wordList = text.split() #Splitting the text into a list of words
for i in range(0, len(wordList)):
if i+3 <= len(wordList) -1 and wordList[i].lower() =='who' and wordList[i+1].lower() == 'is':
return wordList[i+2]+' '+wordList[i+3]
#try to make the code more robust
while True:
#Record the audio
text = recordAudio()
response = '' #var used to append all the rsponses and convert them to audio
#checking for wake words/phrase
if (wakeWord(text)== True):
#Check for greetings by the user
response = response + greeting(text)
#check to see if user said anything about the date
if('date' in text):
get_Date = getDate()
response = response + ' '+get_Date
#check to see if user said anything about time
if('time' in text):
now = datetime.datetime.now()
meridian = ''
if now.hour >=12:
meridian = 'p.m'
hour = now.hour - 12
else:
meridian = 'a.m'
hour = now.hour
#convert minute into proper string
if now.minute <10:
minute = '0'+str(now.minute)
else:
minute = str(now.minute)
response = response + ' ' + 'It is'+ str(hour) +':' +minute +''+meridian+'. '
#check if user said who is
if('who is' in text):
person = getPerson(text)
wiki = wikipedia.summary(person, sentences =2)
response = response +' '+wiki
#assistant responding back using audio and text
assistantResponse(response)
#print("You said the word!")