【python】读取xlsx文件,测Rouge-1,Rouge-2,Rouge-L指标

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要读取 xlsx 文件,可以使用 Python 中的 openpyxl 库。以下是一个简单的例子: ```python import openpyxl # 打开 Excel 文件 wb = openpyxl.load_workbook('example.xlsx') # 选择要操作的表单 sheet = wb['Sheet1'] # 读取单元格的值 cell_value = sheet['A1'].value # 遍历行 for row in sheet.rows: for cell in row: print(cell.value) ``` 在这个例子中,我们打开了名为 `example.xlsx` 的 Excel 文件,并选择了其中的 `Sheet1` 表单。然后我们读取了一个单元格的值,并遍历了整个表单的所有单元格。 ### 回答2: Python可以使用openpyxl库来读取xlsx文件。首先,需要安装openpyxl库,可以使用pip install openpyxl命令来安装。然后,可以使用如下代码来读取xlsx文件: ```python from openpyxl import load_workbook # 打开xlsx文件 workbook = load_workbook(filename='example.xlsx') # 获取第一个工作表 sheet = workbook.active # 遍历工作表中的每一行 for row in sheet.iter_rows(): # 遍历行中的每一列 for cell in row: # 打印每个单元格的值 print(cell.value) # 关闭工作表 workbook.close() ``` 以上代码首先使用`load_workbook`函数打开xlsx文件,可以使用`filename`参数指定文件路径。然后,使用`active`属性获取文件中的第一个工作表,也可以使用`get_sheet_by_name`函数来根据工作表的名称获取指定的工作表。接下来,可以使用`iter_rows`函数遍历工作表中的每一行,再使用内层循环遍历行中的每一列。通过`value`属性可以获取每个单元格的值。 最后,记得使用`close`方法关闭工作表,释放资源。 通过以上代码,可以读取xlsx文件中的数据,并进行相应的操作。 ### 回答3: Python读取xlsx文件可以使用第三方库openpyxl。首先,需要安装openpyxl库,可以通过命令`pip install openpyxl`进行安装。 然后,可以使用openpyxl库中的load_workbook函数来加载xlsx文件。load_workbook函数接受文件路径作为参数,返回一个Workbook对象,表示整个工作簿。例如,可以使用如下代码加载一个名为“example.xlsx”的文件: ``` from openpyxl import load_workbook # 加载xlsx文件 wb = load_workbook('example.xlsx') ``` 接下来,可以通过Workbook对象获取工作簿中的表格。可以使用wb.sheetnames属性获取所有表格名称,并通过wb[sheetname]来获取指定表格。例如,可以使用如下代码获取名为“Sheet1”的表格: ``` # 获取表格 sheet = wb['Sheet1'] ``` 获取表格后,可以通过遍历行和列的方式来获取单元格的数据。例如,可以使用如下代码遍历Sheet1表格中的所有数据: ``` # 遍历数据 for row in sheet.iter_rows(): for cell in row: print(cell.value) ``` 最后,记得使用Workbook对象的close方法来关闭文件: ``` # 关闭文件 wb.close() ``` 通过以上步骤,就可以使用Python读取xlsx文件了。读取到的数据可以根据需求进行处理和分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值