Advertisement
witw78

测试代码

Apr 15th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import re
  2. import pandas as pd
  3.  
  4. # 定义函数从字符串中获取第一个数字
  5. def get_first_number(string):
  6.     match = re.match(r'\d+(\.\d+)?', str(string))
  7.     if match:
  8.         return match.group()
  9.     else:
  10.         return None
  11.  
  12. def load_data(file_path):
  13.     try:
  14.         data = pd.read_csv(file_path)
  15.         return data
  16.     except FileNotFoundError:
  17.         print(f"错误: 文件 {file_path} 未找到。")
  18.         return None
  19.     except Exception as e:
  20.         print(f"错误: 加载文件时发生未知错误: {e}")
  21.         return None
  22.  
  23. # 假设的文件路径,你需要替换为实际的文件路径
  24. file_path = '/home/xie.zhongwei/workspace/comparison_results.csv'
  25. data = load_data(file_path)
  26.  
  27. if data is not None:
  28.     def calculate_score(row, column):
  29.         ground_truth = str(row['output_ground_truth'])
  30.         response = str(row[column])
  31.         has_yes_ground_truth = "Yes" in ground_truth
  32.         has_no_ground_truth = "No" in ground_truth
  33.  
  34.         if has_yes_ground_truth:
  35.             return 1 if "Yes" in response else 0
  36.         elif has_no_ground_truth:
  37.             return 1 if "No" in response else 0
  38.         else:
  39.             return 1 if get_first_number(response) == get_first_number(ground_truth) else 0
  40.  
  41.     # 计算 response_op 与 output_ground_truth 的得分
  42.     data['score_op'] = data.apply(lambda row: calculate_score(row, 'response_op'), axis=1)
  43.  
  44.     # 计算 response_nop 与 output_ground_truth 的得分
  45.     data['score_nop'] = data.apply(lambda row: calculate_score(row, 'response_nop'), axis=1)
  46.  
  47.     # 输出结果
  48.     print(data[['response_op', 'response_nop', 'output_ground_truth', 'score_op', 'score_nop']])
  49.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement