Skip to content

Instantly share code, notes, and snippets.

@sakamer71
Created December 2, 2014 02:26
Show Gist options
  • Save sakamer71/13d8983bf78133ac0c40 to your computer and use it in GitHub Desktop.
Save sakamer71/13d8983bf78133ac0c40 to your computer and use it in GitHub Desktop.
speech recognition
__author__ = 'skamer'
import speech_recognition as sr
from os import system
r = sr.Recognizer()
def talk(x):
phrase = "say -v samantha \"{}\"".format(x)
return system(phrase)
def listen(x):
output = ''
while not output:
with sr.Microphone() as source:
audio = r.listen(source)
try:
output = r.recognize(audio)
return output
except LookupError:
talk("I didn't catch that, try again")
talk(x)
# recognize speech using Google Speech Recognition
getname = "Good morning, what is your name?"
talk(getname)
name = listen(getname)
getbreakfast = "Hi {}, what would you like for breakfast\?".format(name)
talk(getbreakfast)
breakfast = listen(getbreakfast)
accept = "Well {}, I don't have {}, but how would you like some fried monkey brains instead\?".format(name,breakfast)
talk(accept)
choice = listen(accept)
choice = choice.split()
for word in choice:
if word in ['sure', 'ok', 'fine','yes','alright']:
talk("Great, monkey brains it is\!")
exit()
else:
talk("Too bad, I make a mean monkey brains. If you insist on {}, you are on your own\!".format(breakfast))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment