This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具/添加题目到数据库.ipynb

252 lines
12 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#修改起始id,出处,文件名\n",
"starting_id = 14532\n",
"raworigin = \"2023年空中课堂高三复习课\"\n",
"filename = r\"C:\\Users\\weiye\\Documents\\wwy sync\\临时工作区\\空中课堂第五批.tex\"\n",
"editor = \"20230221\\t王伟叶\"\n",
"indexed = False\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"添加题号014532, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014533, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014534, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014535, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014536, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014537, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014538, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014539, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014540, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014541, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014542, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014543, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014544, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014545, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014546, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014547, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014548, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014549, 来源: 2023年空中课堂高三复习课22\n",
"添加题号014550, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014551, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014552, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014553, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014554, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014555, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014556, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014557, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014558, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014559, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014560, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014561, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014562, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014563, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014564, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014565, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014566, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014567, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014568, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014569, 来源: 2023年空中课堂高三复习课23\n",
"添加题号014570, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014571, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014572, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014573, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014574, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014575, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014576, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014577, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014578, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014579, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014580, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014581, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014582, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014583, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014584, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014585, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014586, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014587, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014588, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014589, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014590, 来源: 2023年空中课堂高三复习课25\n",
"添加题号014591, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014592, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014593, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014594, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014595, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014596, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014597, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014598, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014599, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014600, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014601, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014602, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014603, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014604, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014605, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014606, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014607, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014608, 来源: 2023年空中课堂高三复习课26\n",
"添加题号014609, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014610, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014611, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014612, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014613, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014614, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014615, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014616, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014617, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014618, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014619, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014620, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014621, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014622, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014623, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014624, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014625, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014626, 来源: 2023年空中课堂高三复习课29\n",
"添加题号014627, 来源: 2023年空中课堂高三复习课29\n"
]
}
],
"source": [
"import os,re,json\n",
"\n",
"\n",
"#从enumerate环境的字符串生成题目列表\n",
"def GenerateProblemListFromString(data):\n",
" try:\n",
" data = re.findall(r\"\\\\begin\\{document\\}([\\s\\S]*?)\\\\end\\{document\\}\",problems_string)[0]\n",
" except:\n",
" pass\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_raw = [p.strip() for p in re.findall(r\"\\\\item([\\s\\S]*?)\\\\enditem\",data)]\n",
" ProblemsList = []\n",
" for p in ProblemList_raw:\n",
" startpos = data.index(p)\n",
" tempdata = data[:startpos]\n",
" suflist = re.findall(r\"\\n(\\%[\\S]+)\\n\",tempdata)\n",
" if len(suflist) > 0:\n",
" suffix = suflist[-1].replace(\"%\",\"\").strip()\n",
" else:\n",
" suffix = \"\"\n",
" p_strip = re.sub(r\"\\n(\\%[\\S]+)$\",\"\",p).strip()\n",
" ProblemsList.append((p_strip,suffix))\n",
" return ProblemsList\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",
"\n",
"id = starting_id\n",
"for p_and_suffix in problems:\n",
" p = p_and_suffix[0]\n",
" suffix = p_and_suffix[1]\n",
" pid = str(id).zfill(6)\n",
" if pid in pro_dict:\n",
" duplicate_flag = True\n",
" if indexed == False:\n",
" origin = raworigin + suffix\n",
" else:\n",
" origin = raworigin + suffix + \"试题\" + str(id- starting_id+1)\n",
" NewProblem = CreateNewProblem(id = pid, content = p, origin = origin, dict = pro_dict,editor = editor)\n",
" print(\"添加题号\"+pid+\", \"+\"来源: \" + origin)\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": "mathdept",
"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"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "ff3c292c316ba85de6f1ad75f19c731e79d694e741b6f515ec18f14996fe48dc"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}