Last active
January 11, 2023 16:11
-
-
Save kun432/d897f4ece48156beebf779895330c997 to your computer and use it in GitHub Desktop.
隊列パース.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyOxW3+Niy+cCgx3yuOIjmFh", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/kun432/d897f4ece48156beebf779895330c997/.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "zennの記事を元に以下を追加してみた。\n", | |
| "\n", | |
| "- 各コーナーでの内外の位置取り\n", | |
| "- matplotlibでかんたんな可視化\n", | |
| "\n", | |
| "参考)pyparsingで競馬のコーナー通過順位をパース\n", | |
| "https://zenn.dev/moripon/articles/ed5caa9c1d621e\n" | |
| ], | |
| "metadata": { | |
| "id": "qngtoogbjC2m" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "!pip install japanize-matplotlib" | |
| ], | |
| "metadata": { | |
| "id": "46uGuY2ObEWu" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "UCipf59VE4EB" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "import pandas as pd\n", | |
| "import pyparsing as pp\n", | |
| "import matplotlib.cm as cm\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "import japanize_matplotlib" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# DataFrame列名定義\n", | |
| "columns = ['diff', 'horse_no', 'side']\n", | |
| "\n", | |
| "# 差の定数(unit:馬身)\n", | |
| "DIFF_GROUP = 0.3\n", | |
| "DIFF_MIN = 1.5\n", | |
| "DIFF_MID = 3.0\n", | |
| "DIFF_MUCH = 6.0\n", | |
| "\n", | |
| "class ParsePass():\n", | |
| " \n", | |
| " def __init__(self):\n", | |
| " \n", | |
| " # 馬番\n", | |
| " horse_no = pp.Word(pp.nums).setParseAction(self._horse_no_action)\n", | |
| " \n", | |
| " # 馬群\n", | |
| " group = pp.Suppress(pp.Literal('(')) + \\\n", | |
| " pp.Optional(pp.delimitedList(pp.Word(pp.nums), delim=',')) + \\\n", | |
| " pp.Suppress(pp.Literal(')'))\n", | |
| " group.ignore('*')\n", | |
| " group.setParseAction(self._group_action)\n", | |
| "\n", | |
| " # 情報要素\n", | |
| " element = (group | horse_no)\n", | |
| " \n", | |
| " # 前走馬との差\n", | |
| " diff_min = pp.Suppress(pp.Optional(pp.Literal(','))).setParseAction(self._diff_min_action) + element\n", | |
| " diff_mid = pp.Suppress(pp.Literal('-')).setParseAction(self._diff_mid_action) + element\n", | |
| " diff_much = pp.Suppress(pp.Literal('=')).setParseAction(self._diff_much_action) + element\n", | |
| "\n", | |
| " # 全体定義\n", | |
| " self._passing_order = element + pp.ZeroOrMore( diff_mid | diff_much | diff_min )\n", | |
| " \n", | |
| " def _horse_no_action(self, token):\n", | |
| " \n", | |
| " self._data = self._data.append({'diff':self._diff, 'horse_no':token[0], 'side':1 }, ignore_index=True)\n", | |
| " return\n", | |
| "\n", | |
| " def _group_action(self, token):\n", | |
| " \n", | |
| " for i, no in enumerate(token):\n", | |
| " self._data = self._data.append({'diff':self._diff, 'horse_no':no, 'side':1+i}, ignore_index=True)\n", | |
| " self._diff += DIFF_GROUP\n", | |
| " self._diff -= DIFF_GROUP\n", | |
| " return\n", | |
| " \n", | |
| " def _diff_min_action(self, token):\n", | |
| " \n", | |
| " self._diff += DIFF_MIN\n", | |
| " return\n", | |
| " \n", | |
| " def _diff_mid_action(self, token):\n", | |
| " \n", | |
| " self._diff += DIFF_MID\n", | |
| " return\n", | |
| " \n", | |
| " def _diff_much_action(self, token):\n", | |
| " \n", | |
| " self._diff += DIFF_MUCH\n", | |
| " return\n", | |
| " \n", | |
| " def parse(self, pass_str):\n", | |
| " \n", | |
| " # 初期化\n", | |
| " self._data = pd.DataFrame(columns=columns)\n", | |
| " self._diff = 0\n", | |
| " # parse\n", | |
| " self._passing_order.parseString(pass_str)\n", | |
| " # index調整\n", | |
| " self._data.index = np.arange(1, len(self._data)+1)\n", | |
| " self._data.index.name = 'rank'\n", | |
| "\n", | |
| " return(self._data)" | |
| ], | |
| "metadata": { | |
| "id": "mvMLNW17E_0B" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "pass_data = '(*12,10,15)5-(1,13)-(2,4)3(6,9)(14,11)(8,7)'#@param {type: \"string\"}" | |
| ], | |
| "metadata": { | |
| "id": "KndSiMxMkMCh" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "pass_parsing = ParsePass()\n", | |
| "df = pass_parsing.parse(pass_str)\n", | |
| "df" | |
| ], | |
| "metadata": { | |
| "id": "yV6efWYeFLG7" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "plt.figure(figsize=(df[\"diff\"].max(),df[\"side\"].max()))\n", | |
| "plt.scatter(df[\"diff\"], df[\"side\"], s=100)\n", | |
| "plt.xlabel(\"前 ← 前後 → 後\", fontsize=15)\n", | |
| "plt.ylabel(\"内 ← 内外 → 外\", fontsize=15)\n", | |
| "plt.title(\"隊列\", fontsize=20)\n", | |
| "plt.yticks([0,1,2,3,4], color=\"w\") " | |
| ], | |
| "metadata": { | |
| "id": "yac18U0njctj" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment