forked from harvardnlp/seq2seq-attn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
429 lines (390 loc) · 20 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Create the data for the LSTM.
"""
import os
import sys
import argparse
import numpy as np
import h5py
import itertools
from collections import defaultdict
class Indexer:
def __init__(self, symbols = ["<blank>","<unk>","<s>","</s>"]):
self.vocab = defaultdict(int)
self.PAD = symbols[0]
self.UNK = symbols[1]
self.BOS = symbols[2]
self.EOS = symbols[3]
self.d = {self.PAD: 1, self.UNK: 2, self.BOS: 3, self.EOS: 4}
def add_w(self, ws):
for w in ws:
if w not in self.d:
self.d[w] = len(self.d) + 1
def convert(self, w):
return self.d[w] if w in self.d else self.d[self.UNK]
def convert_sequence(self, ls):
return [self.convert(l) for l in ls]
def clean(self, s):
s = s.replace(self.PAD, "")
s = s.replace(self.BOS, "")
s = s.replace(self.EOS, "")
return s
def write(self, outfile, chars=0):
out = open(outfile, "w")
items = [(v, k) for k, v in self.d.iteritems()]
items.sort()
for v, k in items:
if chars == 1:
print >>out, k.encode('utf-8'), v
else:
print >>out, k, v
out.close()
def prune_vocab(self, k, n):
vocab_list = [(word, count) for word, count in self.vocab.iteritems()]
vocab_list.sort(key = lambda x: x[1], reverse=True)
if n == 1:
keyword = open("keyword.txt", 'w')
for item in vocab_list:
print>>keyword, item[0]
keyword.close()
k = min(k, len(vocab_list))
self.pruned_vocab = {pair[0]:pair[1] for pair in vocab_list[:k]}
for word in self.pruned_vocab:
if word not in self.d:
self.d[word] = len(self.d) + 1
def load_vocab(self, vocab_file, chars=0):
self.d = {}
for line in open(vocab_file, 'r'):
if chars == 1:
v, k = line.decode("utf-8").strip().split()
else:
v, k = line.strip().split()
self.d[v] = int(k)
def pad(ls, length, symbol):
if len(ls) >= length:
return ls[:length]
return ls + [symbol] * (length -len(ls))
def get_data(args):
src_indexer = Indexer(["<blank>","<unk>","<s>","</s>"])
target_indexer = Indexer(["<blank>","<unk>","<s>","</s>"])
char_indexer = Indexer(["<blank>","<unk>","{","}"])
sent_indexer = Indexer(["<blank>","<unk>","<s>","</s>"])
char_indexer.add_w([src_indexer.PAD, src_indexer.UNK, src_indexer.BOS, src_indexer.EOS])
def make_vocab(srcfile, targetfile, seqlength, max_word_l=0, chars=0):
num_sents = 0
for _, (src_orig, targ_orig) in \
enumerate(itertools.izip(open(srcfile,'r'), open(targetfile,'r'))):
if chars == 1:
src_orig = src_indexer.clean(src_orig.decode("utf-8").strip())
targ_orig = target_indexer.clean(targ_orig.decode("utf-8").strip())
else:
src_orig = src_indexer.clean(src_orig.strip())
targ_orig = target_indexer.clean(targ_orig.strip())
targ = targ_orig.strip().split()
src = src_orig.strip().split()
if len(targ) > seqlength or len(src) > seqlength or len(targ) < 1 or len(src) < 1:
continue
num_sents += 1
for word in targ:
if chars == 1:
word = char_indexer.clean(word)
if len(word) == 0:
continue
max_word_l = max(len(word)+2, max_word_l)
for char in list(word):
char_indexer.vocab[char] += 1
target_indexer.vocab[word] += 1
for word in src:
if chars == 1:
word = char_indexer.clean(word)
if len(word) == 0:
continue
max_word_l = max(len(word)+2, max_word_l)
for char in list(word):
char_indexer.vocab[char] += 1
src_indexer.vocab[word] += 1
return max_word_l, num_sents
def convert(srcfile, targetfile, batchsize, seqlength, outfile, num_sents,
max_word_l, max_sent_l=0,chars=0, unkfilter=0, shuffle=0):
newseqlength = seqlength + 2 #add 2 for EOS and BOS
targets = np.zeros((num_sents, newseqlength), dtype=int)
target_output = np.zeros((num_sents, newseqlength), dtype=int)
sources = np.zeros((num_sents, newseqlength), dtype=int)
if args.keyword_file != '':
keyword_vecs = np.zeros((num_sents, len(keywords)), dtype=int)
source_lengths = np.zeros((num_sents,), dtype=int)
target_lengths = np.zeros((num_sents,), dtype=int)
if chars==1:
sources_char = np.zeros((num_sents, newseqlength, max_word_l), dtype=int)
targets_char = np.zeros((num_sents, newseqlength, max_word_l), dtype=int)
dropped = 0
sent_id = 0
for _, (src_orig, targ_orig) in \
enumerate(itertools.izip(open(srcfile,'r'), open(targetfile,'r'))):
if chars == 1:
src_orig = src_indexer.clean(src_orig.decode("utf-8").strip())
targ_orig = target_indexer.clean(targ_orig.decode("utf-8").strip())
else:
src_orig = src_indexer.clean(src_orig.strip())
targ_orig = target_indexer.clean(targ_orig.strip())
targ = [target_indexer.BOS] + targ_orig.strip().split() + [target_indexer.EOS]
src = [src_indexer.BOS] + src_orig.strip().split() + [src_indexer.EOS]
max_sent_l = max(len(targ), len(src), max_sent_l)
if len(targ) > newseqlength or len(src) > newseqlength or len(targ) < 3 or len(src) < 3:
dropped += 1
continue
targ = pad(targ, newseqlength+1, target_indexer.PAD)
targ_char = []
if args.keyword_file != '':
keyword_vec = np.zeros(len(keywords), dtype=int)
for word in targ:
if chars == 1:
word = char_indexer.clean(word)
#use UNK for target, but not for source
word = word if word in target_indexer.d else target_indexer.UNK
# for keywords
if args.keyword_file != '':
if word in keywords:
keyword_vec[keywords[word]-1] = 1
if chars == 1:
char = [char_indexer.BOS] + list(word) + [char_indexer.EOS]
if len(char) > max_word_l:
char = char[:max_word_l]
char[-1] = char_indexer.EOS
char_idx = char_indexer.convert_sequence(pad(char, max_word_l, char_indexer.PAD))
targ_char.append(char_idx)
targ = target_indexer.convert_sequence(targ)
targ = np.array(targ, dtype=int)
src = pad(src, newseqlength, src_indexer.PAD)
src_char = []
for word in src:
if chars == 1:
word = char_indexer.clean(word)
char = [char_indexer.BOS] + list(word) + [char_indexer.EOS]
if len(char) > max_word_l:
char = char[:max_word_l]
char[-1] = char_indexer.EOS
char_idx = char_indexer.convert_sequence(pad(char, max_word_l, char_indexer.PAD))
src_char.append(char_idx)
src = src_indexer.convert_sequence(src)
src = np.array(src, dtype=int)
if unkfilter > 0:
targ_unks = float((targ[:-1] == 2).sum())
src_unks = float((src == 2).sum())
if unkfilter < 1: #unkfilter is a percentage if < 1
targ_unks = targ_unks/(len(targ[:-1])-2)
src_unks = src_unks/(len(src)-2)
if targ_unks > unkfilter or src_unks > unkfilter:
dropped += 1
continue
if args.keyword_file != '':
keyword_vecs[sent_id] = keyword_vec
targets[sent_id] = np.array(targ[:-1],dtype=int)
target_lengths[sent_id] = (targets[sent_id] != 1).sum()
if chars == 1:
targets_char[sent_id] = np.array(targ_char[:-1], dtype=int)
target_output[sent_id] = np.array(targ[1:],dtype=int)
sources[sent_id] = np.array(src, dtype=int)
source_lengths[sent_id] = (sources[sent_id] != 1).sum()
if chars == 1:
sources_char[sent_id] = np.array(src_char, dtype=int)
sent_id += 1
if sent_id % 100000 == 0:
print("{}/{} sentences processed".format(sent_id, num_sents))
print(sent_id, num_sents)
if shuffle == 1:
rand_idx = np.random.permutation(sent_id)
targets = targets[rand_idx]
target_output = target_output[rand_idx]
sources = sources[rand_idx]
source_lengths = source_lengths[rand_idx]
target_lengths = target_lengths[rand_idx]
if args.keyword_file != '':
keyword_vecs = keyword_vecs[rand_idx]
if chars==1:
sources_char = sources_char[rand_idx]
targets_char = targets_char[rand_idx]
#break up batches based on source lengths
source_lengths = source_lengths[:sent_id]
source_sort = np.argsort(source_lengths)
if args.keyword_file != '':
keyword_vecs = keyword_vecs[source_sort]
sources = sources[source_sort]
targets = targets[source_sort]
target_output = target_output[source_sort]
target_l = target_lengths[source_sort]
source_l = source_lengths[source_sort]
curr_l = 0
l_location = [] #idx where sent length changes
for j,i in enumerate(source_sort):
if source_lengths[i] > curr_l:
curr_l = source_lengths[i]
l_location.append(j+1)
l_location.append(len(sources))
#get batch sizes
curr_idx = 1
batch_idx = [1]
nonzeros = []
batch_l = []
batch_w = []
target_l_max = []
for i in range(len(l_location)-1):
while curr_idx < l_location[i+1]:
curr_idx = min(curr_idx + batchsize, l_location[i+1])
batch_idx.append(curr_idx)
for i in range(len(batch_idx)-1):
batch_l.append(batch_idx[i+1] - batch_idx[i])
batch_w.append(source_l[batch_idx[i]-1])
nonzeros.append((target_output[batch_idx[i]-1:batch_idx[i+1]-1] != 1).sum().sum())
target_l_max.append(max(target_l[batch_idx[i]-1:batch_idx[i+1]-1]))
# Write output
f = h5py.File(outfile, "w")
f["source"] = sources
f["target"] = targets
f["target_output"] = target_output
f["target_l"] = np.array(target_l_max, dtype=int)
f["target_l_all"] = target_l
f["batch_l"] = np.array(batch_l, dtype=int)
f["batch_w"] = np.array(batch_w, dtype=int)
f["batch_idx"] = np.array(batch_idx[:-1], dtype=int)
f["target_nonzeros"] = np.array(nonzeros, dtype=int)
f["source_size"] = np.array([len(src_indexer.d)])
f["target_size"] = np.array([len(target_indexer.d)])
print("total keywords: {}".format(sum(sum(keyword_vecs))))
if args.keyword_file != '':
f['vecs'] = keyword_vecs
if chars == 1:
del sources, targets, target_output
sources_char = sources_char[source_sort]
f["source_char"] = sources_char
del sources_char
targets_char = targets_char[source_sort]
f["target_char"] = targets_char
f["char_size"] = np.array([len(char_indexer.d)])
print("Saved {} sentences (dropped {} due to length/unk filter)".format(
len(f["source"]), dropped))
f.close()
return max_sent_l
print("First pass through data to get vocab...")
max_word_l, num_sents_train = make_vocab(args.srcfile, args.targetfile,
args.seqlength, 0, args.chars)
print("Number of sentences in training: {}".format(num_sents_train))
max_word_l, num_sents_valid = make_vocab(args.srcvalfile, args.targetvalfile,
args.seqlength, max_word_l, args.chars)
print("Number of sentences in valid: {}".format(num_sents_valid))
if args.chars == 1:
print("Max word length (before cutting): {}".format(max_word_l))
max_word_l = min(max_word_l, args.maxwordlength)
print("Max word length (after cutting): {}".format(max_word_l))
#prune and write vocab
src_indexer.prune_vocab(args.srcvocabsize,0)
target_indexer.prune_vocab(args.targetvocabsize, 0)
if args.srcvocabfile != '':
print('Loading pre-specified source vocab from ' + args.srcvocabfile)
src_indexer.load_vocab(args.srcvocabfile, args.chars)
if args.targetvocabfile != '':
print('Loading pre-specified target vocab from ' + args.targetvocabfile)
target_indexer.load_vocab(args.targetvocabfile, args.chars)
if args.charvocabfile != '':
print('Loading pre-specified char vocab from ' + args.charvocabfile)
char_indexer.load_vocab(args.charvocabfile, args.chars)
src_indexer.write(args.outputfile + ".src.dict", args.chars)
target_indexer.write(args.outputfile + ".targ.dict", args.chars)
if args.chars == 1:
if args.charvocabfile == '':
char_indexer.prune_vocab(500)
char_indexer.write(args.outputfile + ".char.dict", args.chars)
print("Character vocab size: {}".format(len(char_indexer.vocab)))
print("Source vocab size: Original = {}, Pruned = {}".format(len(src_indexer.vocab),
len(src_indexer.d)))
print("Target vocab size: Original = {}, Pruned = {}".format(len(target_indexer.vocab),
len(target_indexer.d)))
max_sent_l = 0
max_sent_l = convert(args.srcvalfile, args.targetvalfile, args.batchsize, args.seqlength,
args.outputfile + "-val.hdf5", num_sents_valid,
max_word_l, max_sent_l, args.chars, args.unkfilter, args.shuffle)
max_sent_l = convert(args.srcfile, args.targetfile, args.batchsize, args.seqlength,
args.outputfile + "-train.hdf5", num_sents_train, max_word_l,
max_sent_l, args.chars, args.unkfilter, args.shuffle)
print("Max sent length (before dropping): {}".format(max_sent_l))
def main(arguments):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--srcvocabsize', help="Size of source vocabulary, constructed "
"by taking the top X most frequent words. "
" Rest are replaced with special UNK tokens.",
type=int, default=50000)
parser.add_argument('--targetvocabsize', help="Size of target vocabulary, constructed "
"by taking the top X most frequent words. "
"Rest are replaced with special UNK tokens.",
type=int, default=50000)
parser.add_argument('--srcfile', help="Path to source training data, "
"where each line represents a single "
"source/target sequence.", required=True)
parser.add_argument('--targetfile', help="Path to target training data, "
"where each line represents a single "
"source/target sequence.", required=True)
parser.add_argument('--srcvalfile', help="Path to source validation data.", required=True)
parser.add_argument('--targetvalfile', help="Path to target validation data.", required=True)
parser.add_argument('--batchsize', help="Size of each minibatch.", type=int, default=64)
parser.add_argument('--seqlength', help="Maximum sequence length. Sequences longer "
"than this are dropped.", type=int, default=50)
parser.add_argument('--outputfile', help="Prefix of the output file names. ", type=str, required=True)
parser.add_argument('--maxwordlength', help="For the character models, words are "
"(if longer than maxwordlength) or zero-padded "
"(if shorter) to maxwordlength", type=int, default=35)
parser.add_argument('--chars', help="If 1, construct the character-level dataset as well. "
"This might take up a lot of space depending on your data "
"size, so you may want to break up the training data into "
"different shards.", type=int, default=0)
parser.add_argument('--srcvocabfile', help="If working with a preset vocab, "
"then including this will ignore srcvocabsize and use the"
"vocab provided here.",
type = str, default='')
parser.add_argument('--targetvocabfile', help="If working with a preset vocab, "
"then including this will ignore targetvocabsize and "
"use the vocab provided here.",
type = str, default='')
parser.add_argument('--charvocabfile', help="If working with a preset vocab, "
"then including this use the char vocab provided here.",
type = str, default='')
parser.add_argument('--unkfilter', help="Ignore sentences with too many UNK tokens. "
"Can be an absolute count limit (if > 1) "
"or a proportional limit (0 < unkfilter < 1).",
type = float, default = 0)
parser.add_argument('--shuffle', help="If = 1, shuffle sentences before sorting (based on "
"source length).",
type = int, default = 0)
parser.add_argument('--keyword_file', help="Path to keyword_file.", default='')
args = parser.parse_args(arguments)
if args.keyword_file != '':
with open(args.keyword_file) as f:
words = f.readlines()
global keywords
keywords= {}
word_count = 1
for item in words:
witem = item.strip('\n')
if not witem in keywords:
keywords[witem] = word_count
word_count = word_count+1
print keywords
w = open(args.outputfile + ".keywords.dict", "w")
items = [(v, k) for k, v in keywords.iteritems()]
items.sort()
for v, k in items:
print >>w, k, v
w.close()
#w = open(args.outputfile + ".keywords.dict", "w")
#for key, val in keywords.items():
# w.write(key)
# w.write(' ')
# s = str(val)
# w.write(s)
# w.write('\n')
#debug
get_data(args)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))