-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·55 lines (50 loc) · 1.48 KB
/
utils.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
#!/usr/bin/env python3
def get_entries(filename="corrected.txt"):
"""
yields entries from the corrected vocabulary, one per line
"""
with open(filename) as f:
s = []
for line in f.readlines()[280:14406]:
if line == "\n":
if s:
yield " ".join(s)
s = []
else:
s.append(line.strip())
if s:
yield " ".join(s)
def extract_etymology(entry):
"""
extracts the etymology part of a given entry string
"""
state = 0
etymology = ""
for ch in entry:
if state == 0:
if ch == "[":
state = 1
elif ch == "]":
raise Exception(f"unexpected ] in: {entry}")
else:
pass
elif state == 1:
if ch == "[":
raise Exception(f"unexpected [ in: {entry}")
elif ch == "]":
state = 2
else:
etymology += ch
elif state == 2:
if ch == "[":
state = 1
etymology = "" # reset etymology so only last [...] is counted
elif ch == "]":
raise Exception(f"unexpected ] in: {entry}")
else:
pass
else:
raise Exception(f"unexpected state {state} in: {entry}")
if state not in [0, 2]:
raise Exception(f"unexpected end state {state} in: {entry}")
return etymology