Skip to content

Instantly share code, notes, and snippets.

@aevans-mms
Created April 17, 2023 15:46
Show Gist options
  • Save aevans-mms/d89d9b207dc5c44f07573868c545699f to your computer and use it in GitHub Desktop.
Save aevans-mms/d89d9b207dc5c44f07573868c545699f to your computer and use it in GitHub Desktop.
import speech_recognition as sr
import pyttsx3
# initialize the speech recongnition and text-to-speech enginers
r = sr.Recognizer()
engine = pyttsx3.init()
# define a function to speak the text
def speak(text):
engine.say(text)
engine.runAndWait()
# define a function to recognize speech
def listen():
with sr.Microphone() as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
return text
except:
return None
# main loop
while True:
# listen for user input
command = listen()
# if command is not recognized, continue to listen
if command is None:
continue
# set reminder
if "remind me" in command:
speak("What should I remind you about?")
reminder = listen()
speak(f"Sure, I'll remind you to {reminder} later")
# create to-do list
elif "create a to-do list" in command:
speak("What are the tasks you want to add to the to-do list?")
tasks = []
while True:
task = listen()
if "stop" in task:
break
tasks.append(task)
speak("Here's your to-do list:")
for i, task in enumerate(tasks):
speak(f"{i+1}, {task}")
# search the web
elif "search for" in command:
query = command.replace("search for", "")
speak(f"Here are your search results for {query}")
# TODO: code to search the web
# quit the program
elif "quit" in command:
speak("Goodbye!")
break
else:
speak("I'm sorry, I didn't understand. Please try again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment