-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_vocab.py
297 lines (269 loc) · 10.3 KB
/
make_vocab.py
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
import sys
from dataclasses import asdict, dataclass
import os
import re
import shutil
import json
from dataclasses import field
from json import JSONDecodeError
TERM_REGEX: str = "[Ꭰ-Ᏼ]+"
ENTRY_REGEX: str = "-?[Ꭰ-Ᏼ]+-?"
@dataclass
class Config:
already_used: list[str] = field(default_factory=list)
entries_file: str = "vocabulary.txt"
unmatched_file: str = "vocabulary-unmatched.txt"
vocab_template: str = "vocabulary-template.lyx"
vocab_lyx: str = "vocabulary.lyx"
source_file: str = "Na-Anijoi-Yona.lyx"
def load(self):
if not os.path.isfile("vocabulary.json"):
with open("vocabulary.json", "w") as w:
json.dump(asdict(self), w, indent=4)
with open("vocabulary.json", "r") as r:
try:
jconfig: dict = json.load(r)
for key in jconfig.keys():
if hasattr(self, key):
setattr(self, key, jconfig[key])
except JSONDecodeError as e:
with open("vocabulary.json", "w") as w:
json.dump(asdict(self), w, indent=4)
def main() -> None:
config: Config = Config()
config.load()
already_used: list[str]
if config.already_used:
already_used = config.already_used
else:
already_used = []
entries_file = config.entries_file
unmatched_file = config.unmatched_file
vocab_template = config.vocab_template
vocab_lyx = config.vocab_lyx
source_file = config.source_file
terms: list[str] = extract_terms(source_file)
entries: dict[str] = dict()
entry_breakdowns: dict[str] = dict()
active_entry_breakdowns: dict[str] = dict()
if not os.path.isfile(entries_file):
with open(entries_file, "w"):
pass
with open(entries_file, "r") as f:
for line in f:
line = line.strip()
if not line:
continue
if line.startswith("#"):
continue
parts: list[str] = line.split("|")
if len(parts) != 4:
print("Bad vocabulary entry: " + line)
continue
entry_word: str = parts[0].strip().upper()
entry_pronounce: str = parts[1].strip()
entry_breakdown: str = parts[2].strip()
entry_definition: str = parts[3].strip()
if not entry_definition:
continue # Ignore entries without a definition
entries[entry_word] = (entry_word, entry_pronounce, entry_breakdown, entry_definition)
if entry_word.startswith("-"):
entry_breakdowns[entry_word] = (entry_word, entry_pronounce, entry_breakdown, entry_definition)
with open(entries_file, "r") as f:
for line in f:
line = line.strip()
if not line:
continue
if line.startswith("#"):
continue
parts: list[str] = line.split("|")
if len(parts) != 4:
print("Bad vocabulary entry: " + line)
continue
entry_term: str = parts[0].strip()
entry_breakdown: str = parts[2].strip()
entry_definition: str = parts[3].strip()
if not entry_definition:
continue # Ignore entries without a definition
if entry_breakdown:
for match in re.finditer(ENTRY_REGEX, entry_breakdown):
item: str = match.group()
if not item:
continue
if not item.startswith("-"):
continue
if item.endswith("-"):
continue
if item not in entries:
entries[item] = (item, "", "", "")
if item not in entry_breakdowns:
entry_breakdowns[item] = (item, "", "", "")
if item not in active_entry_breakdowns and entry_term in terms:
active_entry_breakdowns[item] = (item, "", "", "")
previous_entries: list[str] = []
for already_file in already_used:
if not os.path.isfile(already_file):
print(f"Previous entries file {already_file} not found.")
continue
with open(already_file, "r") as w:
for line in w:
if line.strip().startswith("#"):
continue
parts: list[str] = line.strip().split("|")
if len(parts) == 4:
already_entry: str = parts[0].strip()
if already_entry not in previous_entries:
previous_entries.append(already_entry)
print(f"Loaded {len(previous_entries):,} entries from {already_file}")
for term in terms:
if term in entries:
continue
if term in previous_entries:
continue
entries[term] = (term, "", "", "")
entries_text: list[str] = list()
entries_text.append(f"# {source_file}")
entries_text.append("# ① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ ⑪ ⑫ ⑬ ⑭ ⑮ ⑯ ⑰ ⑱ ⑲ ⑳ ㉑ ㉒ ㉓ ㉔ ㉕ ㉖ ㉗ ㉘ ㉙ ㉚ ㉛ ㉜ ㉝ ㉞ ㉟ ㊱ ㊲ ㊳ ㊴ ㊵ ㊶ ㊷ ㊸ ㊹ ㊺ ㊻ ㊼ ㊽ ㊾ ㊿")
entries_text.append("Syllabary|Pronounce|Breakdown|Definition")
# first add entries with no definitions - in insertion order
for term in entries:
if not re.match(ENTRY_REGEX, term):
continue
(term, pronounce, break_down, definition) = entries[term]
if definition:
continue
entries_text.append(f"{term}|{pronounce}|{break_down}|{definition}")
# second add entries in ascending order with definitions
for term in sorted(list(entries)):
if not re.match(ENTRY_REGEX, term):
continue
(term, pronounce, break_down, definition) = entries[term]
if not definition:
continue
entries_text.append(f"{term}|{pronounce}|{break_down}|{definition}")
skip_entries: list[str] = list()
# Output unmatched entries listing. Useful for manually pruning the master list.
with open(f"{unmatched_file}", "w") as w:
for entry in entries_text:
entry_term: str = entry.split("|")[0]
if entry_term in active_entry_breakdowns:
continue
if entry_term in terms:
continue
if entry_term in previous_entries:
continue
if " " in entry_term:
continue
skip_entries.append(entry_term)
w.write(entry.strip())
w.write("\n")
# Output updated master entries file.
with open(f"{entries_file}.tmp", "w") as w:
for entry in entries_text:
w.write(entry.strip())
w.write("\n")
shutil.copy(entries_file, f"{entries_file}.bak")
shutil.copy(f"{entries_file}.tmp", entries_file)
# Output updated used entries file.
used_count: int = 0
with open(f"used-{entries_file}", "w") as w:
for entry in entries_text:
parts: list[str] = entry.strip().split("|")
if len(parts) != 4:
continue
(entry_term, _, _, definition) = parts
if definition.strip() == "*":
continue
if entry_term in skip_entries:
continue
if entry_term.startswith("-") and entry_term not in active_entry_breakdowns.keys():
continue
w.write(entry.strip())
w.write("\n")
used_count += 1
print(f"Wrote {used_count:,} entries to used-{entries_file}.")
counter: int = 0
section: str = ""
split_section: int = 10
with open(vocab_lyx, "w") as w:
with open(vocab_template, "r") as r:
for line in r:
w.write(line)
if line.startswith("\\begin_body"):
break
w.write("\n")
w.write("\\begin_layout Chapter\n")
w.write("Vocabulary\n")
w.write("\\end_layout\n")
w.write("\n")
for key in sorted(list(entries)):
if not re.match(ENTRY_REGEX, key):
continue
if key in skip_entries:
continue
term: str
pronounce: str
break_down: str
definition: str
(term, pronounce, break_down, definition) = entries[key]
if not definition.strip():
continue
if definition.strip() == "*":
continue
if term in previous_entries:
continue
if section != term[0]:
section = term[0]
w.write(section_entry(section))
counter = 0
elif (counter % split_section) == 0:
w.write(section_entry(term))
counter += 1
w.write(glossary_entry(term, pronounce, break_down, definition))
w.write("\n")
w.write("\n")
w.write("\\end_body")
w.write("\n")
w.write("\\end_document")
w.write("\n")
def section_entry(section: str) -> str:
return ("\n"
"\\begin_layout\n"
"Section*\n"
f"{section}\n"
"\\end_layout\n"
"\n")
def glossary_entry(term: str, pronounce: str, break_down: str, definition: str) -> str:
term = term.replace(" ", "\\begin_inset space ~\n\\end_inset\n\n")
entry: str = (f"\n"
f"\\begin_layout Description\n"
f"{term} ")
if definition[-1] not in ".,?!":
definition += "."
if pronounce:
entry += f"[{pronounce}] "
entry += (f"\\begin_inset Quotes eld\n"
f"\\end_inset\n"
f"\n"
f"{definition}\n"
f"\\begin_inset Quotes erd\n"
f"\\end_inset\n"
f"\n")
if break_down:
entry += f" ({break_down})"
entry += (f"\n"
f"\\end_layout\n"
f"\n")
return entry
def extract_terms(file: str) -> list[str]:
terms: list[str] = list()
with open(file, "r") as f:
for line in f:
for item in re.finditer(TERM_REGEX, line.upper()):
chr_text: str = item.group()
if not chr_text in terms:
terms.append(chr_text)
return terms
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))
main()