{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "⊕ [snips-nlu-parsers · PyPI](https://pypi.org/project/snips-nlu-parsers/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T09:46:09.476262Z",
     "start_time": "2019-09-12T09:46:09.400789Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"value\": \"in three days\",\n",
      "    \"range\": {\n",
      "      \"start\": 25,\n",
      "      \"end\": 38\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"InstantTime\",\n",
      "      \"value\": \"2019-09-15 00:00:00 +08:00\",\n",
      "      \"grain\": \"Day\",\n",
      "      \"precision\": \"Exact\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/date\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "from snips_nlu_parsers import BuiltinEntityParser\n",
    "import json\n",
    "parser = BuiltinEntityParser.build(language=\"en\")\n",
    "parsing = parser.parse(\"what will be the weather in three days ?\")\n",
    "print(json.dumps(parsing, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T09:46:32.176477Z",
     "start_time": "2019-09-12T09:46:32.170087Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"value\": \"in three days\",\n",
      "    \"range\": {\n",
      "      \"start\": 0,\n",
      "      \"end\": 13\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"InstantTime\",\n",
      "      \"value\": \"2019-09-15 00:00:00 +08:00\",\n",
      "      \"grain\": \"Day\",\n",
      "      \"precision\": \"Exact\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/date\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "parsing = parser.parse(\"in three days\")\n",
    "print(json.dumps(parsing, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T09:36:48.384200Z",
     "start_time": "2019-09-12T09:36:48.377539Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cs={'snips/date', 'snips/datetime'}\n",
    "dims=['snips/date']\n",
    "any([c in dims for c in cs])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T09:39:07.641245Z",
     "start_time": "2019-09-12T09:39:07.634073Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'date'}\n"
     ]
    }
   ],
   "source": [
    "y='date'\n",
    "if isinstance(y, str):\n",
    "    x={y}\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T09:54:43.750358Z",
     "start_time": "2019-09-12T09:54:43.740678Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['snips/date']\n",
      "in three days - snips/date\n",
      "Day 2019-09-15 00:00:00 +08:00\n"
     ]
    }
   ],
   "source": [
    "parsing = parser.parse(\"in three days\")\n",
    "dims = [d['entity_kind'] for d in parsing]\n",
    "print(dims)\n",
    "for item in parsing:    \n",
    "    print(f\"{item['value']} - {item['entity_kind']}\")\n",
    "    entity=item['entity']\n",
    "    print(entity['grain'], entity['value'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T15:06:32.052513Z",
     "start_time": "2019-09-12T15:06:32.004551Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"value\": \"先週\",\n",
      "    \"range\": {\n",
      "      \"start\": 0,\n",
      "      \"end\": 2\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"InstantTime\",\n",
      "      \"value\": \"2019-09-02 00:00:00 +08:00\",\n",
      "      \"grain\": \"Week\",\n",
      "      \"precision\": \"Exact\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/datetime\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "parsers={}\n",
    "def extract_dt(text, lang='ja'):\n",
    "    if lang in parsers:\n",
    "        parser=parsers[lang]\n",
    "    else:\n",
    "        parser = BuiltinEntityParser.build(language=lang)\n",
    "        parsers[lang]=parser\n",
    "    parsing = parser.parse(text)\n",
    "    print(json.dumps(parsing, indent=2, ensure_ascii=False))\n",
    "\n",
    "extract_dt(\"先週\", 'ja')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T15:09:12.318888Z",
     "start_time": "2019-09-12T15:09:12.300200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"value\": \"金曜日の午後7時から午後8時まで\",\n",
      "    \"range\": {\n",
      "      \"start\": 0,\n",
      "      \"end\": 16\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"TimeInterval\",\n",
      "      \"from\": \"2019-09-13 12:00:00 +08:00\",\n",
      "      \"to\": \"2019-09-13 20:00:00 +08:00\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/datetime\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "sents='金曜日の午後7時から午後8時まで'\n",
    "extract_dt(sents, 'ja')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T15:08:47.172912Z",
     "start_time": "2019-09-12T15:08:47.158552Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"value\": \"金曜日\",\n",
      "    \"range\": {\n",
      "      \"start\": 0,\n",
      "      \"end\": 3\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"InstantTime\",\n",
      "      \"value\": \"2019-09-13 00:00:00 +08:00\",\n",
      "      \"grain\": \"Day\",\n",
      "      \"precision\": \"Exact\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/datetime\"\n",
      "  }\n",
      "]\n",
      "[\n",
      "  {\n",
      "    \"value\": \"午後7時から午後8時まで\",\n",
      "    \"range\": {\n",
      "      \"start\": 0,\n",
      "      \"end\": 12\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"TimeInterval\",\n",
      "      \"from\": \"2019-09-13 19:00:00 +08:00\",\n",
      "      \"to\": \"2019-09-13 20:00:00 +08:00\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/datetime\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "extract_dt('金曜日', 'ja')\n",
    "extract_dt('午後7時から午後8時まで', 'ja')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-11T06:02:09.861939Z",
     "start_time": "2019-09-11T06:02:09.812191Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"value\": \"금요일 오후 7\",\n",
      "    \"range\": {\n",
      "      \"start\": 0,\n",
      "      \"end\": 8\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"InstantTime\",\n",
      "      \"value\": \"2019-09-13 19:00:00 +08:00\",\n",
      "      \"grain\": \"Hour\",\n",
      "      \"precision\": \"Exact\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/datetime\"\n",
      "  },\n",
      "  {\n",
      "    \"value\": \"오후 8시\",\n",
      "    \"range\": {\n",
      "      \"start\": 14,\n",
      "      \"end\": 19\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"InstantTime\",\n",
      "      \"value\": \"2019-09-11 20:00:00 +08:00\",\n",
      "      \"grain\": \"Hour\",\n",
      "      \"precision\": \"Exact\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/datetime\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "# 周五下午7点到8点; geum-yoil ohu 7 sibu teo ohu 8si\n",
    "extract_dt('금요일 오후 7 시부 터 오후 8시', 'ko')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-10T21:35:20.845500Z",
     "start_time": "2019-09-10T21:35:20.808160Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"value\": \"지난주에\",\n",
      "    \"range\": {\n",
      "      \"start\": 0,\n",
      "      \"end\": 4\n",
      "    },\n",
      "    \"entity\": {\n",
      "      \"kind\": \"InstantTime\",\n",
      "      \"value\": \"2019-09-02 00:00:00 +08:00\",\n",
      "      \"grain\": \"Week\",\n",
      "      \"precision\": \"Exact\"\n",
      "    },\n",
      "    \"alternatives\": [],\n",
      "    \"entity_kind\": \"snips/datetime\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "# phrase='지난주'\n",
    "phrase='지난주에'\n",
    "parser = BuiltinEntityParser.build(language=\"ko\")\n",
    "parsing = parser.parse(phrase)\n",
    "print(json.dumps(parsing, indent=2, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-12T08:10:19.060119Z",
     "start_time": "2019-09-12T08:10:19.051385Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xx'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x='x'*5\n",
    "x[:2]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
