Uthsob

Voice bot test

Dec 3rd, 2020
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. # all our imports
  2. import speech_recognition as sr
  3. from time import sleep
  4. from datetime import datetime
  5. import webbrowser
  6. import pyttsx3
  7.  
  8.  
  9. # make an instance of Recognizer class
  10. r = sr.Recognizer()
  11.  
  12.  
  13. # confs for pyttsx3
  14. engine = pyttsx3.init()
  15.  
  16.  
  17. """ speak (text to speech) """
  18. def speak(text):
  19.   engine.say(text)
  20.   engine.runAndWait()
  21.  
  22.  
  23. """ fn to recognize our voice and return the text_version of it"""
  24. def recognize_voice():
  25.   text = ''
  26.  
  27.   # create an instance of the Microphone class
  28.   with sr.Microphone() as source:
  29.     # adjust for ambient noise
  30.     r.adjust_for_ambient_noise(source)
  31.  
  32.     # capture the voice
  33.     voice = r.listen(source)
  34.  
  35.     # let's recognize it
  36.     try:
  37.       text = r.recognize_google(voice)
  38.     except sr.RequestError:
  39.       speak("Sorry, the I can't access the Google API...")
  40.     except sr.UnknownValueError:
  41.       speak("Sorry, Unable to recognize your speech...")
  42.   return text.lower()
  43.  
  44.  
  45. """ fn to respond back """
  46. def reply(text_version):
  47.   # name
  48.   if "name" in text_version:
  49.     speak("My name is Uthsob")
  50.  
  51.   # how are you?
  52.   if "how are you" in text_version:
  53.     speak("I am fine...")
  54.  
  55.   # date
  56.   if "date" in text_version:
  57.     # get today's date and format it - 9 November 2020
  58.     date = datetime.now().strftime("%-d %B %Y")
  59.     speak(date)
  60.  
  61.   # time
  62.   if "time" in text_version:
  63.     # get current time and format it like - 02 28
  64.     time = datetime.now().time().strftime("%H %M")
  65.     speak("The time is " + time)
  66.  
  67.   # search google
  68.   if "search" in text_version:
  69.     speak("What do you want me to search for?")
  70.     keyword = recognize_voice()
  71.  
  72.     # if "keyword" is not empty
  73.     if keyword != '':
  74.       url = "https://google.com/search?q=" + keyword
  75.  
  76.       # webbrowser module to work with the webbrowser
  77.       speak("Here are the search results for " + keyword)
  78.       webbrowser.open(url)
  79.       sleep(3)
  80.  
  81.   # quit/exit
  82.   if "quit" in text_version or "exit" in text_version:
  83.     speak("Ok, I am going to take a nap...")
  84.     exit()
  85.  
  86.  
  87. # wait a second for adjust_for_ambient_noise() to do its thing
  88. sleep(1)
  89.  
  90. while True:
  91.   speak("Start speaking...")
  92.   # listen for voice and convert it into text format
  93.   text_version = recognize_voice()
  94.  
  95.   # give "text_version" to reply() fn
  96.   reply(text_version)
Add Comment
Please, Sign In to add comment