-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_dataset.py
314 lines (248 loc) · 8.6 KB
/
make_dataset.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
import argparse
import numpy as np
import csv
import os
from tqdm import tqdm
parser = argparse.ArgumentParser()
parser.add_argument(
'-mode',
help="train or infer",
choices=['train', 'infer'],
required=True,
)
parser.add_argument(
'-source_input_path',
help="source document path",
required=True,
)
parser.add_argument(
'-source_out_path',
help="preprocessed source output path",
required=True,
)
parser.add_argument(
'-target_input_path',
help="target document path",
required=True,
)
parser.add_argument(
'-target_out_path',
help="preprocessed target output path",
required=False
)
parser.add_argument(
'-bucket_out_path',
help="bucket output path",
required=True,
)
parser.add_argument(
'-voca_path',
help="Vocabulary_path",
required=True
)
args = parser.parse_args()
mode = args.mode
source_input_path = args.source_input_path
source_out_path = args.source_out_path
target_input_path = args.target_input_path
target_out_path = args.target_out_path
bucket_out_path = args.bucket_out_path
voca_path = args.voca_path
def read_voca(path):
sorted_voca = []
with open(path, 'r', encoding='utf-8') as f:
for bpe_voca in f:
bpe_voca = bpe_voca.strip()
if bpe_voca:
bpe_voca = bpe_voca.split()
sorted_voca.append(bpe_voca)
return sorted_voca
def make_bpe2idx(voca):
bpe2idx = {'</p>':0, '</UNK>':1, '</g>':2, '</e>':3}
idx = 4
for word, _ in voca:
bpe2idx[word] = idx
idx += 1
return bpe2idx
def bpe2idx_out_csv(data_path, out_path, bpe2idx, info='source'): #info: 'source' or 'target'
print('documents to idx csv', data_path, '->', out_path)
o = open(out_path, 'w', newline='', encoding='utf-8')
wr = csv.writer(o)
with open(data_path, 'r', encoding='utf-8') as f:
documents = f.readlines()
for i in tqdm(range(len(documents)), ncols=50):
sentence = documents[i]
# bpe2idx
if info == 'target':
row_idx = [bpe2idx['</g>']]
else:
row_idx = []
for word in sentence.strip().split():
if word in bpe2idx:
row_idx.append(bpe2idx[word])
else:
row_idx.append(bpe2idx['</UNK>']) ## 1
row_idx.append(bpe2idx['</e>']) ## eos:3
wr.writerow(row_idx)
o.close()
print('saved', out_path, '\n')
def _make_bucket_dataset(source_path, target_path, out_path, bucket, pad_idx, file_mode='w', is_trainset=True):
print('make bucket dataset')
print('source:', source_path, 'target:', target_path)
if not os.path.exists(out_path):
os.makedirs(out_path)
# 저장시킬 object 생성
source_open_list = []
target_open_list = []
for bucket_size in bucket:
o_s = open(os.path.join(out_path, 'source_'+str(bucket_size)+'.csv'), file_mode, newline='')
o_s_csv = csv.writer(o_s)
source_open_list.append((o_s, o_s_csv))
if is_trainset:
o_t = open(os.path.join(out_path, 'target_'+str(bucket_size)+'.csv'), file_mode, newline='')
o_t_csv = csv.writer(o_t)
target_open_list.append((o_t, o_t_csv))
else:
o_t = open(os.path.join(out_path, 'target_'+str(bucket_size)+'.txt'), file_mode, encoding='utf-8')
target_open_list.append(o_t)
with open(source_path, 'r') as s:
source = s.readlines()
if is_trainset:
with open(target_path, 'r') as t:
target = t.readlines()
else:
with open(target_path, 'r', encoding='utf-8') as t:
target = t.readlines()
for i in tqdm(range(len(source)), ncols=50):
source_sentence = np.array(source[i].strip().split(','), dtype=np.int32)
if is_trainset:
target_sentence = np.array(target[i].strip().split(','), dtype=np.int32)
else:
target_sentence = target[i]
for bucket_index, bucket_size in enumerate(bucket):
source_size, target_size = bucket_size
# 버켓에 없는것은 데이터는 제외.
if is_trainset:
if len(source_sentence) <= source_size and len(target_sentence) <= target_size: # (1,2) <= (10, 40)
source_sentence = np.pad(
source_sentence,
(0, source_size-len(source_sentence)),
'constant',
constant_values = pad_idx# bpe2idx['</p>'] # pad value
)
target_sentence = np.pad(
target_sentence,
(0, target_size+1-len(target_sentence)), # [0:-1]: decoder_input, [1:]: decoder_target 이므로 +1 해줌.
'constant',
constant_values = pad_idx # bpe2idx['</p>'] # pad value
)
source_open_list[bucket_index][1].writerow(source_sentence)
target_open_list[bucket_index][1].writerow(target_sentence)
break
else:
if len(source_sentence) <= source_size:
source_sentence = np.pad(
source_sentence,
(0, source_size-len(source_sentence)),
'constant',
constant_values = pad_idx# bpe2idx['</p>'] # pad value
)
source_open_list[bucket_index][1].writerow(source_sentence)
target_open_list[bucket_index].write(target_sentence)
break
# close object
for i in range(len(bucket)):
source_open_list[i][0].close()
if is_trainset:
target_open_list[i][0].close()
else:
target_open_list[i].close()
print('saved', out_path)
def make_bucket_dataset(data_path, idx_out_path, bucket_out_path, bucket, bpe2idx, file_mode='w', is_trainset=True):
print('start make_bucket_dataset', 'is_trainset:', is_trainset)
bpe2idx_out_csv(
data_path=data_path['source'],
out_path=idx_out_path['source'],
bpe2idx=bpe2idx,
info='source'
)
if is_trainset:
bpe2idx_out_csv(
data_path=data_path['target'],
out_path=idx_out_path['target'],
bpe2idx=bpe2idx,
info='target'
)
# padding and bucketing
_make_bucket_dataset(
source_path=idx_out_path['source'],
target_path=idx_out_path['target'],
out_path=bucket_out_path,
bucket=bucket,
pad_idx=bpe2idx['</p>'],
file_mode=file_mode,
is_trainset=is_trainset
)
else:
# padding and bucketing
_make_bucket_dataset(
source_path=idx_out_path['source'],
target_path=data_path['target'],
out_path=bucket_out_path,
bucket=bucket,
pad_idx=bpe2idx['</p>'],
file_mode=file_mode,
is_trainset=is_trainset
)
print('\n\n')
voca = read_voca(voca_path)
bpe2idx = make_bpe2idx(voca)
if mode == 'train':
data_path = {'source':source_input_path, 'target':target_input_path}
idx_out_path = {'source':source_out_path, 'target':target_out_path}
#bucket (source, target)
train_bucket = [(i*5, i*5 + j*10) for i in range(1, 31) for j in range(4)]# [(5, 5), (5, 15), .., (5, 35), ... , (150, 150), .., (150, 180)]
print('train_bucket\n', train_bucket,'\n')
make_bucket_dataset(
data_path,
idx_out_path,
bucket_out_path,
train_bucket,
bpe2idx
)
elif mode == 'infer':
data_path = {'source':source_input_path, 'target':target_input_path}
idx_out_path = {'source':source_out_path}
#bucket (source, target)
infer_bucket = [(i*5, i*5+50) for i in range(1, 31)] # [(5, 55), (10, 60), ..., (150, 200)]
print('infer_bucket\n', infer_bucket,'\n')
make_bucket_dataset(
data_path,
idx_out_path,
bucket_out_path,
infer_bucket,
bpe2idx,
is_trainset=False
)
'''
# make trainset
data_path = {'source':'./bpe_dataset/bpe_wmt17.en', 'target':'./bpe_dataset/bpe_wmt17.de'}
idx_out_path = {'source':'./bpe_dataset/source_idx_wmt17_en.csv', 'target':'./bpe_dataset/target_idx_wmt17_de.csv'}
bucket_out_path = './bpe_dataset/train_set_wmt17/'
make_bucket_dataset(data_path, idx_out_path, bucket_out_path, train_bucket, bpe2idx)
# make validset
data_path = {'source':'./bpe_dataset/bpe_newstest2014.en', 'target':'./dataset/dev.tar/newstest2014.tc.de'}
idx_out_path = {'source':'./bpe_dataset/source_idx_newstest2014_en.csv'}
bucket_out_path = './bpe_dataset/valid_set_newstest2014/'
make_bucket_dataset(data_path, idx_out_path, bucket_out_path, infer_bucket, bpe2idx, is_trainset=False)
# make testset
data_path = {'source':'./bpe_dataset/bpe_newstest2015.en', 'target':'./dataset/dev.tar/newstest2015.tc.de'}
idx_out_path = {'source':'./bpe_dataset/source_idx_newstest2015_en.csv'}
bucket_out_path = './bpe_dataset/test_set_newstest2015/'
make_bucket_dataset(data_path, idx_out_path, bucket_out_path, infer_bucket, bpe2idx, is_trainset=False)
# make testset
data_path = {'source':'./bpe_dataset/bpe_newstest2016.en', 'target':'./dataset/dev.tar/newstest2016.tc.de'}
idx_out_path = {'source':'./bpe_dataset/source_idx_newstest2016_en.csv'}
bucket_out_path = './bpe_dataset/test_set_newstest2016/'
make_bucket_dataset(data_path, idx_out_path, bucket_out_path, infer_bucket, bpe2idx, is_trainset=False)
'''