### 这个是将三份人标的数据统一对比得出最终的答案final answer
import json

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

def save_json(data, path):
    with open(path, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)

def determine_correct_answer(labels):
    """
    判断 correct answer：
    如果有两个或三个相同，返回相同的那个
    如果三个都不同，返回 None
    """
    if labels[0] == labels[1] == labels[2]:
        return labels[0]
    elif labels[0] == labels[1]:
        return labels[0]
    elif labels[0] == labels[2]:
        return labels[0]
    elif labels[1] == labels[2]:
        return labels[1]
    else:
        return None

def main(json1_path, json2_path, json3_path, output_path):
    data1 = load_json(json1_path)
    data2 = load_json(json2_path)
    data3 = load_json(json3_path)

    result = []
    null_ids = []

    # 假设三个文件的条数和 id 顺序一致，从 id 1 开始依次比较
    for i in range(len(data1)):
        entry1 = data1[i]
        entry2 = data2[i]
        entry3 = data3[i]

        id1 = entry1["id"]
        id2 = entry2["id"]
        id3 = entry3["id"]

        # 检查 id 是否一致
        if not (id1 == id2 == id3):
            print(f"警告：第 {i+1} 条数据 id 不一致：{id1}, {id2}, {id3}")
        
        labels = [entry1["label"], entry2["label"], entry3["label"]]
        correct = determine_correct_answer(labels)
        if correct is None:
            null_ids.append(id1)

        # 用 data1 的内容为基础，加上 correct answer
        new_entry = {
            "id": entry1["id"],
            "question": entry1["question"],
            "response_A": entry1["response_A"],
            "response_B": entry1["response_B"],
            "model_A": entry1["model_A"],
            "model_B": entry1["model_B"],
            "label": correct
        }

        result.append(new_entry)

    # 打印 correct answer 为 null 的 id
    print("correct answer 为 null 的 id：")
    print(null_ids)

    # 保存结果
    save_json(result, output_path)

if __name__ == "__main__":
    # 修改成你的文件路径
    json1_path = 
    json2_path = 
    json3_path =
    output_path =

    main(json1_path, json2_path, json3_path, output_path)
