Python中的Bleu评分是衡量机器翻译模型优劣的指标。虽然最初它只为翻译模型设计,但现在它也用于其他[自然语言处理应用程序](/community/tutorials/natural-language-processing-tasks)。
BLEU分数将一个句子与一个或多个参考句子进行比较,并说明候选句子与参考句子列表的匹配程度。它给出的输出分数介于0和1之间。
BLEU得分为1表示候选句子与参考句子中的一个完美匹配。
此分数是图像字幕模型的常用度量标准。
在本教程中,我们将使用nltk库中的sentence_bleu()函数。我们开始吧。
Python中计算Bleu分数
要计算BLEU分数,我们需要以令牌的形式提供参考和候选句子。
我们将在这一节中学习如何做到这一点并计算分数。让我们从导入必要的模块开始。
1from nltk.translate.bleu_score import sentence_bleu
现在我们可以以列表的形式输入参考句子。我们还需要在将句子传递给sentence_bleu()函数之前从句子中创建标记。
1.输入拆分句子
我们的参考列表中的句子是:
1'this is a dog'
2 'it is dog
3 'dog it is'
4 'a dog, it is'
我们可以使用Split函数将它们拆分成令牌。
1reference = [
2 'this is a dog'.split(),
3 'it is dog'.split(),
4 'dog it is'.split(),
5 'a dog, it is'.split()
6]
7print(reference)
发帖主题:Re:Kolibrios
1[['this', 'is', 'a', 'dog'], ['it', 'is', 'dog'], ['dog', 'it', 'is'], ['a', 'dog,', 'it', 'is']]
这就是代币形式的句子的样子。现在我们可以调用语句_BLEU()函数来计算分数。
2.计算BLEU在巨蟒中的得分
要计算分数,请使用以下代码行:
1candidate = 'it is dog'.split()
2print('BLEU score -> {}'.format(sentence_bleu(reference, candidate)))
发帖主题:Re:Kolibrios
1BLEU score -> 1.0
我们得到了满分1,因为候选句子属于参考集合。让我们再试一次。
1candidate = 'it is a dog'.split()
2print('BLEU score -> {}'.format(sentence_bleu(reference, candidate)))
发帖主题:Re:Kolibrios
1BLEU score -> 0.8408964152537145
我们在我们的参考集合中有句子,但它不完全匹配。这就是为什么我们得了0.84分。
3.BLEU分数在Python语言中实现的完整代码
以下是本部分的完整代码。
1from nltk.translate.bleu_score import sentence_bleu
2reference = [
3 'this is a dog'.split(),
4 'it is dog'.split(),
5 'dog it is'.split(),
6 'a dog, it is'.split()
7]
8candidate = 'it is dog'.split()
9print('BLEU score -> {}'.format(sentence_bleu(reference, candidate )))
10
11candidate = 'it is a dog'.split()
12print('BLEU score -> {}'.format(sentence_bleu(reference, candidate)))
4.计算n元语法分数
在匹配句子时,您可以选择希望模型一次匹配的词数。例如,您可以选择一次匹配一个单词(1-gram)。或者,您也可以选择匹配对(2-gram) 或** 三元组(3-gram)** 中的单词。
在本节中,我们将学习如何计算这些n-gram分数。
在语句_BLEU()函数 中,您可以传递一个带有与单个克对应的权重的参数。
例如,要单独计算克分数,可以使用以下权重。
1Individual 1-gram: (1, 0, 0, 0)
2Individual 2-gram: (0, 1, 0, 0).
3Individual 3-gram: (1, 0, 1, 0).
4Individual 4-gram: (0, 0, 0, 1).
下面给出了相同的Python代码:
1from nltk.translate.bleu_score import sentence_bleu
2reference = [
3 'this is a dog'.split(),
4 'it is dog'.split(),
5 'dog it is'.split(),
6 'a dog, it is'.split()
7]
8candidate = 'it is a dog'.split()
9
10print('Individual 1-gram: %f' % sentence_bleu(reference, candidate, weights=(1, 0, 0, 0)))
11print('Individual 2-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 1, 0, 0)))
12print('Individual 3-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 0, 1, 0)))
13print('Individual 4-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 0, 0, 1)))
发帖主题:Re:Kolibrios
1Individual 1-gram: 1.000000
2Individual 2-gram: 1.000000
3Individual 3-gram: 0.500000
4Individual 4-gram: 1.000000
默认情况下,语句_BLEU()函数计算累计4克BLEU分数 ,也称为** BLEU-4** 。BLEU-4的权重如下:
1(0.25, 0.25, 0.25, 0.25)
让我们看看BLEU-4代码:
1score = sentence_bleu(reference, candidate, weights=(0.25, 0.25, 0.25, 0.25))
2print(score)
输出:
10.8408964152537145
这是我们在没有添加n-gram权重的情况下得到的确切分数。
结论
本教程是关于计算BLEU在Python语言中的分数。我们了解了它是什么,以及如何计算BLEU的个人和累计n元组分数。希望你和我们一起学习得很开心!