-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.py
332 lines (296 loc) · 10.3 KB
/
tree.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
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
import itertools
import constants
import pandas as pd
import parse
NUM_COLS = 10
assert not 100 % NUM_COLS
class node:
def __init__(self, row: pd.Series) -> None:
self._row = row
self._descendants: list[node] = []
# Descendant information.
self._preprocessed = False
self._key_to_idx: dict[str, int] = {}
def row(self) -> pd.Series:
return self._row
def is_root(self) -> bool:
return "key_word" not in self._row
def cell(self, key: str) -> str:
return str(self.row()[key])
def add_descendant(self, descendant) -> None:
assert self.is_root()
assert isinstance(descendant, node)
assert descendant.cell("key_word") == self.cell("key")
assert not self._preprocessed
self._descendants.append(descendant)
def preprocess(self) -> None:
assert self.is_root()
assert not self._preprocessed
# Sort.
self._descendants = sorted(
self._descendants,
key=lambda n: int(n.cell("pos")),
)
# Populate the field needed for retrieving children by key.
self._key_to_idx = {
d.cell("key"): idx for idx, d in enumerate(self._descendants)
}
self._preprocessed = True
def child(self, key: str):
assert self.is_root()
assert self._preprocessed
return self._descendants[self._key_to_idx[key]]
def descendants(self, include_root: bool = False):
assert self.is_root()
assert self._preprocessed
return (
[self] + self._descendants if include_root else self._descendants
)
def crum_page_range(self) -> str:
assert self.is_root()
assert self._preprocessed
pages: list[parse.crum_page] = [
parse.parse_crum_cell(d.row()["crum"])
for d in self.descendants(include_root=True)
]
pages = [p for p in pages if p.real()]
if not pages:
return ""
if len(pages) == 1:
return pages[0].string()
ordered: list[parse.crum_page] = list(sorted(pages))
first, last = ordered[0], ordered[-1]
if first == last:
assert all(p == first for p in ordered)
return first.string()
return f"{first.string()}-{last.string()}"
def parent(self, child, include_root: bool = False):
assert not include_root, "Not yet implemented!"
assert self._preprocessed
assert self.is_root()
assert not child.is_root()
assert isinstance(child, node)
p = child.cell("key_deriv")
if not int(p):
return None
return self._descendants[self._key_to_idx[p]]
def index(self, child) -> int:
assert isinstance(child, node)
assert self.is_root()
assert self._preprocessed
return self._key_to_idx[child.cell("key")]
def html_table(
self,
explain: bool = True,
include_root: bool = False,
) -> str:
"""
We use the following fields from each child:
- depth
- word-parsed-classify
- type-parsed
- en-parsed
- crum
- key
They are expected to be pre-sorted, and to belong to a single word.
The per-dialected columns are used, but not included in the output.
Args:
explain: If true, include the meaning, type, and Crum page number.
"""
assert (
not include_root
), "An HTML tree with the root is not yet supported."
assert self.is_root()
assert self._preprocessed
descendants = self.descendants()
if not descendants:
return ""
crum_row_spans = build_crum_row_spans(descendants)
out = []
out.extend(
[
'<table class="derivations" id="derivations">',
"<colgroup>",
],
)
out.extend([f'<col style="width: {100/NUM_COLS}%;">'] * NUM_COLS)
out.extend(["</colgroup>"])
for d, crum_row_span in zip(descendants, crum_row_spans):
crum, crum_span = crum_row_span
if not crum_span:
assert not crum
if not crum:
crum_span = 0
if not explain:
crum, crum_span = "", 0
depth = int(d.cell("depth"))
word = d.cell("word-parsed-classify")
type = d.cell("type-parsed")
meaning = d.cell("en-parsed")
key = d.cell("key")
word_width = int((NUM_COLS - depth) / 2) if word else 0
# We keep the meaning column regardless of whether a meaning is
# actually present. However, if the whole table is to be generated
# without a meaning, we remove it.
meaning_width = NUM_COLS - word_width - depth - 1
if not explain and type != "HEADER":
# Skip the English.
meaning_width = 0
assert word_width or meaning_width
out.extend(
[
# New row.
f'<tr id="drv{key}" class="drv">',
# Margin.
f'<td colspan="{depth}"></td>' if depth else "",
# Word.
(
"".join(
[
f'<td colspan="{word_width}" class="marcion bordered">',
word,
(
f'<span hidden="" class="drv-key dev right">{key}</span>'
if not meaning_width
else ""
),
"</td>",
],
)
if word_width
else ""
),
# Meaning.
(
"".join(
[
f'<td colspan="{meaning_width}" class="meaning bordered">',
(
f"<b>({type})</b><br/>"
if type not in ["-", "HEADER"]
else ""
),
meaning,
f'<span hidden="" class="drv-key dev right">{key}</span>'
"</td>",
],
)
if meaning_width
else ""
),
(
f'<td rowspan="{crum_span}" class="dictionary bordered">'
"<b>Crum: </b>"
f'<span class="crum-page">{crum}</span>'
"</td>"
if crum_span
else ""
),
# End row.
"</tr>",
],
)
out.append("</table>")
return " ".join(out)
def html_list(
self,
include_root: bool = False,
) -> str:
assert (
not include_root
), "An HTML tree with the root is not yet supported."
assert self.is_root()
assert self._preprocessed
descendants = self.descendants()
if not descendants:
return ""
out = []
out.extend(
[
"<ul>",
],
)
depth = 0
for d in descendants:
cur_depth = int(d.cell("depth"))
while cur_depth > depth:
out.extend(
[
"<li>",
"<ul>",
],
)
depth += 1
while cur_depth < depth:
out.extend(
[
"</ul>",
"</li>",
],
)
depth -= 1
word = d.cell("word-parsed-prettify")
type = d.cell("type-parsed")
# Calling the parser in tree? A little unorthodox, eh?!
meaning = parse.lighten_greek(d.cell("en-parsed"))
assert word or (type == "HEADER" and meaning)
if type and type not in ["-", "HEADER"]:
meaning = f"({type}) {meaning}"
li = "<br/>".join(filter(None, [word, meaning]))
out.extend(
[
"<li>",
li,
"</li>",
],
)
while depth > 0:
out.extend(
[
"</ul>",
"</li>",
],
)
depth -= 1
out.extend(
[
"</ul>",
],
)
return " ".join(out)
def depths(derivations: pd.DataFrame) -> list[int]:
keys = [int(row["key"]) for _, row in derivations.iterrows()]
parents = [int(row["key_deriv"]) for _, row in derivations.iterrows()]
key_to_parent = {k: p for k, p in zip(keys, parents)}
def depth(key: int) -> int:
parent = key_to_parent[key]
if not parent:
return 0
return 1 + depth(parent)
depths = [depth(k) for k in keys]
assert all(0 <= x <= constants.MAX_DERIVATION_DEPTH for x in depths)
return depths
def build_crum_row_spans(nodes: list[node]) -> list[tuple[str, int]]:
crum_column = [d._row["crum"] for d in nodes]
out = []
for group in itertools.groupby(crum_column):
# Validate that all elements are equal.
crum = group[0]
repetitions = len(list(group[1]))
out.append((crum, repetitions))
for _ in range(repetitions - 1):
out.append(("", 0))
return out
def build_has_cell(tree: node, cell_name: str) -> list[bool]:
assert tree.is_root()
has_cell = [False for _ in tree.descendants()]
for idx, d in enumerate(tree.descendants()):
if d.cell(cell_name):
has_cell[idx] = True
# Travel up the tree!
while True:
d = tree.parent(d)
if not d:
break
has_cell[tree.index(d)] = True
return has_cell