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