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

285 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#修改起始id,出处,文件名\n",
"starting_id = 14400\n",
"raworigin = \"2023年空中课堂高三复习课\"\n",
"filename = r\"C:\\Users\\weiye\\Documents\\wwy sync\\临时工作区\\空中课堂第四批.tex\"\n",
"editor = \"20230209\\t王伟叶\"\n",
"indexed = False\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"添加题号014400, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014401, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014402, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014403, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014404, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014405, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014406, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014407, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014408, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014409, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014410, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014411, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014412, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014413, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014414, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014415, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014416, 来源: 2023年空中课堂高三复习课14\n",
"添加题号014417, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014418, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014419, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014420, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014421, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014422, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014423, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014424, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014425, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014426, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014427, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014428, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014429, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014430, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014431, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014432, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014433, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014434, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014435, 来源: 2023年空中课堂高三复习课15\n",
"添加题号014436, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014437, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014438, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014439, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014440, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014441, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014442, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014443, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014444, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014445, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014446, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014447, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014448, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014449, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014450, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014451, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014452, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014453, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014454, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014455, 来源: 2023年空中课堂高三复习课16\n",
"添加题号014456, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014457, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014458, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014459, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014460, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014461, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014462, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014463, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014464, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014465, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014466, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014467, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014468, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014469, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014470, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014471, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014472, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014473, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014474, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014475, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014476, 来源: 2023年空中课堂高三复习课19\n",
"添加题号014477, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014478, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014479, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014480, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014481, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014482, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014483, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014484, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014485, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014486, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014487, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014488, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014489, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014490, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014491, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014492, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014493, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014494, 来源: 2023年空中课堂高三复习课20\n",
"添加题号014495, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014496, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014497, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014498, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014499, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014500, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014501, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014502, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014503, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014504, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014505, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014506, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014507, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014508, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014509, 来源: 2023年空中课堂高三复习课21\n",
"添加题号014510, 来源: 2023年空中课堂高三复习课21\n"
]
}
],
"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\\}\",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\\%[\\dA-Za-z]+\",tempdata)\n",
" if len(suflist) > 0:\n",
" suffix = suflist[-1].replace(\"%\",\"\").strip()\n",
" else:\n",
" suffix = \"\"\n",
" ProblemsList.append((p,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": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"''"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"suffix"
]
},
{
"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
}