import os
import json

def load_json(path):
    with open(path, "r", encoding="utf-8") as f:
        return json.load(f)

def main():
    # === 路径设置 ===
    ground_truth_folder =   # 文件夹A
    target_folder =              # 文件夹B

    # === 指定id列表（自己修改这里） ===
    id_dict = {
        "coding": [3, 14, 22, 28, 32, 33, 45, 52, 59, 61, 64, 70, 71, 78, 79, 102],
        "knowledge": [11, 17, 42, 85, 104, 110, 114, 116, 119, 126, 145, 157, 170, 175, 190, 205],
        "math": [2, 7, 22, 26, 44, 46, 50, 53, 80, 82, 85, 89, 90, 95, 100, 109],
        "reasoning": [8, 10, 13, 16, 32, 39, 42, 52, 55, 56, 57, 69, 89, 96, 97, 98],
        "roleplay": [2, 7, 10, 11, 13, 23, 39, 41, 55, 67, 105, 113, 121, 123, 134, 140],
        "writing": [22, 27, 29, 32, 33, 46, 50, 52, 65, 91, 92, 104, 115, 118, 133, 145]
    }

    # 六个文件
    json_files = ["coding.json", "knowledge.json", "math.json", 
                  "reasoning.json", "roleplay.json", "writing.json"]

    # 统计
    total_correct = 0
    total_count = 0

    for file in json_files:
        task = file.split(".")[0]  # e.g. coding
        ids = id_dict.get(task, [])

        if not ids:
            continue  # 如果没有指定id则跳过

        # 读取ground_truth和target
        ground_truth_data = load_json(os.path.join(ground_truth_folder, file))
        target_data = load_json(os.path.join(target_folder, file))

        # 转换为字典方便查找
        gt_dict = {int(item["id"]): item["label"] for item in ground_truth_data}
        target_dict = {int(item["id"]): item["judge result"] for item in target_data}

        correct = 0
        for k in ids:
            if k in gt_dict and k in target_dict:
                if gt_dict[k] == target_dict[k]:
                    correct += 1

        total_correct += correct
        total_count += len(ids)

        print(f"{task}: 正确数量 = {correct} / {len(ids)}")

    # 总结果
    acc = (total_correct / total_count * 100) if total_count > 0 else 0
    print("="*40)
    print(f"总正确数量 n = {total_correct}")
    print(f"总数据数量 N = {total_count}")
    print(f"准确率 ACC = {acc:.2f}%")

if __name__ == "__main__":
    main()
