公式:
首先统计候选译文里每个词出现的次数,然后统计每个词在参考译文中出现的次数,Max表示几个参考译文中的最大值,Min表示候选译文和Max两个的最小值。
是N -gram 对应的
是权重系数,可以取平均,总和为1
是过度惩罚系数。由BP的公式可知取值范围是(0,1],候选句子越短,越接近0。
比如:候选翻译句子长度为18,参考翻译分别为:16,18,16。
所以c=18,r=18(参考翻译中选取长度最接近候选翻译的作为r)所以BP=e^0=1
这边如何选择最近候选翻译的代码是:
也就是候选翻译长度为12,参考翻译为13和11 的时候,选择的是长度为11的
应用代码:
import nltk.translate.bleu_score as bleu
reference_translation=['The cat is on the mat.'.split(),
'There is a cat on the mat.'.split()
]
candidate_translation_1='the the the mat on the the.'.split()
candidate_translation_2='The cat is on the mat.'.split()
print("BLEU Score: ",bleu.sentence_bleu(reference_translation, candidate_translation_1))
print("BLEU Score: ",bleu.sentence_bleu(reference_translation, candidate_translation_2))
结果是: BLEU Score: 6.968148412761692e-155 BLEU Score: 1.0