{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os,re,json\n",
    "\n",
    "\n",
    "# 读取题库数据并转换为字典\n",
    "with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "pro_dict = json.loads(database)\n",
    "\n",
    "# 读取已分类的相似文件列表\n",
    "with open(\"临时文件/相似题目.txt\",\"r\",encoding = \"utf8\") as f:\n",
    "    similar_text = \"\\n\"+f.read()\n",
    "\n",
    "similar_types = re.findall(r\"\\n[\\d]\\.[\\d]{4}[\\s]*([srSRnN ])[\\s]*\\n\",similar_text)\n",
    "similar_problems = re.findall(r\"\\n([\\d]{6}) \",similar_text)\n",
    "\n",
    "if len(similar_types) * 2 == len(similar_problems):\n",
    "    for i in similar_types:\n",
    "        id1 = similar_problems.pop(0)\n",
    "        id2 = similar_problems.pop(0)\n",
    "        if i.upper() == \"S\":\n",
    "            if not id2 in pro_dict[id1][\"same\"]:\n",
    "                pro_dict[id1][\"same\"].append(id2)\n",
    "            if not id1 in pro_dict[id2][\"same\"]:\n",
    "                pro_dict[id2][\"same\"].append(id1)\n",
    "            print(\"相同题目已标注:\",id1,id2)\n",
    "        elif i.upper() == \"R\":\n",
    "            if not id2 in pro_dict[id1][\"related\"]:\n",
    "                pro_dict[id1][\"related\"].append(id2)\n",
    "            if not id1 in pro_dict[id2][\"related\"]:\n",
    "                pro_dict[id2][\"related\"].append(id1)\n",
    "            print(\"关联题目已标注:\",id1,id2)\n",
    "\n",
    "else:\n",
    "    print(\"相似程度数据:\",len(similar_types),\"个, 相似题目:\",len(similar_problems),\"题. 数据有问题, 请检查.\")\n",
    "\n",
    "\n",
    "# 将题库字典转换为json文件并保存至原位\n",
    "database = json.dumps(pro_dict,indent=4,ensure_ascii=False)\n",
    "with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(database)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.8 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d311ffef239beb3b8f3764271728f3972d7b090c974f8e972fcdeedf230299ac"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
