forked from aceimnorstuvwxz/chatbot-zh-torch7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.lua
executable file
·98 lines (76 loc) · 2.1 KB
/
eval.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
require 'neuralconvo'
local tokenizer = require "tokenizer"
local list = require "pl.List"
local options = {}
if dataset == nil then
cmd = torch.CmdLine()
cmd:text('Options:')
cmd:option('--cuda', false, 'use CUDA. Training must be done on CUDA')
cmd:option('--opencl', false, 'use OpenCL. Training must be done on OpenCL')
cmd:option('--debug', false, 'show debug info')
cmd:text()
options = cmd:parse(arg)
-- Data
dataset = neuralconvo.DataSet()
-- Enabled CUDA
if options.cuda then
require 'cutorch'
require 'cunn'
elseif options.opencl then
require 'cltorch'
require 'clnn'
end
end
if model == nil then
print("-- Loading model")
model = torch.load("data/model.t7")
end
-- Word IDs to sentence
function pred2sent(wordIds, i)
local words = {}
i = i or 1
for _, wordId in ipairs(wordIds) do
local word = dataset.id2word[wordId[i]]
--print(wordId[i]..word)
table.insert(words, word)
end
return tokenizer.join(words)
end
function printProbabilityTable(wordIds, probabilities, num)
print(string.rep("-", num * 22))
for p, wordId in ipairs(wordIds) do
local line = "| "
for i = 1, num do
local word = dataset.id2word[wordId[i]]
line = line .. string.format("%-10s(%4d%%)", word, probabilities[p][i] * 100) .. " | "
end
print(line)
end
print(string.rep("-", num * 22))
end
function say(text)
local wordIds = {}
--print(text)
local values = {}
for w in text:gmatch("[\33-\127\192-\255]+[\128-\191]*") do
table.insert(values, w)
end
-- print(values)
for i, word in ipairs(values) do
local id = dataset.word2id[word] or dataset.unknownToken
-- print(i.." "..word.." "..id)
table.insert(wordIds, id)
end
--[[
for t, word in tokenizer.tokenize(text) do
local id = dataset.word2id[word:lower()] or dataset.unknownToken
table.insert(wordIds, id)
end
]]--
local input = torch.Tensor(list.reverse(wordIds))
local wordIds, probabilities = model:eval(input)
print(">> " .. pred2sent(wordIds))
if options.debug then
printProbabilityTable(wordIds, probabilities, 4)
end
end