-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
193 lines (158 loc) · 5.86 KB
/
tree.py
File metadata and controls
193 lines (158 loc) · 5.86 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
# tree.py
#
# Parses semrep outputs into trees.
import nltk
from nltk import CFG, Tree, ParentedTree
from nltk.treetransforms import collapse_unary
read_expr = nltk.sem.Expression.fromstring
def collapse_unary(tree, collapsePOS=False, collapseRoot=False):
"""
Collapse subtrees with a single child (ie. unary productions)
into a new non-terminal (Tree node) joined by 'joinChar'.
This is useful when working with algorithms that do not allow
unary productions, and completely removing the unary productions
would require loss of useful information. The Tree is modified
directly (since it is passed by reference) and no value is returned.
:param tree: The Tree to be collapsed
:type tree: Tree
:param collapsePOS: 'False' (default) will not collapse the parent of leaf nodes (ie.
Part-of-Speech tags) since they are always unary productions
:type collapsePOS: bool
:param collapseRoot: 'False' (default) will not modify the root production
if it is unary. For the Penn WSJ treebank corpus, this corresponds
to the TOP -> productions.
:type collapseRoot: bool
:param joinChar: A string used to connect collapsed node values (default = "+")
:type joinChar: str
"""
if collapseRoot == False and isinstance(tree, Tree) and len(tree) == 1:
nodeList = [tree[0]]
else:
nodeList = [tree]
# depth-first traversal of tree
while nodeList != []:
node = nodeList.pop()
if isinstance(node, Tree):
if (
len(node) == 1
and isinstance(node[0], Tree)
and (collapsePOS == True or isinstance(node[0, 0], Tree))
):
# node.set_label(node.label())
node[0:] = [child for child in node[0]]
node = node.label()
# since we assigned the child's children to the current node,
# evaluate the current node again
nodeList.append(node)
else:
for child in node:
nodeList.append(child)
def unary_to_leaf(tree):
"""
Turns unary subtrees into leaves
"""
nodeList = [tree]
while nodeList != []:
node = nodeList.pop()
if isinstance(node, Tree) and node.height() == 2:
print('Pruning: ', node)
print('What I see: ', node.height())
node = node[0]
print('Node is now:', node)
else:
for child in node:
nodeList.append(child)
def prefix_binops(tree):
"""
Converts subtrees involving binary operators (&, ->) to prefix notation.
"""
for node in tree:
if type(node) is Tree:
# print('I am a', node.label())
# print('\t My children are:')
for st in node:
if '&' in st:
print('I am a', node.label())
print(st)
prefix_binops(node)
def trim(tree):
# Remove all extraneous nodes
for sub in reversed(list(tree.subtrees())):
if sub.label() in ['R_PAREN', 'L_PAREN', 'COMMA']:
parent = sub.parent()
while parent and len(parent) == 1:
sub = parent
parent = sub.parent()
# print(sub, "will be deleted")
del tree[sub.treeposition()]
# Replace unary trees with leaves
collapse_unary(tree, collapsePOS=True, collapseRoot=True)
for sub in reversed(list(tree.subtrees())):
if len(sub.leaves()) == 1:
leaf = sub[0]
tree[sub.treeposition()] = leaf
def flat_tree(parse) -> str:
for tree in parse:
tree_str = ' '.join(str(tree).split())
# print(tree_str)
return tree_str
def string_to_tree(string, parser):
string = string.replace('(', '[').replace(')', ']')
parsed_string = parser.parse(string.split())
tst = flat_tree(parsed_string)
tree = ParentedTree.fromstring(tst)
trim(tree)
return tree
def tree_to_expstring(tree):
"""
Converts a tree into the expression syntax that is used to represent the
predicate logic for our semantics.
The following must be done in order to make the expression valid:
(1) Insert L_PAREN immediately after every OP node and R_PAREN after the
last child on OP's level.
(2) Insert COMMAs between two or more arguments
(2) Move binary operators (&, |, ->) to infix notation.
(3) Change the parenthesis from angle brackets to parens.
"""
for i, subtree in enumerate(tree.subtrees()):
if subtree.label() == 'L_PAREN':
for n, c in enumerate(subtree):
subtree[n] = '['
elif subtree.label() == 'R_PAREN':
for n, c in enumerate(subtree):
subtree[n] = ']'
elif subtree.label() == 'COMMA':
for n, c in enumerate(subtree):
subtree[n] = ','
tree_str = ' '.join(filter(lambda s: s != '', tree.leaves())).replace('[', '(').replace(']', ')')
return tree_str
if __name__ == '__main__':
grammar = CFG.fromstring("""
EXP -> OP L_PAREN EXP R_PAREN | OP L_PAREN EXP COMMA EXP R_PAREN | EXP AND EXP | EXP IMPL EXP | VAR
OP -> Q VAR DOT
# Terminals
L_PAREN -> '['
R_PAREN -> ']'
COMMA -> ','
AND -> '&'
IMPL -> '->'
DOT -> '.'
Q -> 'all'
OP -> 'see' | 'meet' | 'like' | 'dislike' | 'throw' | 'notice' | 'know' | 'walk' | 'sleep' | 'eat' | 'run' | 'sing' | 'dance' | 'fly' | 'slumber'
VAR -> 'x' | 'alice' | 'bob' | 'claire' | 'daniel' | 'eliza' | 'francis' | 'grace' | 'henry' | 'isla' | 'john' | 'katherine' | 'lewis' | 'margaret' | 'neha' | 'oswald' | 'patricia' | 'quinn' | 'rachael' | 'samuel' | 'tracy' | 'ursula' | 'victor' | 'winnifred' | 'xerxes' | 'yvettte' | 'zelda'
""")
parser = nltk.parse.BottomUpChartParser(grammar)
alice_alice_str = '( see alice alice )'
alice_bob_str = '(see alice bob )'
for s in [alice_alice_str, alice_bob_str]:
tree = string_to_tree(s, parser)
flat = str(tree).replace('\n', '').split()
print(' '.join(flat))
# print(flat_tree(tree))
tree.collapse_unary()
for subtree in tree.subtrees(filter = lambda t: t.label() in ['DOT', 'L_PAREN', 'R_PAREN', 'COMMA']):
for n, c in enumerate(subtree):
subtree[n] = ''
# prefix_binops(tree)
# print(flat_tree(tree))
print(tree_to_expstring(tree))