forked from grammarly/gector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sentence.py
320 lines (296 loc) · 15.7 KB
/
test_sentence.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
import argparse
from utils.helpers import read_lines, normalize
from gector.gec_model import GecBERTModel
import numpy as np
def predict_for_file(input_file, output_file, model, batch_size=32, to_normalize=False):
test_data = read_lines(input_file)
predictions = []
cnt_corrections = 0
batch = []
correction_labels = []
arr = []
num = []
for sent in test_data:
batch.append(sent.split())
if len(batch) == batch_size:
preds, edits, cnt = model.handle_batch(batch)
correction_labels.extend(edits)
predictions.extend(preds)
cnt_corrections += cnt
batch = []
if batch:
preds, edits, cnt = model.handle_batch(batch)
correction_labels.extend(edits)
predictions.extend(preds)
cnt_corrections += cnt
result_lines = [" ".join(x) for x in predictions]
if to_normalize:
result_lines = [normalize(line) for line in result_lines]
cnt_corr_sentences = len(correction_labels)
test = np.array(correction_labels)
print(test.shape)
print(np.array(predictions).shape)
all_corr_labels = [i for j in correction_labels for i in j]
data = np.array(all_corr_labels)
arr, num = np.unique(data, return_counts=True)
with open(output_file, 'w') as f:
f.write("\n".join(result_lines) + '\n')
return cnt_corrections, arr, num, cnt_corr_sentences
def main(args):
# get all paths
model = GecBERTModel(vocab_path=args.vocab_path,
model_paths=args.model_path,
max_len=args.max_len, min_len=args.min_len,
iterations=args.iteration_count,
min_error_probability=args.min_error_probability,
lowercase_tokens=args.lowercase_tokens,
model_name=args.transformer_model,
special_tokens_fix=args.special_tokens_fix,
log=False,
confidence=args.additional_confidence,
del_confidence=args.additional_del_confidence,
is_ensemble=args.is_ensemble,
weigths=args.weights)
# cnt_corrections = predict_for_sentences(sentences, model)
cnt_corrections,arr,num, cnt_corr_sentences = predict_for_file(args.input_file, args.output_file, model,
batch_size=args.batch_size,
to_normalize=args.normalize)
# evaluate with m2 or ERRANT
print(f"Produced overall corrections: {cnt_corrections}")
print(f"Produced overall sentences: {cnt_corr_sentences}")
print(arr)
print(num)
if __name__ == '__main__':
# read parameters
parser = argparse.ArgumentParser()
parser.add_argument('--model_path',
help='Path to the model file.', nargs='+',
required=True)
parser.add_argument('--vocab_path',
help='Path to the model file.',
default='data/output_vocabulary' # to use pretrained models
)
parser.add_argument('--input_file',
help='Path to the evalset file',
required=True)
parser.add_argument('--output_file',
help='Path to the output file',
required=True)
parser.add_argument('--max_len',
type=int,
help='The max sentence length'
'(all longer will be truncated)',
default=50)
parser.add_argument('--min_len',
type=int,
help='The minimum sentence length'
'(all longer will be returned w/o changes)',
default=3)
parser.add_argument('--batch_size',
type=int,
help='The size of hidden unit cell.',
default=128)
parser.add_argument('--lowercase_tokens',
type=int,
help='Whether to lowercase tokens.',
default=0)
parser.add_argument('--transformer_model',
choices=['bert', 'gpt2', 'transformerxl', 'xlnet', 'distilbert', 'roberta', 'albert'
'bert-large', 'roberta-large', 'xlnet-large'],
help='Name of the transformer model.',
default='roberta')
parser.add_argument('--iteration_count',
type=int,
help='The number of iterations of the model.',
default=5)
parser.add_argument('--additional_confidence',
type=float,
help='How many probability to add to $KEEP token.',
default=0)
parser.add_argument('--additional_del_confidence',
type=float,
help='How many probability to add to $DELETE token.',
default=0)
parser.add_argument('--min_error_probability',
type=float,
help='Minimum probability for each action to apply. '
'Also, minimum error probability, as described in the paper.',
default=0.0)
parser.add_argument('--special_tokens_fix',
type=int,
help='Whether to fix problem with [CLS], [SEP] tokens tokenization. '
'For reproducing reported results it should be 0 for BERT/XLNet and 1 for RoBERTa.',
default=1)
parser.add_argument('--is_ensemble',
type=int,
help='Whether to do ensembling.',
default=0)
parser.add_argument('--weights',
help='Used to calculate weighted average', nargs='+',
default=None)
parser.add_argument('--normalize',
help='Use for text simplification.',
action='store_true')
args = parser.parse_args()
main(args)
# python test_sentence.py --model_path ./model/roberta_1_gectorv2.th --vocab_path ./data/output_vocabulary --input_file ./mr/processed.txt --output_file ./mr/processed_corrected.txt
# def predict_for_sentences(sentences, model, batch_size=32, to_normalize=False):
# # test_data = read_lines(input_file)
# predictions = []
# cnt_corrections = 0
# batch = []
# correction_labels = []
# for sent in sentences:
# batch.append(sent.split())
# if len(batch) == batch_size:
# preds, edits, cnt = model.handle_batch(batch)
# predictions.extend(preds)
# correction_labels.extend(edits)
# cnt_corrections += cnt
# batch = []
# if batch:
# preds, edits, cnt = model.handle_batch(batch)
# predictions.extend(preds)
# correction_labels.extend(edits)
# cnt_corrections += cnt
# result_lines = [" ".join(x) for x in predictions]
# if to_normalize:
# result_lines = [normalize(line) for line in result_lines]
# # for sentence, label in zip(predictions, correction_labels):
# # print('***************')
# # print(correction_labels)
# # print(sentence)
# # print('***************')
# # cnt_corr_sentences = len(correction_labels)
# cnt_corr_sentences = len(correction_labels)
# test = np.array(correction_labels)
# print(correction_labels)
# print(test.shape)
# print(np.array(predictions).shape)
# all_corr_labels = [i for j in correction_labels for i in j]
# data = np.array(all_corr_labels)
# arr, num = np.unique(data, return_counts=True)
# print(cnt_corr_sentences)
# # print(all_corr_labels)
# print(arr)
# print(num)
# # print(correction_labels)
# # print(result_lines)
# # with open(output_file, 'w') as f:
# # f.write("\n".join(result_lines) + '\n')
# return cnt_corrections
# def main(args):
# # get all paths
# model = GecBERTModel(vocab_path=args.vocab_path,
# model_paths=args.model_path,
# max_len=args.max_len, min_len=args.min_len,
# iterations=args.iteration_count,
# min_error_probability=args.min_error_probability,
# lowercase_tokens=args.lowercase_tokens,
# model_name=args.transformer_model,
# special_tokens_fix=args.special_tokens_fix,
# log=False,
# confidence=args.additional_confidence,
# del_confidence=args.additional_del_confidence,
# is_ensemble=args.is_ensemble,
# weigths=args.weights)
# sentences = [
# 'The company intends to provide further updates on this pending merger shortly as it continues with its due diligence process congratulations',
# 'The fact that many have lost their jobs , their homes , their dreams in these difficult times confirms for us that life carries with it a Good Friday experience -- that darkness and disappointment can be constant companions .',
# 'This article was first published on guardian.co.uk at 12.07 BST days Saturday 11 April 2009 .',
# 'Pole-position qualifying its that saturday',
# 'He is accused of running his office like frat house , where cursing and harassing young female staffers was the norm .',
# 'Given this new reputation for openness . his endorsement of Gordon Brown is all the more significant as voters are unlikely to think that John Prescott is the sort of politician who says one thing public and quite another in private .'
# ]
# cor_sentences = [
# 'The company intends to provide further updates on this pending merger shortly as it continues with its due diligence process congratulations',
# 'The company intends to provide further updates on this pending merger shortly as it continues with its due diligence process congratulations',
# 'The fact that many have lost their jobs , their homes , their dreams in these difficult times confirms for us that life carries with it a Good Friday experience -- that darkness and disappointment can be constant companions .',
# 'The fact that many have lost their jobs , their homes , their dreams in these difficult situations confirms for us that life carries with it a Good Friday experience and that darkness and disappointment can be constant companions .',
# 'This article was first published on guardian.co.uk at 12.07 BST days Saturday 11 April 2009 .',
# 'This article was first published on guardian.co.uk at 12.07 BST on Saturday 11 April 2009 .',
# 'Pole-position qualifying its that saturday',
# 'Pole-position qualifying that Saturday',
# 'He is accused of running his office like frat house , where cursing and harassing young female staffers was the norm .',
# 'He is accused of running his office like a house , where cursing and harassing young female staffers was the norm .',
# 'Given this new reputation for openness , his endorsement of Gordon Brown is all the more significant as voters are unlikely to think that John Prescott is the sort of politician who says one thing in public and quite another in private .'
# 'Given this new reputation for openness . his endorsement of Gordon Brown is all the more significant as voters are unlikely to think that John Prescott is the sort of politician who says one thing public and quite another in private .'
# ]
# cnt_corrections = predict_for_sentences(sentences, model)
# # cnt_corrections,arr,num = predict_for_file(sentences, model)
# # evaluate with m2 or ERRANT
# print(f"Produced overall corrections: {cnt_corrections}")
# if __name__ == '__main__':
# # read parameters
# parser = argparse.ArgumentParser()
# parser.add_argument('--model_path',
# help='Path to the model file.', nargs='+',
# required=True)
# parser.add_argument('--vocab_path',
# help='Path to the model file.',
# default='data/output_vocabulary' # to use pretrained models
# )
# # parser.add_argument('--input_file',
# # help='Path to the evalset file',
# # required=True)
# # parser.add_argument('--output_file',
# # help='Path to the output file',
# # required=True)
# parser.add_argument('--max_len',
# type=int,
# help='The max sentence length'
# '(all longer will be truncated)',
# default=50)
# parser.add_argument('--min_len',
# type=int,
# help='The minimum sentence length'
# '(all longer will be returned w/o changes)',
# default=3)
# parser.add_argument('--batch_size',
# type=int,
# help='The size of hidden unit cell.',
# default=128)
# parser.add_argument('--lowercase_tokens',
# type=int,
# help='Whether to lowercase tokens.',
# default=0)
# parser.add_argument('--transformer_model',
# choices=['bert', 'gpt2', 'transformerxl', 'xlnet', 'distilbert', 'roberta', 'albert'
# 'bert-large', 'roberta-large', 'xlnet-large'],
# help='Name of the transformer model.',
# default='roberta')
# parser.add_argument('--iteration_count',
# type=int,
# help='The number of iterations of the model.',
# default=5)
# parser.add_argument('--additional_confidence',
# type=float,
# help='How many probability to add to $KEEP token.',
# default=0)
# parser.add_argument('--additional_del_confidence',
# type=float,
# help='How many probability to add to $DELETE token.',
# default=0)
# parser.add_argument('--min_error_probability',
# type=float,
# help='Minimum probability for each action to apply. '
# 'Also, minimum error probability, as described in the paper.',
# default=0.0)
# parser.add_argument('--special_tokens_fix',
# type=int,
# help='Whether to fix problem with [CLS], [SEP] tokens tokenization. '
# 'For reproducing reported results it should be 0 for BERT/XLNet and 1 for RoBERTa.',
# default=1)
# parser.add_argument('--is_ensemble',
# type=int,
# help='Whether to do ensembling.',
# default=0)
# parser.add_argument('--weights',
# help='Used to calculate weighted average', nargs='+',
# default=None)
# parser.add_argument('--normalize',
# help='Use for text simplification.',
# action='store_true')
# args = parser.parse_args()
# main(args)
# # python test_sentence.py --model_path ./model/roberta_1_gectorv2.th --vocab_path ./data/output_vocabulary