import os,re,json

# filename = "临时文件/相似题目.txt"
filename = "临时文件/相似1.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})\s",similar_text)

print(similar_types)
print(similar_problems)
samecount ,relcount, unrelcount = 0,0,0

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)
                samecount += 1
                print("相同题目已标注: %s <- %s"%(id1,id2))
            if not id1 in pro_dict[id2]["same"]:
                pro_dict[id2]["same"].append(id1)
                samecount += 1
                print("相同题目已标注: %s <- %s"%(id2,id1))
            
            
        elif i.upper() == "R":
            if not id2 in pro_dict[id1]["related"]:
                pro_dict[id1]["related"].append(id2)
                relcount += 1
                print("关联题目已标注: %s <- %s"%(id1,id2))
            if not id1 in pro_dict[id2]["related"]:
                pro_dict[id2]["related"].append(id1)
                relcount += 1
                print("关联题目已标注: %s <- %s"%(id2,id1))
            
        elif i.upper() == "N":
            if not id2 in pro_dict[id1]["unrelated"]:
                pro_dict[id1]["unrelated"].append(id2)
                unrelcount += 1
                print("无关题目已标注: %s <- %s"%(id1,id2))
            if not id1 in pro_dict[id2]["unrelated"]:
                pro_dict[id2]["unrelated"].append(id1)
                unrelcount += 1
                print("无关题目已标注: %s <- %s"%(id2,id1))
            


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

print("已标注相同题目 %d 项, 关联题目 %d 项, 无关题目 %d 项"%(samecount,relcount,unrelcount))

# 将题库字典转换为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)