Spaces:
Runtime error
Runtime error
Commit
•
9b1f754
1
Parent(s):
36289d0
init commit
Browse files- README.md +3 -3
- app.py +108 -0
- requirements.txt +1 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: KanaQuiz
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.27.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: KanaQuiz
|
3 |
+
emoji: 📝
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.27.0
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from romkan import to_hiragana, to_katakana, to_roma
|
5 |
+
|
6 |
+
a_base = ["a", "k", "s", "t", "n", "h", "m", "y", "r", "w"]
|
7 |
+
a_ext1 = ["g", "z", "d", "b"]
|
8 |
+
a_ext2 = ["p"]
|
9 |
+
|
10 |
+
b_base = ["a", "i", "u", "e", "o"]
|
11 |
+
b_ext = ["ya", "yo", "yu"]
|
12 |
+
|
13 |
+
invalid = ["yi", "ye", "wi", "wu", "we"]
|
14 |
+
|
15 |
+
|
16 |
+
def next_question(hira, kata, c, question_list):
|
17 |
+
if not question_list:
|
18 |
+
question_list = init_question(hira, kata, c)
|
19 |
+
return question_list.pop(), question_list
|
20 |
+
|
21 |
+
|
22 |
+
def init_question(hira, kata, c):
|
23 |
+
curr_hira = hira
|
24 |
+
curr_hira += a_ext1 if "濁音" in c else []
|
25 |
+
curr_hira += a_ext2 if "半濁音" in c else []
|
26 |
+
|
27 |
+
curr_kata = kata
|
28 |
+
curr_kata += a_ext1 if "濁音" in c else []
|
29 |
+
curr_kata += a_ext2 if "半濁音" in c else []
|
30 |
+
|
31 |
+
curr_b = b_base
|
32 |
+
curr_b += b_ext if "拗音" in c else []
|
33 |
+
|
34 |
+
hira_list = [to_hiragana(combine(a, b)) for a in curr_hira for b in curr_b if is_valid(a, b)]
|
35 |
+
kata_list = [to_katakana(combine(a, b)) for a in curr_kata for b in curr_b if is_valid(a, b)]
|
36 |
+
|
37 |
+
quiz_list = hira_list + kata_list
|
38 |
+
random.shuffle(quiz_list)
|
39 |
+
|
40 |
+
return quiz_list
|
41 |
+
|
42 |
+
|
43 |
+
def is_valid(aa: str, bb: str):
|
44 |
+
if f"{aa}{bb}" in invalid:
|
45 |
+
return False
|
46 |
+
if aa == "y":
|
47 |
+
if bb[0] == "y":
|
48 |
+
return False
|
49 |
+
if bb[0] == "w":
|
50 |
+
return False
|
51 |
+
if bb == "":
|
52 |
+
return False
|
53 |
+
return True
|
54 |
+
|
55 |
+
|
56 |
+
def combine(a, b):
|
57 |
+
if a == "a":
|
58 |
+
a = ""
|
59 |
+
return f"{a}{b}"
|
60 |
+
|
61 |
+
|
62 |
+
def check(question, answer, correct, total):
|
63 |
+
groundtruth = to_roma(question)
|
64 |
+
ans_correct = groundtruth == answer
|
65 |
+
correct += ans_correct
|
66 |
+
total += 1
|
67 |
+
|
68 |
+
info = "正確" if ans_correct else f"錯誤,答案 {groundtruth}"
|
69 |
+
msg = f"{correct}/{total} - " + info
|
70 |
+
|
71 |
+
return correct, total, msg, ""
|
72 |
+
|
73 |
+
|
74 |
+
font = gr.themes.GoogleFont("NotoSans CJK")
|
75 |
+
theme = gr.themes.Soft(font=font)
|
76 |
+
|
77 |
+
with gr.Blocks(theme=theme, title="假名小測驗") as app:
|
78 |
+
correct = gr.State(0)
|
79 |
+
total = gr.State(0)
|
80 |
+
question_list = gr.State(init_question(a_base[:5], [], []))
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column():
|
84 |
+
with gr.Tab("測驗"):
|
85 |
+
with gr.Row():
|
86 |
+
question = gr.Textbox(label="題目")
|
87 |
+
score = gr.Textbox("0/0", label="分數")
|
88 |
+
answer = gr.Textbox(label="作答")
|
89 |
+
|
90 |
+
with gr.Column():
|
91 |
+
with gr.Tab("設定"):
|
92 |
+
setting_hira = gr.CheckboxGroup(a_base, value=a_base[:5], label="平假名")
|
93 |
+
setting_kata = gr.CheckboxGroup(a_base, label="片假名")
|
94 |
+
setting_c = gr.CheckboxGroup(["濁音", "半濁音", "拗音"], label="延伸")
|
95 |
+
apply_btn = gr.Button("開始測驗")
|
96 |
+
|
97 |
+
chk_inn = [question, answer, correct, total]
|
98 |
+
chk_out = [correct, total, score, answer]
|
99 |
+
chk_arg = dict(fn=check, inputs=chk_inn, outputs=chk_out, show_progress="hidden")
|
100 |
+
|
101 |
+
nq_inn = [setting_hira, setting_kata, setting_c, question_list]
|
102 |
+
nq_out = [question, question_list]
|
103 |
+
nq_arg = dict(fn=next_question, inputs=nq_inn, outputs=nq_out, show_progress="hidden")
|
104 |
+
|
105 |
+
answer.submit(**chk_arg).then(**nq_arg)
|
106 |
+
apply_btn.click(init_question, nq_inn[:3], question_list).then(**nq_arg)
|
107 |
+
|
108 |
+
app.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
romkan
|