-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51.py
61 lines (49 loc) · 1.63 KB
/
51.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
import random
import time
import speech_recognition as sr
f = open('japanese_words2.csv', encoding='utf-8')
lines = f.readlines()[::-1]
#random.shuffle(lines)
r = sr.Recognizer()
mic = sr.Microphone()
for line in lines:
hiragana, kanji, pronunciation = line.split(',')
correct = None
retry = False
while not correct:
if correct is None:
print('Read this word!:')
else:
print('Let\'s try again:')
print(hiragana)
s = input("Press Enter when you ready and then read it(input 's' to skip)")
if s and s.lower()[0] == 's':
print("Okay. Let's skip")
break
ans = ''
try:
with mic as source:
audio = r.listen(source, timeout=3, phrase_time_limit=3)
print('Recognizing...')
ans = r.recognize_google(audio, language='ja')
print(f'You sad "{ans}"')
except sr.WaitTimeoutError as e:
print("Sorry. I heard nothing.")
retry = True
continue
except sr.UnknownValueError as e:
if not retry:
print('What did you say?')
retry = True
continue
else:
print("Sorry. Let's skip this.")
break
if any([w == ans for w in kanji.split('|')]) or hiragana == ans:
correct = True
print('You\'re right!')
else:
correct = False
print(hiragana, 'is pronounced', pronunciation)
print('-' * 20)
time.sleep(2)