Skip to content

Instantly share code, notes, and snippets.

@davidliyutong
Created June 15, 2022 16:09
Show Gist options
  • Save davidliyutong/7a7929c084d90d518b4db978aba58d22 to your computer and use it in GitHub Desktop.
Save davidliyutong/7a7929c084d90d518b4db978aba58d22 to your computer and use it in GitHub Desktop.
Japanese Spelling Test
import random
import re
import numpy as np
import time
from pyrsistent import b
prononciation = ['a',
'i',
'u',
'e',
'o',
'ka',
'ki',
'ku',
'ke',
'ko',
'sa',
'shi',
'su',
'se',
'so',
'ta',
'chi',
'tsu',
'te',
'to',
'na',
'ni',
'nu',
'ne',
'no',
'ha',
'hi',
'fu',
'he',
'ho',
'ma',
'mi',
'mu',
'me',
'mo',
'ya',
'yu',
'yo',
'ra',
'ri',
'ru',
're',
'ro',
'wa',
'wo',
'n']
characters = [
'あ',
'い',
'う',
'え',
'お',
'か',
'き',
'く',
'け',
'こ',
'さ',
'し',
'す',
'せ',
'そ',
'た',
'ち',
'つ',
'て',
'と',
'な',
'に',
'ぬ',
'ね',
'の',
'は',
'ひ',
'ふ',
'へ',
'ほ',
'ま',
'み',
'む',
'め',
'も',
'や',
'ゆ',
'よ',
'ら',
'り',
'る',
'れ',
'ろ',
'わ',
'を',
'ん',
]
def handle_characters():
index = np.random.permutation(np.arange(0, len(characters)))
input("Press Enter to start")
start_t = time.time()
for n in index:
try:
input("> {}:".format(prononciation[n]))
print("\033[2mcontinue\033[0m;")
except KeyboardInterrupt:
return
except EOFError:
return
print("\033[33;1m\nFinished in {} seconds\033[0m;".format(time.time() - start_t))
def handle_prononciation():
index = np.random.permutation(np.arange(0, len(prononciation)))
input("Press Enter to start")
start_t = time.time()
n_correct = 0
n_total = 0
for n in index:
try:
ans = input("> {}:".format(characters[n]))
print("\033[32;1mcorrect\033[0m;") if ans == prononciation[n] else print(
"\033[31;1mwrong, correct answer is {}\033[0m;".format(prononciation[n]))
n_total += 1
n_correct += 1 if ans == prononciation[n] else 0
except KeyboardInterrupt:
break
except EOFError:
break
print("\033[33;1m\n{}/{} in {} seconds\033[0m;".format(n_correct,
n_total, time.time() - start_t))
def main():
while True:
try:
cmd = input("\nInput p for prononciation or c for characters: ")
except KeyboardInterrupt:
print("\033[2m\nExiting...\033[0m;")
exit()
except EOFError:
print("\033[2m\nExiting...\033[0m;")
exit()
if cmd in ['c']:
handle_characters()
elif cmd in ['p']:
handle_prononciation()
elif cmd in ['quit', 'q']:
print("\033[2m\nExiting...\033[0m;")
exit()
else:
continue
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment