当前位置: 首页 > news >正文

【NLP 计算句子之间的BLEU和ROUGE分数】

安装依赖

pip install nltk rouge-score

批量处理代码

from nltk.translate.bleu_score import corpus_bleu, SmoothingFunction
from rouge_score import rouge_scorer
import nltk# 下载必要的资源(第一次运行需要)
nltk.download('wordnet')  # 用于 BLEU 的 tokenizer 等
nltk.download('punkt')     # 用于 tokenizedef batch_bleu(references, candidates):"""计算批量 BLEU 分数 (BLEU-4)Args:references: List of lists of reference sentences (每项是多个参考答案列表)candidates: List of candidate sentences (模型生成的句子列表)Returns:float: 平均 BLEU-4 分数"""smoothing = SmoothingFunction()# 将每个参考句子 tokenizetokenized_references = [[nltk.word_tokenize(sent) for sent in ref] for ref in references]# 将每个候选句子 tokenizetokenized_candidates = [nltk.word_tokenize(sent) for sent in candidates]# 计算 corpus BLEUbleu_score = corpus_bleu(tokenized_references,tokenized_candidates,weights=(0.25, 0.25, 0.25, 0.25),smoothing_function=smoothing.method1)return bleu_scoredef batch_rouge(references, candidates):"""计算批量 ROUGE 分数 (ROUGE-1, ROUGE-2, ROUGE-L)Args:references: List of reference sentences (每个样本一个参考句)candidates: List of candidate sentencesReturns:dict: {'rouge1': f1, 'rouge2': f1, 'rougeL': f1}"""scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'], use_stemmer=True)scores = {'rouge1': [], 'rouge2': [], 'rougeL': []}for ref, cand in zip(references, candidates):score = scorer.score(ref, cand)scores['rouge1'].append(score['rouge1'].fmeasure)scores['rouge2'].append(score['rouge2'].fmeasure)scores['rougeL'].append(score['rougeL'].fmeasure)avg_scores = {k: sum(v)/len(v) for k, v in scores.items()}return avg_scoresdef evaluate_all(references, candidates):"""同时计算 BLEU 和 ROUGE 的批量评估函数Args:references: List of reference sentencescandidates: List of candidate sentencesReturns:dict: 包含 BLEU 和 ROUGE 的平均分数"""bleu = batch_bleu([[ref] for ref in references], candidates)rouge = batch_rouge(references, candidates)return {'BLEU': round(bleu, 4),'ROUGE-1': round(rouge['rouge1'], 4),'ROUGE-2': round(rouge['rouge2'], 4),'ROUGE-L': round(rouge['rougeL'], 4)}

测试代码

# 示例数据:批量输入
references = ["the cat is on the mat","a dog is playing in the garden"
]candidates = ["the cat sat on the mat","a dog plays in the garden"
]# 调用评估函数
results = evaluate_all(references, candidates)
print("Evaluation Results:", results)

输出

Evaluation Results: {'BLEU': 0.1966, 'ROUGE-1': 0.8782, 'ROUGE-2': 0.6636, 'ROUGE-L': 0.8782}

相关文章:

  • 图像识别与 OCR 应用实践
  • 学术论文的科研流程概述 视频会议记录
  • GpuGeek全栈AI开发实战:从零构建企业级大模型生产管线(附完整案例)
  • stm32 ADC单通道转换
  • day20-线性表(链表II)
  • C++(2)
  • 牛顿迭代公式
  • MySQL中的索引下推技术(ICP)
  • 通用软件项目技术报告 - 导读IV(终)
  • MATLAB实现振幅调制(AM调制信号)
  • 构建现代化WPF应用:数据驱动开发与高级特性解析
  • Jenkins里构建一个简单流水线
  • 【常用算法:排序篇】6.归并排序双刃剑:逆序数秒算与搜索引擎海量数据排序
  • Virtualized Table 虚拟化表格 el-table-v2 表头分组 多级表头的简单示例
  • 机器学习基础课程-5-课程实验
  • 使用Docker部署MongoDB
  • AI时代的弯道超车之第七章:如何用AI赋能创业?
  • springboot项目启动报错:找不到或无法加载主类
  • SVNAdmin管理使用教程
  • ECharts:数据可视化的强大引擎
  • 普京批准俄方与乌克兰谈判代表团人员名单
  • 盛和资源海外找稀土矿提速:拟超7亿元收购匹克,加快推动坦桑尼亚项目
  • 沪喀同心|为新疆青少年提供科普大餐,“小小博物家(喀什版)”启动
  • 外企聊营商|特雷通集团:税务服务“及时雨”
  • “75万买299元路由器”事件进展:重庆市纪委等三部门联合介入调查
  • 小米SU7 Ultra风波升级:数百名车主要求退车,车主喊话雷军“保持真诚”