Add --sentence-bleu option to score.py
Summary: Pull Request resolved:
@@ -28,6 +28,8 @@ def get_parser():
help='case-insensitive scoring')
parser.add_argument('--sacrebleu', action='store_true',
help='score with sacrebleu')
+ parser.add_argument('--sentence-bleu', action='store_true',
+ help='report sentence-level BLEUs (i.e., with +1 smoothing)')
# fmt: on
return parser
@@ -57,6 +59,16 @@ def main():
def score(fdsys):
with open(args.ref) as 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:
def score(fdsys):
with open(args.ref) as fdref:
