机器翻译评价指标BLEU介绍

最近需要设计一个机器翻译相关的试验, 其中好多东西都不同, 先从基础的评价指标来吧. 本文翻译自Jason Brownlee的博客[1]. 可能会简化一部分内容, 如有需要请读者直接读原文.

0. 前言

BLEU (其全称为Bilingual Evaluation Understudy), 其意思是双语评估替补。所谓Understudy (替补),意思是代替人进行翻译结果的评估。尽管这项指标是为翻译而发明的,但它可以用于评估一组自然语言处理任务生成的文本。

本教程中, 你将会学会使用Python中的NLTK库来对待评估的文本求BLEU得分. 在本教程结束之后, 你应该知道:

  • 对BLEU的简单介绍以及其计算方式的直觉介绍.
  • 如何使用Python中的NLTK库来计算BLEU值.
  • 利用 待选文本(candidate text)参考文本(reference text) 的差异来分析其对BLEU值的影响.

让我们开始吧~

1. 概述

本教程分为4个部分:

  • ① BLEU
  • ② 计算BLEU的得分
  • ③ 累积和单独的BLEU分数 (Cumulative and Individual BLEU Scores)
  • ④ 例子

2. Bilingual Evaluation Understudy: BLEU

在自然语言处理中的机器翻译任务中, BLEU非常常见, 它是用于评估模型生成的句子(candidate)实际句子(reference)的差异的指标.
它的取值范围在0.0到1.0之间, 如果两个句子完美匹配(perfect match), 那么BLEU是1.0, 反之, 如果两个句子完美不匹配(perfect mismatch), 那么BLEU为0.0.
虽然这个指标不够完美, 但是它有5个非常引人注目的好处(compelling benefits):

  • 计算代价小, 快.
  • 容易理解.
  • 与语言无关(这意味着你可以使用全世界任意的语言来测试).
  • 与人类评价结果高度相关.
  • 被学术界和工业界广泛采用.

BLEU值是2002年由IBM 科学家 Kishore Papineni在其论文[2]中提出的: “BLEU: a Method for Automatic Evaluation of Machine Translation“.

BLEU方法的实现是分别计算candidate句reference句N-grams模型[3], 然后统计其匹配的个数来计算得到的. 显然, 这种比较方法, 是与语序无关的.

论文对匹配的 N-grams 计数进行了修改,以确保它考虑到reference文本中单词的出现,而非奖励生成大量合理翻译单词的候选结果。本文将其称为修正的 N-grams 精度。

此外, 有一种改进版的通过normalize N-grams的改进版BLEU. 其目的是提升对多个句子组成的**块(block)**提升翻译效果.
在实践中得到完美的分数是不太可能的,因为翻译结果必须与reference句完全匹配。甚至人工翻译都不可能做到这一点。而且, 由于不同的数据集reference句的质量和数量不同, 所以跨数据集计算BLEU值可能带来一些麻烦.

对机器翻译来说, 我们可以使用BLEU得分来评价其它的语言生成任务, 比如:

  • 语言生成.
  • 图像标题生成.
  • 文本总结.
  • 语音识别.

及其它.

3. 计算BLEU得分

Python自然语言工具包库: NLTK, 提供了BLEU得分计算的实现, 所以如果你只是像译者一样使用的话, 可以先不去了解其内部的实现.

3.1 句子的BLEU值

NLTK提供了sentence_bleu()来评估一组candidate句reference句的BLEU得分情况. 其中, 这两个句子都必须用一系列tokens表示:

注: 这个reference的形式有点像图像检索里面的图像gallery.

>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'a', 'test'], ['this', 'is' 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)
1.0

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这个例子的结果为1.0, 因为candidate与reference的第1个句子perfect match了.

3.2 Corpus(语料库)的BLEU值

NLTK同样提供了corpus_bleu()函数, 它用于来对多个句子比如一个段落甚至一篇文章进行得分评价.

参考句必须由一系列documents组成, 每个document都应该由一系列references组成, 同样的, 每个句子都应该拆分为一个个token. 如下, 其实比上一部分多了一个维度.

可能上面的解释还是容易让人困惑, 下面是一篇文档的2个参考句的示例(我的理解: 比Sentence的都多加了一个维度):

# two references for one document
>>> from nltk.translate.bleu_score import corpus_bleu
>>> references = [[['this', 'is', 'a', 'test'], ['this', 'is' 'test']]]
>>> candidates = [['this', 'is', 'a', 'test']]
>>> score = corpus_bleu(references, candidates)
>>> print(score)

1.0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4. 累积BLEU和独立BLEU

在NLTK中, 其允许用户显式指定不同的N-grams的权重以便来计算BLEU的值. 这使得用户可以灵活的计算不同类型的BLEU值, 比如独立的BLEU或者累积的BLEU.

4.1 独立N-Gram得分

一个独立的N-gram得分是对gram是否按特定顺序排列的评估方式, 比如1-gram和2-gram (2-gram又成为bigram)[3]:

# 1-gram individual BLEU
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'small', 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> score = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))
>>> print(score)

0.75

>>> candidate = [‘this’, ‘test’, ‘is’, ‘a’]
>>> score = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))
>>> score
0.75

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

可以看到, 1-gram有3/4个词完全对应上, 所以得分为0.75, 那么如果我们试试把candidate的顺序打乱呢? 结果仍为0.75~

下面, 作者测试了1-4gram如下:

>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'a', 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> print('Individual 1-gram: %f' % sentence_bleu(reference, candidate, weights=(1, 0, 0, 0)))
>>> print('Individual 2-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 1, 0, 0)))
>>> print('Individual 3-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 0, 1, 0)))
>>> print('Individual 4-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 0, 0, 1)))

Individual 1-gram: 1.000000
Individual 2-gram: 1.000000
Individual 3-gram: 1.000000
Individual 4-gram: 1.000000

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

尽管我们可以计算独立的BLEU值, 但是这种方式不是常规使用的方式, 而且独立BLEU值的含义也比较有限. 因此, 下面我们引出累积BLEU值.

4.2 累积N-Gram得分

累积N-Gram得分指的是为各个gram对应的权重加权, 来计算得到一个加权几何平均(weighted geometric mean). 默认情况下, sentence_bleu()corpus_bleu()都是计算累积的4-gram BLEU分数的, 也称之为BLEU-4.
BLEU-4的加权策略如下: 1/4 (25%) 或者 0.25 对 1-gram, 2-gram, 3-gram and 4-gram 的得分.

# 4-gram cumulative BLEU
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'small', 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> score = sentence_bleu(reference, candidate)
>>> score
1.0547686614863434e-154
>>> score = sentence_bleu(reference, candidate, weights=(0.25, 0.25, 0.25, 0.25))
>>> score
1.0547686614863434e-154

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

下面, 我们分别计算从BLEU-1到BLEU-4的累积N-Gram得分:

>>> print('Cumulative 1-gram: %f' % sentence_bleu(reference, candidate, weights=(1, 0, 0, 0)))
Cumulative 1-gram: 0.750000
>>> print('Cumulative 2-gram: %f' % sentence_bleu(reference, candidate, weights=(0.5, 0.5, 0, 0)))
Cumulative 2-gram: 0.500000
>>> print('Cumulative 3-gram: %f' % sentence_bleu(reference, candidate, weights=(0.33, 0.33, 0.33, 0)))
Cumulative 3-gram: 0.000000
>>> print('Cumulative 4-gram: %f' % sentence_bleu(reference, candidate, weights=(0.25, 0.25, 0.25, 0.25)))
Cumulative 4-gram: 0.000000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

通常情况下, 我们会统计BLEU1BLEU4的累积值作为评估 文本生成系统(text generation system) 的效果.

5. 样例

本部分, 我们将对BLEU值的设计develop further intuition(通过一些例子), 以一个单句的例子为例:

the quick brown fox jumped over the lazy dog

首先, 让我们看看完美匹配的得分:

# prefect match
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']]
>>> candidate = ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)

1.0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

接下来, 我们把quick同意替换为fast, 看看效果:

# one word different
...
>>> candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)

0.7506238537503395

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

接着在此基础上再改一个单词, 把lazy 改成 sleepy

# two words different
...
>>> candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the', 'sleepy', 'dog']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)

0.4854917717073234

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

可以看到, BLEU值出现了下降, 因为match的对越来越少了. 现在, 我们可以试试这种形式: 将candidate句变短, 再跟reference进行比较

# 删除candidate句子最后两个单词, 即让candidate变短
...
>>> candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)

0.7514772930752859

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

那么, 如果 将candidate句变长, 再跟reference进行比较呢?

...
>>> candidate = ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog', 'from', 'space']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)

0.7860753021519787

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

我们可以看到, 分数反而越来越高啦~, BLEU中的数学理论[2, 4]是相当简单的,我也鼓励你阅读这篇论文,并探索自己在电子表格中计算句子水平分数。

6. 总结

在本教程中, 你会了解到BLEU的使用和得到一些基本的intuition关于其设计思路, 通过学习, 如下3点你应该掌握:

  • ① A gentle introduction to the BLEU score and an intuition for what is being calculated.
  • ② How you can calculate BLEU scores in Python using the NLTK library for sentences and documents.
  • ③ How to can use a suite of small examples to develop an intuition for how differences between a candidate and reference text impact the final BLEU score.

参考资料

[1] A Gentle Introduction to Calculating the BLEU Score for Text in Python
[2] BLEU: a Method for Automatic Evaluation of Machine Translation
[3] 蕉叉熵: N-grams说明
[4] BLEU 维基百科

                                </div>
### 机器翻译评估标准 #### BLEU (Bilingual Evaluation Understudy) BLEU 是一种广泛使用的自动评估指标,主要用于衡量机器翻译的质量。它通过比较生成的翻译结果与参考译文中的 n-gram 匹配情况来计算分数。具体来说,BLEU 计算的是精确度得分,并引入了惩罚机制以应对过短的翻译输出[^1]。 以下是使用 Python 实现 BLEU 的简单代码示例: ```python from datasets import load_metric bleu = load_metric("bleu") predictions = [["the cat is on the mat"]] references = [["the cat is lying on the mat"]] result = bleu.compute(predictions=predictions, references=references) print(result) ``` 尽管 BLEU 广泛应用于英语和其他语言对之间,但在中文环境中可能需要额外的分词工具支持[^3]。 --- #### ROUGE (Recall-Oriented Understudy for Gisting Evaluation) ROUGE 主要用于评估文本摘要质量和自然语言生成任务的表现。它的核心思想在于统计生成文本和参考文本之间的重叠部分,可以是连续片段(skip-bigram)、n-grams 或最长公共子序列等[^2]。对于机器翻译而言,ROUGE 同样能够提供关于翻译质量的部分见解。 下面是一个简单的 ROUGE 使用案例: ```python from rouge_score import rouge_scorer scorer = rouge_scorer.RougeScorer(['rouge-1', 'rouge-l'], use_stemmer=True) scores = scorer.score( "The quick brown fox jumps over a lazy dog.", "The fast brown fox leaps over a sleepy hound." ) for key, value in scores.items(): print(f"{key}: {value}") ``` 需要注意的是,虽然 ROUGE 更关注召回率而非精度,但它仍然无法全面反映语义层面的一致性。 --- #### METEOR (Metric for Evaluation of Translation with Explicit ORdering) METEOR 被设计成更贴近人类判断的一种方法论。除了考虑单词级别的匹配外,还加入了同义词替换、词形还原等功能模块。这种方法试图克服其他传统评分体系仅依赖表面形式匹配所带来的局限性。 实现 METEOR 可能涉及如下操作: ```python import nltk from meteor_score.meteor_score import meteor_score hypothesis = ["this", "is", "a", "test"] reference = ["only", "a", "test"] score = meteor_score([reference], hypothesis) print(score) ``` 然而值得注意的是,由于 METEOR 需要访问外部资源库来进行诸如词林映射等工作,因此其运行效率通常低于前两者。 --- ### 局限性和改进方向 上述提到的各种自动化评测手段都存在一定的缺陷,例如它们均未能有效解决全局连贯性的考量以及潜在的语言偏差等问题。未来的研究可能会集中在开发更加智能化的方法上,这些新方案或许会更多地利用深度学习模型或者强化交互反馈机制来提升评价效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值