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

164 lines
8.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"题号: 002010 , 字段: ans 中已修改数据: $6$或$-2+12\\mathrm{i}$或$2-8\\mathrm{i}$\n",
"题号: 002020 , 字段: ans 中已修改数据: $1+\\dfrac{14}5\\mathrm{i}$\n",
"题号: 003521 , 字段: ans 中已修改数据: $\\dfrac\\pi 2$\n",
"题号: 002018 , 字段: ans 中已修改数据: $4$\n",
"题号: 003524 , 字段: ans 中已修改数据: (1) 直线$y=0$; (2) 圆$(x+1)^2+y^2=1$; (3) 椭圆$\\dfrac{x^2}{36}+\\dfrac{y^2}{11}=1$; (4) 线段$y=0 \\ (x\\in [-1,1])$; (5) 双曲线的一支: $y^2-\\dfrac{x^2}{3}=1 \\ (y<0)$\n",
"题号: 003534 , 字段: ans 中已修改数据: (1) $[4\\sqrt{2}-2,4\\sqrt{2}+2]$; (2) $(3+\\sqrt{2})-(4+\\sqrt{2})\\mathrm{i}$\n",
"题号: 003535 , 字段: ans 中已修改数据: 以$(1,0)$为圆心, $\\dfrac 12$为半径的圆\n",
"题号: 002057 , 字段: ans 中已修改数据: $2+\\mathrm{i}$与$-2-\\mathrm{i}$\n",
"题号: 003542 , 字段: ans 中已修改数据: \\textcircled{2}\\textcircled{5}\n",
"题号: 000168 , 字段: ans 中已修改数据: (1) 解为$2+\\mathrm{i}$与$2-\\mathrm{i}$; (2) 解为$\\dfrac{-1+\\sqrt{10}}3$与$\\dfrac{-1-\\sqrt{10}}3$\n",
"题号: 030110 , 字段: ans 中已修改数据: (1) $-1\\pm \\dfrac{\\sqrt{2}}2\\mathrm{i}$; (2) $2(x+1+\\dfrac{\\sqrt{2}}2\\mathrm{i})(x+1-\\dfrac{\\sqrt{2}}2\\mathrm{i})$\n",
"题号: 003544 , 字段: ans 中已修改数据: $p=12$, $q=26$\n",
"题号: 002085 , 字段: ans 中已修改数据: 证明略\n",
"题号: 002086 , 字段: ans 中已修改数据: $\\dfrac 52$或$-2$\n",
"题号: 000163 , 字段: ans 中已修改数据: (1) $11$, $-60$, $61$, $11+60\\mathrm{i}$; (2) \\textcircled{3}\n",
"题号: 003519 , 字段: ans 中已修改数据: B\n",
"题号: 003520 , 字段: ans 中已修改数据: A\n",
"题号: 003523 , 字段: ans 中已修改数据: $a+b\\mathrm{i}$\n",
"题号: 003522 , 字段: ans 中已修改数据: $-2+\\mathrm{i}$\n",
"题号: 002012 , 字段: ans 中已修改数据: 二\n",
"题号: 002016 , 字段: ans 中已修改数据: $\\{-2,0\\}$\n",
"题号: 002017 , 字段: ans 中已修改数据: $40$\n",
"题号: 003528 , 字段: ans 中已修改数据: 以$(-3,7)$为圆心, $12$为半径的圆\n",
"题号: 007313 , 字段: ans 中已修改数据: $3+2\\mathrm{i}$与$-3-2\\mathrm{i}$\n",
"题号: 007314 , 字段: ans 中已修改数据: 解为$\\dfrac{\\sqrt{2}}2+\\dfrac{\\sqrt{2}}2\\mathrm{i}$与$-\\dfrac{\\sqrt{2}}2-\\dfrac{\\sqrt{2}}2\\mathrm{i}$\n",
"题号: 003758 , 字段: ans 中已修改数据: 不存在推出关系, 因为$P$成立当且仅当$a\\in (-2\\sqrt{2},2\\sqrt{2})$, $Q$成立当且仅当$a\\in [-3,-1]\\cup [1,3]$, $a=0$时$P$成立但$Q$不成立, $a=3$时$Q$成立但$P$不成立\n",
"题号: 004167 , 字段: ans 中已修改数据: $2\\sqrt{3}$\n",
"题号: 009032 , 字段: ans 中已修改数据: (1) $(x+\\sqrt{5}\\mathrm{i}y)(x-\\sqrt{5}\\mathrm{i}y)$; (2) $2(x-\\dfrac{3+\\mathrm{i}}2)(x-\\dfrac{3-\\mathrm{i}}2)$\n",
"题号: 003551 , 字段: ans 中已修改数据: (1) $-\\dfrac 14$或$\\dfrac{17}{4}$; (2) $-\\dfrac 14$或$\\dfrac 94$\n"
]
}
],
"source": [
"import os,re,json\n",
"\n",
"\"\"\"---明确数据文件位置---\"\"\"\n",
"datafile = \"文本文件/metadata.txt\"\n",
"# 双回车分隔,记录内单回车分隔列表,首行为字段名\n",
"\"\"\"---文件位置结束---\"\"\"\n",
"\n",
"def trim(string):\n",
" string = re.sub(r\"^[ \\t\\n]*\",\"\",string)\n",
" string = re.sub(r\"[ \\t\\n]*$\",\"\",string)\n",
" return string\n",
"def FloatToInt(string):\n",
" f = float(string)\n",
" if abs(f-round(f))<0.01:\n",
" f = round(f)\n",
" return f\n",
"\n",
"with open(datafile,\"r\",encoding=\"utf8\") as f:\n",
" data = f.read()\n",
"pos = data.index(\"\\n\")\n",
"field = data[:pos].strip()\n",
"appending_data = data[pos:]\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",
"with open(r\"../题库0.3/LessonObj.json\",\"r\",encoding = \"utf8\") as f:\n",
" database = f.read()\n",
"obj_dict = json.loads(database)\n",
"\n",
"#该字段列表可能需要更新\n",
"fields = [\"content\",\"objs\",\"tags\",\"genre\",\"ans\",\"solution\",\"duration\",\"usages\",\"origin\",\"edit\",\"same\",\"related\",\"remark\",\"space\"]\n",
"\n",
"if field in fields:\n",
" field_type = type(pro_dict[\"000001\"][field])\n",
" datalist = [record.strip() for record in appending_data.split(\"\\n\\n\") if len(trim(record)) > 0]\n",
" for record in datalist:\n",
" id = re.findall(r\"^[\\d]{1,}\",record)[0]\n",
" data = record[len(id):].strip()\n",
" id = id.zfill(6)\n",
" if not id in pro_dict:\n",
" print(\"题号:\",id,\"不在数据库中.\")\n",
" break\n",
" \n",
" #字符串类型字段添加数据\n",
" elif field_type == str and data in pro_dict[id][field]:\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已有该数据:\",data)\n",
" elif field_type == str and not data in pro_dict[id][field] and not field == \"ans\" and not field == \"space\":\n",
" origin_data = pro_dict[id][field]\n",
" new_data = trim(origin_data + \"\\n\" + data)\n",
" pro_dict[id][field] = new_data\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已添加数据:\",data)\n",
" elif field_type == str and not data in pro_dict[id][field] and field == \"ans\" or field == \"space\":\n",
" pro_dict[id][field] = data\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已修改数据:\",data)\n",
" \n",
" #数值类型字段添加数据\n",
" elif (field_type == int or field_type == float) and abs(float(data) - pro_dict[id][field])<0.01:\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已有该数据:\",FloatToInt(data))\n",
" elif (field_type == int or field_type == float) and abs(float(data) - pro_dict[id][field])>=0.01:\n",
" pro_dict[id][field] = FloatToInt(data)\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已修改数据:\",FloatToInt(data))\n",
" \n",
" #列表类型字段添加数据\n",
" elif field_type == list:\n",
" cell_data_list = [d.strip() for d in data.split(\"\\n\")]\n",
" for cell_data in cell_data_list:\n",
" if cell_data in pro_dict[id][field]:\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已有该数据:\",cell_data)\n",
" elif not field == \"objs\":\n",
" pro_dict[id][field].append(cell_data)\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已添加数据:\",cell_data)\n",
" else:\n",
" if not cell_data in obj_dict and not cell_data.upper() == \"KNONE\":\n",
" print(\"题号:\",id,\", 字段:\",field,\"目标编号有误:\",cell_data)\n",
" else:\n",
" pro_dict[id][field].append(cell_data.upper())\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已添加数据:\",cell_data.upper())\n",
"\n",
"with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n",
" f.write(json.dumps(pro_dict,indent=4,ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.7 ('base')",
"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.7"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "e4cce46d6be9934fbd27f9ca0432556941ea5bdf741d4f4d64c6cd7f8dfa8fba"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}