SpeakingDictionary.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import pyttsx3
  2. from PyDictionary import PyDictionary
  3. import speech_recognition as spr
  4. from gtts import gTTS
  5. import os
  6. #Speaking class
  7. class Speak:
  8. def SpeakWord(self, audio):
  9. #Having initial constructor of pyttsx3
  10. pSpeakEngine= pyttsx3.init('sapi5')
  11. pVoices= pSpeakEngine.getProperty('voices')
  12. #Speaking audio that got as parameter
  13. pSpeakEngine.setProperty('voices', pVoices[0].id)
  14. pSpeakEngine.say(audio)
  15. pSpeakEngine.runAndWait()
  16. #Create Recognizer, Microphone instance
  17. sRecog= spr.Recognizer()
  18. sMic= spr.Microphone()
  19. #Capture voice from microphone
  20. with sMic as source:
  21. print("Speak 'Hello' to initiate Speaking Dictionary!")
  22. print("----------------------------------------------")
  23. sRecog.adjust_for_ambient_noise(source, duration= .2)
  24. rAudio= sRecog.listen(source)
  25. szHello= sRecog.recognize_google(rAudio, language= 'en-US')
  26. szHello= szHello.lower()
  27. #If you said 'Hello', initialize the speaking dictionary
  28. if 'hello' in szHello:
  29. sSpeak= Speak()
  30. pDict= PyDictionary()
  31. print("Which word do you want to find? Please speak slowly.")
  32. sSpeak.SpeakWord("Which word do you want to find Please speak slowly")
  33. try:
  34. sRecog2= spr.Recognizer()
  35. sMic2= spr.Microphone()
  36. #Capture the word that the user want to find the meaning of
  37. with sMic2 as source2:
  38. sRecog2.adjust_for_ambient_noise(source2, duration= .2)
  39. rAudio2= sRecog2.listen(source2)
  40. szInput= sRecog2.recognize_google(rAudio2, language= 'en-US')
  41. try:
  42. #Make sure that the recognizer got the correct word
  43. print("Did you said "+ szInput+ "? Please answer with yes or no.")
  44. sSpeak.SpeakWord("Did you said "+ szInput+ "Please answer with yes or no")
  45. sRecog2.adjust_for_ambient_noise(source2, duration= .2)
  46. rAudioYN= sRecog2.listen(source2)
  47. szYN= sRecog2.recognize_google(rAudioYN)
  48. szYN= szYN.lower()
  49. #If the user said 'yes' (When the recognizer got the correct word)
  50. if 'yes' in szYN:
  51. szMeaning= pDict.meaning(szInput)
  52. print("The meaning is ", end="")
  53. for i in szMeaning:
  54. print(szMeaning[i])
  55. sSpeak.SpeakWord("The meaning is"+ str(szMeaning[i]))
  56. #When the recognizer got the wrong word
  57. else: sSpeak.SpeakWord("I am sorry Please try again")
  58. #When the recognizer couldn't understand the answer(yes or no)
  59. except spr.UnknownValueError: sSpeak.SpeakWord("Unable to understand the input Please try again")
  60. except spr.RequestError as e: sSpeak.SpeakWord("Unable to provide required output")
  61. #When the recognizer couldn't understand the word
  62. except spr.UnknownValueError: sSpeak.SpeakWord("Unable to understand the input Please try again")
  63. except spr.RequestError as e: sSpeak.SpeakWord("Unable to provide required output")