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

150 lines
7.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"题号: 001339 , 字段: remark 中已添加数据: 可能是因为没有复习过, 学生做得比想象得差.\n",
"题号: 002859 , 字段: remark 中已添加数据: 学生很少会从定义域出发去考虑奇偶性问题,考虑定义域的学生中还有一部分是说``因为定义域关于原点对称,所以$b=1$''.\n",
"题号: 011131 , 字段: remark 中已添加数据: 最值能够取到学生还是有一些不写周双表示都扣了4分.\n",
"题号: 002968 , 字段: remark 中已添加数据: 第(2)小题学生极少能关注到定义域.\n",
"题号: 003884 , 字段: remark 中已添加数据: 有点抽象, 反例难举.\n",
"题号: 001221 , 字段: remark 中已添加数据: 第(3)小题有学生未考虑定义域,有学生在化简过程中出现了分母为$x$.\n",
"题号: 009517 , 字段: remark 中已添加数据: 学生举不来反例, 有的只考虑了$f(0)$,$f(1)$,$f(2)$的值. 还有一些长篇大论不举反例的.\n",
"题号: 001331 , 字段: remark 中已添加数据: 太难了,不适合作为第一轮讲义的题目. 太难了,不适合作为第一轮讲义的题目. 太难了,不适合作为第一轮讲义的题目.\n",
"题号: 009522 , 字段: remark 中已添加数据: 第(2)小题学生很多不会分三种情况讨论. 第(1)小题有些学生把自变量取在$(-3,0]$.\n",
"题号: 002884 , 字段: remark 中已添加数据: 少数学生不能分辨``定义域上单调''和``分若干个区间单调''.\n",
"题号: 004265 , 字段: remark 中已添加数据: 第(2)小题有点难, 用分离变量和二次函数讨论做对的都有, 但是对面上太难了.\n",
"题号: 010197 , 字段: remark 中已添加数据: 可能需要加一个``用函数观点求解''\n",
"题号: 003013 , 字段: remark 中已添加数据: 许多学生的答案是开区间, 可能是用了零点存在定理.\n",
"题号: 000555 , 字段: remark 中已添加数据: 题目非常难, 通过同样题目的测验确定讲解是否有效.\n",
"题号: 001227 , 字段: remark 中已添加数据: 学生有些未想到求导, 瞎猜的答案.\n"
]
}
],
"source": [
"import os,re,json\n",
"\n",
"\"\"\"---明确数据文件位置---\"\"\"\n",
"datafile = \"临时文件/字段数据.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
}