-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c1b903
commit c213f48
Showing
3 changed files
with
59 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
require 'torch' | ||
require 'nn' | ||
require 'rnn' | ||
|
||
e = {} | ||
|
||
torch.include('e', 'cornell_movie_dialogs.lua') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'e' | ||
local tokenizer = require "tokenizer" | ||
|
||
dataset = e.DataSet("data/cornell_movie_dialogs.t7", | ||
e.CornellMovieDialogs("data/cornell_movie_dialogs")) | ||
|
||
EOS = dataset.word2id["</s>"] | ||
|
||
print("-- Loading model") | ||
model = torch.load("data/model.t7") | ||
|
||
function output2wordId(t) | ||
local max = t:max() | ||
for i = 1, t:size(1) do | ||
if t[i] == max then | ||
return i | ||
end | ||
end | ||
end | ||
|
||
function say(text) | ||
local inputs = {} | ||
for t, word in tokenizer.tokenize(text) do | ||
local t = dataset.word2id[word:lower()] | ||
table.insert(inputs, t) | ||
end | ||
|
||
model:forget() | ||
|
||
for i = #inputs, 1, -1 do | ||
local input = inputs[i] | ||
model:forward(torch.Tensor{input}) | ||
end | ||
|
||
local input = EOS | ||
repeat | ||
local output = model:forward(torch.Tensor{input}) | ||
io.write(dataset.id2word[output2wordId(output)] .. " ") | ||
input = output2wordId(output) | ||
until input == EOS | ||
|
||
print("") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters