bleu全称为Bilingual Evaluation Understudy(双语评估替换),是2002年提出的用于评估机器翻译效果的一种方法,这种方法简单朴素、短平快、易于理解。因为其效果还算说得过去,因此被广泛迁移到自然语言处理的各种评估任务中。这种方法可以说是:山上无老虎,猴子称大王。时无英雄遂使竖子成名。蜀中无大将,廖化做先锋。

问题描述

首先,对bleu算法建立一个直观的印象。

有两类问题:

1、给定一个句子和一个候选句子集,求bleu值,此问题称为sentence_bleu

2、给定一堆句子和一堆候选句子集,求bleu值,此问题称为corpus_bleu

机器翻译得到的句子称为candidate,候选句子集称为references。

计算方式就是计算candidate和references的公共部分。公共部分越多,说明翻译结果越好。

给定一个句子和一个候选句子集计算bleu值

bleu考虑1,2,3,4共4个n-gram,可以给每个n-gram指定权重。

对于n-gram:

  • 对candidate和references分别分词(n-gram分词)
  • 统计candidate和references中每个word的出现频次
  • 对于candidate中的每个word,它的出现频次不能大于references中最大出现频次

    这一步是为了整治形如the the the the the这样的candidate,因为the在candidate中出现次数太多了,导致分值为1。为了限制这种不正常的candidate,使用正常的references加以约束。
  • candidate中每个word的出现频次之和除以总的word数,即为得分score
  • score乘以句子长度惩罚因子即为最终的bleu分数

    这一步是为了整治短句子,比如candidate只有一个词:the,并且the在references中出现过,这就导致得分为1。也就是说,有些人因为怕说错而保持沉默。

bleu的发展不是一蹴而就的,很多人为了修正bleu,不断发现bleu的漏洞并提出解决方案。从bleu的发展历程上,我们可以学到如何设计规则整治badcase。

最后,对于1-gram,2-gram,3-gram的组合,应该采用几何平均,也就是s1^w1*s2^2*s3^w3,而不是算术平均w1*s1+w2*s2+w3*s3

from collections import Counter

import numpy as np
from nltk.translate import bleu_score def bp(references, candidate):
# brevity penality,句子长度惩罚因子
ind = np.argmin([abs(len(i) - len(candidate)) for i in references])
if len(references[ind]) < len(candidate):
return 1
scale = 1 - (len(candidate) / len(references[ind]))
return np.e ** scale def parse_ngram(sentence, gram):
# 把一个句子分成n-gram
return [sentence[i:i + gram] for i in range(len(sentence) - gram + 1)] # 此处一定要注意+1,否则会少一个gram def sentence_bleu(references, candidate, weight):
bp_value = bp(references, candidate)
s = 1
for gram, wei in enumerate(weight):
gram = gram + 1
# 拆分n-gram
ref = [parse_ngram(i, gram) for i in references]
can = parse_ngram(candidate, gram)
# 统计n-gram出现次数
ref_counter = [Counter(i) for i in ref]
can_counter = Counter(can)
# 统计每个词在references中的出现次数
appear = sum(min(cnt, max(i.get(word, 0) for i in ref_counter)) for word, cnt in can_counter.items())
score = appear / len(can)
# 每个score的权值不一样
s *= score ** wei
s *= bp_value # 最后的分数需要乘以惩罚因子
return s references = [
"the dog jumps high",
"the cat runs fast",
"dog and cats are good friends"
]
candidate = "the d o g jump s hig"
weights = [0.25, 0.25, 0.25, 0.25]
print(sentence_bleu(references, candidate, weights))
print(bleu_score.sentence_bleu(references, candidate, weights))

一个corpus是由多个sentence组成的,计算corpus_bleu并非求sentence_bleu的均值,而是一种略微复杂的计算方式,可以说是没什么道理的狂想曲。

corpus_bleu

一个文档包含3个句子,句子的分值分别为a1/b1,a2/b2,a3/b3。

那么全部句子的分值为:(a1+a2+a3)/(b1+b2+b3)

惩罚因子也是一样:三个句子的长度分别为l1,l2,l3,对应的最接近的reference分别为k1,k2,k3。那么相当于bp(l1+l2+l3,k1+k2+k3)。

也就是说:对于corpus_bleu不是单纯地对sentence_bleu求均值,而是基于更统一的一种方法。

from collections import Counter

import numpy as np
from nltk.translate import bleu_score def bp(references_len, candidate_len):
if references_len < candidate_len:
return 1
scale = 1 - (candidate_len / references_len)
return np.e ** scale def parse_ngram(sentence, gram):
return [sentence[i:i + gram] for i in range(len(sentence) - gram + 1)] def corpus_bleu(references_list, candidate_list, weights):
candidate_len = sum(len(i) for i in candidate_list)
reference_len = 0
for candidate, references in zip(candidate_list, references_list):
ind = np.argmin([abs(len(i) - len(candidate)) for i in references])
reference_len += len(references[ind])
s = 1
for index, wei in enumerate(weights):
up = 0 # 分子
down = 0 # 分母
gram = index + 1
for candidate, references in zip(candidate_list, references_list):
# 拆分n-gram
ref = [parse_ngram(i, gram) for i in references]
can = parse_ngram(candidate, gram)
# 统计n-gram出现次数
ref_counter = [Counter(i) for i in ref]
can_counter = Counter(can)
# 统计每个词在references中的出现次数
appear = sum(min(cnt, max(i.get(word, 0) for i in ref_counter)) for word, cnt in can_counter.items())
up += appear
down += len(can)
s *= (up / down) ** wei
return bp(reference_len, candidate_len) * s references = [
[
"the dog jumps high",
"the cat runs fast",
"dog and cats are good friends"],
[
"ba ga ya",
"lu ha a df",
]
]
candidate = ["the d o g jump s hig", 'it is too bad']
weights = [0.25, 0.25, 0.25, 0.25]
print(corpus_bleu(references, candidate, weights))
print(bleu_score.corpus_bleu(references, candidate, weights))

如果你用的NLTK版本是3.2,发布时间是2016年3月份,那么计算corpus_bleu时有一处bug。NLTK在2016年10月份已经修复了此处bug。对于句子分值的求和,NLTK代码中是使用Fraction,Fraction会自动对分子和分母进行化简,导致求和的时候计算错误。

简化代码

在计算sentence_bleu和corpus_bleu过程中,许多步骤都是相似的、可以合并的。精简后的代码如下:

from collections import Counter

import numpy as np
from nltk.translate import bleu_score def bp(references_len, candidate_len):
return np.e ** (1 - (candidate_len / references_len)) if references_len > candidate_len else 1 def nearest_len(references, candidate):
return len(references[np.argmin([abs(len(i) - len(candidate)) for i in references])]) def parse_ngram(sentence, gram):
return [sentence[i:i + gram] for i in range(len(sentence) - gram + 1)] def appear_count(references, candidate, gram):
ref = [parse_ngram(i, gram) for i in references]
can = parse_ngram(candidate, gram)
# 统计n-gram出现次数
ref_counter = [Counter(i) for i in ref]
can_counter = Counter(can)
# 统计每个词在references中的出现次数
appear = sum(min(cnt, max(i.get(word, 0) for i in ref_counter)) for word, cnt in can_counter.items())
return appear, len(can) def corpus_bleu(references_list, candidate_list, weights):
candidate_len = sum(len(i) for i in candidate_list)
reference_len = sum(nearest_len(references, candidate) for candidate, references in zip(candidate_list, references_list))
bp_value = bp(reference_len, candidate_len)
s = 1
for index, wei in enumerate(weights):
up = 0 # 分子
down = 0 # 分母
gram = index + 1
for candidate, references in zip(candidate_list, references_list):
appear, total = appear_count(references, candidate, gram)
up += appear
down += total
s *= (up / down) ** wei
return bp_value * s def sentence_bleu(references, candidate, weight):
bp_value = bp(nearest_len(references, candidate), len(candidate))
s = 1
for gram, wei in enumerate(weight):
gram = gram + 1
appear, total = appear_count(references, candidate, gram)
score = appear / total
# 每个score的权值不一样
s *= score ** wei
# 最后的分数需要乘以惩罚因子
return s * bp_value if __name__ == '__main__':
references = [
[
"the dog jumps high",
"the cat runs fast",
"dog and cats are good friends"],
[
"ba ga ya",
"lu ha a df",
]
]
candidate = ["the d o g jump s hig", 'it is too bad']
weights = [0.25, 0.25, 0.25, 0.25]
print(corpus_bleu(references, candidate, weights))
print(bleu_score.corpus_bleu(references, candidate, weights))
print(sentence_bleu(references[0], candidate[0], weights))
print(bleu_score.sentence_bleu(references[0], candidate[0], weights))

参考资料

https://cloud.tencent.com/developer/article/1042161

https://en.wikipedia.org/wiki/BLEU

https://blog.csdn.net/qq_31584157/article/details/77709454

https://www.jianshu.com/p/15c22fadcba5

理解bleu的更多相关文章

  1. 关于机器翻译评价指标BLEU(bilingual evaluation understudy)的直觉以及个人理解

    最近我在做Natural Language Generating的项目,接触到了BLEU这个指标,虽然知道它衡量的是机器翻译的效果,也在一些文献的experiment的部分看到过该指标,但我实际上经常 ...

  2. 机器翻译评测——BLEU算法详解

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/7679284.html 前言 近年来,在自然语言研究领域中, ...

  3. 机器翻译评价指标 — BLEU算法

    1,概述 机器翻译中常用的自动评价指标是 $BLEU$ 算法,除了在机器翻译中的应用,在其他的 $seq2seq$ 任务中也会使用,例如对话系统. 2 $BLEU$算法详解 假定人工给出的译文为$re ...

  4. 利用BLEU进行机器翻译检测(Python-NLTK-BLEU评分方法)

    双语评估替换分数(简称BLEU)是一种对生成语句进行评估的指标.完美匹配的得分为1.0,而完全不匹配则得分为0.0.这种评分标准是为了评估自动机器翻译系统的预测结果而开发的,具备了以下一些优点: 计算 ...

  5. Deep Learning基础--机器翻译BLEU与Perplexity详解

    前言 近年来,在自然语言研究领域中,评测问题越来越受到广泛的重视,可以说,评测是整个自然语言领域最核心和关键的部分.而机器翻译评价对于机器翻译的研究和发展具有重要意义:机器翻译系统的开发者可以通过评测 ...

  6. 阅读关于DuReader:百度大规模的中文机器阅读理解数据集

    很久之前就得到了百度机器阅读理解关于数据集的这篇文章,今天才进行总结!.... 论文地址:https://arxiv.org/abs/1711.05073 自然语言处理是人工智能皇冠上的明珠,而机器阅 ...

  7. 对于文本生成类4种评价指标的的计算BLEU METEOR ROUGE CIDEr

    github下载链接:https://github.com/Maluuba/nlg-eval 将下载的文件放到工程目录,而后使用如下代码计算结果 具体的写作格式如下: from nlgeval imp ...

  8. 机器阅读理解综述Neural Machine Reading Comprehension Methods and Trends(略读笔记)

    标题:Neural Machine Reading Comprehension: Methods and Trends 作者:Shanshan Liu, Xin Zhang, Sheng Zhang, ...

  9. 理解CSS视觉格式化

    前面的话   CSS视觉格式化这个词可能比较陌生,但说起盒模型可能就恍然大悟了.实际上,盒模型只是CSS视觉格式化的一部分.视觉格式化分为块级和行内两种处理方式.理解视觉格式化,可以确定得到的效果是应 ...

随机推荐

  1. 如何配置官方peerDroid,使其运行起来

    一.Peer Droid是JXME协议到android平台的移植,开发者可以利用它来实现android设备以及传统PC机通讯的应用程序,peerDroid的官方demo主要是实现PC端peer和and ...

  2. Linq-进行Json序列化的过程中出现错误解决办法

    错误截图如下: 这是因为表t_sysuser与表t_sysrole之间存在外键联系导致的 解决办法: 进入到创建的linq to sql类中,右键[属性]-将序列化模式修改为[单向]保存即可

  3. disconf-自动注入属性变化

    disconf-自动注入属性变化 disconf官网:http://disconf.readthedocs.io/zh_CN/latest/ 自动注入属性:http://disconf.readthe ...

  4. (转)Unity3D 游戏贴图(法线贴图,漫反射贴图,高光贴图)

    原帖网址http://www.u3dpro.com/read.php?tid=207  感谢jdk900网友的辛苦编写 我们都知道,一个三维场景的画面的好坏,百分之四十取决于模型,百分之六十取决于贴图 ...

  5. 整数对A满足二叉查找树,B满足最大堆

    1 题目 给出一组整数对 { (a[0], b[0]), (a[1], b[1]) ... (a[n-1], b[n-1]) },全部 a 值和 b 值分别不反复(随意 i != j 满足 a[i] ...

  6. (转)No row with the given identifier exists问题的解决

    产生此问题的原因:         有两张表,table1和table2.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique ...

  7. jetty 9使用

    jetty 9 使用 下载jdk 7 http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-ja ...

  8. 如何让windows更高效?

    首先解释一下个标题: "让windows更高效,既指让windows更友好更优化,也指可以让使用windows来工作或学习的人更高效的工作学习." 解释下本文的动机: 指导我自己或 ...

  9. iOS 国际化 NSLocalizedString

    iOS 国际化.根据系统不同的语言自动切换. 首先.选择项目 Add new file -->iOS -->Resource -->Strings File  . 命名为Locali ...

  10. ES6...扩展运算符(数组或类数组对象)

    数组和类数组对象定义 数组:[] 类数组对象:只包含使用从零开始,且自然递增的整数做键名,并且定义了length表示元素个数的对象,我们就认为他是类数组对象. 数组使用 let foo_arr = [ ...