Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

added quote.py for quote and solved the #7 issue. #13

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("")
5 changes: 4 additions & 1 deletion jarvis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
39 changes: 39 additions & 0 deletions quote.py
Original file line number Diff line number Diff line change
@@ -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)