-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess.py
executable file
·367 lines (336 loc) · 15.8 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Create data files
"""
import os
import sys
import argparse
import numpy as np
import pickle
import itertools
from collections import defaultdict
import utils
import re
class Indexer:
def __init__(self, symbols = ["<pad>","<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: 0, self.UNK: 1, self.BOS: 2, self.EOS: 3}
self.idx2word = {}
def add_w(self, ws):
for w in ws:
if w not in self.d:
self.d[w] = len(self.d)
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 write(self, outfile):
out = open(outfile, "w")
items = [(v, k) for k, v in self.d.items()]
items.sort()
for v, k in items:
out.write(" ".join([k, str(v)]) + "\n")
out.close()
def prune_vocab(self, k, cnt = False):
vocab_list = [(word, count) for word, count in self.vocab.items()]
if cnt:
self.pruned_vocab = {pair[0]:pair[1] for pair in vocab_list if pair[1] > k}
else:
vocab_list.sort(key = lambda x: x[1], reverse=True)
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)
for word, idx in self.d.items():
self.idx2word[idx] = word
def load_vocab(self, vocab_file):
self.d = {}
for line in open(vocab_file, 'r'):
v, k = line.strip().split()
self.d[v] = int(k)
for word, idx in self.d.items():
self.idx2word[idx] = word
def is_next_open_bracket(line, start_idx):
for char in line[(start_idx + 1):]:
if char == '(':
return True
elif char == ')':
return False
raise IndexError('Bracket possibly not balanced, open bracket not followed by closed bracket')
def get_between_brackets(line, start_idx):
output = []
for char in line[(start_idx + 1):]:
if char == ')':
break
assert not(char == '(')
output.append(char)
return ''.join(output)
def get_tags_tokens_lowercase(line):
output = []
line_strip = line.rstrip()
for i in range(len(line_strip)):
if i == 0:
assert line_strip[i] == '('
if line_strip[i] == '(' and not(is_next_open_bracket(line_strip, i)): # fulfilling this condition means this is a terminal symbol
output.append(get_between_brackets(line_strip, i))
#print 'output:',output
output_tags = []
output_tokens = []
output_lowercase = []
for terminal in output:
terminal_split = terminal.split()
# print(terminal, terminal_split)
assert len(terminal_split) == 2, (terminal_split, output) # each terminal contains a POS tag and word
output_tags.append(terminal_split[0])
output_tokens.append(terminal_split[1])
output_lowercase.append(terminal_split[1].lower())
return [output_tags, output_tokens, output_lowercase]
def get_nonterminal(line, start_idx):
assert line[start_idx] == '(' # make sure it's an open bracket
output = []
for char in line[(start_idx + 1):]:
if char == ' ':
break
assert not(char == '(') and not(char == ')')
output.append(char)
return ''.join(output)
def get_actions(line):
output_actions = []
line_strip = line.rstrip()
i = 0
max_idx = (len(line_strip) - 1)
while i <= max_idx:
assert line_strip[i] == '(' or line_strip[i] == ')'
if line_strip[i] == '(':
if is_next_open_bracket(line_strip, i): # open non-terminal
curr_NT = get_nonterminal(line_strip, i)
output_actions.append('NT(' + curr_NT + ')')
i += 1
while line_strip[i] != '(': # get the next open bracket, which may be a terminal or another non-terminal
i += 1
else: # it's a terminal symbol
output_actions.append('SHIFT')
while line_strip[i] != ')':
i += 1
i += 1
while line_strip[i] != ')' and line_strip[i] != '(':
i += 1
else:
output_actions.append('REDUCE')
if i == max_idx:
break
i += 1
while line_strip[i] != ')' and line_strip[i] != '(':
i += 1
assert i == max_idx
return output_actions
def pad(ls, length, symbol):
if len(ls) >= length:
return ls[:length]
return ls + [symbol] * (length -len(ls))
def clean_number(w):
new_w = re.sub('[0-9]{1,}([,.]?[0-9]*)*', 'N', w)
return new_w
def get_data(args):
indexer = Indexer(["<pad>","<unk>","<s>","</s>"])
def make_vocab(textfile, seqlength, minseqlength, lowercase, replace_num,
train=1, apply_length_filter=1):
num_sents = 0
max_seqlength = 0
for tree in open(textfile, 'r'):
tree = tree.strip()
try:
tags, sent, sent_lower = get_tags_tokens_lowercase(tree)
except:
print(tree)
assert(len(tags) == len(sent))
if lowercase == 1:
sent = sent_lower
if replace_num == 1:
sent = [clean_number(w) for w in sent]
if (len(sent) > seqlength and apply_length_filter == 1) or len(sent) < minseqlength:
continue
num_sents += 1
max_seqlength = max(max_seqlength, len(sent))
if train == 1:
for word in sent:
indexer.vocab[word] += 1
return num_sents, max_seqlength
def convert(textfile, lowercase, replace_num,
batchsize, seqlength, minseqlength, outfile, num_sents, max_sent_l=0,
shuffle=0, include_boundary=1, apply_length_filter=1, conllfile=""):
newseqlength = seqlength
if include_boundary == 1:
newseqlength += 2 #add 2 for EOS and BOS
sents = np.zeros((num_sents, newseqlength), dtype=int)
sent_lengths = np.zeros((num_sents,), dtype=int)
dropped = 0
sent_id = 0
other_data = []
if(conllfile != ""):
deptrees = utils.read_conll(open(conllfile, "r"))
for tree in open(textfile, 'r'):
tree = tree.strip()
action = get_actions(tree)
tags, sent, sent_lower = get_tags_tokens_lowercase(tree)
assert(len(tags) == len(sent))
if(conllfile != ""):
words, heads = next(deptrees)
if words != sent:
print("Data mismatch, got {} in {}, but {} in {}.".format(sent, textfile, words, conllfile))
assert(len(words) == len(heads))
assert(len(heads) == len(sent))
if lowercase == 1:
sent = sent_lower
sent_str = " ".join(sent)
if replace_num == 1:
sent = [clean_number(w) for w in sent]
if (len(sent) > seqlength and apply_length_filter == 1) or len(sent) < minseqlength:
continue
if include_boundary == 1:
sent = [indexer.BOS] + sent + [indexer.EOS]
max_sent_l = max(len(sent), max_sent_l)
sent_pad = pad(sent, newseqlength, indexer.PAD)
sents[sent_id] = np.array(indexer.convert_sequence(sent_pad), dtype=int)
sent_lengths[sent_id] = (sents[sent_id] != 0).sum()
span, binary_actions, nonbinary_actions = utils.get_nonbinary_spans(action)
other_data_item = [sent_str, tags, action,
binary_actions, nonbinary_actions, span, tree]
if(conllfile != ""):
other_data_item.append(heads)
other_data.append(other_data_item)
assert(2*(len(sent)- 2) - 1 == len(binary_actions))
assert(sum(binary_actions) + 1 == len(sent) - 2)
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)
sents = sents[rand_idx]
sent_lengths = sent_lengths[rand_idx]
other_data = [other_data[idx] for idx in rand_idx]
print(len(sents), len(other_data))
#break up batches based on source lengths
sent_lengths = sent_lengths[:sent_id]
sent_sort = np.argsort(sent_lengths)
sents = sents[sent_sort]
other_data = [other_data[idx] for idx in sent_sort]
sent_l = sent_lengths[sent_sort]
curr_l = 1
l_location = [] #idx where sent length changes
for j,i in enumerate(sent_sort):
if sent_lengths[i] > curr_l:
curr_l = sent_lengths[i]
l_location.append(j)
l_location.append(len(sents))
#get batch sizes
curr_idx = 0
batch_idx = [0]
nonzeros = []
batch_l = []
batch_w = []
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(sent_l[batch_idx[i]])
# Write output
f = {}
f["source"] = sents
f["other_data"] = other_data
f["batch_l"] = np.array(batch_l, dtype=int)
f["source_l"] = np.array(batch_w, dtype=int)
f["sents_l"] = np.array(sent_l, dtype = int)
f["batch_idx"] = np.array(batch_idx[:-1], dtype=int)
f["vocab_size"] = np.array([len(indexer.d)])
f["idx2word"] = indexer.idx2word
f["word2idx"] = {word : idx for idx, word in indexer.idx2word.items()}
print("Saved {} sentences (dropped {} due to length/unk filter)".format(
len(f["source"]), dropped))
pickle.dump(f, open(outfile, 'wb'))
return max_sent_l
print("First pass through data to get vocab...")
num_sents_train, train_seqlength = make_vocab(args.trainfile, args.seqlength, args.minseqlength,
args.lowercase, args.replace_num, 1, 1)
print("Number of sentences in training: {}".format(num_sents_train))
num_sents_valid, valid_seqlength = make_vocab(args.valfile, args.seqlength, args.minseqlength,
args.lowercase, args.replace_num, 0, 0)
print("Number of sentences in valid: {}".format(num_sents_valid))
num_sents_test, test_seqlength = make_vocab(args.testfile, args.seqlength, args.minseqlength,
args.lowercase, args.replace_num, 0, 0)
print("Number of sentences in test: {}".format(num_sents_test))
if args.vocabminfreq >= 0:
indexer.prune_vocab(args.vocabminfreq, True)
else:
indexer.prune_vocab(args.vocabsize, False)
if args.vocabfile != '':
print('Loading pre-specified source vocab from ' + args.vocabfile)
indexer.load_vocab(args.vocabfile)
indexer.write(args.outputfile + ".dict")
print("Vocab size: Original = {}, Pruned = {}".format(len(indexer.vocab),
len(indexer.d)))
print(train_seqlength, valid_seqlength, test_seqlength)
max_sent_l = 0
max_sent_l = convert(args.testfile, args.lowercase, args.replace_num,
args.batchsize, test_seqlength, args.minseqlength,
args.outputfile + "-test.pkl", num_sents_test,
max_sent_l, args.shuffle, args.include_boundary, 0,
conllfile=os.path.splitext(args.testfile)[0] + ".conllx" if args.dep else "")
max_sent_l = convert(args.valfile, args.lowercase, args.replace_num,
args.batchsize, valid_seqlength, args.minseqlength,
args.outputfile + "-val.pkl", num_sents_valid,
max_sent_l, args.shuffle, args.include_boundary, 0,
conllfile=os.path.splitext(args.valfile)[0] + ".conllx" if args.dep else "")
max_sent_l = convert(args.trainfile, args.lowercase, args.replace_num,
args.batchsize, args.seqlength, args.minseqlength,
args.outputfile + "-train.pkl", num_sents_train,
max_sent_l, args.shuffle, args.include_boundary, 1,
conllfile=os.path.splitext(args.trainfile)[0] + ".conllx" if args.dep else "")
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('--vocabsize', help="Size of source vocabulary, constructed "
"by taking the top X most frequent words. "
" Rest are replaced with special UNK tokens.",
type=int, default=10000)
parser.add_argument('--vocabminfreq', help="Minimum frequency for vocab. Use this instead of "
"vocabsize if > 0",
type=int, default=-1)
parser.add_argument('--include_boundary', help="Add BOS/EOS tokens", type=int, default=1)
parser.add_argument('--lowercase', help="Lower case", type=int, default=1)
parser.add_argument('--replace_num', help="Replace numbers with N", type=int, default=1)
parser.add_argument('--trainfile', help="Path to training data.", required=True)
parser.add_argument('--valfile', help="Path to validation data.", required=True)
parser.add_argument('--testfile', help="Path to test validation data.", required=True)
parser.add_argument('--batchsize', help="Size of each minibatch.", type=int, default=4)
parser.add_argument('--seqlength', help="Maximum sequence length. Sequences longer "
"than this are dropped.", type=int, default=150)
parser.add_argument('--minseqlength', help="Minimum sequence length. Sequences shorter "
"than this are dropped.", type=int, default=0)
parser.add_argument('--outputfile', help="Prefix of the output file names. ", type=str,
required=True)
parser.add_argument('--vocabfile', 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('--shuffle', help="If = 1, shuffle sentences before sorting (based on "
"source length).",
type = int, default = 0)
parser.add_argument('--dep', action="store_true", help="Including dependency parse files. Their "
"names should be same as data file, but extensions "
"are .conllx.")
args = parser.parse_args(arguments)
np.random.seed(3435)
get_data(args)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))