{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os,re,json\n",
    "\n",
    "#读取题库json文件并转化为字典\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",
    "#读取目标数据库json并转化为字典\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",
    "tags_descr = [\"第一单元\",\"第二单元\",\"第三单元\",\"第四单元\",\"第五单元\",\"第六单元\",\"第七单元\",\"第八单元\",\"第九单元\",\"暂无对应\"] \n",
    "tags_dict = {}\n",
    "units_string = \"\"\n",
    "for t in tags_descr:\n",
    "    tags_dict[t] = [0,\"\"]\n",
    "for p in pro_dict:\n",
    "    for t in tags_descr:\n",
    "        if t in pro_dict[p][\"tags\"]:\n",
    "            tags_dict[t][0] += 1\n",
    "            tags_dict[t][1] += p + \",\"\n",
    "#units_string是简要列表, tags_dict是详细清单\n",
    "for t in tags_descr:\n",
    "    units_string += t + \": \" + str(tags_dict[t][0]) + \"\\n\" \n",
    "#单元统计完成\n",
    "\n",
    "#以下是课时题目统计\n",
    "#生成单元课时列表(Kddll)\n",
    "lessons_descr = [] \n",
    "for o in obj_dict:\n",
    "    if not o[:5] in lessons_descr:\n",
    "        lessons_descr.append(o[:5])\n",
    "lessons_dict = {}\n",
    "lessons_string = \"\"\n",
    "for l in lessons_descr:\n",
    "    lessons_dict[l] = [0,\"\"]\n",
    "for p in pro_dict:\n",
    "    for l in lessons_descr:\n",
    "        for o in pro_dict[p][\"objs\"]:\n",
    "            if l in o:\n",
    "                lessons_dict[l][0] += 1\n",
    "                lessons_dict[l][1] += p + \",\"\n",
    "                break\n",
    "#lessons_string是简要列表, lessons_dict是详细清单\n",
    "for l in lessons_descr:\n",
    "    lessons_string += l + \": \" + str(lessons_dict[l][0]) + \"\\n\" \n",
    "#课时统计完成\n",
    "\n",
    "#以下是目标题目统计\n",
    "objs_dict = {}\n",
    "objs_string = \"\"\n",
    "for o in obj_dict:\n",
    "    objs_dict[o] = [0,\"\"]\n",
    "for p in pro_dict:\n",
    "    for o in obj_dict:\n",
    "        if o in pro_dict[p][\"objs\"]:\n",
    "            objs_dict[o][0] += 1\n",
    "            objs_dict[o][1] += p + \",\"\n",
    "# objs_string是简要列表, objs_dict是详细清单\n",
    "for o in obj_dict:\n",
    "    objs_string += o + \": \" + str(objs_dict[o][0]) + \"\\n\" \n",
    "#课时统计完成\n",
    "\n",
    "#以下为清点结果输出\n",
    "with open(r\"临时文件\\单元课时目标题目数据清点结果.txt\",\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(\"---单元清点结果---\\n\")\n",
    "    f.write(units_string)\n",
    "    f.write(\"\\n---课时清点结果---\\n\")\n",
    "    f.write(lessons_string)\n",
    "    f.write(\"\\n---目标清点结果---\\n\")\n",
    "    f.write(objs_string)\n",
    "    f.write(\"\\n\"*10)\n",
    "    f.write(\"---单元详细清点结果数据库---\\n\")\n",
    "    f.write(json.dumps(tags_dict,indent = 4,ensure_ascii = False))\n",
    "    f.write(\"\\n\"*5+\"---课时详细清点结果数据库---\\n\")\n",
    "    f.write(json.dumps(lessons_dict,indent = 4,ensure_ascii = False))\n",
    "    f.write(\"\\n\"*5+\"---目标详细清点结果数据库---\\n\")\n",
    "    f.write(json.dumps(objs_dict,indent = 4,ensure_ascii = False))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.8 ('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.8.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d311ffef239beb3b8f3764271728f3972d7b090c974f8e972fcdeedf230299ac"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
