sacrebleu 是一个用于计算 BLEU(Bilingual Evaluation Understudy)分数的 Python 库。BLEU 是一种常用的机器翻译评估指标,用于衡量生成的翻译文本与参考翻译文本之间的相似度。sacrebleu 提供了简单易用的接口,可以方便地计算 BLEU 分数。
安装
pip install sacrebleu
基本用法
import sacrebleu
# 定义参考翻译和生成的翻译
references = [
"The cat is on the mat.",
"There is a cat on the mat."
]
candidates = [
"The cat is sitting on the mat."
]
# 计算 BLEU 分数
bleu = sacrebleu.corpus_bleu(candidates, [references])
print(bleu.score) # 输出 BLEU 分数
参数说明
- candidates:生成的翻译文本列表。
- references:参考翻译文本列表,可以有多个参考翻译。
- smoothing:是否使用平滑方法,避免因某些 n-gram 未出现而导致分数为 0。
示例
import sacrebleu
# 定义参考翻译和生成的翻译
references = [
["The cat is on the mat.", "There is a cat on the mat."],
["The dog is on the mat.", "There is a dog on the mat."]
]
candidates = [
"The cat is sitting on the mat.",
"The dog is lying on the mat."
]
# 计算 BLEU 分数
bleu = sacrebleu.corpus_bleu(candidates, references, smooth_method='exp')
print(bleu.score) # 输出 BLEU 分数
应用场景
- 机器翻译评估:用于评估机器翻译模型生成的翻译质量。
- 文本生成任务:在其他文本生成任务中,如文本摘要、对话系统等,评估生成文本与参考文本的相似度。
sacrebleu 提供了高效且易用的接口,适用于多种文本生成任务的评估。
1万+

被折叠的 条评论
为什么被折叠?



