From 8a25e9cc9f7c87c16c996b0e44ca4bbad7b673ee Mon Sep 17 00:00:00 2001 From: SeoroMin Date: Tue, 23 Nov 2021 13:59:34 +0900 Subject: [PATCH] @SeoroMin --- bleu.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 bleu.py diff --git a/bleu.py b/bleu.py new file mode 100644 index 0000000..749599e --- /dev/null +++ b/bleu.py @@ -0,0 +1,26 @@ +from nltk.translate.bleu_score import sentence_bleu +import argparse + +def argparser(): + Argparser = argparse.ArgumentParser() + Argparser.add_argument('--reference', type=str, default='summaries.txt', help='Reference File') + Argparser.add_argument('--candidate', type=str, default='candidates.txt', help='Candidate file') + + args = Argparser.parse_args() + return args + +args = argparser() + +reference = open(args.reference, 'r').readlines() +candidate = open(args.candidate, 'r').readlines() + +if len(reference) != len(candidate): + raise ValueError('The number of sentences in both files do not match.') + +score = 0. + +for i in range(len(reference)): + score += sentence_bleu([reference[i].strip().split()], candidate[i].strip().split()) + +score /= len(reference) +print("The bleu score is: "+str(score)) \ No newline at end of file