import pandas as pd
import jieba
from rouge import Rouge
import sys
# 增加递归深度限制
sys.setrecursionlimit(10000)
def preprocess_text(text):
# 使用 jieba 进行中文分词
words = jieba.lcut(text)
return ' '.join(words)
def calculate_rouge_scores(reference, candidate):
rouge = Rouge()
# 预处理文本
reference = preprocess_text(reference)
candidate = preprocess_text(candidate)
# 计算ROUGE-1, ROUGE-2, ROUGE-L
scores = rouge.get_scores(candidate, reference)[0]
# 返回三个指标的 recall 得分
return {
'ROUGE-1': scores['rouge-1']['r'],
'ROUGE-2': scores['rouge-2']['r'],
'ROUGE-L': scores['rouge-l']['r']
}
def main():
# 读取xlsx文件
file_path = '读取路径'
df = pd.read_excel(file_path)
# 打印列名以进行调试
print("Columns in the dataframe:", df.columns)
# 假设数据有一列 '参考答案' 和五列 'one', 'two', 'three', 'four', 'five'
references = df['参考答案']
candidate_columns = ['A', 'B', 'C', 'AB', 'AC', 'BC']
# 计算每个reference与每个candidate的ROUGE指标
for candidate in candidate_columns:
if candidate not in df.columns:
print(f"Column {candidate} not found in the dataframe.")
continue
rouge_1_scores = []
rouge_2_scores = []
rouge_l_scores = []
for ref, cand in zip(references, df[candidate]):
if pd.isna(ref) or pd.isna(cand):
rouge_1_scores.append(0)
rouge_2_scores.append(0)
rouge_l_scores.append(0)
continue
rouge_scores = calculate_rouge_scores(ref, cand)
rouge_1_scores.append(rouge_scores['ROUGE-1'])
rouge_2_scores.append(rouge_scores['ROUGE-2'])
rouge_l_scores.append(rouge_scores['ROUGE-L'])
# 将ROUGE指标添加到DataFrame中
df[f'ROUGE-1_{candidate}'] = rouge_1_scores
df[f'ROUGE-2_{candidate}'] = rouge_2_scores
df[f'ROUGE-L_{candidate}'] = rouge_l_scores
# 保存结果到新的xlsx文件
output_path = '保存路径'
df.to_excel(output_path, index=False)
print("Saved ROUGE scores to:", output_path)
if __name__ == "__main__":
main()
06-16
530
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-08
380
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-09