-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
285 lines (206 loc) · 7.41 KB
/
gen.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
from collections import Counter
from tqdm import tqdm
import nltk
import csv
import criteria
import numpy as np
import torch
import dataloader
from pytorch_pretrained_bert.tokenization import BertTokenizer
from pytorch_pretrained_bert.modeling import BertModel, BertForMaskedLM
from nltk.stem import PorterStemmer
import spacy
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self, unique_id, tokens, input_ids, input_mask, input_type_ids):
self.unique_id = unique_id
self.tokens = tokens
self.input_ids = input_ids
self.input_mask = input_mask
self.input_type_ids = input_type_ids
def convert_whole_word_to_feature(tokens_a, mask_position, seq_length, tokenizer):
"""Loads a data file into a list of `InputFeature`s."""
#tokens_a = tokenizer.tokenize(sentence)
#print(mask_position)
tokens = []
input_type_ids = []
tokens.append("[CLS]")
input_type_ids.append(0)
for token in tokens_a:
tokens.append(token)
input_type_ids.append(0)
tokens.append("[SEP]")
input_type_ids.append(0)
for token in tokens_a:
tokens.append(token)
input_type_ids.append(1)
tokens.append("[SEP]")
input_type_ids.append(1)
true_word = ''
index = 0
count = 0
mask_position_length = len(mask_position)
while count in range(mask_position_length):
index = mask_position_length - 1 - count
pos = mask_position[index]
if index == 0:
tokens[pos] = '[MASK]'
else:
del tokens[pos]
del input_type_ids[pos]
count += 1
input_ids = tokenizer.convert_tokens_to_ids(tokens)
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
input_mask = [1] * len(input_ids)
# Zero-pad up to the sequence length.
while len(input_ids) < seq_length:
input_ids.append(0)
input_mask.append(0)
input_type_ids.append(0)
assert len(input_ids) == seq_length
assert len(input_mask) == seq_length
assert len(input_type_ids) == seq_length
return InputFeatures(unique_id=0, tokens=tokens, input_ids=input_ids,input_mask=input_mask,input_type_ids=input_type_ids)
def convert_token_to_feature(tokens_a, mask_position, seq_length, tokenizer):
"""Loads a data file into a list of `InputFeature`s."""
#tokens_a = tokenizer.tokenize(sentence)
#print(mask_position)
tokens = []
input_type_ids = []
tokens.append("[CLS]")
input_type_ids.append(0)
for token in tokens_a:
tokens.append(token)
input_type_ids.append(0)
tokens.append("[SEP]")
input_type_ids.append(0)
for token in tokens_a:
tokens.append(token)
input_type_ids.append(1)
tokens.append("[SEP]")
input_type_ids.append(1)
true_word = ''
if isinstance(mask_position,list):
for pos in mask_position:
true_word = true_word + tokens[pos]
tokens[pos] = '[MASK]'
else:
true_word = tokens[mask_position]
tokens[mask_position] = '[MASK]'
input_ids = tokenizer.convert_tokens_to_ids(tokens)
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
input_mask = [1] * len(input_ids)
# Zero-pad up to the sequence length.
while len(input_ids) < seq_length:
input_ids.append(0)
input_mask.append(0)
input_type_ids.append(0)
assert len(input_ids) == seq_length
assert len(input_mask) == seq_length
assert len(input_type_ids) == seq_length
return InputFeatures(unique_id=0, tokens=tokens, input_ids=input_ids,input_mask=input_mask,input_type_ids=input_type_ids)
def convert_sentence_to_token(sentence, seq_length, tokenizer):
tokenized_text = tokenizer.tokenize(sentence.lower())
assert len(tokenized_text) < seq_length-2
nltk_sent = sentence.split(" ")
#print(nltk_sent)
#print(tokenized_text)
position2 = []
token_index = 0
start_pos = len(tokenized_text) + 2
pre_word = ""
for i,word in enumerate(nltk_sent):
if word=="n't" and pre_word[-1]=="n":
word = "'t"
if tokenized_text[token_index]=="\"":
len_token = 2
else:
len_token = len(tokenized_text[token_index])
if tokenized_text[token_index]==word or len_token>=len(word):
position2.append(start_pos+token_index)
pre_word = tokenized_text[token_index]
token_index += 1
else:
new_pos = []
new_pos.append(start_pos+token_index)
new_word = tokenized_text[token_index]
while new_word != word:
token_index += 1
new_word += tokenized_text[token_index].replace('##','')
new_pos.append(start_pos+token_index)
if len(new_word)==len(word):
break
token_index += 1
pre_word = new_word
position2.append(new_pos)
return tokenized_text, nltk_sent, position2
def substitution_generation(source_word, pre_tokens, pre_scores, ps, num_selection=10):
cur_tokens=[]
source_stem = ps.stem(source_word)
assert num_selection<=len(pre_tokens)
for i in range(len(pre_tokens)):
token = pre_tokens[i]
if token[0:2]=="##":
continue
if(token==source_word):
continue
token_stem = ps.stem(token)
if(token_stem == source_stem):
continue
if (len(token_stem)>=3) and (token_stem[:3]==source_stem[:3]):
continue
cur_tokens.append(token)
if(len(cur_tokens)==num_selection):
break
if(len(cur_tokens)==0):
cur_tokens = pre_tokens[0:num_selection+1]
assert len(cur_tokens)>0
return cur_tokens
#stop_words_set = criteria.get_stopwords()
#tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
#model = BertForMaskedLM.from_pretrained('bert-base-uncased')
#texts, labels = dataloader.read_corpus('data/mr',csvf=False)
#data = list(zip(texts, labels))
#sim_score_window = 40
#nlp = spacy.load("en_core_web_sm")
#for sample_idx, (text, true_label) in enumerate(data):
# rows = []
# txts = []
# words_perturb = []
# with open('scores-mr.csv', 'r') as csvfile:
# csvreader = csv.reader(csvfile)
# fields = next(csvreader)
# for row in csvreader:
# rows.append(row)
# doc = nlp(' '.join(text))
# text = []
# for sent in doc.sents:
# for token in sent:
# text.append(token.text)
# tok_text = []
# for item in text:
# ap = item.find("'")
# if ap>=0:
# tok_text.append(item[0:ap])
# tok_text.append("'")
# tok_text.append(item[ap+1:len(item)])
# else:
# tok_text.append(item)
# text = []
# for item in tok_text:
# if len(item) > 0:
# text.append(item)
# for wrd in rows[sample_idx]:
# indx = wrd.find(" ")
# iidx = wrd[1:indx-1]
# widx = int(iidx)
# if widx < len(text):
# final_word = text[widx]
# if final_word not in stop_words_set and len(final_word) > 2:
# words_perturb.append((final_word,widx))
# for wrd,idx in words_perturb:
# len_text = len(text)
# half_sim_score_window = (sim_score_window - 1) // 2
# Output top 10 of candidates