import os,re,json

filename = "临时文件/相似题目.txt"

# 读取题库数据并转换为字典
with open(r"../题库0.3/Problems.json","r",encoding = "utf8") as f:
    database = f.read()
pro_dict = json.loads(database)

# 读取已分类的相似文件列表
with open(filename,"r",encoding = "utf8") as f:
    similar_text = "\n"+f.read()

similar_types = re.findall(r"\n[\d]\.[\d]{4}[\s]*([srSRnN ])[\s]*\n",similar_text)
similar_problems = re.findall(r"\n([\d]{6}) ",similar_text)

if len(similar_types) * 2 == len(similar_problems):
    for i in similar_types:
        id1 = similar_problems.pop(0)
        id2 = similar_problems.pop(0)
        if i.upper() == "S":
            if not id2 in pro_dict[id1]["same"]:
                pro_dict[id1]["same"].append(id2)
            if not id1 in pro_dict[id2]["same"]:
                pro_dict[id2]["same"].append(id1)
            print("相同题目已标注:",id1,id2)
        elif i.upper() == "R":
            if not id2 in pro_dict[id1]["related"]:
                pro_dict[id1]["related"].append(id2)
            if not id1 in pro_dict[id2]["related"]:
                pro_dict[id2]["related"].append(id1)
            print("关联题目已标注:",id1,id2)

else:
    print("相似程度数据:",len(similar_types),"个, 相似题目:",len(similar_problems),"题. 数据有问题, 请检查.")


# 将题库字典转换为json文件并保存至原位
database = json.dumps(pro_dict,indent=4,ensure_ascii=False)
with open(r"../题库0.3/Problems.json","w",encoding = "utf8") as f:
    f.write(database)