-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_modified_datasets.py
429 lines (273 loc) · 15.9 KB
/
create_modified_datasets.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#######################################################################
# Content:
# Functions for processing the raw data.
#######################################################################
import numpy as np
import pandas as pd
import string
from random import choices
import unicodedata
import re
#######################################################################
# Functions:
# Creates one of the three modified datasets.
# dataset_path: the directory where the raw data is.
# mode: determines which dataset is to be created
# mode = 1: dateset only has best translation
# mode = 2: dateset has each translation only once
# mode = 3: dateset has each translation appear based on weight
def create_modified_dataset(dataset_path, mode):
with open(dataset_path,'r',encoding="utf-8") as f:
lines = np.loadtxt(f,delimiter='|', dtype='U')
max_length = 0
for line in lines:
for sentence in line:
if len(sentence) > max_length:
max_length = len(sentence)
modified_dataset = np.empty((1000000,2),dtype='U'+str(max_length+1))
if mode == 1:
i = 0
j = 0
for line in lines:
if line[0].startswith('prompt_'):
modified_dataset[j,0] = line[1]
modified_dataset[j,1] = lines[i+1,0]
j += 1
i += 1
elif mode == 2:
j = 0
for line in lines:
if line[0].startswith('prompt_'):
promt = line[1]
else:
modified_dataset[j,0] = promt
modified_dataset[j,1] = line[0]
j += 1
elif mode == 3:
length = lines.shape[0]
i = 0
j = 0
while i < length:
promt = lines[i,1]
i += 1
translations = np.empty((0),dtype='U')
weights = np.empty((0))
while i < length and not(lines[i,0].startswith('prompt_')):
translations = np.append(translations,lines[i,0])
weights = np.append(weights,float(lines[i,1]))
i += 1
for _ in range(130):
modified_dataset[j,0] = promt
modified_dataset[j,1] = choices(translations,weights)[0]
j += 1
else:
print("Error: Not a valid mode value.")
return np.delete(modified_dataset,(modified_dataset == '')[:,0],0)
# Creates dev or test datasets.
# dataset_path: the directory where the raw data is.
def create_testing_dataset(dataset_path):
with open(dataset_path,'r',encoding="utf-8") as f:
lines = pd.read_table(f,delimiter='|', dtype='U',header=None)
modified_dataset = pd.DataFrame(None,index=range(lines.shape[0]),columns=['promt','translation','weights'], dtype='U' )
j = 0
for line in lines.itertuples(index=False,name=None):
if line[0].startswith('prompt_'):
promt = line[1]
else:
modified_dataset.iat[j,0] = promt
modified_dataset.iat[j,1] = line[0]
modified_dataset.iat[j,2] = line[1]
j += 1
return modified_dataset.dropna(0)
# Creates dev or test datasets with only the best translation.
# dataset_path: the directory where the raw data is.
def create_testing_dataset_best(dataset_path):
with open(dataset_path,'r',encoding="utf-8") as f:
lines = pd.read_table(f,delimiter='|', dtype='U',header=None)
modified_dataset = pd.DataFrame(None,index=range(lines.shape[0]),columns=['promt','translation','weights'], dtype='U' )
i = 0
j = 0
for line in lines.itertuples(index=False,name=None):
if line[0].startswith('prompt_'):
modified_dataset.iat[j,0] = line[1]
modified_dataset.iat[j,1] = lines.iat[i+1,0]
modified_dataset.iat[j,2] = lines.iat[i+1,1]
j += 1
i += 1
return modified_dataset.dropna(0)
# Creates Amazon's Answers.
# dataset_path: the directory where the raw data is.
def create_amazon_baseline(dataset_path):
with open(dataset_path,'r',encoding="utf-8") as f:
lines = pd.read_table(f,delimiter='|', dtype='U',header=None)
modified_dataset = pd.DataFrame('__',index=range(500),columns=['promt','translation'], dtype='U' )
i = 0
j = 0
for line in lines.itertuples(index=False,name=None):
if line[0].startswith('prompt_'):
modified_dataset.iat[j,0] = line[1]
modified_dataset.iat[j,1] = lines.iat[i+1,0]
j += 1
i += 1
return modified_dataset
# Creates Worst Translation Baseline Answers.
# dataset_path: the directory where the raw data is.
def create_worst_baseline(dataset_path):
with open(dataset_path,'r',encoding="utf-8") as f:
lines = pd.read_table(f,delimiter='|', dtype='U',header=None)
modified_dataset = pd.DataFrame('__',index=range(500),columns=['promt','translation'], dtype='U' )
max_length = lines.shape[0]-1
i = 0
j = 0
for line in lines.itertuples(index=False,name=None):
if line[0].startswith('prompt_'):
promt = line[1]
elif i == max_length or lines.iat[i+1,0].startswith('prompt_'):
modified_dataset.iat[j,0] = promt
modified_dataset.iat[j,1] = lines.iat[i,0]
j += 1
i += 1
return modified_dataset
def cleaning_punctuation_and_uppercase(sentence):
sentence = (sentence.translate(str.maketrans('', '', string.punctuation))).lower().strip()
return sentence
# Convert dataset to gold format.
# dataset_path: the directory where the raw data is.
# reference_path: the directory of the dataset the raw data is trying to predict.
# head: does raw data have a header line (None for no header and 0 for a header).
def convert_to_gold(dataset_path,reference_path,head):
with open(dataset_path,'r',encoding="utf-8") as f:
dataset_data = pd.read_table(f,delimiter='|', dtype='U',header=head)
with open(reference_path,'r',encoding="utf-8") as f:
reference_data = pd.read_table(f,delimiter='|', dtype='U',header=None)
gold_dataset = pd.DataFrame('',index=range(501*3), columns=['line'], dtype='U')
portu = ''
promt_found = False
promt_id = ''
promt = ''
i = 0
for reference in reference_data.itertuples(index=False,name=None):
if reference[0].startswith('prompt_'):
if promt_found:
gold_dataset.iat[i,0] = portu # .strip()
gold_dataset.iat[i+1,0] = ''
i += 2
promt_found = False
else:
gold_dataset.iat[i,0] = promt_id + '|' + promt
gold_dataset.iat[i+1,0] = ''
gold_dataset.iat[i+2,0] = ''
i += 3
for data in dataset_data.itertuples(index=False,name=None):
if reference[1] == data[0]:
gold_dataset.iat[i,0] = reference[0] + '|' + data[0]
i += 1
portu = data[1]
promt_found = True
break
promt_id = reference[0]
promt = reference[1]
gold_dataset.iat[i,0] = portu # .strip()
gold_dataset.iat[i+1,0] = ''
return gold_dataset.drop([0,1,2])
# Converts the unicode file to ascii
def unicode_to_ascii(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
def preprocess_sentence(w):
w = unicode_to_ascii(w.lower().strip())
# creating a space between a word and the punctuation following it
# eg: "he is a boy." => "he is a boy ."
# Reference:- https://stackoverflow.com/questions/3645931/python-padding-punctuation-with-white-spaces-keeping-punctuation
w = re.sub(r"([?.!,¿])", r" \1 ", w)
w = re.sub(r'[" "]+', " ", w)
# replacing everything with space except (a-z, A-Z, ".", "?", "!", ",")
w = re.sub(r"[^a-zA-Z?!,¿]+", " ", w)
w = w.strip()
return w
# Create a no accent gold version of the data
# dataset_path: the directory where the raw data is.
def no_accent_gold(dataset_path):
with open(dataset_path,'r',encoding="utf-8") as f:
dataset = pd.read_table(f,delimiter='|', dtype='U',header=None)
no_accent_dataset = pd.DataFrame('',index=range(dataset.shape[0]+500), columns=['line'], dtype='U')
i = 0
for data in dataset.itertuples(index=False,name=None):
if data[0].startswith('prompt_'):
no_accent_dataset.iat[i,0] = ''
no_accent_dataset.iat[i+1,0] = data[0] + '|' + data[1]
i += 2
else:
no_accent_dataset.iat[i,0] = preprocess_sentence(data[0]) + '|' + data[1]
i += 1
return no_accent_dataset
#######################################################################
# Test code
# output = no_accent_gold('CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt')
# print(output)
# print(output.shape)
# print("End")
# Save Modified Datasets
output = create_modified_dataset('datasets/staple-2020/en_pt/train.en_pt.2020-01-13.gold.txt', 1)
np.savetxt('datasets/modified_datasets/dataset_1.txt', output, fmt='%s',delimiter='|',encoding='utf-8')
# print("1 done")
output = create_modified_dataset('datasets/staple-2020/en_pt/train.en_pt.2020-01-13.gold.txt', 2)
np.savetxt('datasets/modified_datasets/dataset_2.txt', output, fmt='%s',delimiter='|',encoding='utf-8')
# print("2 done")
output = create_modified_dataset('datasets/staple-2020/en_pt/train.en_pt.2020-01-13.gold.txt', 3)
np.savetxt('datasets/modified_datasets/dataset_3.txt', output, fmt='%s',delimiter='|',encoding='utf-8')
# print("3 done")
# print("End")
# Save Test and Dev Datasets
# output = create_testing_dataset('CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt')
# output.to_csv('CMPUT566-MOTH/datasets/testing_datasets/test.txt',sep="|",encoding='utf-8',index=False,header=False)
# output = create_testing_dataset('CMPUT566-MOTH/datasets/staple-2020/en_pt/dev.en_pt.2020-02-20.gold.txt')
# output.to_csv('CMPUT566-MOTH/datasets/testing_datasets/dev.txt',sep="|",encoding='utf-8',index=False,header=False)
# Save Dev Datasets (Best Translation only)
# output = create_testing_dataset_best('CMPUT566-MOTH/datasets/staple-2020/en_pt/dev.en_pt.2020-02-20.gold.txt')
# output.to_csv('CMPUT566-MOTH/datasets/testing_datasets/dev_best.txt',sep="|",encoding='utf-8',index=False,header=False)
# Save Amazon Basline Dataset
# output = create_amazon_baseline('CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.aws_baseline.pred.txt')
# output.to_csv('CMPUT566-MOTH/datasets/baseline_datasets/amazon.txt',sep="|",encoding='utf-8',index=False,header=False)
# Save Worst Basline Dataset
# output = create_worst_baseline('CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt')
# output.to_csv('CMPUT566-MOTH/datasets/baseline_datasets/worst.txt',sep="|",encoding='utf-8',index=False,header=False)
# Save Gold version of the Transformer's Predictions Datasets
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_1_trial1.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset1_trial1.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_1_trial2.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset1_trial2.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_1_trial3.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset1_trial3.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_2_trial1.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset2_trial1.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_2_trial2.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset2_trial2.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_2_trial3.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset2_trial3.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_3_trial1.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset3_trial1.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_3_trial2.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset3_trial2.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/Transformer_Result/result_dataset_3_trial3.csv','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/dataset3_trial3.txt', output, fmt='%s',encoding='utf-8')
# Save Gold version of the RNN's Predictions Datasets
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.habib1.updated.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset1_trial1_h.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.habib2.updated.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset2_trial1_h.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.maisha1.updated.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset1_trial1_m.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.maisha2.updated.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset2_trial1_m.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.maisha3.updated.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset3_trial1_m.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.maisha1.gold_format.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset1_m.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.maisha2.gold_format.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset2_m.txt', output, fmt='%s',encoding='utf-8')
# output = convert_to_gold('CMPUT566-MOTH/datasets/RNN_Result/predict.maisha3.gold_format.txt','CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt',0)
# np.savetxt('CMPUT566-MOTH/datasets/gold_rnn/dataset3_m.txt', output, fmt='%s',encoding='utf-8')
# Create Test Dataset with no Accents
# output = no_accent_gold('CMPUT566-MOTH/datasets/staple-2020/en_pt/test.en_pt.2020-02-20.gold.txt')
# np.savetxt('CMPUT566-MOTH/datasets/gold_transformer/test.txt', output, fmt='%s',encoding='utf-8')