forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7ad68f
commit c3fdb07
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from difflib import get_close_matches | ||
import pyttsx3 | ||
import json | ||
import speech_recognition as sr | ||
|
||
data = json.load(open('data.json')) | ||
engine = pyttsx3.init() | ||
voices = engine.getProperty('voices') | ||
engine.setProperty('voice', voices[0].id) | ||
|
||
|
||
def speak(audio): | ||
engine.say(audio) | ||
engine.runAndWait() | ||
|
||
|
||
def takeCommand(): | ||
r = sr.Recognizer() | ||
with sr.Microphone() as source: | ||
print('Listening...') | ||
r.pause_threshold = 1 | ||
r.energy_threshold = 494 | ||
r.adjust_for_ambient_noise(source, duration=1.5) | ||
audio = r.listen(source) | ||
|
||
try: | ||
print('Recognizing..') | ||
query = r.recognize_google(audio, language='en-in') | ||
print(f'User said: {query}\n') | ||
|
||
except Exception as e: | ||
# print(e) | ||
|
||
print('Say that again please...') | ||
return 'None' | ||
return query | ||
|
||
|
||
def translate(word): | ||
word = word.lower() | ||
if word in data: | ||
speak('Here is what I found in dictionary..') | ||
speak(data[word]) | ||
elif len(get_close_matches(word, data.keys())) > 0: | ||
x = get_close_matches(word, data.keys())[0] | ||
speak('Did you mean ' + x + | ||
' instead, respond with Yes or No.') | ||
ans = takeCommand().lower() | ||
if 'yes' in ans: | ||
speak('ok ' + 'It means..' + data[x]) | ||
elif 'no' in ans: | ||
speak("Word doesn't exist. Please make sure you spelled it correctly.") | ||
else: | ||
speak("We didn't understand your entry.") | ||
|
||
else: | ||
speak("Word doesn't exist. Please double check it.") | ||
|
||
|
||
if __name__ == '__main__': | ||
translate() |