forked from OpenNMT/OpenNMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.lua
208 lines (170 loc) · 5.39 KB
/
translate.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
require('onmt.init')
local cmd = onmt.utils.ExtendedCmdLine.new('translate.lua')
local options = {
{
'-src', '',
[[Source sequences to translate.]],
{
valid = onmt.utils.ExtendedCmdLine.fileExists
}
},
{
'-tgt', '',
[[Optional true target sequences.]]
},
{
'-output', 'pred.txt',
[[Output file.]]
},
{
'-batch_size', 30,
[[Batch size.]],
{
valid = onmt.utils.ExtendedCmdLine.isInt(1)
}
},
{
'-idx_files', false,
[[If set, source and target files are 'key value' with key match between source and target.]]
}
}
cmd:setCmdLineOptions(options, 'Data')
onmt.translate.Translator.declareOpts(cmd)
onmt.utils.Cuda.declareOpts(cmd)
onmt.utils.Logger.declareOpts(cmd)
cmd:text('')
cmd:text('Other options')
cmd:text('')
cmd:option('-time', false, [[Measure average translation time.]])
local function reportScore(name, scoreTotal, wordsTotal)
_G.logger:info(name .. " AVG SCORE: %.2f, " .. name .. " PPL: %.2f",
scoreTotal / wordsTotal,
math.exp(-scoreTotal/wordsTotal))
end
local function main()
local opt = cmd:parse(arg)
_G.logger = onmt.utils.Logger.new(opt.log_file, opt.disable_logs, opt.log_level)
_G.profiler = onmt.utils.Profiler.new()
onmt.utils.Cuda.init(opt)
local translator = onmt.translate.Translator.new(opt)
local srcReader = onmt.utils.FileReader.new(opt.src, opt.idx_files, translator:srcFeat())
local srcBatch = {}
local srcIdBatch = {}
local goldReader
local goldBatch
local withGoldScore = opt.tgt:len() > 0
if withGoldScore then
goldReader = onmt.utils.FileReader.new(opt.tgt, opt.idx_files)
goldBatch = {}
end
local outFile = io.open(opt.output, 'w')
local sentId = 1
local batchId = 1
local predScoreTotal = 0
local predWordsTotal = 0
local goldScoreTotal = 0
local goldWordsTotal = 0
local timer
if opt.time then
timer = torch.Timer()
timer:stop()
timer:reset()
end
while true do
local srcSeq, srcSeqId = srcReader:next()
local goldOutputSeq
if withGoldScore then
goldOutputSeq = goldReader:next()
end
if srcSeq ~= nil then
table.insert(srcBatch, translator:buildInput(srcSeq))
table.insert(srcIdBatch, srcSeqId)
if withGoldScore then
table.insert(goldBatch, translator:buildInputGold(goldOutputSeq))
end
elseif #srcBatch == 0 then
break
end
if srcSeq == nil or #srcBatch == opt.batch_size then
if opt.time then
timer:resume()
end
local results = translator:translate(srcBatch, goldBatch)
if opt.time then
timer:stop()
end
for b = 1, #results do
if (srcBatch[b].words and #srcBatch[b].words == 0) then
_G.logger:warning('Line ' .. sentId .. ' is empty.')
outFile:write('\n')
else
if srcBatch[b].words then
_G.logger:info('SENT %d: %s', sentId, translator:buildOutput(srcBatch[b]))
else
_G.logger:info('FEATS %d: IDX - %s - SIZE %d', sentId, srcIdBatch[b], srcBatch[b].vectors:size(1))
end
if withGoldScore then
_G.logger:info('GOLD %d: %s', sentId, translator:buildOutput(goldBatch[b]), results[b].goldScore)
_G.logger:info("GOLD SCORE: %.2f", results[b].goldScore)
goldScoreTotal = goldScoreTotal + results[b].goldScore
goldWordsTotal = goldWordsTotal + #goldBatch[b].words
end
if opt.dump_input_encoding then
outFile:write(sentId, ' ', table.concat(torch.totable(results[b]), " "), '\n')
else
for n = 1, #results[b].preds do
local sentence = translator:buildOutput(results[b].preds[n])
outFile:write(sentence .. '\n')
if n == 1 then
predScoreTotal = predScoreTotal + results[b].preds[n].score
predWordsTotal = predWordsTotal + #results[b].preds[n].words
if #results[b].preds > 1 then
_G.logger:info('')
_G.logger:info('BEST HYP:')
end
end
if #results[b].preds > 1 then
_G.logger:info("[%.2f] %s", results[b].preds[n].score, sentence)
else
_G.logger:info("PRED %d: %s", sentId, sentence)
_G.logger:info("PRED SCORE: %.2f", results[b].preds[n].score)
end
end
end
end
_G.logger:info('')
sentId = sentId + 1
end
if srcSeq == nil then
break
end
batchId = batchId + 1
srcBatch = {}
srcIdBatch = {}
if withGoldScore then
goldBatch = {}
end
collectgarbage()
end
end
if opt.time then
local time = timer:time()
local sentenceCount = sentId-1
_G.logger:info("Average sentence translation time (in seconds):\n")
_G.logger:info("avg real\t" .. time.real / sentenceCount .. "\n")
_G.logger:info("avg user\t" .. time.user / sentenceCount .. "\n")
_G.logger:info("avg sys\t" .. time.sys / sentenceCount .. "\n")
end
if opt.dump_input_encoding == false then
reportScore('PRED', predScoreTotal, predWordsTotal)
if withGoldScore then
reportScore('GOLD', goldScoreTotal, goldWordsTotal)
end
end
if opt.save_beam_to:len() > 0 then
translator:saveBeamHistories(opt.save_beam_to)
end
outFile:close()
_G.logger:shutDown()
end
main()