Penut commited on
Commit
d811247
1 Parent(s): 47e16f4

add "n" and select/deselect button

Browse files
Files changed (1) hide show
  1. app.py +34 -4
app.py CHANGED
@@ -4,21 +4,33 @@ from copy import deepcopy
4
  import gradio as gr
5
  from romkan import to_hiragana, to_katakana, to_roma
6
 
7
- a_base = ["a", "k", "s", "t", "n", "h", "m", "y", "r", "w"]
8
  a_ext1 = ["g", "z", "d", "b"]
9
  a_ext2 = ["p"]
10
 
11
  b_base = ["a", "i", "u", "e", "o"]
12
  b_ext = ["ya", "yo", "yu"]
13
 
 
 
14
  invalid = ["yi", "ye", "wi", "wu", "we"]
15
  spec_map = {"cya": "cha", "cyu": "chu", "cyo": "cho"}
16
 
17
 
18
  def next_question(hira, kata, c, quiz_list):
 
 
19
  if not quiz_list:
20
- gr.Info("已完成一輪測試!")
21
  quiz_list = init_question(hira, kata, c)
 
 
 
 
 
 
 
 
22
  return quiz_list.pop(), quiz_list
23
 
24
 
@@ -39,6 +51,7 @@ def init_question(hira, kata, c):
39
  kata_list = [to_kata(a, b) for a in curr_kata for b in curr_b if is_valid(a, b)]
40
 
41
  quiz_list = hira_list + kata_list
 
42
  random.shuffle(quiz_list)
43
 
44
  return quiz_list
@@ -68,6 +81,8 @@ def to_kata(a, b):
68
 
69
 
70
  def combine(a, b):
 
 
71
  if a == "a":
72
  a = ""
73
  return f"{a}{b}"
@@ -103,6 +118,14 @@ def reset_score():
103
  return 0, 0, "0/0", ""
104
 
105
 
 
 
 
 
 
 
 
 
106
  font = gr.themes.GoogleFont("NotoSans CJK")
107
  theme = gr.themes.Soft(font=font)
108
 
@@ -124,8 +147,11 @@ with gr.Blocks(theme=theme, title="假名小測驗") as app:
124
  with gr.Tab("設定"):
125
  setting_hira = gr.CheckboxGroup(a_base, value=a_base[:5], label="平假名")
126
  setting_kata = gr.CheckboxGroup(a_base, label="片假名")
127
- setting_c = gr.CheckboxGroup(["濁音", "半濁音", "拗音"], label="延伸")
128
- apply_btn = gr.Button("開始測驗")
 
 
 
129
 
130
  chk_inn = [question, answer, correct, total]
131
  chk_out = [correct, total, score, answer]
@@ -141,7 +167,11 @@ with gr.Blocks(theme=theme, title="假名小測驗") as app:
141
 
142
  reset_arg = dict(fn=reset_score, outputs=chk_out, show_progress="hidden")
143
 
 
 
144
  answer.submit(**chk_arg).then(**nq_arg)
145
  apply_btn.click(**ini_arg).then(**reset_arg).then(**nq_arg)
 
 
146
 
147
  app.launch(favicon_path="icon.png")
 
4
  import gradio as gr
5
  from romkan import to_hiragana, to_katakana, to_roma
6
 
7
+ a_base = ["a", "k", "s", "t", "n", "h", "m", "y", "r", "w", "nn"]
8
  a_ext1 = ["g", "z", "d", "b"]
9
  a_ext2 = ["p"]
10
 
11
  b_base = ["a", "i", "u", "e", "o"]
12
  b_ext = ["ya", "yo", "yu"]
13
 
14
+ c_opt = ["濁音", "半濁音", "拗音"]
15
+
16
  invalid = ["yi", "ye", "wi", "wu", "we"]
17
  spec_map = {"cya": "cha", "cyu": "chu", "cyo": "cho"}
18
 
19
 
20
  def next_question(hira, kata, c, quiz_list):
21
+ info_flag = False
22
+
23
  if not quiz_list:
24
+ info_flag = True
25
  quiz_list = init_question(hira, kata, c)
26
+
27
+ if not quiz_list:
28
+ gr.Info("請選擇至少一項測驗!")
29
+ return None, quiz_list
30
+
31
+ if info_flag:
32
+ gr.Info("已完成一輪測試!")
33
+
34
  return quiz_list.pop(), quiz_list
35
 
36
 
 
51
  kata_list = [to_kata(a, b) for a in curr_kata for b in curr_b if is_valid(a, b)]
52
 
53
  quiz_list = hira_list + kata_list
54
+ quiz_list = list(set(quiz_list))
55
  random.shuffle(quiz_list)
56
 
57
  return quiz_list
 
81
 
82
 
83
  def combine(a, b):
84
+ if a == "nn":
85
+ return "n"
86
  if a == "a":
87
  a = ""
88
  return f"{a}{b}"
 
118
  return 0, 0, "0/0", ""
119
 
120
 
121
+ def select_all():
122
+ return a_base, a_base, c_opt
123
+
124
+
125
+ def deselect_all():
126
+ return [], [], []
127
+
128
+
129
  font = gr.themes.GoogleFont("NotoSans CJK")
130
  theme = gr.themes.Soft(font=font)
131
 
 
147
  with gr.Tab("設定"):
148
  setting_hira = gr.CheckboxGroup(a_base, value=a_base[:5], label="平假名")
149
  setting_kata = gr.CheckboxGroup(a_base, label="片假名")
150
+ setting_c = gr.CheckboxGroup(c_opt, label="延伸")
151
+ with gr.Row():
152
+ select_all_btn = gr.Button("全選")
153
+ deselect_all_btn = gr.Button("全不選")
154
+ apply_btn = gr.Button("開始測驗")
155
 
156
  chk_inn = [question, answer, correct, total]
157
  chk_out = [correct, total, score, answer]
 
167
 
168
  reset_arg = dict(fn=reset_score, outputs=chk_out, show_progress="hidden")
169
 
170
+ opt_out = [setting_hira, setting_kata, setting_c]
171
+
172
  answer.submit(**chk_arg).then(**nq_arg)
173
  apply_btn.click(**ini_arg).then(**reset_arg).then(**nq_arg)
174
+ select_all_btn.click(select_all, outputs=opt_out, show_progress="hidden")
175
+ deselect_all_btn.click(deselect_all, outputs=opt_out, show_progress="hidden")
176
 
177
  app.launch(favicon_path="icon.png")