{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os,re,json\n",
    "\n",
    "\"\"\"---设置关键字, 同一field下不同选项为or关系, 同一字典中不同字段间为and关系, 不同字典间为or关系, _not表示列表中的关键字都不含, 同一字典中的数字用来供应同一字段不同的条件之间的and---\"\"\"\n",
    "keywords_dict_table = [\n",
    "    {\"origin\":[\"素养\"]}\n",
    "]\n",
    "\"\"\"---关键字设置完毕---\"\"\"\n",
    "# 示例： keywords_dict_table = [\n",
    "#     {\"tags\": [\"第三单元\"], \"content1\": [r\"[\\d]\\alpha\",\"2x\"], \"content2\": [\"sin\"], \"content3\": [\"cos\"],\"content4\": [\"cot\"], \"content5\": [\"tan\"]},\n",
    "# ]\n",
    "# 实例2: keywords_dict_table = [\n",
    "#     {\"tags\":[\"第三单元\"],\"content\":[\"f\\(\",\"y=\",\"函数\"],\"usages\":[r\"0\\.9\",r\"0\\.8[3-9]\"],\"usages_not\":[r\"0\\.[0-7]\",r\"0\\.8[0-2]\"],\"usages1\":[\"2023届\"]},\n",
    "#     {\"tags\":[\"第五单元\"],\"usages\":[r\"0\\.9\"],\"usages_not\":[r\"0\\.[0-7]\",r\"0\\.8[0-2]\"],\"usages1\":[\"2023届\"]}\n",
    "# ]\n",
    "# 实例3: \n",
    "# keywords_dict_table = [\n",
    "#     {\"usages\":[r\"2023届高三02班\"],\"usages2\":[r\"202209\",r\"20221[012]\"],\"usages3\":[r\"0\\.[678][\\d]{2}\"],\"usages_not\":[r\"2023届高三02班[^\\n]*0\\.[0-59][\\d]{2}\"]}\n",
    "    \n",
    "# ]\n",
    "\n",
    "\n",
    "\"\"\"---设置输出文件名---\"\"\"\n",
    "filename = \"文本文件/题号筛选.txt\"\n",
    "\"\"\"---文件名设置完毕---\"\"\"\n",
    "\n",
    "\n",
    "def match_condition(problem,condition_dict):\n",
    "    match = True\n",
    "    for field1 in [c for c in condition_dict if not \"_not\" in c]:\n",
    "        cond_list = condition_dict[field1]\n",
    "        field = re.sub(\"\\d\",\"\",field1)\n",
    "        if type(problem[field]) == list:\n",
    "            string = \"\\n\".join((problem[field]))\n",
    "        else:\n",
    "            string = str(problem[field])\n",
    "        current_match = False\n",
    "        for cond in cond_list:\n",
    "            if len(re.findall(cond,string)) > 0:\n",
    "                current_match = True\n",
    "        if current_match == False:\n",
    "            match = False\n",
    "    for field1 in [c for c in condition_dict if \"_not\" in c]:\n",
    "        cond_list = condition_dict[field1]\n",
    "        field1 = field1.replace(\"_not\",\"\")\n",
    "        field = re.sub(\"\\d\",\"\",field1)\n",
    "        if type(problem[field]) == list:\n",
    "            string = \"\\n\".join((problem[field]))\n",
    "        else:\n",
    "            string = str(problem[field])\n",
    "        current_match = True\n",
    "        for cond in cond_list:\n",
    "            if len(re.findall(cond,string)) > 0:\n",
    "                current_match = False\n",
    "        if current_match == False:\n",
    "            match = False\n",
    "        \n",
    "    return match\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",
    "match_list = []\n",
    "for condition in keywords_dict_table:\n",
    "    for id in pro_dict:\n",
    "        if match_condition(pro_dict[id],condition) and not id in match_list:\n",
    "            match_list.append(id)\n",
    "\n",
    "with open(filename,\"w\",encoding=\"utf8\") as f:\n",
    "    f.write(\",\".join(match_list))\n",
    "\n",
    "os.system(\"code \"+filename)"
   ]
  },
  {
   "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
}
