-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiritori.py
87 lines (77 loc) · 2.42 KB
/
shiritori.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
# variables and initial setup
import speech_recognition as sr
import sqlite3
import pygame
from tempfile import TemporaryFile
from gtts import gTTS
from pykakasi import kakasi
def speak(c_voice):
mp3_fp = TemporaryFile()
tts = gTTS(c_voice, "ja")
tts.write_to_fp(mp3_fp)
pygame.mixer.init()
mp3_fp.seek(0)
pygame.mixer.music.load(mp3_fp)
pygame.mixer.music.play()
print("press enter")
input()
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Say something!")
audio = r.listen(source)
try:
print(r.recognize_google(audio, language="ja-JP"))
user_text = r.recognize_google(audio, language="ja-JP")
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google SR service; {0}".format(e))
return user_text
# setting up the kakasi lybrary to convert every word to hiragana
kakasi = kakasi()
kakasi.setMode("H", None)
kakasi.setMode("K", "H")
kakasi.setMode("J", "H")
kakasi.setMode("s", False)
kakasi.setMode("C", True)
kakasi.setMode("E", None)
kakasi.setMode("a", None)
converter = kakasi.getConverter()
conn = sqlite3.connect("wnjpn.db")
cursor = conn.cursor()
word = get_audio()
used_word = []
used = True
first = True
letter = "a"
# main game loop
while True:
# checking if the word is valid
word = converter.do(word)
cursor.execute("SELECT * from shiritori_dict where reading=?", [word])
wordrow = cursor.fetchone()
if wordrow is None or wordrow[4] == "ん":
print("word does not exist or finish in ん")
break
used_word.append(word)
if (not first) and (wordrow[3] != letter):
print("not match the letter")
break
first = False
# computer word selector
while used is True:
cursor.execute("SELECT * from shiritori_dict where first=? and last!='ん' ORDER BY RANDOM()", [wordrow[4]])
wordrow = cursor.fetchone()
if wordrow is None:
print("You won")
break
if not wordrow[1] in used_word:
used = False
print(wordrow[1]+"("+wordrow[2]+")")
speak(wordrow[1])
used_word.append(wordrow[1])
used = True
word = get_audio()
letter = wordrow[4]