diff --git a/diction.py b/diction.py index bc2d8c2..74f5c55 100644 --- a/diction.py +++ b/diction.py @@ -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) @@ -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("") diff --git a/jarvis.py b/jarvis.py index 13d3a67..3ccc86c 100644 --- a/jarvis.py +++ b/jarvis.py @@ -13,6 +13,7 @@ from diction import translate from loc import weather from youtube import youtube +from quote import tell_quote import psutil import pyjokes from sys import platform @@ -151,7 +152,9 @@ def screenshot(): else: engine.setProperty('voice', voices[0].id) speak("Hello Sir, I have switched my voice. How is it?") - + + if 'inspirational quote' in query: + tell_quote() if 'jarvis are you there' in query: speak("Yes Sir, at your service") diff --git a/quote.py b/quote.py new file mode 100644 index 0000000..21b56d6 --- /dev/null +++ b/quote.py @@ -0,0 +1,39 @@ +import pyttsx3 +import wikiquotes +import random +import re + + +engine = pyttsx3.init() +voices = engine.getProperty('voices') +engine.setProperty('voice', voices[0].id) + + +def speak(audio): + engine.say(audio) + engine.runAndWait() + +#text cleaning function to eliminate parenthesis and backslash. +def text_clean(text): + text = re.sub("\\\\",'',text) + text = re.sub(r'\([^)]*\)', '', text) + return text + +#you can always add more speakers +speakers = ["inspiration","tonny robbins", "love","life","les brown", + "eric thomas","jim rohn","brian tracy","mel robbins"] + +def tell_quote(how_many =1): + quotes = wikiquotes.get_quotes(random.sample(speakers,how_many)[0],"english") + acceptables = [] + + for quote in quotes: + length = len(quote.split(" ")) + sents = len(quote.split(".")) + if length>5 and sents<=10: + acceptables.append(quote) + + tell_quote = random.sample(acceptables,1)[0] + tell_quote = text_clean(tell_quote) + + speak(tell_quote) \ No newline at end of file