import json import os import csv # 定义文件所在的目录 input_dir = 'E:\python-testn\pythonProject3\hh_1\evaluate_result' # 获取目录中的所有文件 files = os.listdir(input_dir) # with open("token_counts_QS.csv","w", newline='') as csvfile: # writer = csv.writer(csvfile) # writer.writerow(["Model", "token_subset_1", "token_subset_2","token_subset_3"]) # with open("line_counts_QS.csv","w", newline='') as csvfile: # writer = csv.writer(csvfile) # writer.writerow(["Model", "line_subset_1", "line_subset_2","line_subset_3"]) with open("CC_QS.csv", "w", newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(["Model", "CC_subset_1", "CC_subset_2","CC_subset_3"]) for file_name in files: # 构建完整的文件路径 input_file_path = os.path.join(input_dir, file_name) first_underscore_index = file_name.find('_') # 找到最后一个 - 的位置 last_dash_index = file_name.rfind('-') model_name = file_name[first_underscore_index + 1:last_dash_index] print(model_name) with open(input_file_path,"r",encoding="utf-8") as file: data1=json.load(file) with open("QS.json", "r", encoding="utf-8") as file: data2=json.load(file) sum0=0 count0=0 sum1=0 count1=0 sum2=0 count2=0 for (item1,item2) in zip(data1["humaneval"]["pass@1"],data2): # #按照token个数划分后的评估结果 # if item2["token_diff"] == 0: # index, value = item1 # print(item2["token_diff"],index,value) # sum0=sum0+value # count0=count0+1 # if item2["token_diff"] == 1: # index, value = item1 # print(item2["token_diff"], index, value) # sum1=sum1+value # count1=count1+1 # if item2["token_diff"] == 2: # index, value = item1 # print(item2["token_diff"], index, value) # sum2=sum2+value # count2=count2+1 #按照行数划分后的评估结果 # if item2["line_diff"] == 0: # index, value = item1 # print(item2["line_diff"],index,value) # sum0=sum0+value # count0=count0+1 # if item2["line_diff"] == 1: # index, value = item1 # print(item2["line_diff"], index, value) # sum1=sum1+value # count1=count1+1 # if item2["line_diff"] == 2: # index, value = item1 # print(item2["line_diff"], index, value) # sum2=sum2+value # count2=count2+1 #按照圈复杂度划分后的评估结果 if item2["CC_diff"] == 0: index, value = item1 print(item2["CC_diff"],index,value) sum0=sum0+value count0=count0+1 if item2["CC_diff"] == 1: index, value = item1 print(item2["CC_diff"], index, value) sum1=sum1+value count1=count1+1 if item2["CC_diff"] == 2: index, value = item1 print(item2["CC_diff"], index, value) sum2=sum2+value count2=count2+1 mean0=round(sum0/count0*100,2) mean1=round(sum1/count1*100,2) mean2=round(sum2/count2*100,2) print("count_result!!") print(count0,count1,count2) print(mean0,mean1,mean2) # with open("token_counts_QS.csv", mode='a', newline='', encoding='utf-8') as file: # writer = csv.writer(file) # writer.writerow([model_name,mean0,mean1,mean2]) with open("CC_QS.csv", mode='a', newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow([model_name,mean0,mean1,mean2])