-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_node.py
More file actions
563 lines (435 loc) · 24 KB
/
tree_node.py
File metadata and controls
563 lines (435 loc) · 24 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# CWDE615
# A file for parse tree nodes
""" This code defines the Abstract Syntax Tree (AST) node classes used in the Mixtec Parser
for interpreting a sequence of tokens representing Mixtec codices,
based on the provided Context-Free Grammar (CFG)"""
import tokens as tokens
from abc import ABC, abstractmethod
class TreeNode(ABC):
"""
This is the base class for all AST nodes.
Attributes:
- children: child nodes in the parse tree (can be other TreeNodes).
- first_token: the token (like Human, Obj, etc.) that started this tree node.
Methods:
- add_children(chs): Adds children to the current node.
- get_children(), get_token(): Accessor methods.
- is_first_token_type(exp): Checks if this node’s first token is of a given type.
- interpret(): Abstract method that must be implemented by subclasses to produce human-readable output.
"""
def __init__(self, first_token, children : list[object] = None):
self.children : list[object] = children if children != None else []
self.first_token : tokens.Token = first_token
def add_children(self, chs):
for ch in chs:
self.children.append(ch)
def get_children(self):
return self.children
def get_token(self):
return self.first_token
def is_first_token_type(self, exp : type):
return type(self.first_token) == exp
@abstractmethod
def interpret(self) -> str:
pass
class Start(TreeNode):
"""
This is the root node of the tree, corresponding to the CFG rule S ::= (Sent end)+.
Methods:
- interpret() : Iterates through children (which alternate between Sent and end) and calls their interpret() method.
Joins all sentence interpretations into a final string.
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
def interpret(self) -> str:
sentence_list : list[str] = []
# the Start symbol should have an alternating sequence of Sent and end children.
for sent_or_end in self.children:
sentence_list.append(sent_or_end.interpret())
return ". ".join(sentence_list)
class Sent(TreeNode):
"""
Represents a sentence, following the Sent rule in the CFG.
Sent ::= Clause | obj (Date Clause | Clause) | Date (obj Clause | Clause)
Methods:
- interpret(): Handles three major types of sentences:
1. If the sentence starts with a Human token → it's a Clause.
2. If it starts with an Obj or Year,
we process the first part (e.g., date or place),
capitalize it, and interpret the rest accordingly.
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
def interpret(self):
sentence_list : list[str] = []
if self.is_first_token_type(tokens.Human):
return self.children[0].interpret() # Clause
if self.is_first_token_type(tokens.Obj) or self.is_first_token_type(tokens.Year):
first_part = self.children[0].interpret().split() # obj || Date
capitalized_first_word = first_part[0].capitalize()
sentence_list.append(" ".join([capitalized_first_word] + first_part[1:]))
if len(self.children) > 2: # TODO: handle specific objects differently
sentence_list.append(self.children[1].interpret()) # Date || obj
sentence_list.append(self.children[2].interpret()) # Clause || Clause
else:
sentence_list.append(self.children[1].interpret()) # Clause || Clause
return " ".join(sentence_list)
class Clause(TreeNode):
"""
Represents the rule -> Clause ::= Clause_f+ (Date_tail | Obj_tail | ɛ)
This class contains domain-specific knowledge for interpreting scenes based on human pose, gender, and symbolic artifacts.
Methods:
- interpret(): This is the core logic of interpreting relationships between humans, objects, and time.
Steps:
1. Collect all leading ClauseF nodes (representing people).
2. Check if a DateTail or ObjTail exists (to be interpreted later).
3. If the tail is present:
a. Parse its elements: objects, dates, and right-hand side human figures (if any).
b. Use @ and $ to delimit parsed segments (e.g., obj@date@humans).
Note: The logic varies significantly depending on:
a. Number of people involved.
b. Their orientation (e.g., facing each other = interaction).
c. Pose (e.g., both sitting = marriage or ritual).
d. Presence of objects (e.g., throne, sacrificed_animal, weapon).
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
def interpret(self) -> str:
i = 0
l_human_list : list[str] = []
l_pose_list : list[int] = []
l_orientation_list : list[int] = []
l_gender_list : list[int] = []
while i < len(self.children) and self.children[i].is_first_token_type(tokens.Human):
i_first_token = self.children[i].first_token
l_human_list.append(self.children[i].interpret())
l_pose_list.append(i_first_token.pose)
l_orientation_list.append(i_first_token.orientation)
l_gender_list.append(i_first_token.gender)
i += 1
obj_tail = None
date_tail = None
# get the possible Obj_tail or Date_tail. These may have their own human_list to consider.
if i < len(self.children) and self.children[i].is_first_token_type(tokens.Obj):
obj_tail = self.children[i].interpret()
elif i < len(self.children) and self.children[i].is_first_token_type(tokens.Year):
date_tail = self.children[i].interpret()
# real_tail is a tuple if either of the ifs above executed. Otherwise, it is None
real_tail = obj_tail if obj_tail != None else date_tail
# bit records whether obj_tail is None.
# If real_tail is a tuple and tail_bit is 1, then real_tail corresponds to a date_tail tuple.
tail_bit = 0 if obj_tail != None else 1
# All of the following are UD and set to None if there is no tail for this Clause (real_tail == None)
tail_list = None
r_pose_list = None
r_orientation_list = None
r_gender_list = None
obj_string = None
date_string = None
human_string = None
if real_tail != None:
tail_string, r_pose_list, r_orientation_list, r_gender_list = real_tail
tail_list = tail_string.split('@')
if len(tail_list) == 1:
obj_string = tail_list[0]
date_string = None
human_string = None
if len(tail_list) == 2:
obj_string = tail_list[0]
date_string = None
human_string = tail_list[1]
elif len(tail_list) == 3:
obj_string = tail_list[0]
date_string = tail_list[1]
human_string = tail_list[2]
if tail_bit == 1: # Clause has a Date_tail
tmp = obj_string
obj_string = date_string
date_string = tmp
check_for_date = lambda result_string : result_string if date_string == None else " ".join([date_string, result_string])
check_for_obj = lambda result_string : result_string if obj_string == None else " ".join([result_string, "near a", obj_string])
check_date_obj = lambda result_string : check_for_obj(check_for_date(result_string))
def handle_two_human_figures(
l_pose : int,
l_orientation: int,
l_gender : int,
l_human : str,
r_pose : int,
r_orientation : int,
r_gender : int,
r_human : str
):
# These have the form [Lord/Lady # Symbol, NearObjIdentity]
l_name_near_obj = l_human.split('$')
r_name_near_obj = r_human.split('$')
# the figures are facing the same direction
if l_orientation == r_orientation:
result_l = l_name_near_obj[0] if len(l_name_near_obj) == 1 else f"{l_name_near_obj[0]} with a {l_name_near_obj[1]}"
result_r = r_name_near_obj[0] if len(r_name_near_obj) == 1 else f"{r_name_near_obj[0]} with a {r_name_near_obj[1]}"
# For there to be a r_human_list, there must be a right Clause_f+ in the AST, implying that there must also
# be some kind of tail. Include it with the output.
return check_date_obj(" ".join(["there was", result_l, "and", result_r, "near", obj_string]))
# beyond this point, orientation is different
# handle the rare case that two figures are drawn facing away from each
# other in a scene.
if l_orientation == 0 and r_orientation == 1:
result_l = l_name_near_obj[0] if len(l_name_near_obj) == 1 else f"{l_name_near_obj[0]} with a {l_name_near_obj[1]}"
result_r = r_name_near_obj[0] if len(r_name_near_obj) == 1 else f"{r_name_near_obj[0]} with a {r_name_near_obj[1]}"
return check_date_obj(" ".join(["there was", result_l, "and", result_r]))
# beyond this point, the two human figures are facing one another.
# This usually implies interaction between the two of them, and there
# are several interesting cases.
# tail has table or house obj and two h with opposite gender are sitting. This is usually taken to
# mean that the two figures were married, and is common in genealogical segments.
if obj_string in ["house", "table"] and l_gender is not r_gender and l_pose == 0 and r_pose == 0:
return check_for_date(" ".join([l_name_near_obj[0], "married", r_name_near_obj[0]]))
# There is no object between the figures, and one is sitting while the other is standing. This normally indicates
# that the stander is consulting with the sitter. The sitter often appears on a throne, which will appear as a near_obj
# token and is handled.
if l_pose is not r_pose:
sitter = l_name_near_obj if l_pose == 0 else r_name_near_obj
stander = r_name_near_obj if l_pose == 0 else l_name_near_obj
sit_gender = l_gender if l_pose == 0 else r_gender
possessive = "his" if sit_gender == 0 else "her"
if len(sitter) == 2 and sitter[1] == "throne":
result_a = " ".join([stander[0], "consulted", sitter[0], f"sitting on {possessive}", sitter[1]])
else:
result_a = " ".join([stander[0], "consulted", sitter[0]])
return check_date_obj(result_a)
# At this point, the two figures are either both sitting or both standing in addition to facing one
# another.
# Both are sitting. This usually implies a seance, ritual or conversation. The generic verb 'communed' is chosen to reflect
# the idea that the two are communicating on equal terms, possibly with unmentioned third parties.
if l_pose == 0 and r_pose == 0:
return check_date_obj(" ".join([l_name_near_obj[0], "communed with", r_name_near_obj[0]]))
# Both are standing. Typical interpretations depend on the near_obj of the two actors. A generic interpretation is also given.
# sacrificed_animal near either person implies that the two are involved in ritual sacrifice.
if "sacrificed_animal" in l_name_near_obj or "sacrificed_animal" in r_name_near_obj:
return check_date_obj(" ".join([l_name_near_obj[0], "and", r_name_near_obj[0], "participated in a ritual sacrifice"]))
# At this point, neither human can have a sacrificed animal near_obj. This disambiguates what is happening
# should either or both of them have weapons. If not to kill an animal, they must be using the weapons on each other.
if "weapon" in l_name_near_obj or "shield" in l_name_near_obj or "weapon" in r_name_near_obj or 'shield' in r_name_near_obj:
return check_date_obj(" ".join([l_name_near_obj[0], "fought", r_name_near_obj[0]]))
# Catch all return case to prevent fall through to other if statements. It is known that two humans are standing and facing
# one another in this sentence, and that neither of them holds a weapon, shield or sacrificed animal. Simply say they met.
return check_date_obj(" ".join([l_name_near_obj[0], "met", r_name_near_obj[0]]))
if human_string != None:
r_human_list = human_string.split('#')
else: # no human_string means there is either no tail, or the tail is just a Date or obj
return_list : list[str] = []
if len(l_human_list) == 1:
result_a = f"there was {l_human_list[0].split('$')[0]}"
elif len(l_human_list) == 2:
return handle_two_human_figures(
l_pose = l_pose_list[0],
l_orientation = l_orientation_list[0],
l_gender = l_gender_list[0],
l_human = l_human_list[0],
r_pose = l_pose_list[1],
r_orientation = l_orientation_list[1],
r_gender = l_gender_list[1],
r_human = l_human_list[1]
)
else:
l_standing = True
l_same = True
direction = None
for l in l_pose_list:
if l == 0:
l_standing = False
for dir in l_orientation_list:
if direction == None:
direction = dir
else:
if dir != direction:
l_same = False
break
for human in l_human_list:
h_nd_near = human.split('$') # [(Lady/Lord # Symbol OR a Lady/Lord)] OR [(Lady/Lord # Symbol OR a Lady/Lord), NearObjIdentity]
return_list.append(h_nd_near[0])
joined_return_list = ", ".join(return_list[0:-1])
joined_return_list = ", and ".join([joined_return_list, return_list[-1]])
if l_same and l_standing:
result_a = " ".join(["there was a procession or journey including", joined_return_list])
if not l_same and l_standing:
result_a = " ".join(["there was a gathering including", joined_return_list])
result_a = " ".join(["there were", joined_return_list])
# if there really is tail, then there is either an obj associated with the clause or date. Figure out which
# and annotate accordingly.
if real_tail != None:
if tail_bit == 0: # Obj_tail without second Clause_f+
return " near a(n) ".join([result_a, obj_string])
else: # Date_tail without a second Clause_f+
return " ".join([result_a, date_string]) # simply join the result_a with the date string, since Date formats itself.
# Here, real_tail == None, so there really was no tail, and the interpretation of the one Clause_f sequence is returned.
return result_a
r_human_list : list[str] = human_string.split("#")
# one figure on either side.
if len(l_human_list) == 1 and len(r_human_list) == 1:
return handle_two_human_figures(
l_pose = l_pose_list[0],
l_orientation = l_orientation_list[0],
l_gender = l_gender_list[0],
l_human = l_human_list[0],
r_pose = r_pose_list[0],
r_orientation = r_orientation_list[0],
r_gender = r_gender_list[0],
r_human = r_human_list[0],
)
# one to many or many to one TODO
# many to many TODO
# ERROR: NO INTERPRETATION MATCHES
return "<ERROR: Clause is in grammar but no interpretation exists for it.>"
class DateTail(TreeNode):
"""
Represents optional Date_tail ::= Date (obj Clause_f+ | Clause_f+ | ɛ).
Methods:
interpret() -> tuple
Returns a tuple: A string in the format date@obj@humans, or just date.
Lists of pose, orientation, gender for further semantic interpretation.
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
# TODO: Check that returning a tuple like this doesn't break the interface.
def interpret(self) -> tuple[str, list[int], list[int]]:
date_string : str = self.children[0].interpret()
if len(self.children) > 1:
obj_string : str | None = None
i : int = 1
if self.children[1].is_first_token_type(tokens.Obj):
obj_string = self.children[1].interpret()
i += 1
r_human_list = []
r_pose_list : list[int] = []
r_orientation_list : list[int] = []
r_gender_list : list[int] = []
while i < len(self.children) and self.children[i].is_first_token_type(tokens.Human):
i_first_token = self.children[i].first_token
r_human_list.append(self.children[i].interpret())
r_pose_list.append(i_first_token.pose)
r_orientation_list.append(i_first_token.orientation)
r_gender_list.append(i_first_token.gender)
i += 1
human_string = "#".join(r_human_list)
# Note that, according to the grammar, it's not actually possible
# for the human_string to be empty.
if obj_string == None:
return "@".join([date_string, human_string]), r_pose_list, r_orientation_list, r_gender_list
else:
return "@".join(date_string, obj_string, human_string), r_pose_list, r_orientation_list, r_gender_list
else:
return date_string, [], [], []
class ObjTail(TreeNode):
"""
Represents optional Obj_tail ::= obj (Date Clause_f+ | Clause_f+ | ɛ).
Methods:
interpret() -> tuple
Returns a tuple: A string in the format obj@date@humans or just obj.
Lists of pose, orientation, gender for further semantic interpretation.
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
def interpret(self) -> tuple[str, list[int], list[int]]:
obj_string = self.first_token.interpret()
if len(self.children) > 1:
date_string : str | None = None
i : int = 1
if self.children[1].is_first_token_type(tokens.Year):
date_string = self.children[1].interpret()
i += 1
r_human_list = []
r_pose_list : list[int] = []
r_orientation_list : list[int] = []
r_gender_list : list[int] = []
while i < len(self.children) and self.children[i].is_first_token_type(tokens.Human):
i_first_token = self.children[i].first_token
r_human_list.append(self.children[i].interpret())
r_pose_list.append(i_first_token.pose)
r_orientation_list.append(i_first_token.orientation)
r_gender_list.append(i_first_token.gender)
i += 1
human_string = "#".join(r_human_list)
# Note that, according to the grammar, it's not actually possible
# for the human_string to be empty.
if date_string == None:
return "@".join([obj_string, human_string]), r_pose_list, r_orientation_list, r_gender_list
else:
return "@".join(obj_string, date_string, human_string), r_pose_list, r_orientation_list, r_gender_list
else:
return obj_string, [], [], []
class Date(TreeNode):
"""
Represents a date token: Date ::= y (nd | ɛ).
Methods:
interpret()
If an nd (name-date) follows a y (year), returns: in Year 12 Reed Day 3 Deerslac
If no nd, just: in Year 12 Reed
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
def interpret(self):
year_string : str = self.first_token.interpret() # interpret the year
if len(self.children) > 1:
date_string : str = self.children[1].interpret() # nd
return " ".join(["in Year", year_string, "Day", date_string])
else:
return " ".join(["in Year", year_string])
class ClauseF(TreeNode):
"""
Represents an individual clause fragment: Clause_f ::= h ( nd | Near_date | ɛ ).
Methods:
interpret()
If alone → returns "a Lord" / "a Lady"
If followed by:
NameDate → returns "Lord 3 Deer"
NearObj → returns "a Lord$torch" or "Lord 3 Deer$torch"
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
def interpret(self):
h_string : str = self.first_token.interpret() # h
# there is a tail.
if len(self.children) > 1:
tail_string : str = self.children[1].interpret()
nd_near_obj : list[str] = tail_string.split("$")
# tail starts with a name date
if self.children[1].is_first_token_type(tokens.NameDate):
return f'{h_string} {nd_near_obj[0]}' # Lord/Lady # Symbol
# tail starts with a NearObj.
elif self.children[1].is_first_token_type(tokens.NearObj):
# if it starts with a NearObj, there could be a NameDate
# associated with the parent h token after it. Handle
# accordingly.
if len(nd_near_obj) == 2:
return "$".join([f'{h_string} {nd_near_obj[1]}',f'{nd_near_obj[0]}']) # Lord/Lady # Symbol$NearObjIdentity
else:
return "$".join([f'a {h_string}',f'{nd_near_obj[0]}']) # a Lord/Lady$NearObjIdentity
else:
# h is alone, without a tail. Corresponds to nameless person not associated with a near_obj token.
return f'a {h_string}' # 'a Lord' or 'a Lady' since they have no name-date.
class NearDate(TreeNode):
"""
Represents Near_date ::= near_obj (nd | ɛ)
interpret()
Returns a string like "torch$3 Deer" to indicate possession or association
(e.g., "Lady with a torch named 3 Deer").
"""
def __init__(self, first_token, children = None):
super().__init__(first_token, children)
def interpret(self) -> str:
near_obj_string : str = self.first_token.interpret() # near_obj
# somewhat bends the rules of the interpretation interface by inserting a meta-character '$'
# to represent that the two pieces of information provided are not meant to be in sequence.
if len(self.children) > 1:
name_date_string : str = self.children[1].interpret() # nd
return "$".join([near_obj_string, name_date_string])
else:
return near_obj_string
class LeafNode(TreeNode):
def __init__(self, token: "Token"):
super().__init__(token, children=None)
def interpret(self) -> str:
# For leaf nodes, interpretation is just the token's interpretation
return self.first_token.interpret()
def to_xml(self) -> str:
return self.first_token.to_xml()