-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
130 lines (111 loc) · 3.98 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from gtts import gTTS
from playsound import playsound
import random
import speech_recognition as sr
import os
from difflib import SequenceMatcher
from createvocab import vocab
choice = "invalid"
while choice == "invalid":
spanOrEnglish = input("Would you like to translate to spanish or english? (s/e): ")
spanOrEnglish = spanOrEnglish.lower()
if spanOrEnglish == "s":
print("Spanish mode selected.")
mode = "spanish"
choice = "valid"
elif spanOrEnglish == "e" :
print("English mode selected.")
mode = "english"
choice = "valid"
else:
print("Invalid input.")
choice = "invalid"
choice = "invalid"
while choice == "invalid":
amountOfWords = input("\nHow many words would you like to learn (a for all): ")
if amountOfWords == "a":
amountOfWords = len(vocab)
choice = "valid"
elif amountOfWords.isnumeric() == False:
print("Invalid input.")
choice = "invalid"
elif amountOfWords.isnumeric() == True:
choice = "valid"
#Function to check similarity of words
def check_similarity(lanugageWord, userResponse):
if mode == "english":
ttsMode = "en"
elif mode == "spanish":
ttsMode = "es"
'''Checks if your answer is correct based off how similar it is to the correct answer'''
similarity = (SequenceMatcher(None, lanugageWord, userResponse).ratio())
if similarity >= 0.46:
print("Correct!")
text_to_speech("Correct!", "en")
elif text == "default":
print("No input detected. The correct answer is " + lanugageWord + ".")
text_to_speech("No input detected. The correct answer is " + lanugageWord + ".", ttsMode)
else:
print("Incorrect. The correct answer is " + lanugageWord + ". You said " + userResponse + ".")
text_to_speech("Incorrect. The correct answer is " + lanugageWord + ". You said " + userResponse + ".", ttsMode)
#Function to convert text to speech
def text_to_speech(text, lang):
'''Converts text to speech'''
tts = gTTS(text=text, lang=lang)
tts.save("current.mp3")
playsound("current.mp3")
os.remove("current.mp3")
#Function to get user voice input
def get_user_voice_input(language=None):
'''Gets user input'''
r = sr.Recognizer()
text = "default"
with sr.Microphone() as source:
try:
audio = r.listen(source)
text = r.recognize_google(audio, language)
except:
pass
if text.lower() == "pause":
print("Pause command detected. Pausing...")
print("Say resume to resume...")
while True:
r = sr.Recognizer()
text = "default"
with sr.Microphone() as source:
try:
audio = r.listen(source)
text = r.recognize_google(audio, language)
except:
pass
if text.lower() == "resume":
print("Resuming...")
break
else:
print("Invalid command. Say resume to resume...")
return "resume"
else:
return text
#Initialize variables for loop
completedWords = []
wordsDone = 0
#Loop through vocab
for spanishWord, englishWord in vocab.items():
#Check if word has been done
if englishWord not in completedWords and spanishWord not in completedWords and wordsDone < int(amountOfWords):
if mode == "english":
#Text to speech for Spanish
text_to_speech(spanishWord, "es")
# Recognize speech for English
text = get_user_voice_input()
check_similarity(englishWord, text)
elif mode == "spanish":
#Text to speech for English
text_to_speech(englishWord, "en")
#Recognize speech for gSpanish
text = get_user_voice_input("es-ES")
check_similarity(spanishWord, text)
#Add words to completed list
completedWords.append(englishWord)
completedWords.append(spanishWord)
wordsDone += 1