{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#修改起始id,出处,文件名\n",
    "starting_id = 12781\n",
    "origin = \"2022届高三第二轮复习讲义\"\n",
    "filename = r\"C:\\Users\\weiye\\Documents\\wwy sync\\临时工作区\\第二轮讲义转码.tex\"\n",
    "editor = \"20230118\\t王伟叶\"\n",
    "indexed = False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os,re,json\n",
    "\n",
    "#从enumerate环境的字符串生成题目列表\n",
    "def GenerateProblemListFromString(data):\n",
    "    try:\n",
    "        data = re.findall(r\"\\\\begin\\{document\\}([\\s\\S]*?)\\\\end\\{document\\}\",data)[0]\n",
    "    except:\n",
    "        pass\n",
    "    data = re.sub(r\"\\n[\\s]*?\\%[\\s\\S]*?\\n\",\"\\n\",data)\n",
    "    data = re.sub(r\"\\n{2,}\",\"\\n\",data)\n",
    "    data = re.sub(r\"\\\\item\",r\"\\\\enditem\\\\item\",data)\n",
    "    data = re.sub(r\"\\\\end\\{enumerate\\}\",r\"\\\\enditem\",data)\n",
    "    ProblemList = [p.strip() for p in re.findall(r\"\\\\item([\\s\\S]*?)\\\\enditem\",data)]\n",
    "    return ProblemList\n",
    "\n",
    "# 创建新的空题目\n",
    "def CreateEmptyProblem(problem):\n",
    "    NewProblem = problem.copy()\n",
    "    for field in NewProblem:\n",
    "        if type(NewProblem[field]) == str:\n",
    "            NewProblem[field] = \"\"\n",
    "        elif type(NewProblem[field]) == list:\n",
    "            NewProblem[field] = []\n",
    "        elif type(NewProblem[field]) == int or type(NewProblem[field]) == float:\n",
    "            NewProblem[field] = -1\n",
    "    return NewProblem\n",
    "\n",
    "# 创建新题目\n",
    "def CreateNewProblem(id,content,origin,dict,editor):\n",
    "    NewProblem = CreateEmptyProblem(dict[\"000001\"])\n",
    "    NewProblem[\"id\"] = str(id).zfill(6)\n",
    "    NewProblem[\"content\"] = content\n",
    "    NewProblem[\"origin\"] = origin\n",
    "    NewProblem[\"edit\"] = [editor]\n",
    "    return NewProblem\n",
    "\n",
    "duplicate_flag = False\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",
    "with open(filename,\"r\",encoding = \"utf8\") as f:\n",
    "    problems_string = f.read()\n",
    "problems = GenerateProblemListFromString(problems_string)\n",
    "\n",
    "id = starting_id\n",
    "for p in problems:\n",
    "    pid = str(id).zfill(6)\n",
    "    if pid in pro_dict:\n",
    "        duplicate_flag = True\n",
    "    if indexed == False:\n",
    "        NewProblem = CreateNewProblem(id = pid, content = p, origin = origin , dict = pro_dict,editor = editor)\n",
    "    else:\n",
    "        NewProblem = CreateNewProblem(id = pid, content = p, origin = origin + \"试题\" + str(id- starting_id+1), dict = pro_dict,editor = editor)\n",
    "    pro_dict[pid] = NewProblem\n",
    "    id += 1\n",
    "\n",
    "#按id排序生成字典\n",
    "sorted_dict_id = sorted(pro_dict)\n",
    "sorted_dict = {}\n",
    "for id in sorted_dict_id:\n",
    "    sorted_dict[id] = pro_dict[id]\n",
    "#将排序后的字典转为json\n",
    "\n",
    "if not duplicate_flag:\n",
    "    new_database = json.dumps(sorted_dict,indent = 4,ensure_ascii=False)\n",
    "    #写入json数据库文件\n",
    "    with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n",
    "        f.write(new_database)\n",
    "else:\n",
    "    print(\"题号有重复, 请检查.\\n\"*5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.15 ('pythontest')",
   "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.9.15 (main, Nov 24 2022, 14:39:17) [MSC v.1916 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
