-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnouns.py
More file actions
408 lines (356 loc) · 17 KB
/
nouns.py
File metadata and controls
408 lines (356 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import streamlit as st
import random
import time
from utils import radio_change, reset, new_question, submit_and_check_answer, clear_page
from vocab import import_nouns
st.set_page_config("Latin Morph! Nouns", layout="centered")
# if st.session_state.question_list:
questions_asked = st.session_state.question_list
noun_vocab = import_nouns()
page_id = "nouns"
clear_page(page_id)
st.markdown("# Nouns")
st.warning('If you come across any incorrectly generated forms, please fill out the "Latin mistake" part of [this Google form](https://forms.gle/xT8hQ27sjposeXPc9).')
declension_dict = {"1st": 1, "2nd":["2_us", "2_er", "2_neut"], "3rd": [3, "3_istem", "3_neut", "3_istem_neut"], "4th": [4, "4_neut"], "5th": ["5_vowel", "5_consonant"]}
## SET OPTIONS ##
option_expander = st.expander("Settings", expanded=True)
with option_expander:
col_declension, col_options = st.columns([3,2])
with col_options:
st.markdown("Options:", help="You can adjust these options at any point.")
st.checkbox("Enforce macrons?", help="If this box is selected, macron mistakes will be considered incorrect. If not selected, macrons can be used but will not be evaluated.", key="enforce_macrons")
macrons = st.session_state.enforce_macrons
if macrons:
st.markdown("You can copy and paste letters from here:")
st.code("āēīōū", language=None)
show_declension = st.checkbox("Show declension?", help="Select this box to show the noun's declension.")
show_stem = st.checkbox("Show noun stem/base?", help="Select this box to show the noun base. (The base is the stem without any of the trailing vowels that sometimes combine with endings.)")
with col_declension:
# radio_change() is defined in utils.py
# declension = st.radio("Choose a declension to practice:",{"random":"random"} | declension_dict, on_change=radio_change)
declension = st.multiselect("Choose which declensions to practice (they are all selected by default):", options=list(declension_dict.keys()), default=list(declension_dict.keys()), help="If the selected declension(s) include irregular nouns, an option will be shown to include or exclude them.")
## DEFINE AVAILABLE NOUNS AND NOUN ENDINGS ##
# based on radio button 'declension' (which may still need its own key in session_state), filter noun_vocab.
active_vocab = {}
for noun_key, noun_val in noun_vocab.items():
noun_decl = noun_val["decl"]
for decl_sel in declension:
if isinstance(declension_dict[decl_sel], list) and noun_decl in declension_dict[decl_sel]:
active_vocab = active_vocab | {noun_key: noun_val}
elif noun_decl == declension_dict[decl_sel]:
active_vocab = active_vocab | {noun_key: noun_val}
else:
continue
# deal with irregular nouns
avail_decl = []
for item in {k:v for k,v in declension_dict.items() if k in declension}.values():
if isinstance(item, list):
avail_decl += item
else:
avail_decl.append(item)
irreg_nouns = [noun for noun in active_vocab.keys() if noun_vocab[noun].get("irreg", {}).get("irreg") and noun_vocab[noun]["decl"] in avail_decl]
irregs_include = []
irregs_only = "No"
with col_declension:
if len(irreg_nouns) > 0:
irregs_include = st.multiselect("Choose which irregular nouns to include:", options=irreg_nouns, default=irreg_nouns, help="Only irregular nouns for the selected declension(s) are shown.")
if len(irregs_include) > 0:
irregs_only = st.radio("Include *only* the selected irregular nouns?", options=["No", "Yes"], horizontal=True)
for noun in irreg_nouns:
if noun not in irregs_include:
if noun in active_vocab:
active_vocab.pop(noun)
if irregs_only == "Yes":
irreg_decl = []
for noun in irregs_include:
irreg_decl.append(noun_vocab[noun]["decl"])
active_vocab = {k:v for k,v in active_vocab.items() if k in irregs_include}
noun_options = {"case": {"nom": "nominative",
"gen": "genitive",
"dat": "dative",
"acc": "accusative",
"abl": "ablative",
"voc": "vocative"},
"number": {"sg": "singular",
"pl": "plural"}}
noun_endings = {1: {"sg": {"gen": "ae",
"dat": "ae",
"acc": "am",
"abl": "ā",
"voc": None},
"pl": {"nom": "ae",
"gen": "ārum",
"dat": "īs",
"acc": "ās",
"abl": "īs",
"voc": None}},
"2_us": {"sg": {"gen": "ī",
"dat": "ō",
"acc": "um",
"abl": "ō",
"voc": "e"},
"pl": {"nom": "ī",
"gen": "ōrum",
"dat": "īs",
"acc": "ōs",
"abl": "īs",
"voc": None}},
"2_er": {"sg": {"gen": "ī",
"dat": "ō",
"acc": "um",
"abl": "ō",
"voc": None},
"pl": {"nom": "ī",
"gen": "ōrum",
"dat": "īs",
"acc": "ōs",
"abl": "īs",
"voc": None}},
"2_neut": {"sg": {"gen": "ī",
"dat": "ō",
"acc": "um",
"abl": "ō",
"voc": None},
"pl": {"nom": "a",
"gen": "ōrum",
"dat": "īs",
"acc": "a",
"abl": "īs",
"voc": None}},
3: {"sg": {"gen": "is",
"dat": "ī",
"acc": "em",
"abl": "e",
"voc": None},
"pl": {"nom": "ēs",
"gen": "um",
"dat": "ibus",
"acc": "ēs",
"abl": "ibus",
"voc": None}},
"3_neut": {"sg": {"gen": "is",
"dat": "ī",
"acc": None,
"abl": "e",
"voc": None},
"pl": {"nom": "a",
"gen": "um",
"dat": "ibus",
"acc": "a",
"abl": "ibus",
"voc": None}},
"3_istem": {"sg": {"gen": "is",
"dat": "ī",
"acc": "em",
"abl": "e",
"voc": None},
"pl": {"nom": "ēs",
"gen": "ium",
"dat": "ibus",
"acc": ["īs","ēs"],
"abl": "ibus",
"voc": None}},
"3_istem_neut": {"sg": {"gen": "is",
"dat": "ī",
"acc": None,
"abl": "ī",
"voc": None},
"pl": {"nom": "ia",
"gen": "ium",
"dat": "ibus",
"acc": "ia",
"abl": "ibus",
"voc": None}},
4: {"sg": {"gen": "ūs",
"dat": ["uī","ū"],
"acc": "um",
"abl": "ū",
"voc": None},
"pl": {"nom": "ūs",
"gen": "uum",
"dat": "ibus",
"acc": "ūs",
"abl": "ibus",
"voc": None}},
"4_neut": {"sg": {"gen": "ūs",
"dat": "ū",
"acc": "ū",
"abl": "ū",
"voc": None},
"pl": {"nom": "ua",
"gen": "uum",
"dat": "ibus",
"acc": "ua",
"abl": "ibus",
"voc": None}},
"5_vowel": {"sg": {"gen": "ēī",
"dat": "ēī",
"acc": "em",
"abl": "ē",
"voc": None},
"pl": {"nom": "ēs",
"gen": "ērum",
"dat": "ēbus",
"acc": "ēs",
"abl": "ēbus",
"voc": None}},
"5_consonant": {"sg": {"gen": "eī",
"dat": "eī",
"acc": "em",
"abl": "ē",
"voc": None},
"pl": {"nom": "ēs",
"gen": "ērum",
"dat": "ēbus",
"acc": "ēs",
"abl": "ēbus",
"voc": None}},
}
# st.write(st.session_state.question_list)
## CREATE THE QUIZ ##
if len(declension) == 0 and not st.session_state.current_question:
st.write("You need to choose at least one declension.")
else:
def gen_question():
if len(st.session_state.question_list) > 0:
last_question = st.session_state.question_list[-1]
else:
last_question = {}
decl_rand = random.choice(declension)
decl_dict_subset = declension_dict.get(decl_rand)
if irregs_only == "Yes":
decl_dict_subset = random.choice(irreg_decl)
# st.write(decl_dict_subset)
if isinstance(decl_dict_subset, list):
vocab_subset = {k: v for k,v in active_vocab.items() if v["decl"] in decl_dict_subset}
else:
vocab_subset = {k: v for k,v in active_vocab.items() if v["decl"] == decl_dict_subset}
noun = random.choice(list(vocab_subset.keys()))
number = random.choice(list(noun_options["number"].keys()))
if number == "sg":
case_weights = [10,90,90,90,90]
if decl_rand == "2nd":
case_weights.append(80)
else:
case_weights.append(10)
else:
case_weights = [90,90,90,90,90,10]
case = ""
if last_question:
if noun_vocab[noun]["decl"] == last_question["id"].get("decl") and number == last_question["id"].get("num"):
case = last_question["id"].get("case")
while case == "" or case == last_question.get("id", {}).get("case") or (case in noun_vocab[noun].get("irreg", {}).get(number, {}) and noun_vocab[noun]["irreg"][number][case] is None):
case = random.choices(list(noun_options["case"].keys()),case_weights)[0]
# st.write(noun, case, number)
return [noun, case, number]
st.session_state.gen_func = gen_question
if st.session_state.current_question:
noun, case, number = st.session_state.current_question
# # Uncomment for testing purposes.
# st.write(st.session_state.current_question)
# st.write(f"Give the {noun_options["case"][case]} {noun_options["number"][number]} of *{noun}*.")
noun_decl = noun_vocab.get(noun, {}).get("decl")
noun_stem = noun_vocab.get(noun, {}).get("stem")
correct_answer = ""
if case == "nom" and number == "sg": # nominative singulars don't choose from list
correct_answer = noun
else:
if "irreg" in noun_vocab[noun]:
# pass
irreg_form = noun_vocab[noun]["irreg"].get(number, {}).get(case)
if irreg_form:
correct_answer = irreg_form
if not correct_answer:
correct_ending = noun_endings[noun_decl][number][case]
if noun[-3:] == "ius" and noun_decl == "2_us":
if case in ["voc", "gen"]:
noun_stem = noun_stem[:-1]
if case == "voc":
correct_ending = "ī"
if case == "gen":
correct_ending = ["iī","ī"]
if correct_ending is None and number == "sg": # deal with vocative singular other than 2nd decl. -us nouns
correct_answer = noun
else:
if correct_ending is None and number == "pl": # deal with vocative plurals
correct_ending = noun_endings[noun_decl][number]["nom"]
correct_answer = noun_stem + correct_ending
elif isinstance(correct_ending, list): # deal with alternative forms
#correct_ending = correct_ending[0]
correct_answer = []
for ending in correct_ending:
correct_answer.append(noun_stem + ending)
else:
correct_answer = noun_stem + correct_ending
st.session_state["correct_answer"] = correct_answer
question = f'For *{noun}*, give the **{noun_options["case"][case]} {noun_options["number"][number]}**.'
if show_declension:
for key, val in declension_dict.items():
if isinstance(val, list):
if noun_decl in val:
decl = key
if key == "3rd":
if noun_decl == "3_istem":
third_logic = "i-stem "
elif noun_decl == "3_neut":
third_logic = "neuter "
elif noun_decl == "3_istem_neut":
third_logic = "neuter i-stem "
else:
third_logic = ""
else:
pass
else:
if noun_decl == val:
decl = key
else:
pass
question += f" This is a {decl} declension {third_logic}noun."
if show_stem:
question += f' (The base is: {noun_vocab[noun]["stem"]}-)'
st.markdown("### Current question")
with st.form(key="noun_form", clear_on_submit=True):
current_answer = st.text_input(question, key="answer_input")
submit_button_col, user_answer_col = st.columns([1,2])
with submit_button_col:
def disable_button():
st.session_state.button_disable = True
st.form_submit_button(
"Check Answer",
key="form_submission_button",
on_click=submit_and_check_answer,
disabled=st.session_state.button_disable,
)
with user_answer_col:
st.markdown(st.session_state.answer_display_message)
curr_question = {
"pos": "noun",
"word": noun,
"id": {
"case": case,
"num": number,
"decl": "1st" if str(noun_decl)[0] == "1" else "2nd" if str(noun_decl)[0] == "2" else "3rd (i-stem)" if "istem" in str(noun_decl) else "3rd" if str(noun_decl)[0] == "3" else "4th" if str(noun_decl)[0] == "4" else "5th"
},
# "correct": False
}
if st.session_state.append_answer is True:
questions_asked.append(
curr_question
)
st.session_state.append_answer = False
## GENERATE NEW QUESTIONS AND CHECK ANSWERS ##
new_question_col, results_col, score_col = st.columns(3)
with new_question_col:
st.button("New Question", on_click=new_question, args=(gen_question,), key="question_button", width="stretch",
disabled=True if len(declension) == 0 else False
)
with results_col:
st.markdown(st.session_state.result_message) # just write the result message, rather than other things as well.
with score_col:
st.button("Reset Score", "reset", on_click=reset, width="stretch")
st.markdown(f"Current score: **{st.session_state.current_score}** out of **{st.session_state.total_questions}**")
if st.session_state.auto_advance_trigger and st.session_state.answer_checked:
time.sleep(st.session_state.auto_advance)
new_question(st.session_state.gen_func)
st.rerun()
#st.write(questions_asked)