Spaces:
Runtime error
Runtime error
use deepcopy
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import random
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
from romkan import to_hiragana, to_katakana, to_roma
|
@@ -13,26 +14,27 @@ b_ext = ["ya", "yo", "yu"]
|
|
13 |
invalid = ["yi", "ye", "wi", "wu", "we"]
|
14 |
|
15 |
|
16 |
-
def next_question(hira, kata, c,
|
17 |
-
if not
|
18 |
-
|
19 |
-
return
|
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 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
curr_b = b_base
|
32 |
-
curr_b += b_ext if "拗音" in c else []
|
33 |
|
34 |
-
hira_list = [
|
35 |
-
kata_list = [
|
36 |
|
37 |
quiz_list = hira_list + kata_list
|
38 |
random.shuffle(quiz_list)
|
@@ -53,6 +55,14 @@ def is_valid(aa: str, bb: str):
|
|
53 |
return True
|
54 |
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def combine(a, b):
|
57 |
if a == "a":
|
58 |
a = ""
|
@@ -65,19 +75,23 @@ def check(question, answer, correct, total):
|
|
65 |
correct += ans_correct
|
66 |
total += 1
|
67 |
|
68 |
-
info = "正確" if ans_correct else f"
|
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 |
-
|
81 |
|
82 |
with gr.Row():
|
83 |
with gr.Column():
|
@@ -98,11 +112,17 @@ with gr.Blocks(theme=theme, title="假名小測驗") as app:
|
|
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,
|
102 |
-
nq_out = [question,
|
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(
|
107 |
|
108 |
-
app.launch(
|
|
|
1 |
import random
|
2 |
+
from copy import deepcopy
|
3 |
|
4 |
import gradio as gr
|
5 |
from romkan import to_hiragana, to_katakana, to_roma
|
|
|
14 |
invalid = ["yi", "ye", "wi", "wu", "we"]
|
15 |
|
16 |
|
17 |
+
def next_question(hira, kata, c, quiz_list):
|
18 |
+
if not quiz_list:
|
19 |
+
quiz_list = init_question(hira, kata, c)
|
20 |
+
return quiz_list.pop(), quiz_list
|
21 |
|
22 |
|
23 |
def init_question(hira, kata, c):
|
24 |
+
curr_hira = deepcopy(hira)
|
25 |
+
curr_hira += deepcopy(a_ext1) if "濁音" in c else []
|
26 |
+
curr_hira += deepcopy(a_ext2) if "半濁音" in c else []
|
27 |
|
28 |
+
curr_kata = deepcopy(kata)
|
29 |
+
if curr_kata:
|
30 |
+
curr_kata += deepcopy(a_ext1) if "濁音" in c else []
|
31 |
+
curr_kata += deepcopy(a_ext2) if "半濁音" in c else []
|
32 |
|
33 |
+
curr_b = deepcopy(b_base)
|
34 |
+
curr_b += deepcopy(b_ext) if "拗音" in c else []
|
35 |
|
36 |
+
hira_list = [to_hira(a, b) for a in curr_hira for b in curr_b if is_valid(a, b)]
|
37 |
+
kata_list = [to_kata(a, b) for a in curr_kata for b in curr_b if is_valid(a, b)]
|
38 |
|
39 |
quiz_list = hira_list + kata_list
|
40 |
random.shuffle(quiz_list)
|
|
|
55 |
return True
|
56 |
|
57 |
|
58 |
+
def to_hira(a, b):
|
59 |
+
return to_hiragana(combine(a, b))
|
60 |
+
|
61 |
+
|
62 |
+
def to_kata(a, b):
|
63 |
+
return to_katakana(combine(a, b))
|
64 |
+
|
65 |
+
|
66 |
def combine(a, b):
|
67 |
if a == "a":
|
68 |
a = ""
|
|
|
75 |
correct += ans_correct
|
76 |
total += 1
|
77 |
|
78 |
+
info = "正確" if ans_correct else f"錯誤 - {question} ({groundtruth})"
|
79 |
msg = f"{correct}/{total} - " + info
|
80 |
|
81 |
return correct, total, msg, ""
|
82 |
|
83 |
|
84 |
+
def reset_score():
|
85 |
+
return 0, 0, "0/0", ""
|
86 |
+
|
87 |
+
|
88 |
font = gr.themes.GoogleFont("NotoSans CJK")
|
89 |
theme = gr.themes.Soft(font=font)
|
90 |
|
91 |
with gr.Blocks(theme=theme, title="假名小測驗") as app:
|
92 |
correct = gr.State(0)
|
93 |
total = gr.State(0)
|
94 |
+
quiz_list = gr.State(init_question(a_base[:5], [], []))
|
95 |
|
96 |
with gr.Row():
|
97 |
with gr.Column():
|
|
|
112 |
chk_out = [correct, total, score, answer]
|
113 |
chk_arg = dict(fn=check, inputs=chk_inn, outputs=chk_out, show_progress="hidden")
|
114 |
|
115 |
+
nq_inn = [setting_hira, setting_kata, setting_c, quiz_list]
|
116 |
+
nq_out = [question, quiz_list]
|
117 |
nq_arg = dict(fn=next_question, inputs=nq_inn, outputs=nq_out, show_progress="hidden")
|
118 |
|
119 |
+
ini_inn = [setting_hira, setting_kata, setting_c]
|
120 |
+
ini_out = [quiz_list]
|
121 |
+
ini_arg = dict(fn=init_question, inputs=ini_inn, outputs=ini_out, show_progress="hidden")
|
122 |
+
|
123 |
+
reset_arg = dict(fn=reset_score, outputs=chk_out, show_progress="hidden")
|
124 |
+
|
125 |
answer.submit(**chk_arg).then(**nq_arg)
|
126 |
+
apply_btn.click(**ini_arg).then(**reset_arg).then(**nq_arg)
|
127 |
|
128 |
+
app.launch()
|