def get_bleu_score(out_file, gold_file): weights = [0.25, 0.25, 0.25, 0.25] sum_bleu = 0 sent_num = 0 with open(out_file) as o_stream, open(gold_file) as g_stream: for o_sent, g_sent in zip(o_stream, g_stream): sum_bleu += bleu(o_sent, [g_sent], weights) sent_num += 1 return sum_bleu / sent_num
from nltk.align import bleu from nltk import word_tokenize import argparse parser = argparse.ArgumentParser() parser.add_argument("-c", "--candidate") parser.add_argument("-r", "--reference") args = parser.parse_args() #candidate = 'He is very skilled' #reference = 'He is very talented' assert len(args.candidate) > 1 assert len(args.reference) > 1 print bleu(word_tokenize(args.candidate), [word_tokenize(args.reference)], [1])