Skip to content

Commit

Permalink
added dictionary implementation using pydictionary lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyambhu-mukherjee committed Oct 7, 2020
1 parent bd8af25 commit 06473e6
Showing 1 changed file with 73 additions and 12 deletions.
85 changes: 73 additions & 12 deletions diction.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
from difflib import get_close_matches
#from difflib import get_close_matches
import pyttsx3
import json
#import json
import re
import speech_recognition as sr
from io import StringIO
import sys
from PyDictionary import PyDictionary
from spellchecker import SpellChecker

data = json.load(open('data.json'))
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout

#data = json.load(open('data.json'))
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

dictionary = PyDictionary()
spell = SpellChecker()

def speak(audio):
engine.say(audio)
Expand Down Expand Up @@ -35,26 +51,71 @@ def takeCommand():
return 'None'
return query

def tell_meaning(word):
with Capturing() as output:
value = dictionary.meaning(word)
if len(output) != 0:
error_message = output[0]
if "list index out of range" in error_message:
return "get_correction"
elif "HTTPConnectionPool" in error_message:
return "network_problem"
else:
return "too many words"
else:
keys = value.keys()
string_to_tell = ''
for key in keys:
string_to_tell += "As a " + key + " meaning is " + ','.join(value[key]) + '. '
string_to_tell = re.sub("\(",'',string_to_tell)

print(string_to_tell)
return value

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 +
value = tell_meaning(word)
if value not in ['get_correction','network_problem','too many words']:
speak(value)
elif value == 'network_problem':
speak('your network is off. please turn it on.')
elif value == 'too many words':
speak("please tell one word at a time.")
elif value == "get_correction":
word_corr = spell.correction(word)
speak('Did you mean ' + word_corr +
' instead, respond with Yes or No.')
print("word_corrected:", word_corr)
ans = takeCommand().lower()
if 'yes' in ans:
speak(data[x])
speak(tell_meaning(word_corr))
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.")


#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:
# speak("We didn't understand your entry.")
#
# else:
# speak("Word doesn't exist. Please double check it.")


if __name__ == '__main__':
translate()
translate("")

0 comments on commit 06473e6

Please # to comment.