forked from De7vID/klingon-assistant-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_sv.py
More file actions
executable file
·140 lines (119 loc) · 5.67 KB
/
import_sv.py
File metadata and controls
executable file
·140 lines (119 loc) · 5.67 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
#!/usr/bin/env python3
from collections import defaultdict
from collections import namedtuple
import fileinput
import glob
import re
EntryKey = namedtuple("EntryKey", ["entry_name", "part_of_speech"])
DefinitionPair = namedtuple("DefinitionPair", ["definition", "definition_sv"])
definitions_map = defaultdict(list)
strip_characters = dict.fromkeys(map(ord, '<>«»~'), None)
# Note: This file must be downloaded from [ http://klingonska.org/dict/dict.zdb ].
dictfile = fileinput.FileInput("dict.zdb", mode='r')
# Skip everything up to the start of the word list.
while dictfile.readline() != "=== start-of-word-list ===\n":
pass
# Read and process the word list and the verb prefix list.
for line in dictfile:
# Each entry begins after a blank line.
if line == "\n":
tlh = dictfile.readline()
if tlh == "=== end-of-verb-prefix-list ===\n":
break
tlh = re.sub(r"tlh:\t(?:\[\d\] )?{(.*)}(?: \[\d?\.?\d\])?", r"\1", tlh.rstrip())
tlh = tlh.translate(strip_characters)
pos = dictfile.readline().rstrip()
pos = re.sub(r"pos:\t(.*)", r"\1", pos)
en = ""
nextline = dictfile.readline().rstrip()
while not(re.compile("sv:.*").match(nextline)):
en = en + nextline
nextline = dictfile.readline().rstrip()
en = re.sub(r"(.*) \[.*\]", r"\1", en)
en = re.sub(r"en:\t(.*)", r"\1", en)
en = en.replace("--", "-")
en = en.translate(strip_characters)
sv = nextline
nextline = dictfile.readline().rstrip()
while not(re.compile("[a-z][a-z][a-z]?[a-z]?:.*").match(nextline)):
nextline = dictfile.readline().rstrip()
sv += nextline
sv = re.sub(r"(.*) \[.*\]", r"\1", sv)
sv = re.sub(r"sv:\t(.*)", r"\1", sv)
sv = sv.replace("--", "-")
sv = sv.translate(strip_characters)
key = EntryKey(entry_name = tlh, part_of_speech = pos)
pair = DefinitionPair(definition = en, definition_sv = sv)
definitions_map[key].append(pair)
# print(definitions_map)
# Put the Swedish entries into the mem-*.xml files.
for filename in glob.glob("mem-*.xml"):
print(filename)
with fileinput.FileInput(filename, inplace=True) as memfile:
for line in memfile:
# Find the beginning of an entry.
if re.compile(" <column name=\"_id\">\d*</column>\n").match(line):
print(line, end='')
entry_name = memfile.readline().rstrip()
print(entry_name)
entry_name = re.sub(r" <column name=\"entry_name\">(.*)</column>", r"\1", entry_name)
part_of_speech = memfile.readline().rstrip()
while not(re.compile(".*part_of_speech.*").match(part_of_speech)):
print(part_of_speech)
part_of_speech = memfile.readline().rstrip()
print(part_of_speech)
part_of_speech = re.sub(r" <column name=\"part_of_speech\">(.*)</column>", r"\1", part_of_speech)
if re.compile("adv(?:.*)").match(part_of_speech):
part_of_speech = "adverbial"
elif re.compile("conj(?:.*)").match(part_of_speech):
part_of_speech = "conjunction"
elif re.compile("excl(?:.*)").match(part_of_speech):
part_of_speech = "exclamation"
elif re.compile("n:(?:.*)name(?:.*)").match(part_of_speech):
part_of_speech = "name"
elif re.compile("n:(?:.*)num(?:.*)").match(part_of_speech):
part_of_speech = "numeral"
elif re.compile("n:(?:.*)pro(?:.*)").match(part_of_speech):
part_of_speech = "pronoun"
elif re.compile("n|n:(?:.*)").match(part_of_speech):
part_of_speech = "noun"
elif re.compile("ques(?:.*)").match(part_of_speech):
part_of_speech = "question word"
elif re.compile("v:pref").match(part_of_speech):
part_of_speech = "verb prefix"
elif re.compile("v:.*").match(part_of_speech):
part_of_speech = "verb"
definition = memfile.readline().rstrip()
print(definition)
definition = re.sub(r" <column name=\"definition\">(.*)</column>", r"\1", definition)
# Skip over "de" and "fa" definitions.
print(memfile.readline().rstrip())
print(memfile.readline().rstrip())
# Try to match the Swedish definition.
key = EntryKey(entry_name = entry_name, part_of_speech = part_of_speech)
pairs_list = definitions_map[key]
if len(pairs_list) == 0:
definition_sv = ""
elif len(pairs_list) == 1:
definition_sv = pairs_list[0].definition_sv
del definitions_map[key]
else:
for i in range(0, len(pairs_list)):
if pairs_list[i].definition == definition:
definition_sv = pairs_list[i].definition_sv
del pairs_list[i]
break
sv_line = memfile.readline().rstrip()
if definition_sv != "":
sv_line = re.sub(r"><", ">%s<" % definition_sv, sv_line)
print(sv_line)
else:
print(line, end='')
count = 0
for key in definitions_map:
if not(definitions_map[key] == []):
print(key, definitions_map[key])
count = count + 1
print(count)
# print(definitions_map)
# print(len(definitions_map))