BLEU-4
- 簡(jiǎn)介:BLEU-4(Bilingual Evaluation Understudy):BLEU是一種常用的自動(dòng)評(píng)估指標(biāo),用于測(cè)量候選文本與參考文本之間的相似性。BLEU-4表示使用四個(gè)單位長(zhǎng)度的n-gram(n=1,2,3,4)進(jìn)行計(jì)算。它通過(guò)比較候選文本中的n-gram是否出現(xiàn)在參考文本中來(lái)計(jì)算得分,同時(shí)也考慮了候選文本的長(zhǎng)度。BLEU-4的取值范圍通常在0到1之間,越接近1表示候選文本與參考文本越相似。
-
公式:
其中,BP(Brevity Penalty)是長(zhǎng)度懲罰項(xiàng),用于懲罰較短的候選文本;是權(quán)重,平均分布為 1/4;
是 n-gram 精確匹配率的幾何平均。
METEOR
簡(jiǎn)介:METEOR(Metric for Evaluation of Translation with Explicit ORdering):METEOR是另一種機(jī)器翻譯評(píng)估指標(biāo),它考慮了候選文本與參考文本之間的詞匯、語(yǔ)法和語(yǔ)義等多個(gè)層面的匹配。METEOR使用了單詞精確匹配率、單詞級(jí)別的重疊率和一些外部資源(如WordNet)來(lái)計(jì)算得分。METEOR的取值范圍也在0到1之間,越接近1表示候選文本與參考文本越相似。
公式:
其中,是單詞精確匹配率,
是單詞級(jí)別的重疊率,
是綜合評(píng)分;
是一個(gè)權(quán)衡精確度和召回率的參數(shù)。
ROUGE-L
簡(jiǎn)介:ROUGE-L(Recall-Oriented Understudy for Gisting Evaluation - Longest Common Subsequence):ROUGE是一系列用于評(píng)估文本摘要生成系統(tǒng)的指標(biāo)。ROUGE-L是其中的一種,它衡量候選文本與參考文本之間最長(zhǎng)公共子序列(LCS)的相似性。LCS是指兩個(gè)文本中具有最長(zhǎng)共同序列的部分,ROUGE-L通過(guò)計(jì)算LCS的長(zhǎng)度與參考文本總長(zhǎng)度的比值來(lái)評(píng)估候選文本的質(zhì)量。
公式:
其中,是候選文本與參考文本之間的最長(zhǎng)公共子序列的長(zhǎng)度;
是參考文本的總長(zhǎng)度。
CIDEr
簡(jiǎn)介:CIDEr(Consensus-based Image Description Evaluation):CIDEr是一種用于評(píng)估圖像描述生成系統(tǒng)的指標(biāo),但也可以應(yīng)用于其他文本生成任務(wù)。CIDEr考慮了候選文本與參考文本之間的詞匯多樣性和一致性。它使用多個(gè)參考文本計(jì)算n-gram的權(quán)重,并考慮了候選文本與參考文本之間的詞匯重疊率。CIDEr的得分范圍沒(méi)有限制,越高表示候選文本與參考文本越匹配。
公式:
其中,是候選文本的數(shù)量;
是候選文本與參考文本之間的 n-gram 重疊率得分;
是參考文本之間的 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 # 平滑函數(shù)
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)