-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.py
132 lines (81 loc) · 2.75 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
from itertools import chain
import tokens
class Tree:
def __init__(self, token):
assert token
self.token = token
self.children = []
def __iter__(self):
yield self
for generator in chain(map(iter, self.children)):
for child in generator:
yield child
def leaf(self):
return not self.children
def size(self):
return sum(map(self.size, self.children), 1)
def append(self, node):
if self.saturated():
return False
for child in self.children:
if child.append(node):
return True
if not self.token.arity == len(self.children):
self.children.append(node)
return True
raise SystemError()
def saturated(self):
for child in self.children:
if not child.saturated():
return False
return self.token.arity == len(self.children)
def clone(self):
tree = Tree(self.token)
tree.children = list(map(self.clone, self.children))
return tree
def string(self):
tmp = self.token.name + '['
for child in self.children:
tmp += child.string() + ','
if not self.children:
return tmp[:-1]
return tmp[:-1] + ']'
def latex(self):
stub = Tree(tokens.TokenInfo(None, 0, None, ''))
for _ in range(self.token.arity - len(self.children)):
self.children.append(stub)
# stdl string does not allow generic partial string formatting
if self.token.arity == 0:
return self.token.latex
if self.token.arity == 1:
return self.token.latex.format(self.children[0].latex())
if self.token.arity == 2:
return self.token.latex.format(self.children[0].latex(), self.children[1].latex())
if self.token.arity == 3:
return self.token.latex.format(self.children[0].latex(), self.children[1].latex(), self.children[2].latex())
def to_tree(sequence: list):
root = Tree(tokens.get(sequence[0]))
for code in sequence[1:]:
node = Tree(tokens.get(code))
saturated = not root.append(node)
if saturated:
break
return root
def to_sequence(tree: Tree):
sequence = []
for node in iter(tree):
id = tokens.id(node.token.onehot)
sequence.append(id)
return sequence
def to_trees(sequences: list):
trees = []
for s in sequences:
sequence = []
for onehot in s:
sequence.append(tokens.id(onehot))
trees.append(to_tree(sequence))
return trees
def to_latex(sequences: list):
trees = to_trees(sequences)
latexs = [t.latex() for t in trees]
return latexs