-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
334 lines (285 loc) · 14.4 KB
/
utils.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
# 2018 Su Wang ([email protected])
import os
import random
import torch
import numpy as np
from torch.autograd import Variable
class Indexer:
"""Word <-> Index mapper."""
def __init__(self, specialTokenList=None):
self.word2index = {}
self.index2word = {}
self.wordSet = set()
self.size = 0
if specialTokenList is not None:
assert type(specialTokenList)==list
for token in specialTokenList:
self.get_index(token, add=True)
def __repr__(self):
return "The indexer currently has %d words" % self.size
def get_word(self, index):
return self.index2word[index] if index<self.size else 'UNK'
def get_index(self, word, add=True):
if add and word not in self.wordSet:
self.word2index[word] = self.size
self.index2word[self.size] = word
self.wordSet.add(word)
self.size += 1
return self.word2index[word] if word in self.wordSet else self.word2index['UNK']
def contains(self, word):
return word in self.wordSet
def add_sentence(self, sentence, returnIndices=True):
indices = [self.get_index(word, add=True) for word in sentence.split()]
return (indices,len(indices)) if returnIndices else None
def add_document(self, docPath, returnIndices=True):
with open(docPath, 'r') as doc:
if returnIndices:
indicesList, lengthList = [], []
for line in doc:
indices,length = self.add_sentence(line,returnIndices)
if length<=0: continue # handle bad sentences in .txt.
indicesList.append(indices)
lengthList.append(length)
return indicesList, lengthList
else:
for line in doc:
self.add_sentence(line,returnIndices=False)
return None
def to_words(self, indices):
return [self.get_word(index) for index in indices]
def to_sent(self, indices):
return ' '.join(self.to_words(indices))
def to_indices(self, words):
return [self.get_index(word) for word in words]
class IndexerTransformer:
"""Word <-> Index mapper."""
def __init__(self):
self.word2index = {}
self.index2word = {}
self.wordSet = set()
self.size = 0
def __repr__(self):
return "The indexer currently has %d words" % self.size
def get_word(self, index):
return self.index2word[index] if index<self.size else 'UNK'
def get_index(self, word, add=True):
if add and word not in self.wordSet:
self.word2index[word] = self.size+1 # [CHANGE] spare 0 for masking
self.index2word[self.size+1] = word # [CHANGE] same
self.wordSet.add(word)
self.size += 1
return self.word2index[word] if word in self.wordSet else self.word2index['UNK']
def contains(self, word):
return word in self.wordSet
def add_sentence(self, sentence, returnIndices=True):
indices = [self.get_index(word, add=True) for word in sentence.split()]
return (indices,len(indices)) if returnIndices else None
def add_document(self, docPath, returnIndices=True):
with open(docPath, 'r') as doc:
if returnIndices:
indicesList, lengthList = [], []
for line in doc:
indices,length = self.add_sentence(line,returnIndices)
if length<=0: continue # handle bad sentences in .txt.
indicesList.append(indices)
lengthList.append(length)
return indicesList, lengthList
else:
for line in doc:
self.add_sentence(line,returnIndices=False)
return None
def to_words(self, indices):
return [self.get_word(index) for index in indices]
def to_sent(self, indices):
return ' '.join(self.to_words(indices))
def to_indices(self, words):
return [self.get_index(word) for word in words]
class VocabControlIndexer(Indexer):
def __init__(self, specialTokenList=None, wordSet=None):
Indexer.__init__(self, specialTokenList=None)
self.wordSet = wordSet
specialTokenList = [] if specialTokenList is None else list(set(specialTokenList).union(set(['UNK'])))
for i,word in enumerate(specialTokenList+list(wordSet)):
self.word2index[word] = i
self.index2word[i] = word
self.wordSet.update(set(specialTokenList))
self.size = len(specialTokenList) + len(wordSet)
def get_index(self, word, add=True):
if add and word not in self.wordSet:
return self.word2index['UNK']
return self.word2index[word]
class DataLoader:
def __init__(self, dataDir):
self.dataDir = dataDir # str
self.dataDict = {path.split('.')[0]:self.dataDir+path
for path in os.listdir(self.dataDir) if path.endswith('.txt')}
self.filenames = set(['train_source', 'train_target',
'test_source', 'test_target'])
if self._filename_mismatch():
raise Exception("Expected filenames under the directory:\n"+str(self.filenames)+
'\nGot:\n'+str(self.dataDict.keys())+'\n')
def _filename_mismatch(self):
return self.filenames - set(self.dataDict.keys()) != set([])
class DataIterator:
"""Data feeder by batch."""
def __init__(self, indexer, pairs, lengths):
"""
Args:
indexer: an Indexer object.
pairs: a list of pairs of token index lists.
lengths: a list of pairs of sentence length lists.
"""
self.indexer = indexer
self.pairs = pairs
self.lengths = lengths
self.size = len(pairs)
self.indices = range(self.size)
def _get_padded_sentence(self, index, maxSentLen, maxTargetLen):
"""Pad a sentence pair by EOS (pad both to the largest length of respective batch).
Args:
index: index of a sentence & length pair in self.pairs, self.lengths.
maxSentLen: the length of the longest source sentence.
maxTargetLen: the length of the longest target sentence.
Returns:
padded source sentence (list), its length (int),
padded target sentence (list), its length (int).
"""
sent1,sent2 = self.pairs[index][0], self.pairs[index][1]
length1,length2 = self.lengths[index][0], self.lengths[index][1]
paddedSent1 = sent1[:maxSentLen] if length1>maxSentLen else sent1+[self.indexer.get_index('EOS')]*(maxSentLen-length1)
paddedSent2 = sent2[:maxTargetLen] if length2>maxTargetLen else sent2+[self.indexer.get_index('EOS')]*(maxTargetLen-length2)
return paddedSent1,length1,paddedSent2,length2
def random_batch(self, batchSize):
"""Random batching.
Args:
batchSize: size of a batch of sentence pairs and respective lengths.
Returns:
the batch of source sentence (Variable(torch.LongTensor())),
the lengths of source sentences (numpy.array())
and the same for target sentences and lengths.
"""
batchIndices = np.random.choice(self.indices, size=batchSize, replace=False)
batchSents,batchTargets,batchSentLens,batchTargetLens = [], [], [], []
maxSentLen, maxTargetLen = np.array([self.lengths[index] for index in batchIndices]).max(axis=0)
for index in batchIndices:
paddedSent1,length1,paddedSent2,length2 = self._get_padded_sentence(index, maxSentLen, maxTargetLen)
batchSents.append(paddedSent1)
batchTargets.append(paddedSent2)
batchSentLens.append(length1)
batchTargetLens.append(length2)
batchIndices = range(batchSize) # reindex from 0 for sorting.
batchIndices = [i for i,l in sorted(zip(batchIndices,batchSentLens),key=lambda p:p[1],reverse=True)]
batchSents = Variable(torch.LongTensor(np.array(batchSents)[batchIndices])).transpose(0,1) # <bc,mt> -> <mt,bc>
batchTargets = Variable(torch.LongTensor(np.array(batchTargets)[batchIndices])).transpose(0,1)
batchSentLens = np.array(batchSentLens)[batchIndices]
batchTargetLens = np.array(batchTargetLens)[batchIndices]
return batchSents, batchSentLens, batchTargets, batchTargetLens
class DataIteratorTransformer:
"""Data feeder by batch."""
def __init__(self, indexer, pairs, lengths):
"""
Args:
indexer: an Indexer object.
pairs: a list of pairs of token index lists.
lengths: a list of pairs of sentence length lists.
"""
self.indexer = indexer
self.pairs = pairs
self.lengths = lengths
self.size = len(pairs)
self.indices = range(self.size)
def _get_padded_sentence(self, index, maxSentLen, maxTargetLen):
"""Pad a sentence pair by EOS (pad both to the largest length of respective batch).
Args:
index: index of a sentence & length pair in self.pairs, self.lengths.
maxSentLen: the length of the longest source sentence.
maxTargetLen: the length of the longest target sentence.
Returns:
padded source sentence (list), its length (int),
padded target sentence (list), its length (int).
"""
sent1,sent2 = self.pairs[index][0], self.pairs[index][1]
length1,length2 = self.lengths[index][0], self.lengths[index][1]
paddedSent1 = sent1[:maxSentLen] if length1>maxSentLen else sent1+[0]*(maxSentLen-length1) # [CHANGE] 0 is padding token.
paddedSent2 = sent2[:maxTargetLen] if length2>maxTargetLen else sent2+[0]*(maxTargetLen-length2)
return paddedSent1,length1,paddedSent2,length2
def random_batch(self, batchSize):
"""Random batching.
Args:
batchSize: size of a batch of sentence pairs and respective lengths.
Returns:
the batch of source sentence (Variable(torch.LongTensor())),
the lengths of source sentences (numpy.array())
and the same for target sentences and lengths.
"""
batchIndices = np.random.choice(self.indices, size=batchSize, replace=False)
batchSents,batchTargets,batchSentLens,batchTargetLens = [], [], [], []
maxSentLen, maxTargetLen = np.array([self.lengths[index] for index in batchIndices]).max(axis=0)
for index in batchIndices:
paddedSent1,length1,paddedSent2,length2 = self._get_padded_sentence(index, maxSentLen, maxTargetLen)
batchSents.append(paddedSent1)
batchTargets.append(paddedSent2)
batchSentLens.append(length1)
batchTargetLens.append(length2)
batchIndices = range(batchSize) # reindex from 0 for sorting.
batchIndices = [i for i,l in sorted(zip(batchIndices,batchSentLens),key=lambda p:p[1],reverse=True)]
batchSents = Variable(torch.LongTensor(np.array(batchSents)[batchIndices])) # [CHANGE] <bc,mt> rather than <mt,bc>
batchTargets = Variable(torch.LongTensor(np.array(batchTargets)[batchIndices])) # [CHANGE] same
return batchSents, batchTargets
class BilingualDataIterator:
"""Data feeder by batch, for bilingual data."""
def __init__(self, indexerLang1, indexerLang2, pairs, lengths, maxTargetLen=50):
"""
Args:
indexerLang1, indexerLang2: a pair of Indexer objects.
pairs: a list of pairs of token index lists.
lengths: a list of pairs of sentence length lists.
maxTargetLen: uniform to decoding length later (for cross entropy computing).
"""
self.indexerLang1 = indexerLang1
self.indexerLang2 = indexerLang2
self.pairs = pairs
self.lengths = lengths
self.maxTargetLen = maxTargetLen
self.size = len(pairs)
self.indices = range(self.size)
def _get_padded_sentence(self, index, maxSentLen, maxTargetLen):
"""Pad a sentence pair by EOS (pad both to the largest length of respective batch).
Args:
index: index of a sentence & length pair in self.pairs, self.lengths.
maxSentLen: the length of the longest source sentence.
maxTargetLen: the length of the longest target sentence.
Returns:
padded source sentence (list), its length (int),
padded target sentence (list), its length (int).
"""
sent1,sent2 = self.pairs[index][0], self.pairs[index][1]
length1,length2 = self.lengths[index][0], self.lengths[index][1]
paddedSent1 = sent1[:maxSentLen] if length1>maxSentLen else sent1+[self.indexerLang1.get_index('EOS')]*(maxSentLen-length1)
paddedSent2 = sent2[:maxTargetLen] if length2>maxTargetLen else sent2+[self.indexerLang2.get_index('EOS')]*(maxTargetLen-length2)
return paddedSent1,length1,paddedSent2,length2
def random_batch(self, batchSize):
"""Random batching.
Args:
batchSize: size of a batch of sentence pairs and respective lengths.
Returns:
the batch of source sentence (Variable(torch.LongTensor())),
the lengths of source sentences (numpy.array())
and the same for target sentences and lengths.
"""
batchIndices = np.random.choice(self.indices, size=batchSize, replace=False)
batchSents,batchTargets,batchSentLens,batchTargetLens = [], [], [], []
maxSentLen, _ = np.array([self.lengths[index] for index in batchIndices]).max(axis=0)
for index in batchIndices:
paddedSent1,length1,paddedSent2,length2 = self._get_padded_sentence(index, maxSentLen, self.maxTargetLen)
batchSents.append(paddedSent1)
batchTargets.append(paddedSent2)
batchSentLens.append(length1)
batchTargetLens.append(length2)
batchIndices = range(batchSize) # reindex from 0 for sorting.
batchIndices = [i for i,l in sorted(zip(batchIndices,batchSentLens),key=lambda p:p[1],reverse=True)]
batchSents = torch.LongTensor(np.array(batchSents)[batchIndices]).transpose(0,1) # <bc,mt> -> <mt,bc>
batchTargets = torch.LongTensor(np.array(batchTargets)[batchIndices]).transpose(0,1)
batchSentLens = np.array(batchSentLens)[batchIndices]
batchTargetLens = np.array(batchTargetLens)[batchIndices]
return batchSents, batchSentLens, batchTargets, batchTargetLens