forked from OpenNMT/OpenNMT-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
296 lines (237 loc) · 10.6 KB
/
preprocess.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
# -*- coding: utf-8 -*-
import onmt
import onmt.Markdown
import onmt.IO
import argparse
import torch
import codecs
parser = argparse.ArgumentParser(description='preprocess.py')
onmt.Markdown.add_md_help_argument(parser)
# **Preprocess Options**
parser.add_argument('-config', help="Read options from this file")
parser.add_argument('-src_type', default="text",
help="Type of the source input. Options are [text|img].")
parser.add_argument('-src_img_dir', default=".",
help="Location of source images")
parser.add_argument('-train_src', required=True,
help="Path to the training source data")
parser.add_argument('-train_tgt', required=True,
help="Path to the training target data")
parser.add_argument('-valid_src', required=True,
help="Path to the validation source data")
parser.add_argument('-valid_tgt', required=True,
help="Path to the validation target data")
parser.add_argument('-save_data', required=True,
help="Output file for the prepared data")
parser.add_argument('-src_vocab_size', type=int, default=50000,
help="Size of the source vocabulary")
parser.add_argument('-tgt_vocab_size', type=int, default=50000,
help="Size of the target vocabulary")
parser.add_argument('-src_vocab',
help="Path to an existing source vocabulary")
parser.add_argument('-tgt_vocab',
help="Path to an existing target vocabulary")
parser.add_argument('-features_vocabs_prefix', type=str, default='',
help="Path prefix to existing features vocabularies")
parser.add_argument('-src_seq_length', type=int, default=50,
help="Maximum source sequence length")
parser.add_argument('-src_seq_length_trunc', type=int, default=0,
help="Truncate source sequence length.")
parser.add_argument('-tgt_seq_length', type=int, default=50,
help="Maximum target sequence length to keep.")
parser.add_argument('-tgt_seq_length_trunc', type=int, default=0,
help="Truncate target sequence length.")
parser.add_argument('-shuffle', type=int, default=1,
help="Shuffle data")
parser.add_argument('-seed', type=int, default=3435,
help="Random seed")
parser.add_argument('-lower', action='store_true', help='lowercase data')
parser.add_argument('-report_every', type=int, default=100000,
help="Report status every this many sentences")
opt = parser.parse_args()
torch.manual_seed(opt.seed)
def makeVocabulary(filename, size):
"Construct the word and feature vocabs."
vocab = onmt.Dict([onmt.Constants.PAD_WORD, onmt.Constants.UNK_WORD,
onmt.Constants.BOS_WORD, onmt.Constants.EOS_WORD],
lower=opt.lower)
featuresVocabs = []
with codecs.open(filename, "r", "utf-8") as f:
for sent in f.readlines():
words, features, numFeatures \
= onmt.IO.extractFeatures(sent.split())
if len(featuresVocabs) == 0 and numFeatures > 0:
for j in range(numFeatures):
featuresVocabs.append(onmt.Dict([onmt.Constants.PAD_WORD,
onmt.Constants.UNK_WORD,
onmt.Constants.BOS_WORD,
onmt.Constants.EOS_WORD]))
else:
assert len(featuresVocabs) == numFeatures, \
"all sentences must have the same number of features"
for i in range(len(words)):
vocab.add(words[i])
for j in range(numFeatures):
featuresVocabs[j].add(features[j][i])
originalSize = vocab.size()
if size != 0:
vocab = vocab.prune(size)
print('Created dictionary of size %d (pruned from %d)' %
(vocab.size(), originalSize))
else:
print('Created dictionary of size %d' % (vocab.size()))
return vocab, featuresVocabs
def initVocabulary(name, dataFile, vocabFile, vocabSize):
"""If `vocabFile` exists, read it in,
Else, generate from data."""
vocab = None
if vocabFile is not None:
# If given, load existing word dictionary.
print('Reading ' + name + ' vocabulary from \'' + vocabFile + '\'...')
vocab = onmt.Dict()
vocab.loadFile(vocabFile)
print('Loaded ' + str(vocab.size()) + ' ' + name + ' words')
if vocab is None:
# If a dictionary is still missing, generate it.
print('Building ' + name + ' vocabulary...')
genWordVocab, genFeaturesVocabs = makeVocabulary(dataFile, vocabSize)
vocab = genWordVocab
featuresVocabs = genFeaturesVocabs
print()
return vocab, featuresVocabs
def saveVocabulary(name, vocab, file):
print('Saving ' + name + ' vocabulary to \'' + file + '\'...')
vocab.writeFile(file)
def saveFeaturesVocabularies(name, vocabs, prefix):
for j in range(len(vocabs)):
file = prefix + '.' + name + '_feature_' + str(j) + '.dict'
print('Saving ' + name + ' feature ' + str(j) +
' vocabulary to \'' + file + '\'...')
vocabs[j].writeFile(file)
def makeData(srcFile, tgtFile, srcDicts, tgtDicts,
srcFeatureDicts, tgtFeatureDicts):
src, tgt = [], []
srcFeats = [[] for i in range(len(srcFeatureDicts))]
tgtFeats = [[] for i in range(len(tgtFeatureDicts))]
alignments = []
sizes = []
count, ignored = 0, 0
print('Processing %s & %s ...' % (srcFile, tgtFile))
srcF = codecs.open(srcFile, "r", "utf-8")
tgtF = codecs.open(tgtFile, "r", "utf-8")
while True:
sline = srcF.readline()
tline = tgtF.readline()
# normal end of file
if sline == "" and tline == "":
break
# source or target does not have same number of lines
if sline == "" or tline == "":
print('WARNING: src and tgt do not have the same # of sentences')
break
sline = sline.strip()
tline = tline.strip()
# source and/or target are empty
if sline == "" or tline == "":
print('WARNING: ignoring an empty line ('+str(count+1)+')')
continue
srcLine = sline.split()
tgtLine = tline.split()
if len(srcLine) <= opt.src_seq_length \
and len(tgtLine) <= opt.tgt_seq_length:
# Check truncation condition.
if opt.src_seq_length_trunc != 0:
srcLine = srcLine[:opt.src_seq_length_trunc]
if opt.tgt_seq_length_trunc != 0:
tgtLine = tgtLine[:opt.tgt_seq_length_trunc]
srcWords, srcData, srcFeat \
= onmt.IO.readSrcLine(srcLine, srcDicts,
srcFeatureDicts,
_type=opt.src_type,
src_img_dir=opt.src_img_dir)
src += [srcData]
for i in range(len(srcFeats)):
srcFeats[i] += [srcFeat[i]]
tgtWords, tgtData, tgtFeat = onmt.IO.readTgtLine(tgtLine, tgtDicts,
tgtFeatureDicts)
tgt += [tgtData]
for i in range(len(tgtFeats)):
tgtFeats[i] += [tgtFeat[i]]
alignments += [onmt.IO.align(srcWords, tgtWords)]
sizes += [len(srcData)]
else:
ignored += 1
count += 1
if count % opt.report_every == 0:
print('... %d sentences prepared' % count)
srcF.close()
tgtF.close()
if opt.shuffle == 1:
print('... shuffling sentences')
perm = torch.randperm(len(src))
src = [src[idx] for idx in perm]
tgt = [tgt[idx] for idx in perm]
alignments = [alignments[idx] for idx in perm]
for j in range(len(srcFeatureDicts)):
srcFeats[j] = [srcFeats[j][idx] for idx in perm]
for j in range(len(tgtFeatureDicts)):
tgtFeats[j] = [tgtFeats[j][idx] for idx in perm]
sizes = [sizes[idx] for idx in perm]
print('... sorting sentences by size')
_, perm = torch.sort(torch.Tensor(sizes))
src = [src[idx] for idx in perm]
tgt = [tgt[idx] for idx in perm]
alignments = [alignments[idx] for idx in perm]
for j in range(len(srcFeatureDicts)):
srcFeats[j] = [srcFeats[j][idx] for idx in perm]
for j in range(len(tgtFeatureDicts)):
tgtFeats[j] = [tgtFeats[j][idx] for idx in perm]
print(('Prepared %d sentences ' +
'(%d ignored due to length == 0 or src len > %d or tgt len > %d)') %
(len(src), ignored, opt.src_seq_length, opt.tgt_seq_length))
return src, tgt, srcFeats, tgtFeats, alignments
def main():
dicts = {}
dicts['src'] = onmt.Dict()
if opt.src_type == "text":
dicts['src'], dicts['src_features'] = \
initVocabulary('source', opt.train_src, opt.src_vocab,
opt.src_vocab_size)
dicts['tgt'], dicts['tgt_features'] = \
initVocabulary('target',
opt.train_tgt,
opt.tgt_vocab,
opt.tgt_vocab_size)
print('Preparing training ...')
train = {}
train['src'], train['tgt'], \
train['src_features'], train['tgt_features'], \
train['alignments'] \
= makeData(opt.train_src, opt.train_tgt,
dicts['src'], dicts['tgt'],
dicts['src_features'], dicts['tgt_features'])
print('Preparing validation ...')
valid = {}
valid['src'], valid['tgt'], \
valid['src_features'], valid['tgt_features'], \
valid['alignments'] \
= makeData(opt.valid_src, opt.valid_tgt,
dicts['src'], dicts['tgt'],
dicts['src_features'], dicts['tgt_features'])
if opt.src_vocab is None:
saveVocabulary('source', dicts['src'], opt.save_data + '.src.dict')
if opt.tgt_vocab is None:
saveVocabulary('target', dicts['tgt'], opt.save_data + '.tgt.dict')
if opt.features_vocabs_prefix:
saveFeaturesVocabularies('source', dicts['src_features'],
opt.save_data)
saveFeaturesVocabularies('target', dicts['tgt_features'],
opt.save_data)
print('Saving data to \'' + opt.save_data + '.train.pt\'...')
save_data = {'dicts': dicts,
'type': opt.src_type,
'train': train,
'valid': valid}
torch.save(save_data, opt.save_data + '.train.pt')
if __name__ == "__main__":
main()