import sacrebleu

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 提供了高效且易用的接口,适用于多种文本生成任务的评估。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值