forked from macournoyer/neuralconvo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.lua
351 lines (287 loc) · 9.33 KB
/
dataset.lua
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
--[[
Format movie dialog data as a table of line 1:
{ {word_ids of character1}, {word_ids of character2} }
Then flips it around and get the dialog from the other character's perspective:
{ {word_ids of character2}, {word_ids of character1} }
Also builds the vocabulary.
]]--
local DataSet = torch.class("neuralconvo.DataSet")
local xlua = require "xlua"
local tokenizer = require "tokenizer"
local list = require "pl.List"
local utils = require "pl.utils"
local function_arg = utils.function_arg
function DataSet:__init(loader, options)
options = options or {}
self.examplesFilename = "data/examples.t7"
self.createNewVocabAndExamples = options.createNewVocabAndExamples
-- Reject words once vocab size reaches this threshold
self.maxVocabSize = options.maxVocabSize or 0
-- Maximum number of words in an example sentence
self.maxExampleLen = options.maxExampleLen or 25
-- Load only first fews examples (approximately)
self.loadFirst = options.loadFirst
self.examples = {}
self.word2id = {}
self.id2word = {}
self.wordsCount = 0
self:load(loader)
end
function DataSet:buildVocab(conversations)
print("-- Building vocab")
-- Add magic tokens
self.goToken = self:makeWordId("<go>") -- Start of sequence
self.eosToken = self:makeWordId("<eos>") -- End of sequence
self.unknownToken = self:makeWordId("<unknown>") -- Word dropped from vocabulary
self.wordFreqs = {}
-- number of conversations to be traversed
local total = self.loadFirst or #conversations
-- traverse all the conversations to count the frequency of words
for i, conversation in ipairs(conversations) do
if i > total then break end
for j = 1, #conversation do
local conversationLine = conversation[j]
-- accumulate the word frequency
self:countWords(conversationLine.text)
end
if i % 1000 == 0 then
xlua.progress(i,total)
end
end
-- sort the words on their frequencies
local sortedCounts = f_sortv(self.wordFreqs,function(x,y) return x>y end)
for word,freq in sortedCounts do
nWordId = self:addWordToVocab(word)
if self.maxVocabSize > 0 and nWordId >= self.maxVocabSize then
break
end
end
print("-- Vocab built")
end
function DataSet:load(loader)
local filename = "data/vocab.t7"
if not self.createNewVocabAndExamples and path.exists(filename) then
print("Loading vocabulary from " .. filename .. " ...")
local data = torch.load(filename)
self.word2id = data.word2id
self.id2word = data.id2word
self.wordsCount = data.wordsCount
self.goToken = data.goToken
self.eosToken = data.eosToken
self.unknownToken = data.unknownToken
self.examplesCount = data.examplesCount
else
print("" .. filename .. " not found")
local conversations = loader:load()
self:buildVocab(conversations)
self:visit(conversations)
print("Writing " .. filename .. " ...")
torch.save(filename, {
word2id = self.word2id,
id2word = self.id2word,
wordsCount = self.wordsCount,
goToken = self.goToken,
eosToken = self.eosToken,
unknownToken = self.unknownToken,
examplesCount = self.examplesCount
})
end
end
function DataSet:visit(conversations)
self.examples = {}
print("-- Pre-processing data")
local total = self.loadFirst or #conversations * 2
for i, conversation in ipairs(conversations) do
if i > total then break end
self:visitConversation(conversation)
xlua.progress(i, total)
end
-- Revisit from the perspective of 2nd character
for i, conversation in ipairs(conversations) do
if #conversations + i > total then break end
self:visitConversation(conversation, 2)
xlua.progress(#conversations + i, total)
end
print("-- Shuffling ")
newIdxs = torch.randperm(#self.examples)
local sExamples = {}
for i, sample in ipairs(self.examples) do
sExamples[i] = self.examples[newIdxs[i]]
end
self.examples = sExamples
self.examplesCount = #self.examples
self:writeExamplesToFile()
self.examples = nil
collectgarbage()
end
function DataSet:writeExamplesToFile()
print("Writing " .. self.examplesFilename .. " ...")
local file = torch.DiskFile(self.examplesFilename, "w")
for i, example in ipairs(self.examples) do
file:writeObject(example)
xlua.progress(i, #self.examples)
end
file:close()
end
function DataSet:batches(size)
local file = torch.DiskFile(self.examplesFilename, "r")
file:quiet()
local done = false
return function()
if done then
return
end
local inputSeqs,targetSeqs = {},{}
local maxInputSeqLen,maxTargetOutputSeqLen = 0,0
for i = 1, size do
local example = file:readObject()
if example == nil then
done = true
file:close()
return examples
end
inputSeq,targetSeq = unpack(example)
if inputSeq:size(1) > maxInputSeqLen then
maxInputSeqLen = inputSeq:size(1)
end
if targetSeq:size(1) > maxTargetOutputSeqLen then
maxTargetOutputSeqLen = targetSeq:size(1)
end
table.insert(inputSeqs, inputSeq)
table.insert(targetSeqs, targetSeq)
end
local encoderInputs,decoderInputs,decoderTargets = nil,nil,nil
if size == 1 then
encoderInputs = torch.IntTensor(maxInputSeqLen):fill(0)
decoderInputs = torch.IntTensor(maxTargetOutputSeqLen-1):fill(0)
decoderTargets = torch.IntTensor(maxTargetOutputSeqLen-1):fill(0)
else
encoderInputs = torch.IntTensor(maxInputSeqLen,size):fill(0)
decoderInputs = torch.IntTensor(maxTargetOutputSeqLen-1,size):fill(0)
decoderTargets = torch.IntTensor(maxTargetOutputSeqLen-1,size):fill(0)
end
for samplenb = 1, #inputSeqs do
for word = 1,inputSeqs[samplenb]:size(1) do
eosOffset = maxInputSeqLen - inputSeqs[samplenb]:size(1) -- for left padding
if size == 1 then
encoderInputs[word] = inputSeqs[samplenb][word]
else
encoderInputs[word+eosOffset][samplenb] = inputSeqs[samplenb][word]
end
end
end
for samplenb = 1, #targetSeqs do
trimmedEosToken = targetSeqs[samplenb]:sub(1,-2)
for word = 1, trimmedEosToken:size(1) do
if size == 1 then
decoderInputs[word] = trimmedEosToken[word]
else
decoderInputs[word][samplenb] = trimmedEosToken[word]
end
end
end
for samplenb = 1, #targetSeqs do
trimmedGoToken = targetSeqs[samplenb]:sub(2,-1)
for word = 1, trimmedGoToken:size(1) do
if size == 1 then
decoderTargets[word] = trimmedGoToken[word]
else
decoderTargets[word][samplenb] = trimmedGoToken[word]
end
end
end
return encoderInputs,decoderInputs,decoderTargets
end
end
function DataSet:visitConversation(lines, start)
start = start or 1
for i = start, #lines, 2 do
local input = lines[i]
local target = lines[i+1]
if target then
local inputIds = self:visitText(input.text)
local targetIds = self:visitText(target.text, 2)
if inputIds and targetIds then
-- Revert inputs
inputIds = list.reverse(inputIds)
table.insert(targetIds, 1, self.goToken)
table.insert(targetIds, self.eosToken)
table.insert(self.examples, { torch.IntTensor(inputIds), torch.IntTensor(targetIds) })
end
end
end
end
function DataSet:visitText(text, additionalTokens)
local words = {}
additionalTokens = additionalTokens or 0
if text == "" then
return
end
for t, word in tokenizer.tokenize(text) do
local cWord = self.word2id[word:lower()]
if not cWord then
cWord = self.unknownToken
end
table.insert(words, cWord)
-- Only keep the first sentence
if t == "endpunct" or #words >= self.maxExampleLen - additionalTokens then
break
end
end
if #words == 0 then
return
end
return words
end
function DataSet:countWords(sentence)
--if text == "" then
-- return
--end
for t, word in tokenizer.tokenize(sentence) do
local lword = word:lower()
if self.wordFreqs[lword] == nil then
self.wordFreqs[lword] = 0
end
self.wordFreqs[lword] = self.wordFreqs[lword] + 1
end
end
function DataSet:makeWordId(word)
if self.maxVocabSize > 0 and self.wordsCount >= self.maxVocabSize then
-- We've reached the maximum size for the vocab. Replace w/ unknown token
return self.unknownToken
end
word = word:lower()
local id = self.word2id[word]
if not id then
self.wordsCount = self.wordsCount + 1
id = self.wordsCount
self.id2word[id] = word
self.word2id[word] = id
end
return id
end
function DataSet:addWordToVocab(word)
word = word:lower()
self.wordsCount = self.wordsCount + 1
self.word2id[word] = self.wordsCount
self.id2word[self.wordsCount] = word
return self.wordsCount
end
-- penlight from luarocks is outdated.. below fixed version for sortv
--- return an iterator to a table sorted by its values
-- @within Iterating
-- @tab t the table
-- @func f an optional comparison function (f(x,y) is true if x < y)
-- @usage for k,v in tablex.sortv(t) do print(k,v) end
-- @return an iterator to traverse elements sorted by the values
function f_sortv(t,f)
f = function_arg(2, f or '<')
local keys = {}
for k in pairs(t) do keys[#keys + 1] = k end
table.sort(keys,function(x, y) return f(t[x], t[y]) end)
local i = 0
return function()
i = i + 1
return keys[i], t[keys[i]]
end
end