-
Notifications
You must be signed in to change notification settings - Fork 1
/
Syntax_Tree2Value_EN.py
356 lines (338 loc) · 15.5 KB
/
Syntax_Tree2Value_EN.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from stanza.server import CoreNLPClient
import os
import re
import scipy.io as scio
corenlp_dir = 'D:\Project\Data\stanza_model'
os.environ["CORENLP_HOME"] = corenlp_dir
class BiNode(object):
'''
binary tree node (has either two or no children but not full binary tree)
self.value: value
self.lchild: left child
self.rchild: right child
'''
def __init__(self,value=None,lchild=None,rchild=None):
self.value=value
self.lchild=lchild
self.rchild=rchild
def ifLeafNode(self):
'''
In this tree, every node will have 0/2 child nodes,but leaf node can be in
different depth.
:return:
'''
if self.lchild==None and self.rchild==None:
return True
elif self.lchild!=None and self.lchild!=None:
return False
else:
print("ERROR: Tree structure wrong, at least one node has only one child.")
return False
def print_tree(self):
print(self.value,end=" ")
if self.lchild!=None and self.rchild!=None:
self.lchild.print_tree()
self.rchild.print_tree()
elif self.lchild==None and self.lchild==None:
print("#", end=" ")
print("#", end=" ")
else:
print("\nERROR: Tree structure wrong, at least one node has only one child.")
return
def RChildExtension(self,childlist):
BT = BiNode("void")
BT.lchild = self.CreatFromParseTree(childlist[0])
if len(childlist)==2:
BT.rchild = self.CreatFromParseTree(childlist[1])
elif len(childlist)>2:
BT.rchild = self.RChildExtension(childlist[1:])
else:
print("ERROR: extend less than 2 children")
return BT
def CreatFromParseTree(self,tree):
'''
The binary tree is generated from real syntax parse tree. Real Syntax tree
created by CoreNLP is a tree whose nodes have different numbers of child nodes
rather than a binary tree. According to Nelson's strategy (Nelson et al, 2017),
they used a binary-branching tree representing the syntax structure. So we have
to change the original tree into binary tree without changing the syntax
structure too much.
There is a traditional algorithm on changing tree to binary tree in Computer
Science, which is let the first child of the parent node be the left child and
let all other child nodes to right child of the child node on its left. However,
this algorithm break the syntax structure of the original tree, which make no
sense to our research.
So the final solution is if the node A has more than two child, we will treat the
first child node as left child of A and create a new node B as the right child.
let the second child of A as the left child of B, and let the third child of A be
the right child of B or the left child of the right child of B if A has more than
three child nodes.
Another action we do is that we delete all of the nodes which only has one child.
Because what we mainly focus is merging operation when listening a sentence.
'''
childlist=[]
for node in tree.child: # delete punctuation
if node.value!="":
childlist.append(node)
if len(tree.child)==0: # leaf node
BT = BiNode(tree.value)
return BT
elif len(childlist)==1: # should be deleted
BT=self.CreatFromParseTree(childlist[0])
return BT
elif len(childlist)==2: # keep it as binary tree node with two children
BT=BiNode(tree.value)
BT.lchild=self.CreatFromParseTree(childlist[0])
BT.rchild=self.CreatFromParseTree(childlist[1])
else: # more than 2 children
BT = BiNode(tree.value)
BT.lchild = self.CreatFromParseTree(childlist[0])
BT.rchild=self.RChildExtension(childlist[1:])
return BT
def Tree2Stack_n_Operations(tree,StackLengthList,OperationsNumList,NodeStack):
# if re.match(r'\W+$',tree.value)!=None and tree.value!='-':
# return StackLengthList,OperationsNumList,NodeStack
if len(tree.child)!=0:
puncNum=0
for ChildNode in tree.child:
if ChildNode.value!="":
StackLengthList,OperationsNumList,NodeStack=Tree2Stack_n_Operations(ChildNode,StackLengthList,OperationsNumList,NodeStack)
else:
puncNum+=1
NodeStack=NodeStack[0:len(NodeStack)-len(tree.child)+puncNum]
OperationsNumList[-1]+=1
else:
StackLengthList.append(len(NodeStack)+1)
OperationsNumList.append(1)
# print(tree.value,end="$")
NodeStack.append(tree)
return StackLengthList,OperationsNumList,NodeStack
def Tree2Stack_n_Operations_final(tree):
StackLengthList, OperationsNumList, NodeStack1 = Tree2Stack_n_Operations(tree, [], [], [])
OperationsNumList[-1] -= 1
return StackLengthList, OperationsNumList
def Creat_wordlist(sentence):
wordlist=[]
for token in sentence.token:
if re.match('\W+$',token.originalText)==None:
wordlist.append(token.originalText)
return wordlist
def CountStackLength_Condition_BiTree(NodeStack,tag):
'''
count how many nodes in the stack under some condition
:param NodeStack:
:param tag:
1: individual word (leaf node)
2: close constituent (non leaf node)
:return:
'''
num=0
if tag==1:
Flag=True
elif tag==2:
Flag=False
else:
print("ERROR: tag input error")
return
for node in NodeStack:
if node.ifLeafNode()==Flag:
num+=1
return num
def BiTree2NumofOpenNode(Bitree,IndividualWordNumList,CloseConstituentNumList,NodeStack):
if Bitree.ifLeafNode()==True:
NodeStack.append(Bitree)
IDnum=CountStackLength_Condition_BiTree(NodeStack,1)
CCnum=CountStackLength_Condition_BiTree(NodeStack,2)
IndividualWordNumList.append(IDnum)
CloseConstituentNumList.append(CCnum)
else:
IndividualWordNumList,CloseConstituentNumList,NodeStack=BiTree2NumofOpenNode(Bitree.lchild,IndividualWordNumList,CloseConstituentNumList,NodeStack)
IndividualWordNumList,CloseConstituentNumList,NodeStack=BiTree2NumofOpenNode(Bitree.rchild, IndividualWordNumList, CloseConstituentNumList, NodeStack)
NodeStack=NodeStack[0:len(NodeStack)-2]
NodeStack.append(Bitree)
return IndividualWordNumList,CloseConstituentNumList,NodeStack
def CountStackLength_Condition_Tree(NodeStack,tag):
'''
count how many nodes in the stack under some condition
:param NodeStack:
:param tag:
1: individual word (leaf node)
2: close constituent (non leaf node)
:return:
'''
num=0
if tag==1:
for node in NodeStack:
if len(node.child) == 0:
num += 1
elif tag==2:
for node in NodeStack:
if len(node.child) != 0:
num += 1
else:
print("ERROR: tag input error")
return
return num
def Tree2NumofOpenNode(tree,IndividualWordNumList,CloseConstituentNumList,NodeStack):
# if re.match(r'\W+$', tree.value) != None and tree.value!='-':
# return IndividualWordNumList,CloseConstituentNumList,NodeStack
if len(tree.child)==0:
NodeStack.append(tree)
IDnum=CountStackLength_Condition_Tree(NodeStack,1)
CCnum=CountStackLength_Condition_Tree(NodeStack,2)
IndividualWordNumList.append(IDnum)
CloseConstituentNumList.append(CCnum)
elif len(tree.child)==1:
if tree.child[0].value!="":
IndividualWordNumList, CloseConstituentNumList, NodeStack = Tree2NumofOpenNode(tree.child[0],
IndividualWordNumList,
CloseConstituentNumList,
NodeStack)
else:
punc_num=0
for childnode in tree.child:
if childnode.value != "":
IndividualWordNumList, CloseConstituentNumList, NodeStack = Tree2NumofOpenNode(childnode,
IndividualWordNumList,
CloseConstituentNumList,
NodeStack)
else:
punc_num+=1
NodeStack=NodeStack[0:len(NodeStack)-len(tree.child)+punc_num]
NodeStack.append(tree)
return IndividualWordNumList,CloseConstituentNumList,NodeStack
def ListCombineOnebyOne(List1,List2):
if len(List1)!=len(List2):
print("ERROR: The length of list doesn't match")
return
else:
List3=[i + j for i, j in zip(List1, List2)]
return List3
def ClearPunctuation(tree,delete_next):
if len(tree.child)==0:
if delete_next==True:
tree.Clear()
delete_next=False
if re.search(r'\W+',tree.value)!=None and tree.value!="Mr.":
if tree.value=="-":
delete_next=True
tree.Clear()
# else:
# print(tree.value,end="$")
elif len(tree.child)==1:
tree_no_punc,delete_next=ClearPunctuation(tree.child[0],delete_next)
tree.child[0].CopyFrom(tree_no_punc)
if tree.child[0].value=="":
tree.Clear()
else:
VoidFlag=True
for child in tree.child:
tree_no_punc, delete_next = ClearPunctuation(child, delete_next)
child.CopyFrom(tree_no_punc)
if child.value!="":
VoidFlag=False
if VoidFlag==True:
tree.Clear()
return tree,delete_next
def check_empty(word):
if word == '\n' or word=="" or re.match('\W+$',word):
return False
return True
if __name__ == "__main__":
with CoreNLPClient(
properties='English',
annotators=['parse'],
timeout=30000,
memory='6G') as client:
# text="I will sing you a song and take you both for a jungle-ride."
# ann = client.annotate(text)
# sentence = ann.sentence[0]
# tree = sentence.parseTree
# tree = tree.child[0]
# # print(tree)
# tree_NoPunc,tag=ClearPunctuation(tree,False)
# # print(tree_NoPunc)
# #
# StackLengthList, OperationsNumList = Tree2Stack_n_Operations_final(tree_NoPunc)
# print(StackLengthList)
# print(OperationsNumList)
#
# VOID=BiNode("fake")
# BiTree=VOID.CreatFromParseTree(tree_NoPunc)
# IndividualWordNumList_Bi,CloseConstituentNumList_Bi,NodeStack2=BiTree2NumofOpenNode(BiTree,[],[],[])
# print(IndividualWordNumList_Bi)
# print(CloseConstituentNumList_Bi)
#
# IndividualWordNumList_Original, CloseConstituentNumList_Original, NodeStack3 = Tree2NumofOpenNode(tree_NoPunc, [], [], [])
# print(IndividualWordNumList_Original)
# print(CloseConstituentNumList_Original)
#
# Value_bottomup=ListCombineOnebyOne(StackLengthList,OperationsNumList)
# Value_BiOpennode=ListCombineOnebyOne(IndividualWordNumList_Bi,CloseConstituentNumList_Bi)
# Value_OriOpennode=ListCombineOnebyOne(IndividualWordNumList_Original,CloseConstituentNumList_Original)
# print(Value_bottomup)
# print(Value_BiOpennode)
# print(Value_OriOpennode)
for i in range(4):
for j in range(4):
result_bottomup=[]
result_BiOpennode=[]
result_OriOpennode=[]
Wordlist_all=[]
name = chr(ord('A') + i) + str(j + 1)
print(name)
# if name!="B1"and name !="C4"and name!="D1":
# continue
file = open("stimuli_Eng\\" + name + ".txt", "r", encoding='gbk')
text = file.read()
file.close()
ann = client.annotate(text)
# allWordFromToken=[]
for sentence in ann.sentence:
# wordlist=Creat_wordlist(sentence)
# allWordFromToken+=Creat_wordlist(sentence)
# print(wordlist)
tree = sentence.parseTree
tree = tree.child[0]
tree_NoPunc, tag = ClearPunctuation(tree, False)
StackLengthList, OperationsNumList = Tree2Stack_n_Operations_final(tree_NoPunc)
VOID = BiNode("fake")
BiTree = VOID.CreatFromParseTree(tree_NoPunc)
IndividualWordNumList_Bi, CloseConstituentNumList_Bi, NodeStack2 = BiTree2NumofOpenNode(BiTree, [], [], [])
IndividualWordNumList_Original, CloseConstituentNumList_Original, NodeStack3 = Tree2NumofOpenNode(tree_NoPunc, [], [], [])
Value_bottomup=ListCombineOnebyOne(StackLengthList,OperationsNumList)
Value_BiOpennode=ListCombineOnebyOne(IndividualWordNumList_Bi,CloseConstituentNumList_Bi)
Value_OriOpennode=ListCombineOnebyOne(IndividualWordNumList_Original,CloseConstituentNumList_Original)
# if Value_BiOpennode!=Value_OriOpennode:
# print("Your guess is wrong.")
# Wordlist_all+=wordlist
result_bottomup+=Value_bottomup
result_BiOpennode+=Value_BiOpennode
result_OriOpennode+=Value_OriOpennode
file = open("stimuli_Eng\\" + name + ".txt", "r", encoding='gbk')
wordlist_fromsplit=[]
for line in file:
wordlist=line.split(' ')
# wordlist_fromsplit+=wordlist
for word in wordlist:
wordlist_fromsplit.append(re.sub(r'\W+','',word))
file.close()
# wordlist_filtered = list(filter(check_empty, wordlist))
# if len(wordlist_filtered)!=len(result_bottomup) or len(wordlist_filtered)!=len(result_BiOpennode) or len(wordlist_filtered) != len(result_OriOpennode):
# print("")
# print(len(wordlist_fromsplit))
print(len(result_bottomup))
if len(wordlist_fromsplit) != len(result_bottomup) or len(wordlist_fromsplit) != len(result_BiOpennode) or len(wordlist_fromsplit) != len(result_OriOpennode):
print("ERROR:"+name)
# print(allWordFromToken)
# for word in wordlist_fromsplit:
# print(word,end="$")
mat_path_bottomup = 'D:\Project\Data\stimuli_SyntaxComplexity\Exp1\\' + name + '_bottomup.mat'
mat_path_BiOpennode = 'D:\Project\Data\stimuli_SyntaxComplexity\Exp1\\' + name + '_BiOpennode.mat'
mat_path_OriOpennode = 'D:\Project\Data\stimuli_SyntaxComplexity\Exp1\\' + name + '_OriOpennode.mat'
# scio.savemat(mat_path_bottomup,
# {'WordVec': result_bottomup, 'wordlist': wordlist_fromsplit})
# scio.savemat(mat_path_BiOpennode,
# {'WordVec': result_BiOpennode, 'wordlist': wordlist_fromsplit})
# scio.savemat(mat_path_OriOpennode,
# {'WordVec': result_OriOpennode, 'wordlist': wordlist_fromsplit})