-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
328 lines (312 loc) · 9.89 KB
/
data.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
from bs4 import BeautifulSoup
# import seaborn as sns
# import matplotlib.pyplot as plt
from node import *
import math
POS="+"
NEG='-'
cur_label=0
class lctreenode:
def __init__(self, con, prim):
if "_" in prim:
self.prim = prim[:prim.find('_')]
else:
self.prim = prim
self.con = con
self.child = []
self.par = None
self.name = con+self.prim
def count_prim(self):
count = 0
if self.prim!="L":
count = 1
for c in self.child:
count += c.count_prim()
# print("prim", count)
return count
def count_lambda(self):
count = 0
if self.prim=="L":
count = 1
for c in self.child:
count += c.count_lambda()
# print("lambda:",count)
return count
def count_cat(self):
return self.count_prim() - self.count_lambda()
def count_lambda_subtree_cat_equal_one(self):
cur = 0
if self.prim=="L":
# print("L")
cur = int(self.count_cat()==1)
for c in self.child:
cur += c.count_lambda_subtree_cat_equal_one()
return cur
def __str__(self,level=0,cidx=0):
res = ""
if cidx==0:
if self.child!=[]:
res = res + "("+ self.name + "\t"
else:
res = res + self.name
else:
if self.child!=[]:
res = res+"\n" +"\t"*level + "("+self.name+"\t"
else:
res = res+"\n" +"\t"*level + self.name+"\t"
for (i,c) in enumerate(self.child):
res+=c.__str__(level+1,i)
if c.child!=[]:
res+=")"
if level==0:
res+=")"
return res
def decrement_label():
global cur_label
cur_label-=1
def get_cur_lable():
global cur_label
# ch = chr(ord('a')+cur_label)
ch = str(cur_label)
cur_label+=1
return ch
def reset_cur_label():
global cur_label
cur_label = 0
def generate_node(cat,sign,label, lambda_pairs, label_to_tree_prim, con=" "):
"str->Node"
if not ("\\" in cat or "/" in cat):
if sign == POS:
label_to_tree_prim[label] = [con, cat]
return Node(cat,sign,label)
else:
left_p=0
for i,c in enumerate(cat):
if c=="(":
left_p+=1
elif c==")":
left_p-=1
elif (c=="/" or c=="\\") and left_p==0:
left = cat[:i]
if "/" in left or "\\" in left:
left = left[1:-1]
right = cat[i+1:]
if "/" in right or "\\" in right:
right = right[1:-1]
break
if c=="/" and sign==POS:
left_node=generate_node(right,NEG,get_cur_lable(), lambda_pairs, label_to_tree_prim)
right_node=generate_node(left,POS,get_cur_lable(), lambda_pairs,label_to_tree_prim,'/')
lambda_pairs[label] = left_node.label+" "+right_node.label
label_to_tree_prim[label] = [con, 'L']
elif c=="/" and sign==NEG:
right_node = generate_node(right,POS,get_cur_lable(), lambda_pairs, label_to_tree_prim,"/")
left_node=generate_node(left,NEG,label+" "+right_node.label, lambda_pairs,label_to_tree_prim)
elif c=="\\" and sign==POS:
left_node=generate_node(left,POS,get_cur_lable(), lambda_pairs, label_to_tree_prim, '\\')
right_node=generate_node(right,NEG,get_cur_lable(), lambda_pairs,label_to_tree_prim)
lambda_pairs[label] = left_node.label+" "+right_node.label
label_to_tree_prim[label] = [con, 'L']
elif c=="\\" and sign==NEG:
left_node=generate_node(right,POS, get_cur_lable(), lambda_pairs, label_to_tree_prim,"\\")
right_node=generate_node(left,NEG, label+" "+left_node.label, lambda_pairs, label_to_tree_prim)
return Node(pol=sign,label=label,left=left_node,right=right_node,op=c)
def generate_proofnet(cat, match):
folding = []
unfolding = []
lambda_pairs = {}
label_to_tree_prim = {}
for c in cat[:-1]:
cur_cat_node = generate_node(c,NEG,get_cur_lable(), lambda_pairs, label_to_tree_prim)
folding.append(cur_cat_node)
unfolding+=cur_cat_node.leaf()
cur_cat_node=generate_node(cat[-1],POS,get_cur_lable(), lambda_pairs, label_to_tree_prim)
folding.append(cur_cat_node)
unfolding+=cur_cat_node.leaf()
for m in match:
unfolding[m[0]-1].add_link(unfolding[m[1]-1])
return (folding, unfolding, lambda_pairs, label_to_tree_prim)
def build_tree_from_proofnet(label_to_tree_prim,pairs,cur):
cur_node = lctreenode(label_to_tree_prim[cur][0],label_to_tree_prim[cur][1])
if len(pairs[cur].split())==1:
return cur_node
for c in pairs[cur].split():
if c in label_to_tree_prim:
c_node = build_tree_from_proofnet(label_to_tree_prim, pairs, c)
cur_node.child.append(c_node)
c_node.par = cur_node
return cur_node
def proofnet_2_tree(cats, matches):
'''
'''
# print(cats)
# print(matches)
# exit()
global cur_label
cur_label = 0
folding, unfolding, pairs, label_to_tree_prim = generate_proofnet(cats, matches)
# print(folding, unfolding, pairs, label_to_tree_prim)
root = folding[-1].label
# print(root)
for u in unfolding:
if u.pol==POS:
pairs[u.label] = u.link.label
# print(pairs)
root_node = build_tree_from_proofnet(label_to_tree_prim, pairs,root)
# print(root_node)
return root_node
def print_cat(cur_cat):
for c in cur_cat[:-1]:
cc = ''.join([i for i in c if not (i.isdigit() or i=="_")])
print(cc,end = " ")
for c in cur_cat[-1:]:
cc = ''.join([i for i in c if not (i.isdigit() or i=="_")])
print(cc)
def print_word(cur_word):
for c in cur_word[:-1]:
print(c,end = " ")
for c in cur_word[-1:]:
print(c)
def load_trees(folder='tst',primitives = ["S","NP","N","PP","conj"]):
word_to_cat = {}
cat_to_word = {}
all_trees = []
all_words = []
all_matches = []
with open('./data/LCGbank.'+folder+'.xml') as f:
data = f.read()
_data = BeautifulSoup(data,"xml")
right_ = _data.find_all("sentential")
words = _data.find_all("words")
matching = _data.find_all("matching")
for ws,mt,r in zip(words[:],matching[:],right_[:]):
cur_cat = []
cur_match = []
cur_word = []
for w in ws.find_all("word"):
try:
c = w["cat"]
text = w["text"].lower()
if text in word_to_cat:
word_to_cat[text].append(''.join([i for i in c if (not i.isdigit()) and i!="_"]))
else:
word_to_cat[text] = [''.join([i for i in c if (not i.isdigit()) and i!="_"])]
if ''.join([i for i in c if (not i.isdigit()) and i!="_"]) in cat_to_word:
cat_to_word[''.join([i for i in c if (not i.isdigit()) and i!="_"])].append(text)
else:
cat_to_word[''.join([i for i in c if (not i.isdigit()) and i!="_"])] = [text]
cur_cat.append(c)
cur_word.append(w["text"].lower())
except:
pass
for m in mt.find_all("match"):
cur_match.append((int(m["negative"]),int(m["positive"])))
assert len(cur_cat)==len(cur_word)
# print_cat(cur_cat)
# print_word(cur_word)
cur_cat.append(r["cat"])
tree = proofnet_2_tree(cur_cat,cur_match)
#print(tree)
#print(tree.count_prim(), tree.count_lambda(), tree.count_cat())
all_trees.append(tree)
all_words.append(cur_word)
all_matches.append(cur_match)
print("---load {} set is done with {} sequents----".format(folder,len(all_trees)))
return all_trees,all_words,all_matches
def load_standard_word(folder='trn'):
with open('./data/LCGbank.'+folder+'.xml') as f:
data = f.read()
_data = BeautifulSoup(data,"xml")
right_ = _data.find_all("sentential")
words = _data.find_all("words")
matching = _data.find_all("matching")
allwords = {}
for ws,mt,r in zip(words[:],matching[:],right_[:]):
for w in ws.find_all("word"):
try:
c = w["cat"]
text = w["text"].lower()
if text.replace('.','').replace('-',"").replace("/","").isnumeric() or text=='%':
text = 'N'
if text in allwords:
allwords[text]+=1
else:
allwords[text]=1
except:
pass
standard_words = [w for w in allwords if allwords[w]>=6]
print(len(standard_words))
# print(standard_words)
return standard_words
def get_cat_word_mapping(standard_voc, folder = 'trn'):
cat_to_word = {}
word_to_cat = {}
with open('./data/LCGbank.'+folder+'.xml') as f:
data = f.read()
_data = BeautifulSoup(data,"xml")
right_ = _data.find_all("sentential")
words = _data.find_all("words")
matching = _data.find_all("matching")
for ws,mt,r in zip(words[:],matching[:],right_[:]):
for w in ws.find_all("word"):
try:
c = w["cat"]
c = ''.join([i for i in c if (not i.isdigit()) and i!="_"])
text = w["text"].lower()
if text.replace('.','').replace('-',"").replace("/","").isnumeric() or text=='%':
text = 'N'
if text not in standard_voc:
text = '<unk>'
if text in word_to_cat:
word_to_cat[text].append(c)
else:
word_to_cat[text] = [c]
if c in cat_to_word:
cat_to_word[c].append(text)
else:
cat_to_word[c] = [text]
except:
pass
return cat_to_word, word_to_cat
def count_words(standard_voc, cat_to_word, folder='tst'):
total_word = 0
total_p = 0
with open('./data/LCGbank.'+folder+'.xml') as f:
data = f.read()
_data = BeautifulSoup(data,"xml")
right_ = _data.find_all("sentential")
words = _data.find_all("words")
matching = _data.find_all("matching")
allwords = {'<unk>':0}
for ws,mt,r in zip(words[:],matching[:],right_[:]):
for w in ws.find_all("word"):
try:
c = w["cat"]
text = w["text"].lower()
c=''.join([i for i in c if (not i.isdigit()) and i!="_"])
if text.replace('.','').replace('-',"").replace("/","").isnumeric() or text=='%':
text = 'N'
if text in standard_voc:
if text in allwords:
allwords[text]+=1
else:
allwords[text] = 1
else:
text = '<unk>'
allwords[text]=0
print(text)
total_p+=math.log(float(cat_to_word[c].count(text))/float(len(cat_to_word[c])))
total_word+=1
except:
pass
print(total_p, total_word)
print(len(allwords.keys()))
if __name__=='__main__':
trees = load_trees("dev")
# print(len(trees))
# print(trees[-1])
# standard_voc = load_standard_word('trn')
# cat_to_word, word_to_cat = get_cat_word_mapping(standard_voc,'trn')
# print("total words in trn: ", sum([len(cat_to_word[c]) for c in cat_to_word]))
# print(len(cat_to_word['NP/NP']),len(word_to_cat))
# count_words(standard_voc,cat_to_word,'tst')