diff --git a/momoko/chapter01/knock00.ipynb b/momoko/chapter01/knock00.ipynb new file mode 100644 index 0000000..e654fc9 --- /dev/null +++ b/momoko/chapter01/knock00.ipynb @@ -0,0 +1,60 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "desserts\n" + ] + } + ], + "source": [ + "#00\n", + "\n", + "input = \"stressed\"\n", + "output = \"\"\n", + "for m in range (len(input)):\n", + " output = output + input[len(input)-int(m+1)]#後ろからどんどん参照する\n", + "\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock01.ipynb b/momoko/chapter01/knock01.ipynb new file mode 100644 index 0000000..6a15cf1 --- /dev/null +++ b/momoko/chapter01/knock01.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "タクシー\n" + ] + } + ], + "source": [ + "#01\n", + "\n", + "input = \"パタトクカシーー\"\n", + "output = \"\"\n", + "#print(len(input)) ->8だった\n", + "\n", + "for i in range (int(len(input)/2)):\n", + " output = output + input[2*i+1]#奇数番目だけ\n", + "\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock02.ipynb b/momoko/chapter01/knock02.ipynb new file mode 100644 index 0000000..57e2990 --- /dev/null +++ b/momoko/chapter01/knock02.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "パタトクカシーー\n" + ] + } + ], + "source": [ + "#02\n", + "\n", + "pato=\"パトカー\"\n", + "taku=\"タクシー\"\n", + "output=\"\"\n", + "\n", + "for i in range(len(pato)):\n", + " output = output + pato[i] + taku[i]\n", + "\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock03.ipynb b/momoko/chapter01/knock03.ipynb new file mode 100644 index 0000000..5032378 --- /dev/null +++ b/momoko/chapter01/knock03.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 1, 4, 1, 6, 9, 2, 7, 5, 3, 5, 8, 9, 7, 10]\n" + ] + } + ], + "source": [ + "#03\n", + "\n", + "input=\"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.\"\n", + "mojisuu_list=[]\n", + "\n", + "imput_sp = input.split(\" \")\n", + "for word in imput_sp:\n", + " mojisuu_list.append(len(word))\n", + "\n", + "print(mojisuu_list)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock04.ipynb b/momoko/chapter01/knock04.ipynb new file mode 100644 index 0000000..5daa1a6 --- /dev/null +++ b/momoko/chapter01/knock04.ipynb @@ -0,0 +1,69 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "{0: 'Hi', 1: 'H', 2: 'Li', 3: 'Be', 4: 'Bo', 5: 'C', 6: 'N', 7: 'O', 8: 'F', 9: 'N', 10: 'Na', 11: 'Mi', 12: 'Al', 13: 'Si', 14: 'Pe', 15: 'S', 16: 'C', 17: 'Ar', 18: 'Ki', 19: 'C'}\n" + ] + } + ], + "source": [ + "#04\n", + "\n", + "input=\"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.\"\n", + "king_list=[]\n", + "num_list = [1, 5, 6, 7, 8, 9, 15, 16, 19]\n", + "input_sp = input.split(\" \")\n", + "\n", + "for num in range (len(input_sp)):\n", + " if num in num_list:\n", + " king_list.append([num,input_sp[num][0]])#何番目か,input_spから対応する一文字目だけ持ってくる\n", + " else:\n", + " king_list.append([num,input_sp[num][0:2]])#何番目か,input_spから対応する1&2文字目だけ持ってくる\n", + "\n", + "king_dict = dict(king_list)#リストから辞書化\n", + "print(type(king_dict))\n", + "print(king_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock05.ipynb b/momoko/chapter01/knock05.ipynb new file mode 100644 index 0000000..64d6ea9 --- /dev/null +++ b/momoko/chapter01/knock05.ipynb @@ -0,0 +1,118 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#05\n", + "#n-gram:任意の文字数で文章を分割する手法.\n", + "#bi-gram:n=2のn-gram\n", + "\n", + "def ngram_tukuro(inp,num):#inp:分割対象,num:分割数\n", + " tango_bi_gram=[]\n", + " moji_bi_gram=[]\n", + " if len(inp) < num:#分割数が上回った際は終了\n", + " return (\"nの値が大きすぎます.終了します.\")\n", + " \n", + " #単語bi-gramパート\n", + " if type(inp)==str:#文字列なら空白区切り,単語bi_gram作成.\n", + " inp_word = inp.split(\" \")\n", + " for i in range (len(inp_word)-num+1):\n", + " tango_bi_gram.append(inp_word[i:i+num])\n", + " else:#リストなら要素を前から順に取り,単語bi_gram作成.\n", + " for i in range(len(inp)-num+1):\n", + " tango_bi_gram.append(inp[i:i+num])\n", + " \n", + " #文字bi-gramパート\n", + " if type(inp)==str:#文字列なら,空白区切りしたリストを.join\n", + " tango_tati=\"\".join(inp_word)\n", + " else:\n", + " tango_tati=\"\".join(inp)\n", + " \n", + " for i in range(len(tango_tati)-num+1):#リストなら元のやつを.join\n", + " moji_bi_gram.append(tango_tati[i:i+num])\n", + "\n", + " return tango_bi_gram,moji_bi_gram" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([['I', 'am'], ['am', 'an'], ['an', 'NLPer']],\n", + " ['Ia', 'am', 'ma', 'an', 'nN', 'NL', 'LP', 'Pe', 'er'])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ngram_tukuro(\"I am an NLPer\",2)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([['私', 'は', 'コーヒー'],\n", + " ['は', 'コーヒー', 'が'],\n", + " ['コーヒー', 'が', '好き'],\n", + " ['が', '好き', '!']],\n", + " ['私はコ', 'はコー', 'コーヒ', 'ーヒー', 'ヒーが', 'ーが好', 'が好き', '好き!'])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ngram_tukuro([\"私\",\"は\",\"コーヒー\",\"が\",\"好き\",\"!\"],3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock06.ipynb b/momoko/chapter01/knock06.ipynb new file mode 100644 index 0000000..a92315d --- /dev/null +++ b/momoko/chapter01/knock06.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#05で作ったものを活用します.\n", + "#n-gram:任意の文字数で文章を分割する手法.\n", + "#bi-gram:n=2のn-gram\n", + "\n", + "def ngram_tukuro(inp,num):#inp:分割対象,num:分割数\n", + " tango_bi_gram=[]\n", + " moji_bi_gram=[]\n", + " if len(inp) < num:#分割数が上回った際は終了\n", + " return (\"nの値が大きすぎます.終了します.\")\n", + " \n", + " #単語bi-gramパート\n", + " if type(inp)==str:#文字列なら空白区切り,単語bi_gram作成.\n", + " inp_word = inp.split(\" \")\n", + " for i in range (len(inp_word)-num+1):\n", + " tango_bi_gram.append(inp_word[i:i+num])\n", + " else:#リストなら要素を前から順に取り,単語bi_gram作成.\n", + " for i in range(len(inp)-num+1):\n", + " tango_bi_gram.append(inp[i:i+num])\n", + " \n", + " #文字bi-gramパート\n", + " if type(inp)==str:#文字列なら,空白区切りしたリストを.join\n", + " tango_tati=\"\".join(inp_word)\n", + " else:\n", + " tango_tati=\"\".join(inp)\n", + " \n", + " for i in range(len(tango_tati)-num+1):#リストなら元のやつを.join\n", + " moji_bi_gram.append(tango_tati[i:i+num])\n", + "\n", + " return tango_bi_gram,moji_bi_gram" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "和集合:{'ap', 'is', 'ra', 'ph', 'ar', 'ag', 'ad', 'gr', 'pa', 'di', 'se'}\n", + "積集合:{'ar', 'ap', 'pa', 'ra'}\n", + "差集合:{'se', 'ad', 'is', 'di'}\n", + "seというbi-gramはXのみに含まれます\n" + ] + } + ], + "source": [ + "#06\n", + "moto_x = \"paraparaparadise\"\n", + "moto_y = \"paragraph\"\n", + "\n", + "#05から文字bi-gramのみ取得する.\n", + "X = set(ngram_tukuro(moto_x,2)[1])#あとで計算するときに,[]->{}にしとかないとだめだからset\n", + "Y = set(ngram_tukuro(moto_y,2)[1])\n", + "\n", + "#和集合,積集合,差集合を求める\n", + "wa_XY = X|Y\n", + "print(f\"和集合:{wa_XY}\")\n", + "seki_XY = X&Y\n", + "print(f\"積集合:{seki_XY}\")\n", + "sa_XY = X-Y\n", + "print(f\"差集合:{sa_XY}\")\n", + "\n", + "#\"se\"がXおよびYに含まれるか\n", + "if \"se\" in X :\n", + " if \"se\" in Y:\n", + " print(\"seというbi-gramはX,Yに含まれます\")\n", + " else:\n", + " print(\"seというbi-gramはXのみに含まれます\")\n", + "else:\n", + " if \"se\" in Y:\n", + " print(\"seというbi-gramはYのみに含まれます\")\n", + " else:\n", + " print(\"seというbi-gramはX,Yに含まれません\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock07.ipynb b/momoko/chapter01/knock07.ipynb new file mode 100644 index 0000000..3dcab09 --- /dev/null +++ b/momoko/chapter01/knock07.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'12時の気温は22.4'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#07\n", + "\n", + "def kion_dayo(x,y,z):\n", + " ans = str(x) +\"時の\"+ str(y) +\"は\"+ str(z)#intとstr混ぜるとエラーになる\n", + " return ans\n", + "\n", + "kion_dayo(12,\"気温\",22.4)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock08.ipynb b/momoko/chapter01/knock08.ipynb new file mode 100644 index 0000000..b87f421 --- /dev/null +++ b/momoko/chapter01/knock08.ipynb @@ -0,0 +1,94 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#08\n", + "\n", + "def cipher(moji):\n", + " ans=\"\"\n", + " for m in moji:\n", + " if m.islower() == True:#islower:全ての文字が小文字の場合のみTrueを返す.\n", + " ans = ans + chr(219-ord(m))#(219 - 文字コード)の文字に置換してansに足す\n", + " else:#その他の文字\n", + " ans = ans + m\n", + " return ans\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Nzgfizo Lzmtfztv Pilxvhhrmt'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cipher(\"Natural Language Processing\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Natural Language Processing'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cipher(\"Nzgfizo Lzmtfztv Pilxvhhrmt\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/momoko/chapter01/knock09.ipynb b/momoko/chapter01/knock09.ipynb new file mode 100644 index 0000000..4cc966c --- /dev/null +++ b/momoko/chapter01/knock09.ipynb @@ -0,0 +1,90 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#09\n", + "import random\n", + "\n", + "def narabikae(moji):\n", + " ans_list = []\n", + " moji_sp = moji.split(\" \")\n", + "\n", + " for m in moji_sp:\n", + " if len(m) > 4:#4文字より多い時\n", + " shu_m = m[1:len(m)-1]#並び替え対象の抽出\n", + " shu_d_m = random.sample(shu_m,len(m)-2)\n", + " shu_d_m = \"\".join(shu_d_m)#[\"a\",\"b\",\"c\"] -> abc\n", + " m_for_ans = m[0] + shu_d_m + m[-1]\n", + " ans_list.append(m_for_ans)\n", + " else:#4文字以下の時\n", + " ans_list.append(m)\n", + "\n", + " #リスト->文章\n", + " ans_bun = \"\"\n", + " for x in range (len(ans_list)):\n", + " if x != len(ans_list):#最後の単語以外は後ろに空白を追加\n", + " ans_bun = ans_bun + ans_list[x] + \" \"\n", + " else:\n", + " ans_bun = ans_bun + ans_list[x]\n", + " \n", + " return ans_bun" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'I cound’lt bievlee that I cluod aaltulcy utedrnsand what I was rineadg : the pmnehoenal pwoer of the huamn mind . '" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "narabikae(\"I couldn’t believe that I could actually understand what I was reading : the phenomenal power of the human mind .\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "34ccacd3b6fdbd462b750c5ef896413565fa613738e46158bd59146bdd5a1dd5" + }, + "kernelspec": { + "display_name": "Python 3.11.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.11.3" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}