BLEU原理详解

BLEU全称是:a Method for Automatic Evaluation of Machine Translation(是一种用来评估机器翻译的评价指标)广泛出现在,文本生成的论文当中。
BLEU采用一种N-Gram的匹配规则,具体来说就是比较译文和参考文献之间的N组词的相似度:
举个例子:
Source Sentence: 今天天气不错.
Target Sentence: It is a nice day today.
Candidate Sentence:Today is a nice day
当你使用1-Gram匹配时:
在这里插入图片描述可以发现,在你不考虑语序的前提下,Candidate中五个词命中了参考译文,于是Score(1-gram)=5/6

比如,以3-Gram举例子
在这里插入图片描述
可以看到Candidate中有两个三元词汇命中了Reference中的四个三元词汇,于是Score(3-gram)=2/4
N-gram匹配法则可以理解为,1-gram匹配时,代表Candidate中有多少个单词是正确翻译的,而2-gram~4-gram匹配就代表了,Candidate的流畅程度

BLEU的定义
BLEU(Biligual Evaluation understudy):是一种基于单词精确度的相似性度量方式,可以用来比较Reference和Candidate中n元组共同出现的程度
BLEU的好处:
(1)速度快、成本低廉
(2)容易理解
(3)不受语种限制
(4)运用广泛
BLEU的缺点
但是BLEU也存在着十分严重的缺点:
(1)忽略同义词
(2)N-gram的机制会导致某项分数特别低
(3)BLEU不考虑意义
(4)BLEU不考虑句子的结构,很多时候,只要单词相同BLEU就会给出很高的分数
(5)BLEU不能够很好地处理形态丰富的语言
(6)BLEU与人类的判断并不相符合

import math
from nltk.translate.bleu_score import sentence_bleu



reference = [['this', 'is', 'an','test']]
candidate = ['this', 'is', 'a', 'test']
score = sentence_bleu(reference, candidate)#计算真实的BLEU分数

#计算1-gram~4-gram的Pn分数
score_1 = sentence_bleu(reference, candidate, weights=(1,0,0,0))
score_2 = sentence_bleu(reference, candidate, weights=(0.5,0.5,0,0))
score_3 = sentence_bleu(reference, candidate, weights=(0.33,0.33,0.33,0))
score_4 = sentence_bleu(reference, candidate, weights=(0.25,0.25,0.25,0.25))

score_total = [score_1, score_2,score_3, score_4]
for score in score_total:
    print(score)

Pn_1 = math.log(score_1)
Pn_2 = math.log(score_2)
Pn_3 = math.log(score_3)
Pn_4 = math.log(score_4)

#因为Reference和Candidate的句子长度一致,所以BP惩罚因子为1
BP = 1
W_n = 1/4 #Wn是均匀加权,N的上限取值为4,即最多统计4-gram的精度
BLEU_score= BP * math.exp((Pn_1 + Pn_2 + Pn_3 + Pn_4)/W_n)

print(score)
print(BLEU_score)

结合代码,我们可以观察一下BLEU分数的推导分析:

在这里插入图片描述

在这里插入图片描述

接下来再进行一个实例推导:

在这里插入图片描述

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值