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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class KanaQuizApp:
|
| 8 |
+
def __init__(self, spell_path="kana-spell.json", font_name="Kiwi Maru"):
|
| 9 |
+
self.font_name = font_name
|
| 10 |
+
|
| 11 |
+
self.data = load_json(spell_path)
|
| 12 |
+
self.hiragana = self.data["hiragana"]
|
| 13 |
+
self.katakana = self.data["katakana"]
|
| 14 |
+
self.category = self.data["category"]
|
| 15 |
+
self.spell = self.data["spell"]
|
| 16 |
+
|
| 17 |
+
self.__init_app()
|
| 18 |
+
|
| 19 |
+
def __init_blocks(self):
|
| 20 |
+
font = gr.themes.GoogleFont(self.font_name)
|
| 21 |
+
theme = gr.themes.Ocean(font=font)
|
| 22 |
+
return gr.Blocks(theme=theme)
|
| 23 |
+
|
| 24 |
+
def __init_app(self):
|
| 25 |
+
with self.__init_blocks() as self.app:
|
| 26 |
+
self.__init_states()
|
| 27 |
+
self.__init_layout()
|
| 28 |
+
self.__register_events()
|
| 29 |
+
|
| 30 |
+
def __init_states(self):
|
| 31 |
+
self.st_queue = gr.State(None)
|
| 32 |
+
|
| 33 |
+
def __init_layout(self):
|
| 34 |
+
self.title = gr.HTML("<h1>日文五十音大進擊 🚀</h1>")
|
| 35 |
+
with gr.Tabs(selected=0) as self.tabs:
|
| 36 |
+
with gr.Tab(label="設定 ⚙️", id=0):
|
| 37 |
+
self.__init_setting_tab()
|
| 38 |
+
|
| 39 |
+
with gr.Tab(label="測驗 📝", id=1):
|
| 40 |
+
self.__init_quiz_tab()
|
| 41 |
+
|
| 42 |
+
with gr.Tab(label="紀錄 📜", id=2):
|
| 43 |
+
self.__init_record_tab()
|
| 44 |
+
|
| 45 |
+
self.debug = gr.TextArea(label="Debug", visible=False)
|
| 46 |
+
|
| 47 |
+
def __init_setting_tab(self):
|
| 48 |
+
with gr.Group():
|
| 49 |
+
self.chk_kana = gr.CheckboxGroup(["平假名", "片假名"], value=["平假名"], label="假名")
|
| 50 |
+
self.chk_seion = gr.CheckboxGroup(self.category["seion"], value=["a"], label="清音")
|
| 51 |
+
with gr.Row():
|
| 52 |
+
self.chk_dakuon = gr.CheckboxGroup(self.category["dakuon"], label="濁音")
|
| 53 |
+
self.chk_handakuon = gr.CheckboxGroup(self.category["handakuon"], label="半濁音")
|
| 54 |
+
self.chk_youon = gr.CheckboxGroup(self.category["youon"], label="拗音")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
self.btn_select_all = gr.Button("全選")
|
| 58 |
+
self.btn_select_none = gr.Button("全不選")
|
| 59 |
+
self.btn_start = gr.Button("開始測驗 ✨")
|
| 60 |
+
|
| 61 |
+
def __init_quiz_tab(self):
|
| 62 |
+
with gr.Group():
|
| 63 |
+
with gr.Row():
|
| 64 |
+
self.txt_test = gr.Textbox(label="題目 👀", interactive=False)
|
| 65 |
+
self.txt_info = gr.Textbox(label="資訊 📊", interactive=False)
|
| 66 |
+
|
| 67 |
+
self.txt_input = gr.Textbox(label="作答 ✍️", submit_btn=True)
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
self.n_correct = gr.Number(label="答對題數 ✅", interactive=False)
|
| 71 |
+
self.n_total = gr.Number(label="總答題數 🧮", interactive=False)
|
| 72 |
+
self.txt_accuracy = gr.Textbox("100.00%", label="答對比率", interactive=False)
|
| 73 |
+
|
| 74 |
+
def __init_record_tab(self):
|
| 75 |
+
self.txt_records = gr.TextArea(show_label=False, interactive=False)
|
| 76 |
+
self.btn_again = gr.Button("再次測驗 ⚙️")
|
| 77 |
+
self.btn_back = gr.Button("回到設定 🔄")
|
| 78 |
+
|
| 79 |
+
def __register_events(self):
|
| 80 |
+
# event arguments
|
| 81 |
+
reset_outputs = [self.n_correct, self.n_total, self.txt_accuracy]
|
| 82 |
+
reset_outputs += [self.txt_info, self.txt_records]
|
| 83 |
+
reset_args = gr_args(self.reset, outputs=reset_outputs)
|
| 84 |
+
|
| 85 |
+
start_inputs = [self.chk_kana, self.chk_seion, self.chk_dakuon]
|
| 86 |
+
start_inputs += [self.chk_handakuon, self.chk_youon]
|
| 87 |
+
start_outputs = [self.txt_test, self.st_queue, self.debug, self.tabs]
|
| 88 |
+
start_args = gr_args(self.start_test, start_inputs, start_outputs)
|
| 89 |
+
|
| 90 |
+
check_inputs = [self.txt_test, self.txt_input, self.n_correct]
|
| 91 |
+
check_inputs += [self.n_total, self.txt_records]
|
| 92 |
+
check_outputs = [self.txt_input, self.txt_info, self.n_correct]
|
| 93 |
+
check_outputs += [self.n_total, self.txt_accuracy, self.txt_records]
|
| 94 |
+
check_args = gr_args(self.check_answer, check_inputs, check_outputs)
|
| 95 |
+
|
| 96 |
+
next_inputs = [self.st_queue, self.n_correct, self.n_total, self.txt_records]
|
| 97 |
+
next_outputs = [self.txt_test, self.st_queue, self.debug, self.txt_records, self.tabs]
|
| 98 |
+
next_args = gr_args(self.next_char, next_inputs, next_outputs)
|
| 99 |
+
|
| 100 |
+
select_outputs = [self.chk_kana, self.chk_seion, self.chk_dakuon]
|
| 101 |
+
select_outputs += [self.chk_handakuon, self.chk_youon]
|
| 102 |
+
select_all_args = gr_args(self.select_all, outputs=select_outputs)
|
| 103 |
+
|
| 104 |
+
select_none_args = gr_args(self.select_none, outputs=select_outputs)
|
| 105 |
+
back_args = gr_args(self.back, outputs=self.tabs)
|
| 106 |
+
|
| 107 |
+
# register events
|
| 108 |
+
self.btn_start.click(**reset_args).then(**start_args)
|
| 109 |
+
self.txt_input.submit(**check_args).then(**next_args)
|
| 110 |
+
self.btn_select_all.click(**select_all_args)
|
| 111 |
+
self.btn_select_none.click(**select_none_args)
|
| 112 |
+
self.btn_again.click(**reset_args).then(**start_args)
|
| 113 |
+
self.btn_back.click(**back_args)
|
| 114 |
+
|
| 115 |
+
def start_test(self, kana, seion, dakuon, handakuon, yoon):
|
| 116 |
+
category = [*seion, *dakuon, *handakuon, *yoon]
|
| 117 |
+
|
| 118 |
+
char_list = list()
|
| 119 |
+
char_list += [ch for k in category for ch in self.hiragana[k]] if "平假名" in kana else []
|
| 120 |
+
char_list += [ch for k in category for ch in self.katakana[k]] if "片假名" in kana else []
|
| 121 |
+
|
| 122 |
+
if not char_list:
|
| 123 |
+
raise gr.Error("請至少選擇一個類別")
|
| 124 |
+
|
| 125 |
+
random.shuffle(char_list)
|
| 126 |
+
char = char_list.pop(0)
|
| 127 |
+
return char, char_list, char_list, gr.Tabs(selected=1)
|
| 128 |
+
|
| 129 |
+
def check_answer(self, txt_test, txt_input, n_correct, n_total, txt_records):
|
| 130 |
+
txt_input = str.lower(txt_input).strip()
|
| 131 |
+
|
| 132 |
+
if txt_input in self.spell[txt_test]:
|
| 133 |
+
n_correct += 1
|
| 134 |
+
txt_info = "正確!"
|
| 135 |
+
else:
|
| 136 |
+
answer = ", ".join(self.spell[txt_test])
|
| 137 |
+
txt_info = f"錯誤,正確答案為 {answer}"
|
| 138 |
+
txt_records += f"題目:{txt_test}、正解:{answer}、輸入:{txt_input}\n"
|
| 139 |
+
|
| 140 |
+
n_total += 1
|
| 141 |
+
accuracy = n_correct / n_total
|
| 142 |
+
|
| 143 |
+
return None, txt_info, n_correct, n_total, f"{accuracy:.2%}", txt_records
|
| 144 |
+
|
| 145 |
+
def next_char(self, st_queue, n_correct, n_total, txt_record):
|
| 146 |
+
if not st_queue:
|
| 147 |
+
gr.Info("測驗結束!")
|
| 148 |
+
accuracy = n_correct / n_total
|
| 149 |
+
txt_record += f"正確率 {accuracy:.2%} ({n_correct}/{n_total})"
|
| 150 |
+
return None, None, None, txt_record, gr.Tabs(selected=2)
|
| 151 |
+
|
| 152 |
+
char = list.pop(st_queue, 0)
|
| 153 |
+
return char, st_queue, st_queue, txt_record, gr.Tabs(selected=1)
|
| 154 |
+
|
| 155 |
+
def select_all(self):
|
| 156 |
+
return (
|
| 157 |
+
["平假名", "片假名"],
|
| 158 |
+
self.category["seion"],
|
| 159 |
+
self.category["dakuon"],
|
| 160 |
+
self.category["handakuon"],
|
| 161 |
+
self.category["youon"],
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
def select_none(self):
|
| 165 |
+
return [], [], [], [], []
|
| 166 |
+
|
| 167 |
+
def reset(self):
|
| 168 |
+
return 0, 0, "100.00%", None, None
|
| 169 |
+
|
| 170 |
+
def back(self):
|
| 171 |
+
return gr.Tabs(selected=0)
|
| 172 |
+
|
| 173 |
+
def launch(self):
|
| 174 |
+
self.app.launch()
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def load_json(path):
|
| 178 |
+
with open(path, "rt", encoding="UTF-8") as fp:
|
| 179 |
+
return json.load(fp)
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def gr_args(fn=None, inputs=None, outputs=None, show_progress="hidden", **kwargs):
|
| 183 |
+
return dict(fn=fn, inputs=inputs, outputs=outputs, show_progress=show_progress, **kwargs)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
KanaQuizApp().launch()
|