20221007 morning

This commit is contained in:
weiye.wang 2022-10-07 09:17:10 +08:00
parent 5e63e0b681
commit 94ba5fe5ac
2 changed files with 34 additions and 8 deletions

View File

@ -2,20 +2,20 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"#修改起始id,出处,文件名\n", "#修改起始id,出处,文件名\n",
"starting_id = 30103\n", "starting_id = 30105\n",
"origin = \"2005年上海秋考理科试题21\"\n", "origin = \"2005年上海秋考理科试题21\"\n",
"filename = r\"C:\\Users\\weiye\\Documents\\wwy sync\\临时工作区\\自拟题目.tex\"\n", "filename = r\"C:\\Users\\weiye\\Documents\\wwy sync\\临时工作区\\自拟题目1.tex\"\n",
"editor = \"20221003\\t王伟叶\"" "editor = \"20221003\\t王伟叶\""
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -55,6 +55,7 @@
" NewProblem[\"edit\"] = [editor]\n", " NewProblem[\"edit\"] = [editor]\n",
" return NewProblem\n", " return NewProblem\n",
"\n", "\n",
"duplicate_flag = False\n",
"\n", "\n",
"with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n", "with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n",
" database = f.read()\n", " database = f.read()\n",
@ -67,6 +68,8 @@
"id = starting_id\n", "id = starting_id\n",
"for p in problems:\n", "for p in problems:\n",
" pid = str(id).zfill(6)\n", " pid = str(id).zfill(6)\n",
" if pid in pro_dict:\n",
" duplicate_flag = True\n",
" NewProblem = CreateNewProblem(id = pid, content = p, origin = origin, dict = pro_dict,editor = editor)\n", " NewProblem = CreateNewProblem(id = pid, content = p, origin = origin, dict = pro_dict,editor = editor)\n",
" pro_dict[pid] = NewProblem\n", " pro_dict[pid] = NewProblem\n",
" id += 1\n", " id += 1\n",
@ -77,10 +80,14 @@
"for id in sorted_dict_id:\n", "for id in sorted_dict_id:\n",
" sorted_dict[id] = pro_dict[id]\n", " sorted_dict[id] = pro_dict[id]\n",
"#将排序后的字典转为json\n", "#将排序后的字典转为json\n",
"new_database = json.dumps(sorted_dict,indent = 4,ensure_ascii=False)\n", "\n",
"#写入json数据库文件\n", "if not duplicate_flag:\n",
"with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n", " new_database = json.dumps(sorted_dict,indent = 4,ensure_ascii=False)\n",
" f.write(new_database)" " #写入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)"
] ]
}, },
{ {

View File

@ -0,0 +1,19 @@
# 添加新题目到数据库
## 工具文件
``添加题目到数据库.ipynb``
## 功能简介
从一个.tex文件中提取题目内容(用\item开头, 放置在\begin{enumerate}\end{enumerate}环境中), 并从起始id开始将.tex文件中题目逐题添加到数据库中
## 用法
第一个单元格中需要设置四个位置:
- ``starting_id``: 整数. 起始id号, 这个号码注意不要和已有的id重复, 可在``Problems.json``中查询空闲id. 处理中如果有重复id的话程序会报重复, 并且不会写入新题目到数据库
- ``origin``: 字符串. 题目出处, 同一批题目的出处字符串一致, 尽可能详尽, 对应到``Problems.json``数据库中的``origin``项
- ``filename``: 字符串. .tex文件的文件名(含路径)
- ``editor``: 字符串. 编辑及收录者姓名, 用"\t"分隔日期(yyyymmdd)及姓名, 例如"20221007\t张三"