-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask.py
382 lines (351 loc) · 12 KB
/
mask.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
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
"""
This is role oriented mask generation
"""
from nltk.corpus import stopwords
import numpy as np
# np.set_printoptions(threshold=np.inf)
import heapq
import re
from scipy import sparse
class RoleMask(object):
def __init__(self, opt):
self.opt = opt
# POS tag category
self.noun_list = ['NN','NNS','NNP','NNPS']
self.verb_list = ['VB', 'VBZ', 'VBD', 'VBG','VBN','VBP']
self.adjective_list = ['JJ','JJR','JJS']
self.punctuations = [';','?',',','.',':']
self.major_rels = ['nsubj', 'dobj', 'amod', 'advmod']
self.is_numberic = re.compile(r'^[-+]?[0-9.]+$')
def enable_neibor(self,mask,i,neib_num,MAX_SEQUENCE_LENGTH,last=False):
if last == True:
for j in range(neib_num):
if i-j>=0: mask[i][i-j]=1.
else:
for j in range(neib_num):
if i+j<MAX_SEQUENCE_LENGTH: mask[i][i+j]=1.
if i-j>=0: mask[i][i-j]=1.
# tested
def positional_masks_of_texts(self, tokens_lists, word_index, MAX_SEQUENCE_LENGTH,neib_num=3):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
# test_count=0
for text_id, text in enumerate(tokens_lists):
mask = masks[text_id]
# START
lenth = min(len(text)+2,MAX_SEQUENCE_LENGTH)
for i in range(lenth):
if i == lenth-1: self.enable_neibor(mask,i,neib_num,MAX_SEQUENCE_LENGTH,last=True)
else: self.enable_neibor(mask,i,neib_num,MAX_SEQUENCE_LENGTH)
# masks = sparse.csr_matrix(masks)
return masks
# tested
def POS_masks_of_texts(self, tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
if self.opt.cus_pos in ['A','a']:
include_tags = self.adjective_list #+ self.verb_list, self.noun_list +
elif self.opt.cus_pos in ['N','n']:
include_tags = self.noun_list
else:
include_tags = self.verb_list
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
# Start
val_index = [0]
# Body
row=1
for semtok in text:
if semtok.tag_ in include_tags:
val_index.append(row)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign val part
for m in val_index:
for n in val_index:
mask[m][n]=1.
return masks
def POS_Noun_mask(self, tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
include_tags = self.noun_list
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
# Start
val_index = [0]
# Body
row=1
for semtok in text:
if semtok.tag_ in include_tags:
val_index.append(row)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign val part
for m in val_index:
for n in val_index:
mask[m][n]=1.
return masks
def POS_Verb_mask(self, tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
include_tags = self.verb_list
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
# Start
val_index = [0]
# Body
row=1
for semtok in text:
if semtok.tag_ in include_tags:
val_index.append(row)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign val part
for m in val_index:
for n in val_index:
mask[m][n]=1.
return masks
def POS_Adjective_mask(self, tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
include_tags = self.adjective_list
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
# Start
val_index = [0]
# Body
row=1
for semtok in text:
if semtok.tag_ in include_tags:
val_index.append(row)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign val part
for m in val_index:
for n in val_index:
mask[m][n]=1.
return masks
def POS_masks_of_texts2(self, tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
if self.opt.cus_pos in ['A','a']:
include_tags = self.adjective_list #+ self.verb_list, self.noun_list +
elif self.opt.cus_pos in ['N','n']:
include_tags = self.noun_list
else:
include_tags = self.verb_list
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
# Start
val_index = [0]
# Body
row=1
for semtok in text:
if semtok.tag_ in include_tags:
val_index.append(row)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign val part
lenth = min(len(text)+2,MAX_SEQUENCE_LENGTH)
for m in range(lenth):
for n in val_index:
mask[m][n]=1.
return masks
# negation mask
def negation_mask(self,tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
include_tags = ['neg']
test_count=0
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
val_index = [0]
row = 1
for semtok in text:
if semtok.dep_ in include_tags:
val_index.append(row)
# related tokens
val_index.append(semtok.head.i+1)
for child in semtok.children: val_index.append(child.i+1)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign
val_index = [val for val in val_index if val<MAX_SEQUENCE_LENGTH]
val_index = list(set(val_index))
lenth = min(len(text)+2,MAX_SEQUENCE_LENGTH)
for m in range(lenth):
for n in val_index:
mask[m][n]=1.
return masks
def major_rel_of_texts(self,tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
include_tags = self.major_rels
test_count=0
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
val_index = [0]
row = 1
for semtok in text:
if semtok.dep_ in include_tags:
val_index.append(row)
# related tokens
val_index.append(semtok.head.i+1)
for child in semtok.children: val_index.append(child.i+1)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign
val_index = [val for val in val_index if val<MAX_SEQUENCE_LENGTH]
val_index = list(set(val_index))
for m in val_index:
for n in val_index:
mask[m][n]=1.
return masks
# tested
def major_rel_of_texts2(self,tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
include_tags = self.major_rels
test_count=0
for tid, text in enumerate(tokens_lists):
mask = masks[tid]
val_index = [0]
row = 1
for semtok in text:
if semtok.dep_ in include_tags:
val_index.append(row)
# related tokens
val_index.append(semtok.head.i+1)
for child in semtok.children: val_index.append(child.i+1)
row+=1
if row>=MAX_SEQUENCE_LENGTH: break
# END
if row<MAX_SEQUENCE_LENGTH:
val_index.append(row)
# assign
val_index = [val for val in val_index if val<MAX_SEQUENCE_LENGTH]
val_index = list(set(val_index))
lenth = min(len(text)+2,MAX_SEQUENCE_LENGTH)
for m in range(lenth):
for n in val_index:
mask[m][n]=1.
return masks
# tested;
def both_direct_masks_of_texts(self,tokens_lists, word_index, MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
# test_count = 0
for text_id, text in enumerate(tokens_lists):
# for each text or sentence
mask = masks[text_id]
mask[0][0]=1.
i = 1
for semtok in text:
looks = [i]
looks.append(semtok.head.i+1) # parent
looks+=[child.i+1 for child in semtok.children] # children
# looks+=[sib.i+1 for sib in semtok.head.children] # siblings
looks = list(set(looks))
for look in looks:
if look<MAX_SEQUENCE_LENGTH: mask[i][look]=1.
i+=1
if i>=MAX_SEQUENCE_LENGTH: break
if i<MAX_SEQUENCE_LENGTH: mask[i][i]=1.
return masks
# tested;
def stop_word_mask(self,tokens_lists,word_index,MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
test_count = 0
for text_id, text in enumerate(tokens_lists):
mask = masks[text_id]
keep_index = [0]
row = 1
for semtok in text:
if semtok.text.lower() not in stopwords.words(): keep_index.append(row)
row+=1
if row>= MAX_SEQUENCE_LENGTH: break
if row<MAX_SEQUENCE_LENGTH: keep_index.append(row)
# assign
for m in keep_index:
# for m in range(min(len(text)+2,MAX_SEQUENCE_LENGTH)):
for n in keep_index:
mask[m][n]=1.
return masks
# 1/10
def rare_word_mask(self,tokens_lists,word_index,MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
for text_id, text in enumerate(tokens_lists):
mask = masks[text_id]
rare_num = max(2,len(text)//10)
keep_index = [(0.0,0) for i in range(rare_num)]
row = 1
for semtok in text:
token = semtok.text.lower()
if len(token)>1 and not self.is_numberic.match(token):
idf = self.opt.idf_dict[token] if token in self.opt.idf_dict else 0.0
heapq.heappushpop(keep_index,(idf,row))
row+=1
if row>= MAX_SEQUENCE_LENGTH: break
rare_words = [keep_index[i][1] for i in range(rare_num)]
# assign
# for m in keep_index:
for m in range(min(len(text)+2,MAX_SEQUENCE_LENGTH)):
for n in rare_words:
mask[m][n]=1.
return masks
# separator and punctuations
def separator_mask(self,tokens_lists,word_index,MAX_SEQUENCE_LENGTH):
masks = np.zeros((len(tokens_lists),MAX_SEQUENCE_LENGTH,MAX_SEQUENCE_LENGTH),dtype='float16')
for text_id, text in enumerate(tokens_lists):
# for each text or sentence
mask = masks[text_id]
# get all separators and punctuations
sep = [0]
i=1
for semtok in text:
if semtok.text in self.punctuations: sep.append(i)
i+=1
if i>=MAX_SEQUENCE_LENGTH: break
if i<MAX_SEQUENCE_LENGTH: sep.append(i)
# assign
for m in range(min(len(text)+2,MAX_SEQUENCE_LENGTH)):
for n in sep:
mask[m][n]=1.
return masks
def get_masks(self,tokens_lists,word_index, MAX_SEQUENCE_LENGTH,mask_list=['major_rels']):
res = []
for mask in mask_list:
if mask == 'major_rels':
res.append(self.major_rel_of_texts2(tokens_lists,word_index, MAX_SEQUENCE_LENGTH))
elif mask == 'positional':
res.append(self.positional_masks_of_texts(tokens_lists,word_index, MAX_SEQUENCE_LENGTH))
elif mask == 'POS':
res.append(self.POS_masks_of_texts2(tokens_lists,word_index, MAX_SEQUENCE_LENGTH))
elif mask == 'both_direct':
res.append(self.both_direct_masks_of_texts(tokens_lists,word_index, MAX_SEQUENCE_LENGTH))
elif mask == 'separator':
res.append(self.separator_mask(tokens_lists,word_index, MAX_SEQUENCE_LENGTH))
elif mask == 'stop_word':
res.append(self.stop_word_mask(tokens_lists,word_index,MAX_SEQUENCE_LENGTH))
elif mask == 'rare_word':
res.append(self.rare_word_mask(tokens_lists,word_index,MAX_SEQUENCE_LENGTH))
elif mask == 'noun':
res.append(self.POS_Noun_mask(tokens_lists,word_index,MAX_SEQUENCE_LENGTH))
elif mask == 'verb':
res.append(self.POS_Verb_mask(tokens_lists,word_index,MAX_SEQUENCE_LENGTH))
elif mask == 'adjective':
res.append(self.POS_Adjective_mask(tokens_lists,word_index,MAX_SEQUENCE_LENGTH))
elif mask == 'negation':
res.append(self.negation_mask(tokens_lists,word_index,MAX_SEQUENCE_LENGTH))
return res