-
Notifications
You must be signed in to change notification settings - Fork 203
/
diction.py
61 lines (48 loc) · 1.51 KB
/
diction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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(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(data[x])
elif 'no' in ans:
speak("Word doesn't exist. Please make sure you spelled it correctly.")
else:
#changed from we to I
speak("I did not understand your entry.")
else:
speak("Word doesn't exist. Please double check it.")
if __name__ == '__main__':
translate()