Skip to content

Instantly share code, notes, and snippets.

@nymwa
Created January 29, 2022 02:52
Show Gist options
  • Save nymwa/6dd2ab0d932c1357d4597addaf368346 to your computer and use it in GitHub Desktop.
Save nymwa/6dd2ab0d932c1357d4597addaf368346 to your computer and use it in GitHub Desktop.
wana_nimi_solver.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "wana_nimi_solver.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyM1AufzQDvvvmzvHZDDm4u7",
"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/nymwa/6dd2ab0d932c1357d4597addaf368346/wana_nimi_solver.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YXIBL1qyvJsT",
"outputId": "09cd28d6-1d71-4209-8766-7a278a0db68e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"66\n",
"14\n",
"XWXX\n",
"['ante']\n",
"['kasi']\n",
"2.5385224930608845\n",
"4.441508514179351\n"
]
}
],
"source": [
"import numpy as np\n",
"from collections import Counter, defaultdict\n",
"\n",
"words = 'anpa ante awen esun insa jaki jelo kala kama kasi kili kule kute lape laso lawa lete lili lipu loje luka lupa mama mani meli mije moku moli musi mute nasa nena nimi noka olin open pali pana pini pipi poka poki pona sama seli selo seme sewi sike sina sona suli suno supa suwi taso tawa telo toki tomo unpa walo waso wawa weka wile'.split()\n",
"print(len(words))\n",
"chars = list(set([char for word in words for char in word]))\n",
"chars.sort()\n",
"print(len(chars))\n",
"\n",
"\n",
"def judge(ans, cand):\n",
" show = [2, 2, 2, 2] # 0: green, 1: yellow, 2: no\n",
" \n",
" for i in range(4):\n",
" if ans[i] == cand[i]:\n",
" show[i] = 0\n",
" \n",
" for x in list(set(ans)):\n",
" indices = [i for i in range(4) if cand[i] == x]\n",
" if len(indices) > 0 and show[indices[0]] == 2:\n",
" show[indices[0]] = 1\n",
" \n",
" j = ''\n",
" chars = ['C', 'W', 'X']\n",
" for n in show:\n",
" j += chars[n]\n",
" return j\n",
"\n",
"print(judge('ante', 'wawa')) # 正解 ante に対して,候補 wawa を入力する\n",
"\n",
"\n",
"def filter_words(words, j, cand):\n",
" words = [x for x in words]\n",
" \n",
" # Wの定義を,厳密に「ここではなく他のところにある」にしてjを書き換え\n",
" yellow_chars = ''\n",
" for i in range(4):\n",
" if j[i] == 'W' or j[i] == 'C':\n",
" yellow_chars += cand[i]\n",
" for i in range(4):\n",
" if cand[i] in yellow_chars and j[i] == 'X':\n",
" j = j[:i] + 'W' + j[i+1:]\n",
" \n",
" for i in range(4):\n",
" if j[i] == 'C':\n",
" words = [word for word in words if word[i] == cand[i]]\n",
" \n",
" for i in range(4):\n",
" if j[i] == 'W':\n",
" words = [word for word in words if cand[i] in word]\n",
" words = [word for word in words if word[i] != cand[i]]\n",
" \n",
" for i in range(4):\n",
" if j[i] == 'X':\n",
" words = [word for word in words if cand[i] not in word]\n",
" \n",
" return words\n",
"\n",
"print(filter_words(words, 'XWXX', 'wawa')) # 初手wawaに対する回答がXWXXだったとき,次に候補となる単語のリスト\n",
"print(filter_words(['kama', 'kasi'], 'CCXX', 'kama'))\n",
"\n",
"def ent(rest, cand):\n",
" dct = defaultdict(int)\n",
" \n",
" for word in rest:\n",
" dct[judge(word, cand)] += 1\n",
" \n",
" ent = 0\n",
" denom = sum(dct.values())\n",
" for i in dct.values():\n",
" p = i / len(words)\n",
" ent -= p * np.log2(p)\n",
" return ent\n",
"\n",
"print(ent(words, 'wawa')) # 全単語の中での wawa のエントロピー (情報量がすくない)\n",
"print(ent(words, 'laso')) # 全単語の中での laso のエントロピー (情報量がおおきい)"
]
},
{
"cell_type": "code",
"source": [
"rest = [x for x in words]\n",
"\n",
"# 現状,最初の単語はkuleが最適と予測しているが,本当にそうかは謎\n",
"# 最初の2単語までのエントロピーをみたらkule,sonaが一番たかかったが,3単語以降まで考慮した場合どうなるのかはわからない\n",
"# 最初の入力単語として最適なのがなにかは誰か教えてください\n",
"word = input('input first word > ')\n",
"while True:\n",
" j = input('input return > ')\n",
" if j == '':\n",
" break\n",
" rest = filter_words(rest, j, word)\n",
" cands = [(word, ent(rest, word)) for word in rest]\n",
" cands.sort(key = lambda x: -x[-1])\n",
" if len(cands) == 0:\n",
" break\n",
" elif len(cands) == 1:\n",
" print('prediction: {} (entropy: {})'.format(*cands[0]))\n",
" break\n",
" else:\n",
" print('prediciton: {} (entropy: {})'.format(*cands[0]))\n",
" print('(others: ' + ', '.join(['{} ({})'.format(w, h) for w, h in cands[1:]]) + ')')\n",
" word = cands[0][0]"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0bOvIINjvkzS",
"outputId": "3a153758-932a-4421-9f52-30fa3bbadd1b"
},
"execution_count": 2,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"input first word > kule\n",
"input return > CXXX\n",
"prediciton: kama (entropy: 0.1831634581623774)\n",
"(others: kasi (0.1831634581623774))\n",
"input return > \n"
]
}
]
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "8omnexpXvlzX"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment