-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.py
91 lines (78 loc) · 3.16 KB
/
function.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
# Record audio and return it as a string
import speech_recognition as sr
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import wikipedia
def recordAudio():
# Record the audio
r = sr.Recognizer()
with sr.Microphone() as source:
print('Say something!')
audio = r.listen(source)
# Speech recognition using Google's Speech Recognition
data = ''
try:
data = r.recognize_google(audio)
print('You said: ' + data)
except sr.UnknownValueError:
print('Google Speech Recognition could not understand')
except sr.RequestError as e:
print('Request error from Google Speech Recognition')
return data
# 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)
# Save the converted audio to a file
myobj.save('assistant_response.mp3')
# Play the converted file
os.system('afplay assistant_response.mp3')
# A function to check for wake word(s)
def wakeWord(text):
WAKE_WORDS = ['hey mountain', 'okay mountain']
text = text.lower() # Convert the text to all lower case words
# Check to see if the users command/text contains a wake word
for phrase in WAKE_WORDS:
if phrase in text:
return True
# If the wake word was not found return false
return False
def getDate():
now = datetime.datetime.now()
my_date = datetime.datetime.today()
weekday = calendar.day_name[my_date.weekday()]# e.g. Monday
monthNum = now.month
dayNum = now.day
month_names = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October', 'November',
'December']
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', '31st']
return 'Today is ' + weekday + ' ' + month_names[monthNum - 1] + ' the ' + ordinalNumbers[dayNum - 1] + '.'
# Function to return a random greeting response
def greeting(text):
# Greeting Inputs
GREETING_INPUTS = ['hi', 'hey', 'hola', 'greetings', 'wassup', 'hello']
# Greeting Response back to the user
GREETING_RESPONSES = ['howdy', 'whats good', 'hello', 'hey there']
# If the users input is a greeting, then return random response
for word in text.split():
if word.lower() in GREETING_INPUTS:
return random.choice(GREETING_RESPONSES) + '.'
# If no greeting was detected then return an empty string
return ''
# Function to get a person first and last name
def getPerson(text):
wordList = text.split()# Split 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]