Commit 65f46473 authored by Myle Ott's avatar Myle Ott Committed by Facebook Github Bot
Browse files

Add --sentence-bleu option to score.py

Summary: Pull Request resolved: https://github.com/fairinternal/fairseq-py/pull/605

Differential Revision: D15518167

Pulled By: myleott

fbshipit-source-id: 8b0e6b32adff018136d0d251b7fde3818e373d6f
parent 8ce2c35d
...@@ -28,6 +28,8 @@ def get_parser(): ...@@ -28,6 +28,8 @@ def get_parser():
help='case-insensitive scoring') help='case-insensitive scoring')
parser.add_argument('--sacrebleu', action='store_true', parser.add_argument('--sacrebleu', action='store_true',
help='score with sacrebleu') help='score with sacrebleu')
parser.add_argument('--sentence-bleu', action='store_true',
help='report sentence-level BLEUs (i.e., with +1 smoothing)')
# fmt: on # fmt: on
return parser return parser
...@@ -57,6 +59,16 @@ def main(): ...@@ -57,6 +59,16 @@ def main():
def score(fdsys): def score(fdsys):
with open(args.ref) as fdref: with open(args.ref) as fdref:
print(sacrebleu.corpus_bleu(fdsys, [fdref])) print(sacrebleu.corpus_bleu(fdsys, [fdref]))
elif args.sentence_bleu:
def score(fdsys):
with open(args.ref) as fdref:
scorer = bleu.Scorer(dict.pad(), dict.eos(), dict.unk())
for i, (sys_tok, ref_tok) in enumerate(zip(readlines(fdsys), readlines(fdref))):
scorer.reset(one_init=True)
sys_tok = dict.encode_line(sys_tok)
ref_tok = dict.encode_line(ref_tok)
scorer.add(ref_tok, sys_tok)
print(i, scorer.result_string(args.order))
else: else:
def score(fdsys): def score(fdsys):
with open(args.ref) as fdref: with open(args.ref) as fdref:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment