Skip to content

Commit 993c5d6

Browse files
Create virtual_assistant.py
1 parent bcf3ec4 commit 993c5d6

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

virtual_assistant.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#Description: This is a virtual assistant program that gets the date, responds back with a random greeting, and returns a response
2+
# getting information on a person e.g. 'who is LeBron James'
3+
4+
#A virtual assistant is an application that can understand voice commands and complete tasks for a user.
5+
6+
#Resources:
7+
# (1) https://towardsdatascience.com/build-your-first-voice-assistant-85a5a49f6cc1
8+
# (2) https://pythonspot.com/personal-assistant-jarvis-in-python/
9+
# (3) https://realpython.com/python-speech-recognition/
10+
# (4) https://pypi.org/project/SpeechRecognition/1.2.3/
11+
# (5) https://stackabuse.com/getting-started-with-pythons-wikipedia-api/
12+
13+
14+
#pip install pyaudio
15+
#pip install SpeechRecognition
16+
#pip install gTTS
17+
#pip install wikipedia
18+
19+
#Import the libraries
20+
import speech_recognition as sr
21+
import os
22+
from gtts import gTTS
23+
import datetime
24+
import warnings
25+
import calendar
26+
import random
27+
import wikipedia
28+
29+
#Ignore any warning messages
30+
warnings.filterwarnings('ignore')
31+
32+
#Record audio and return it as a string
33+
def recordAudio():
34+
35+
#Record the audio
36+
r = sr.Recognizer()
37+
with sr.Microphone() as source: #The with statement itself ensures proper acquisition and release of resources
38+
print('Say something!')
39+
audio = r.listen(source)
40+
41+
#Speech recognition using Google's Speech Recognition
42+
data = ''
43+
try:
44+
data = r.recognize_google(audio)
45+
print('You said: ' + data)
46+
except sr.UnknownValueError:
47+
print('Google Speech Recognition could not understand the audio, unknown error')
48+
except sr.RequestError as e:
49+
print('Request results from Google Speech Recognition service error ' + e)
50+
51+
return data
52+
53+
#Function to get the virtual assistant response
54+
def assistantResponse(text):
55+
print(text)
56+
57+
#Convert the text to speech
58+
myobj = gTTS(text=text, lang='en', slow=False)
59+
60+
#Save the converted audio to a file
61+
myobj.save('assistant_response.mp3')
62+
63+
#Play the converted file
64+
os.system('start assistant_response.mp3')
65+
66+
# A function to check for wake word(s)
67+
def wakeWord(text):
68+
WAKE_WORDS = ['hey computer', 'okay computer']
69+
70+
text = text.lower() #Convert the text to all lower case words
71+
#Check to see if the users command/text contains a wake word
72+
for phrase in WAKE_WORDS:
73+
if phrase in text:
74+
return True
75+
76+
#If the wake word wasn't found in the loop then return False
77+
return False
78+
79+
def getDate():
80+
now = datetime.datetime.now()
81+
my_date = datetime.datetime.today()
82+
weekday = calendar.day_name[my_date.weekday()] #Monday
83+
monthNum = now.month
84+
dayNum = now.day
85+
86+
month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
87+
'October', 'November', 'December']
88+
ordinalNumbers = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th',
89+
'13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd',
90+
'24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st']
91+
return 'Today is ' + weekday +' '+ month_names[monthNum-1] +' the '+ ordinalNumbers[dayNum-1]+'.'
92+
93+
#Function to return a random greeting response
94+
def greeting(text):
95+
#Greeting Inputs
96+
GREETING_INPUTS = ['hi', 'hey', 'hola', 'greetings', 'wassup', 'hello']
97+
98+
#Greeting Response back to the user
99+
GREETING_RESPONSES = ['howdy', 'whats good' , 'hello', 'hey there']
100+
101+
#if the users input is a greeting, then return a randomly chosen greeting response
102+
for word in text.split():
103+
if word.lower() in GREETING_INPUTS:
104+
return random.choice(GREETING_RESPONSES) +'.'
105+
106+
def getPerson(text):
107+
wordList = text.split() #Split the text into a list of words
108+
109+
for i in range(0, len(wordList)):
110+
if i+3 <= len(wordList) - 1 and wordList[i].lower() == 'who' and wordList[i+1].lower() == 'is':
111+
return wordList[i+2] + ' ' + wordList[i+3]
112+
113+
while True:
114+
# Record the audio
115+
text = recordAudio()
116+
response = ''
117+
118+
#Checking for the wake word
119+
if( wakeWord(text) == True ):
120+
if( 'date' in text):
121+
get_date = getDate()
122+
response = response +' '+ get_date
123+
124+
if('hello' in text):
125+
greet = greeting(text)
126+
response = response + ' ' + greet
127+
128+
if('who is' in text):
129+
person = getPerson(text)
130+
wiki = wikipedia.summary(person, sentences=2)
131+
response = response + ' ' + wiki
132+
133+
assistantResponse(response)

0 commit comments

Comments
 (0)