机器翻译评价指标之BLEU详细计算过程

机器翻译评价指标之BLEU详细计算过程1 简介 BLEU BilingualEva 相信大家对这个评价指标的概念已经很熟悉 随便百度谷歌就有相关介绍 原论文为 BLEU aMethodforAu IBM 出品 本文通过一个例子详细介绍 BLEU 是如何计算以及 NLTKnltk align bleu scor

1. 简介

BLEU(Bilingual Evaluation Understudy),相信大家对这个评价指标的概念已经很熟悉,随便百度谷歌就有相关介绍。原论文为BLEU: a Method for Automatic Evaluation of Machine Translation,IBM出品。

本文通过一个例子详细介绍BLEU是如何计算以及NLTKnltk.align.bleu_score模块的源码。

首先祭出公式:

BLEU=BPexp(n=1NwnlogPn) B L E U = B P ⋅ e x p ( ∑ n = 1 N w n l o g P n )



其中,

BP={
1e1r/cif c>rif cr
B P = { 1 if  c > r e 1 − r / c if  c ≤ r

注意这里的BLEU值是针对一条翻译(一个样本)来说的。

NLTKnltk.align.bleu_score模块实现了这里的公式,主要包括三个函数,两个私有函数分别计算P和BP,一个函数整合计算BLEU值。

# 计算BLEU值 def bleu(candidate, references, weights) # (1)私有函数,计算修正的n元精确率(Modified n-gram Precision) def _modified_precision(candidate, references, n) # (2)私有函数,计算BP惩罚因子 def _brevity_penalty(candidate, references)

例子:

候选译文(Predicted)
It is a guide to action which ensures that the military always obeys the commands of the party

参考译文(Gold Standard)
1:It is a guide to action that ensures that the military will forever heed Party commands
2:It is the guiding principle which guarantees the military forces always being under the command of the Party
3:It is the practical guide for the army always to heed the directions of the party






2. Modified n-gram Precision计算(也即是 Pn P n

def _modified_precision(candidate, references, n): counts = Counter(ngrams(candidate, n)) if not counts: return 0 max_counts = {} for reference in references: reference_counts = Counter(ngrams(reference, n)) for ngram in counts: max_counts[ngram] = max(max_counts.get(ngram, 0), reference_counts[ngram]) clipped_counts = dict((ngram, min(count, max_counts[ngram])) for ngram, count in counts.items()) return sum(clipped_counts.values()) / sum(counts.values())

我们这里 n n 取值为4,也就是从1-gram计算到4-gram。

Modified 1-gram precision:

首先统计候选译文里每个词出现的次数,然后统计每个词在参考译文中出现的次数,Max表示3个参考译文中的最大值,Min表示候选译文和Max两个的最小值。

候选译文 参考译文1 参考译文2 参考译文3 Max Min
the 3 1 4 4 4 3
obeys 1 0 0 0 0 0
a 1 1 0 0 1 1
which 1 0 1 0 1 1
ensures 1 1 0 0 1 1
guide 1 1 0 1 1 1
always 1 0 1 1 1 1
is 1 1 1 1 1 1
of 1 0 1 1 1 1
to 1 1 0 1 1 1
commands 1 1 0 0 1 1
that 1 2 0 0 2 1
It 1 1 1 1 1 1
action 1 1 0 0 1 1
party 1 0 0 1 1 1
military 1 1 1 0 1 1

然后将每个词的Min值相加,将候选译文每个词出现的次数相加,然后两值相除即得 P 1 = 3 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 3 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 0.95 ” role=”presentation” style=”position: relative;”> P 1 = 3 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 3 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 0.95

类似可得:

Modified 2-gram precision:

候选译文 参考译文1 参考译文2 参考译文3 Max Min
ensures that 1 1 0 0 1 1
guide to 1 1 0 0 1 1
which ensures 1 0 0 0 0 0
obeys the 1 0 0 0 0 0
commands of 1 0 0 0 0 0
that the 1 1 0 0 1 1
a guide 1 1 0 0 1 1
of the 1 0 1 1 1 1
always obeys 1 0 0 0 0 0
the commands 1 0 0 0 0 0
to action 1 1 0 0 1 1
the party 1 0 0 1 1 1
is a 1 1 0 0 1 1
action which 1 0 0 0 0 0
It is 1 1 1 1 1 1
military always 1 0 0 0 0 0
the military 1 1 1 0 1 1

P2=1017=0. P 2 = 10 17 = 0.

Modified 3-gram precision:

候选译文 参考译文1 参考译文2 参考译文3 Max Min
ensures that the 1 1 0 0 1 1
which ensures that 1 0 0 0 0 0
action which ensures 1 0 0 0 0 0
a guide to 1 1 0 0 1 1
military always obeys 1 0 0 0 0 0
the commands of 1 0 0 0 0 0
commands of the 1 0 0 0 0 0
to action which 1 0 0 0 0 0
the military always 1 0 0 0 0 0
obeys the commands 1 0 0 0 0 0
It is a 1 1 0 0 1 1
of the party 1 0 0 1 1 1
is a guide 1 1 0 0 1 1
that the military 1 1 0 0 1 1
always obeys the 1 0 0 0 0 0
guide to action 1 1 0 0 1 1

P3=716=0.4375 P 3 = 7 16 = 0.4375

Modified 4-gram precision:

候选译文 参考译文1 参考译文2 参考译文3 Max Min
to action which ensures 1 0 0 0 0 0
action which ensures that 1 0 0 0 0 0
guide to action which 1 0 0 0 0 0
obeys the commands of 1 0 0 0 0 0
which ensures that the 1 0 0 0 0 0
commands of the party 1 0 0 0 0 0
ensures that the military 1 1 0 0 1 1
a guide to action 1 1 0 0 1 1
always obeys the commands 1 0 0 0 0 0
that the military always 1 0 0 0 0 0
the commands of the 1 0 0 0 0 0
the military always obeys 1 0 0 0 0 0
military always obeys the 1 0 0 0 0 0
is a guide to 1 1 0 0 1 1
It is a guide 1 1 0 0 1 1

P4=415=0. P 4 = 4 15 = 0.

然后我们取 w1=w2=w3=w4=0.25 w 1 = w 2 = w 3 = w 4 = 0.25 ,也就是Uniform Weights。

所以:

Ni=1wnlogPn=0.25logP1+0.25logP2+0.25logP3+0.25logP4=0.7 ∑ i = 1 N w n log ⁡ P n = 0.25 ∗ log ⁡ P 1 + 0.25 ∗ log ⁡ P 2 + 0.25 ∗ log ⁡ P 3 + 0.25 ∗ log ⁡ P 4 = − 0.7

3. Brevity Penalty 计算

def _brevity_penalty(candidate, references): c = len(candidate) ref_lens = (len(reference) for reference in references) #这里有个知识点是Python中元组是可以比较的,如(0,1)>(1,0)返回False,这里利用元组比较实现了选取参考翻译中长度最接近候选翻译的句子,当最接近的参考翻译有多个时,选取最短的。例如候选翻译长度是10,两个参考翻译长度分别为9和11,则r=9. r = min(ref_lens, key=lambda ref_len: (abs(ref_len - c), ref_len)) print 'r:',r if c > r: return 1 else: return math.exp(1 - r / c)

下面计算BP(Brevity Penalty),翻译过来就是“过短惩罚”。由BP的公式可知取值范围是(0,1],候选句子越短,越接近0。

所以 B P = e 0 = 1 ” role=”presentation” style=”position: relative;”> B P = e 0 = 1

4. 整合

最终 BLEU=1exp(0.7)=0.6 B L E U = 1 ⋅ e x p ( − 0.7 ) = 0.6

BLEU的取值范围是[0,1],0最差,1最好。

通过计算过程,我们可以看到,BLEU值其实也就是“改进版的n-gram”加上“过短惩罚因子”。




版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205213.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午6:32
下一篇 2026年3月19日 下午6:32


相关推荐

  • activiti7入门_react demo

    activiti7入门_react demo项目框架描述项目基于springboot2.1.1<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.1.RELE…

    2022年8月30日
    7
  • Linux上卸载JDK

    Linux上卸载JDK卸载jdk先输入java-version查看是否安装了JDK查看jdk安装的路径whichjava卸载命令rm-rfJDK地址,比如小编的是rm-rf/usr/java/jdk1.8.0_181/进去/usr/java目录查看一下,ls是显示目录下文件,发现确实什么都没有了接下来删除环境变量,装的时候环境变量都是在/etc/profile下面的进入profile,把以下环境变量全给删除vim编辑器汇总,i进入insert模式,然后此环境下可编辑内容,删除之后

    2022年6月25日
    30
  • C++之迭代器(Iterator)篇

    C++之迭代器(Iterator)篇迭代器 Iterator 的介绍背景 指针可以用来遍历存储空间连续的数据结构 但是对于存储空间费连续的 就需要寻找一个行为类似指针的类 来对非数组的数据结构进行遍历 定义 迭代器提供对一个容器中的对象的访问方法 并且定义了容器中对象的范围 迭代器 Iterator 是指针 pointer 的泛化 它允许程序员用相同的方式处理不同的数据结构 容器 1 迭代器类似于 C 语言里面的指针类

    2026年3月19日
    2
  • Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果「建议收藏」

    Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果「建议收藏」转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38140505自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果。的确HorizontalScrollView可以实现Gallery的效果,但是HorizontalScrollView存在一个

    2022年7月26日
    12
  • 小程序onlaunch和onload(小程序onunload)

    所述问题:前端时间开发了一个微信小程序商城项目,因为这个项目我们的需求是进入小程序就通过wx.login({})这个api进行用户登录,获取系统后台的用户基本信息。再此之前,一直以为微信小程序中的App.js中onLaunch(小程序初始化完成执行该方法)方法比其他页面的的onload方法要先执行。那么问题就来了,我每次进入小程序首页的时候有时候会先执行onlaunch方法,有时又会先执…

    2022年4月14日
    148
  • 从零开始ARM裸机开发之建立开发环境

    从零开始ARM裸机开发之建立开发环境从零开始ARM裸机开发之建立开发环境                                   –参考朱有鹏ARM裸机课程1、前言:以前学过TQ2440的程序,在Ubuntu中建立的环境是不符合S5PV210需要的开发环境的这篇博文主要讲解一下建立一个简单的嵌入式开发环境需要什么?2、回

    2022年5月6日
    72

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号