Skip to content

Commit c3fdb07

Browse files
Intelligent Dictionary added
1 parent f7ad68f commit c3fdb07

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

data.json

+1
Large diffs are not rendered by default.

diction.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from difflib import get_close_matches
2+
import pyttsx3
3+
import json
4+
import speech_recognition as sr
5+
6+
data = json.load(open('data.json'))
7+
engine = pyttsx3.init()
8+
voices = engine.getProperty('voices')
9+
engine.setProperty('voice', voices[0].id)
10+
11+
12+
def speak(audio):
13+
engine.say(audio)
14+
engine.runAndWait()
15+
16+
17+
def takeCommand():
18+
r = sr.Recognizer()
19+
with sr.Microphone() as source:
20+
print('Listening...')
21+
r.pause_threshold = 1
22+
r.energy_threshold = 494
23+
r.adjust_for_ambient_noise(source, duration=1.5)
24+
audio = r.listen(source)
25+
26+
try:
27+
print('Recognizing..')
28+
query = r.recognize_google(audio, language='en-in')
29+
print(f'User said: {query}\n')
30+
31+
except Exception as e:
32+
# print(e)
33+
34+
print('Say that again please...')
35+
return 'None'
36+
return query
37+
38+
39+
def translate(word):
40+
word = word.lower()
41+
if word in data:
42+
speak('Here is what I found in dictionary..')
43+
speak(data[word])
44+
elif len(get_close_matches(word, data.keys())) > 0:
45+
x = get_close_matches(word, data.keys())[0]
46+
speak('Did you mean ' + x +
47+
' instead, respond with Yes or No.')
48+
ans = takeCommand().lower()
49+
if 'yes' in ans:
50+
speak('ok ' + 'It means..' + data[x])
51+
elif 'no' in ans:
52+
speak("Word doesn't exist. Please make sure you spelled it correctly.")
53+
else:
54+
speak("We didn't understand your entry.")
55+
56+
else:
57+
speak("Word doesn't exist. Please double check it.")
58+
59+
60+
if __name__ == '__main__':
61+
translate()

0 commit comments

Comments
 (0)