-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_data.py
executable file
·266 lines (229 loc) · 10.8 KB
/
prepare_data.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
#!/usr/bin/python
import argparse
import logging
import os
import subprocess
import tarfile
import urllib2
import uuid
import configurations
from picklable_itertools.extras import equizip
TRAIN_DATA_URL = 'http://www.statmt.org/wmt15/training-parallel-nc-v10.tgz'
VALID_DATA_URL = 'http://www.statmt.org/wmt15/dev-v2.tgz'
PREPROCESS_URL = 'https://raw.githubusercontent.com/lisa-groundhog/' +\
'GroundHog/master/experiments/nmt/preprocess/preprocess.py'
TOKENIZER_URL = 'https://raw.githubusercontent.com/moses-smt/mosesdecoder/' +\
'master/scripts/tokenizer/tokenizer.perl'
TOKENIZER_PREFIXES = 'https://raw.githubusercontent.com/moses-smt/' +\
'mosesdecoder/master/scripts/share/nonbreaking_' +\
'prefixes/nonbreaking_prefix.'
BLEU_SCRIPT_URL = 'https://raw.githubusercontent.com/moses-smt/mosesdecoder' +\
'/master/scripts/generic/multi-bleu.perl'
OUTPUT_DIR = './data'
PREFIX_DIR = './share/nonbreaking_prefixes'
parser = argparse.ArgumentParser(
description="""
This script donwloads parallel corpora given source and target pair language
indicators and preprocess it respectively for neural machine translation.For
the preprocessing, moses tokenizer is applied first then tokenized corpora
are used to extract vocabularies for source and target languages. Finally the
tokenized parallel corpora are shuffled for SGD.
Note that, this script is written specificaly for WMT15 training and
development corpora, hence change the corresponding sections if you plan to use
some other data.
""", formatter_class=argparse.RawTextHelpFormatter)
# parser.add_argument("-s", "--source", type=str, help="Source language",
# default="train.zh")
# parser.add_argument("-t", "--target", type=str, help="Target language",
# default="train.en")
parser.add_argument("--source-dev", type=str, default="newstest2013.cs",
help="Source language dev filename")
parser.add_argument("--target-dev", type=str, default="newstest2013.en",
help="Target language dev filename")
parser.add_argument("--source-vocab", type=int, default=30000,
help="Source language vocabulary size")
parser.add_argument("--target-vocab", type=int, default=30000,
help="Target language vocabulary size")
def download_and_write_file(url, file_name):
logger.info("Downloading [{}]".format(url))
if not os.path.exists(file_name):
path = os.path.dirname(file_name)
if not os.path.exists(path):
os.makedirs(path)
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
logger.info("...saving to: %s Bytes: %s" % (file_name, file_size))
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % \
(file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8) * (len(status) + 1)
print status,
f.close()
else:
logger.info("...file exists [{}]".format(file_name))
def extract_tar_file_to(file_to_extract, extract_into, names_to_look):
extracted_filenames = []
try:
logger.info("Extracting file [{}] into [{}]"
.format(file_to_extract, extract_into))
tar = tarfile.open(file_to_extract, 'r')
src_trg_files = [ff for ff in tar.getnames()
if any([ff.find(nn) > -1 for nn in names_to_look])]
if not len(src_trg_files):
raise ValueError("[{}] pair does not exist in the archive!"
.format(src_trg_files))
for item in tar:
# extract only source-target pair
if item.name in src_trg_files:
file_path = os.path.join(extract_into, item.path)
if not os.path.exists(file_path):
logger.info("...extracting [{}] into [{}]"
.format(item.name, file_path))
tar.extract(item, extract_into)
else:
logger.info("...file exists [{}]".format(file_path))
extracted_filenames.append(
os.path.join(extract_into, item.path))
except Exception as e:
logger.error("{}".format(str(e)))
return extracted_filenames
def tokenize_text_files(files_to_tokenize, tokenizer):
for name in files_to_tokenize:
logger.info("Tokenizing file [{}]".format(name))
out_file = os.path.join(
OUTPUT_DIR, os.path.basename(name) + '.tok')
logger.info("...writing tokenized file [{}]".format(out_file))
var = ["perl", tokenizer, "-l", name.split('.')[-1]]
if not os.path.exists(out_file):
with open(name, 'r') as inp:
with open(out_file, 'w', 0) as out:
subprocess.check_call(
var, stdin=inp, stdout=out, shell=False)
else:
logger.info("...file exists [{}]".format(out_file))
def create_vocabularies(src_data, trg_data, preprocess_file, topk_trg_vocab, **kwargs):
src_vocab_size = kwargs.pop('src_vocab_size', 30000)
trg_vocab_size = kwargs.pop('trg_vocab_size', 30000)
tr_files = [src_data, trg_data]
source_suffix = tr_files[0].split('.')[-1]
target_suffix = tr_files[1].split('.')[-1]
src_vocab_name = os.path.join(
OUTPUT_DIR, 'vocab.{}-{}.{}.pkl'.format(
source_suffix, target_suffix, source_suffix))
trg_vocab_name = os.path.join(
OUTPUT_DIR, 'vocab.{}-{}.{}.pkl'.format(
source_suffix, target_suffix, target_suffix))
src_filename = os.path.basename(
tr_files[[i for i, n in enumerate(tr_files)
if n.endswith(source_suffix)][0]]) # remove previous path: /zhang/wen -> wen
trg_filename = os.path.basename(
tr_files[[i for i, n in enumerate(tr_files)
if n.endswith(target_suffix)][0]])
# dict
src_dict_name = os.path.join(
OUTPUT_DIR, 'vocab.{}-{}.{}.dict'.format(
source_suffix, target_suffix, source_suffix))
trg_dict_name = os.path.join(
OUTPUT_DIR, 'vocab.{}-{}.{}.dict'.format(
source_suffix, target_suffix, target_suffix))
logger.info("Creating source vocabulary [{}]".format(src_vocab_name))
if not os.path.exists(src_vocab_name):
subprocess.check_call(" python {} -d {} -v {} -rl {} {}".format(
preprocess_file, src_vocab_name, src_vocab_size,
src_dict_name, os.path.join(OUTPUT_DIR, src_filename)),
shell=True)
else:
logger.info("...file exists [{}]".format(src_vocab_name))
'''
subprocess.check_call(" python {} -o -d {} -v {} -rl {} {}".format(
preprocess_file, src_vocab_name, args.source_vocab,
src_dict_name, os.path.join(OUTPUT_DIR, src_filename)),
shell=True)
'''
logger.info("Creating target vocabulary [{}]".format(trg_vocab_name))
if not os.path.exists(trg_vocab_name):
subprocess.check_call(" python {} -d {} -v {} -rl {} -top {} {}".format(
preprocess_file, trg_vocab_name, trg_vocab_size,
trg_dict_name, topk_trg_vocab, os.path.join(OUTPUT_DIR, trg_filename)),
shell=True)
else:
logger.info("...file exists [{}]".format(trg_vocab_name))
'''
subprocess.check_call(" python {} -o -d {} -v {} -rl {} -top {} {}".format(
preprocess_file, trg_vocab_name, args.target_vocab,
trg_dict_name, topk_trg_vocab, os.path.join(OUTPUT_DIR, trg_filename)),
shell=True)
'''
# return src_filename, trg_filename, aln_filename
def merge_parallel(src_filename, trg_filename, merged_filename):
with open(src_filename, 'r') as left:
with open(trg_filename, 'r') as right:
with open(merged_filename, 'w') as final:
for lline, rline in equizip(left, right):
if (lline != '\n') and (rline != '\n'):
final.write(lline[:-1] + ' ||| ' + rline)
def split_parallel(merged_filename, src_filename, trg_filename):
with open(merged_filename) as combined:
with open(src_filename, 'w') as left:
with open(trg_filename, 'w') as right:
for line in combined:
line = line.split('|||')
left.write(line[0].strip() + '\n')
right.write(line[1].strip() + '\n')
def shuffle_parallel(src_filename, trg_filename):
logger.info("Shuffling jointly [{}] and [{}]".format(src_filename,
trg_filename))
out_src = src_filename + '.shuf'
out_trg = trg_filename + '.shuf'
merged_filename = str(uuid.uuid4())
shuffled_filename = str(uuid.uuid4())
if not os.path.exists(out_src) or not os.path.exists(out_trg):
try:
merge_parallel(src_filename, trg_filename, merged_filename)
subprocess.check_call(
" shuf {} > {} ".format(merged_filename, shuffled_filename),
shell=True)
split_parallel(shuffled_filename, out_src, out_trg)
logger.info(
"...files shuffled [{}] and [{}]".format(out_src, out_trg))
except Exception as e:
logger.error("{}".format(str(e)))
else:
logger.info("...files exist [{}] and [{}]".format(out_src, out_trg))
if os.path.exists(merged_filename):
os.remove(merged_filename)
if os.path.exists(shuffled_filename):
os.remove(shuffled_filename)
def main():
config = getattr(configurations, 'get_config_cs2en')()
OUTPUT_DIR = config['datadir']
#preprocess_file = os.path.join(OUTPUT_DIR, 'preprocess.py')
#bleuscore_file = os.path.join(OUTPUT_DIR, 'multi-bleu.perl')
# Download the News Commentary v10 ~122Mb and extract it
# Download bleu score calculation script
#download_and_write_file(BLEU_SCRIPT_URL, bleuscore_file)
# Download preprocessing script
#download_and_write_file(PREPROCESS_URL, preprocess_file)
# Download tokenizer
# Apply preprocessing and construct vocabularies
#tr_files = ["trn.src", "trn.trg"]
create_vocabularies(**config)
# Shuffle datasets
shuffle_parallel(config['src_data'], config['trg_data'])
from manvocab import write_tr_target_word_into_file, valid_sent_target_vcab_set
write_tr_target_word_into_file(**config)
valid_sent_target_vcab_set(**config)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('prepare_data')
args = parser.parse_args()
main()