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

195 lines
6.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#修改起始id,出处,文件名\n",
"starting_id = 14379\n",
"raworigin = \"2023年空中课堂高三复习题\"\n",
"filename = r\"D:\\temp\\空中课堂第三批.tex\"\n",
"editor = \"20230203\\t王伟叶\"\n",
"indexed = False\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"添加题号014379, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014380, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014381, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014382, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014383, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014384, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014385, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014386, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014387, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014388, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014389, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014390, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014391, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014392, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014393, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014394, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014395, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014396, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014397, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014398, 来源: 2023年空中课堂高三复习题13\n",
"添加题号014399, 来源: 2023年空中课堂高三复习题13\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": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'18'"
]
},
"execution_count": 5,
"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
}