-
Notifications
You must be signed in to change notification settings - Fork 29
/
score.lua
60 lines (50 loc) · 1.61 KB
/
score.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
-- Copyright (c) 2017-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the license found in the LICENSE file in
-- the root directory of this source tree. An additional grant of patent rights
-- can be found in the PATENTS file in the same directory.
--
--[[
--
-- Command-line script for BLEU scoring.
--
--]]
local tok = require 'fairseq.text.tokenizer'
local plpath = require 'pl.path'
local bleu = require 'fairseq.text.bleu'
local cmd = torch.CmdLine()
cmd:option('-sys', '-', 'system output')
cmd:option('-ref', '', 'references')
cmd:option('-order', 4, 'consider ngrams up to this order')
cmd:option('-ignore_case', false, 'case-insensitive scoring')
local config = cmd:parse(arg)
assert(config.sys == '-' or plpath.exists(config.sys))
local fdsys = config.sys == '-' and io.stdin or io.open(config.sys)
assert(plpath.exists(config.ref))
local fdref = io.open(config.ref)
local function readLine(fd)
local s = fd:read()
if s == nil then
return nil
end
if config.ignore_case then
s = string.lower(s)
end
return tok.tokenize(s)
end
local scorer = bleu.scorer(config.order)
-- Process system output and reference file
while true do
local sysTok = readLine(fdsys)
local refTok = readLine(fdref)
if sysTok == nil and refTok ~= nil then
error 'Insufficient number of lines in system output'
elseif refTok == nil and sysTok ~= nil then
error 'Insufficient number of lines in reference output'
elseif sysTok == nil and refTok == nil then
break
end
scorer.update(sysTok, refTok)
end
print(scorer.resultString())