-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_iterator.py
220 lines (183 loc) · 7.47 KB
/
data_iterator.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
import numpy
import gzip
import shuffle
from util import load_dict
def fopen(filename, mode='r'):
if filename.endswith('.gz'):
return gzip.open(filename, mode)
return open(filename, mode)
class FileWrapper(object):
def __init__(self, fname):
self.pos = 0
self.lines = fopen(fname).readlines()
self.lines = numpy.array(self.lines, dtype=numpy.object)
def __iter__(self):
return self
def next(self):
if self.pos >= len(self.lines):
raise StopIteration
l = self.lines[self.pos]
self.pos += 1
return l
def reset(self):
self.pos = 0
def seek(self, pos):
assert pos == 0
self.pos = 0
def readline(self):
return self.next()
def shuffle_lines(self, perm):
self.lines = self.lines[perm]
self.pos = 0
def __len__(self):
return len(self.lines)
class TextIterator:
"""Simple Bitext iterator."""
def __init__(self, source, target,
source_dicts, target_dict,
batch_size=128,
maxlen=100,
source_vocab_sizes=None,
target_vocab_size=None,
skip_empty=False,
shuffle_each_epoch=False,
sort_by_length=True,
use_factor=False,
maxibatch_size=20,
token_batch_size=0,
keep_data_in_memory=False):
if keep_data_in_memory:
self.source, self.target = FileWrapper(source), FileWrapper(target)
if shuffle_each_epoch:
r = numpy.random.permutation(len(self.source))
self.source.shuffle_lines(r)
self.target.shuffle_lines(r)
elif shuffle_each_epoch:
self.source_orig = source
self.target_orig = target
self.source, self.target = shuffle.main([self.source_orig, self.target_orig], temporary=True)
else:
self.source = fopen(source, 'r')
self.target = fopen(target, 'r')
self.source_dicts = []
for source_dict in source_dicts:
self.source_dicts.append(load_dict(source_dict))
self.target_dict = load_dict(target_dict)
self.keep_data_in_memory = keep_data_in_memory
self.batch_size = batch_size
self.maxlen = maxlen
self.skip_empty = skip_empty
self.use_factor = use_factor
self.source_vocab_sizes = source_vocab_sizes
self.target_vocab_size = target_vocab_size
self.token_batch_size = token_batch_size
if self.source_vocab_sizes != None:
assert len(self.source_vocab_sizes) == len(self.source_dicts)
for d, vocab_size in zip(self.source_dicts, self.source_vocab_sizes):
if vocab_size != None and vocab_size > 0:
for key, idx in d.items():
if idx >= vocab_size:
del d[key]
if self.target_vocab_size != None and self.target_vocab_size > 0:
for key, idx in self.target_dict.items():
if idx >= self.target_vocab_size:
del self.target_dict[key]
self.shuffle = shuffle_each_epoch
self.sort_by_length = sort_by_length
self.source_buffer = []
self.target_buffer = []
self.k = batch_size * maxibatch_size
self.end_of_data = False
def __iter__(self):
return self
def reset(self):
if self.shuffle:
if self.keep_data_in_memory:
r = numpy.random.permutation(len(self.source))
self.source.shuffle_lines(r)
self.target.shuffle_lines(r)
else:
self.source, self.target = shuffle.main([self.source_orig, self.target_orig], temporary=True)
else:
self.source.seek(0)
self.target.seek(0)
def next(self):
if self.end_of_data:
self.end_of_data = False
self.reset()
raise StopIteration
source = []
target = []
longest_source = 0
longest_target = 0
# fill buffer, if it's empty
assert len(self.source_buffer) == len(self.target_buffer), 'Buffer size mismatch!'
if len(self.source_buffer) == 0:
for ss in self.source:
ss = ss.split()
tt = self.target.readline().split()
if self.skip_empty and (len(ss) == 0 or len(tt) == 0):
continue
if len(ss) > self.maxlen or len(tt) > self.maxlen:
continue
self.source_buffer.append(ss)
self.target_buffer.append(tt)
if len(self.source_buffer) == self.k:
break
if len(self.source_buffer) == 0 or len(self.target_buffer) == 0:
self.end_of_data = False
self.reset()
raise StopIteration
# sort by source/target buffer length
if self.sort_by_length:
tlen = numpy.array([max(len(s),len(t)) for (s,t) in zip(self.source_buffer,self.target_buffer)])
tidx = tlen.argsort()
_sbuf = [self.source_buffer[i] for i in tidx]
_tbuf = [self.target_buffer[i] for i in tidx]
self.source_buffer = _sbuf
self.target_buffer = _tbuf
else:
self.source_buffer.reverse()
self.target_buffer.reverse()
try:
# actual work here
while True:
# read from source file and map to word index
try:
ss = self.source_buffer.pop()
except IndexError:
break
tmp = []
for w in ss:
if self.use_factor:
w = [self.source_dicts[i][f] if f in self.source_dicts[i] else 1 for (i,f) in enumerate(w.split('|'))]
else:
w = [self.source_dicts[0][w] if w in self.source_dicts[0] else 1]
tmp.append(w)
ss_indices = tmp
# read from source file and map to word index
tt = self.target_buffer.pop()
tt_indices = [self.target_dict[w] if w in self.target_dict else 1
for w in tt]
if self.target_vocab_size != None:
tt_indices = [w if w < self.target_vocab_size else 1 for w in tt_indices]
source.append(ss_indices)
target.append(tt_indices)
longest_source = max(longest_source, len(ss_indices))
longest_target = max(longest_target, len(tt_indices))
if self.token_batch_size:
if len(source)*longest_source > self.token_batch_size or \
len(target)*longest_target > self.token_batch_size:
# remove last sentence pair (that made batch over-long)
source.pop()
target.pop()
self.source_buffer.append(ss)
self.target_buffer.append(tt)
break
else:
if len(source) >= self.batch_size or \
len(target) >= self.batch_size:
break
except IOError:
self.end_of_data = True
return source, target