文本生成-机器翻译评估指标及代码

BLEU-4

  • 简介:BLEU-4(Bilingual Evaluation Understudy):BLEU是一种常用的自动评估指标,用于测量候选文本与参考文本之间的相似性。BLEU-4表示使用四个单位长度的n-gram(n=1,2,3,4)进行计算。它通过比较候选文本中的n-gram是否出现在参考文本中来计算得分,同时也考虑了候选文本的长度。BLEU-4的取值范围通常在0到1之间,越接近1表示候选文本与参考文本越相似。
  • 公式
    BLEU-4 = \text{BP} \times \exp\left(\sum_{i=1}^{4} w_i \log(p_i)\right)
    其中,BP(Brevity Penalty)是长度惩罚项,用于惩罚较短的候选文本;w_i 是权重,平均分布为 1/4;p_i 是 n-gram 精确匹配率的几何平均。

METEOR

  • 简介:METEOR(Metric for Evaluation of Translation with Explicit ORdering):METEOR是另一种机器翻译评估指标,它考虑了候选文本与参考文本之间的词汇、语法和语义等多个层面的匹配。METEOR使用了单词精确匹配率、单词级别的重叠率和一些外部资源(如WordNet)来计算得分。METEOR的取值范围也在0到1之间,越接近1表示候选文本与参考文本越相似。

  • 公式
    \text{METEOR} = (1 - \alpha) \times P + \alpha \times R \times F
    其中,P 是单词精确匹配率,R是单词级别的重叠率,F 是综合评分;\alpha 是一个权衡精确度和召回率的参数。

ROUGE-L

  • 简介:ROUGE-L(Recall-Oriented Understudy for Gisting Evaluation - Longest Common Subsequence):ROUGE是一系列用于评估文本摘要生成系统的指标。ROUGE-L是其中的一种,它衡量候选文本与参考文本之间最长公共子序列(LCS)的相似性。LCS是指两个文本中具有最长共同序列的部分,ROUGE-L通过计算LCS的长度与参考文本总长度的比值来评估候选文本的质量。

  • 公式
    \text{ROUGE-L} = \frac{\text{LCS}}{\text{Ref_len}}
    其中,LCS 是候选文本与参考文本之间的最长公共子序列的长度;\text{Ref_len} 是参考文本的总长度。

CIDEr

  • 简介:CIDEr(Consensus-based Image Description Evaluation):CIDEr是一种用于评估图像描述生成系统的指标,但也可以应用于其他文本生成任务。CIDEr考虑了候选文本与参考文本之间的词汇多样性和一致性。它使用多个参考文本计算n-gram的权重,并考虑了候选文本与参考文本之间的词汇重叠率。CIDEr的得分范围没有限制,越高表示候选文本与参考文本越匹配。

  • 公式
    \text{CIDEr} = \frac{1}{n} \sum_{i=1}^{n} \frac{{\text{cider}{\text{sent}}}}{{\text{cider}{\text{ref}}}}
    其中,n 是候选文本的数量;\text{cider}{\text{sent}}是候选文本与参考文本之间的 n-gram 重叠率得分;\text{cider}{\text{ref}}是参考文本之间的 n-gram 重叠率得分。

代码

import nltk
from nltk.translate.bleu_score import sentence_bleu
from nltk.translate.meteor_score import meteor_score
from nltk.translate.bleu_score import SmoothingFunction
from rouge import Rouge
from cider import Cider

# 候选文本和参考文本
candidate = "the cat sat on the mat"
reference = "the cat is on the mat"

# BLEU-4
candidate_tokens = candidate.split()
reference_tokens = reference.split()
smoothing_function = SmoothingFunction().method1  # 平滑函数
bleu_4 = sentence_bleu([reference_tokens], candidate_tokens, weights=(0.25, 0.25, 0.25, 0.25), smoothing_function=smoothing_function)
print("BLEU-4:", bleu_4)

# METEOR
meteor = meteor_score([reference], candidate)
print("METEOR:", meteor)

# ROUGE-L
rouge = Rouge()
scores = rouge.get_scores(candidate, reference)
rouge_l = scores[0]["rouge-l"]["f"]
print("ROUGE-L:", rouge_l)

# CIDEr
cider = Cider()
cider_score = cider.compute_score({0: [reference]}, {0: [candidate]})  # 注意输入要求是字典形式
cider_score = cider_score[0]
cider_score_avg = sum(cider_score["CIDEr"]) / len(cider_score["CIDEr"])
print("CIDEr:", cider_score_avg)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容