【文本生成评价指标】 BLEU原理及代码示例py

【文本生成评价指标】 ROUGE原理及代码示例py
【文本生成评价指标】 METEOR原理及代码示例py
【文本生成评价指标】 DISTINCT原理及代码示例py
【文本生成评价指标】文本生成客观评价指标总结py

1. BLEU原理

Bleu (Bilingual Evaluation Understudy) 是衡量一个有多个正确输出结果的模型的精确度的评估指标。比较候选译文和参考译文里的 n-gram 的重合程度。多用于翻译质量评估。

Bleu 可以说是所有评价指标的鼻祖,它的核心思想是比较候选译文和参考译文里的 n-gram 的重合程度,重合程度越高就认为译文质量越高。unigram用于衡量单词翻译的准确性,高阶n-gram用于衡量句子翻译的流畅性。 实践中,通常是取N=1~4,然后对进行加权平均。

2. 代码实现

权重被指定为一个数组,其中每个索引对应相应次序的n元组。
以1-gram匹配的BLEU分数为例,individual_bleu(reference, candidate) 指定1-gram权重为1,对于2元、3元和4元指定权重为0,也就是权重为(1,0,0,0)。

from nltk.translate.bleu_score import sentence_bleu

def individual_bleu(reference, candidate):
    
    bleu_1_gram = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))
    bleu_2_gram = sentence_bleu(reference, candidate, weights=(0, 1, 0, 0))
    bleu_3_gram = sentence_bleu(reference, candidate, weights=(0, 0, 1, 0))
    bleu_4_gram = sentence_bleu(reference, candidate, weights=(0, 0, 0, 1))

    # print('bleu 1-gram: %f' % bleu_1_gram)
    # print('bleu 2-gram: %f' % bleu_2_gram)
    # print('bleu 3-gram: %f' % bleu_3_gram)
    # print('bleu 4-gram: %f' % bleu_4_gram)

    return bleu_1_gram, bleu_2_gram, bleu_3_gram, bleu_4_gram

虽然我们可以计算出单独的BLEU分数,但这并不是使用这个方法的初衷,而且得出的分数也没有过多的含义,或者看起来具有说明性。

累加分数是指对从1到n的所有单独n-gram分数的计算,通过计算加权几何平均值来对它们进行加权计算。以BLEU-4分数计算为例,cumulative_bleu(reference, candidate) 计算的是累加的4元组BLEU分数,BLEU-4对1元组,2元组,3元组和4元组分数的权重为1/4(25%)或0.25。

from nltk.translate.bleu_score import sentence_bleu
def cumulative_bleu(reference, candidate):

    bleu_1_gram = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))
    bleu_2_gram = sentence_bleu(reference, candidate, weights=(0.5, 0.5, 0, 0))
    bleu_3_gram = sentence_bleu(reference, candidate, weights=(0.33, 0.33, 0.33, 0))
    bleu_4_gram = sentence_bleu(reference, candidate, weights=(0.25, 0.25, 0.25, 0.25))

    # print('bleu 1-gram: %f' % bleu_1_gram)
    # print('bleu 2-gram: %f' % bleu_2_gram)
    # print('bleu 3-gram: %f' % bleu_3_gram)
    # print('bleu 4-gram: %f' % bleu_4_gram)

    return bleu_1_gram, bleu_2_gram, bleu_3_gram, bleu_4_gram

这段代码使用 Python 的 Natural Language Toolkit(nltk)库中的 sentence_bleu 函数来计算生成文本和参考文本之间的 Bleu 指标。

首先,代码导入 nltk 库,并定义一个包含生成文本的字符串 generated_text 和一个包含参考文本的字符串列表 reference_texts。接下来,代码使用 sentence_bleu 函数计算生成文本和参考文本之间的 Bleu 指标,并将结果存储在变量 bleu 中。最后,代码输出一个消息,显示计算出的 Bleu 指标。

3. 代码测试

# 生成文本
generated_text = "This is some generated text."

# 参考文本列表
reference_texts = ["This is a reference text.", "This is another reference text."]

# 计算 Bleu 指标
i_bleu = individual_bleu(reference_texts, generated_text)
c_bleu = cumulative_bleu(reference_texts, generated_text)

# 打印结果
print("The Bleu score is:", i_bleu)
print("The Bleu score is:", c_bleu)

参考链接:BLEU原理及代码示例

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz_Lambda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值