Last active
November 2, 2015 03:30
-
-
Save reiyw/c3e58effe2f038b45dc0 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"http://www.cl.ecei.tohoku.ac.jp/nlp100/" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 第1章: 準備運動" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 00. 文字列の逆順\n", | |
"文字列\"stressed\"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'desserts'" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'stressed'[::-1]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 01. 「パタトクカシーー」\n", | |
"「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"タクシー\n" | |
] | |
} | |
], | |
"source": [ | |
"print u'パタトクカシーー'[1::2]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 02. 「パトカー」+「タクシー」=「パタトクカシーー」\n", | |
"「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"パタトクカシーー\n" | |
] | |
} | |
], | |
"source": [ | |
"print''.join(sum(zip(u'パトカー',u'タクシー'),()))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 03. 円周率\n", | |
"\"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.\"という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[len(w.strip(',.'))for w in'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'.split()]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 04. 元素記号\n", | |
"\"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.\"という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Al': 13,\n", | |
" 'Ar': 18,\n", | |
" 'B': 5,\n", | |
" 'Be': 4,\n", | |
" 'C': 6,\n", | |
" 'Ca': 20,\n", | |
" 'Cl': 17,\n", | |
" 'F': 9,\n", | |
" 'H': 1,\n", | |
" 'He': 2,\n", | |
" 'K': 19,\n", | |
" 'Li': 3,\n", | |
" 'Mi': 12,\n", | |
" 'N': 7,\n", | |
" 'Na': 11,\n", | |
" 'Ne': 10,\n", | |
" 'O': 8,\n", | |
" 'P': 15,\n", | |
" 'S': 16,\n", | |
" 'Si': 14}" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"{w[:-(i in[0,4,5,6,7,8,14,15,18])+2]:i+1 for i,w in enumerate('Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'.split())}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 05. n-gram\n", | |
"与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,\"I am an NLPer\"という文から単語bi-gram,文字bi-gramを得よ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']\n", | |
"[('I', 'am'), ('am', 'an'), ('an', 'NLPer')]\n", | |
"['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']\n", | |
"[['I', 'am'], ['am', 'an'], ['an', 'NLPer']]\n", | |
"[['I', 'am'], ['am', 'an'], ['an', 'NLPer']]\n", | |
"[['I', 'am'], ['am', 'an'], ['an', 'NLPer']]\n" | |
] | |
} | |
], | |
"source": [ | |
"# bi-gram\n", | |
"f=lambda s:[a+b for a,b in zip('_'+s,s+'_')[1:-1]]\n", | |
"g=lambda s:zip([[]]+s.split(),s.split()+[[]])[1:-1]\n", | |
"print f('I am an NLPer')\n", | |
"print g('I am an NLPer')\n", | |
"\n", | |
"# 文字 n-gram\n", | |
"f=lambda s,n:[(s*n)[i::len(s)+1]for i in range(len(s)+1-n)]\n", | |
"print f('I am an NLPer',2)\n", | |
"\n", | |
"# 単語 n-gram\n", | |
"def g(s,n):s=s.split();l=len(s)+1;return[(s*n)[i::l]for i in range(l-n)]\n", | |
"print g('I am an NLPer',2)\n", | |
"\n", | |
"# 単語 n-gram\n", | |
"# リストも OK\n", | |
"def h(s,n):s=s.split()if s>''else s;l=len(s)+1;return[(s*n)[i::l]for i in range(l-n)]\n", | |
"print h('I am an NLPer',2)\n", | |
"print h(['I', 'am', 'an', 'NLPer'],2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 06. 集合\n", | |
"\"paraparaparadise\"と\"paragraph\"に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,'se'というbi-gramがXおよびYに含まれるかどうかを調べよ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"set(['ad', 'ag', 'di', 'is', 'ap', 'pa', 'ra', 'ph', 'ar', 'se', 'gr'])\n", | |
"set(['ap', 'pa', 'ar', 'ra'])\n", | |
"set(['is', 'ad', 'se', 'di'])\n", | |
"set(['ph', 'gr', 'ag'])\n", | |
"True\n", | |
"False\n" | |
] | |
} | |
], | |
"source": [ | |
"X=set(f('paraparaparadise',2))\n", | |
"Y=set(f('paragraph',2))\n", | |
"print X|Y\n", | |
"print X&Y\n", | |
"print X-Y\n", | |
"print Y-X\n", | |
"print'se'in X\n", | |
"print'se'in Y" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 07. テンプレートによる文生成\n", | |
"引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ.さらに,x=12, y=\"気温\", z=22.4として,実行結果を確認せよ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"12時の気温は22.4\n" | |
] | |
} | |
], | |
"source": [ | |
"f='{}時の{}は{}'.format\n", | |
"print f(12,'気温',22.4)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 08. 暗号文\n", | |
"与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.\n", | |
"\n", | |
"英小文字ならば(219 - 文字コード)の文字に置換\n", | |
"その他の文字はそのまま出力\n", | |
"この関数を用い,英語のメッセージを暗号化・復号化せよ." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"FllBzi\n", | |
"FooBar\n" | |
] | |
} | |
], | |
"source": [ | |
"cipher=lambda s:''.join([c,chr(219-ord(c))][c.islower()]for c in s)\n", | |
"print cipher('FooBar')\n", | |
"print cipher(cipher('FooBar'))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 09. Typoglycemia\n", | |
"スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする.適当な英語の文(例えば\"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": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"I cdunol't bvleiee that I could aullcaty uearsdtnnd what I was reaidng : the pnnmeoheal pewor of the hamun mind .\"" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"' '.join([w,w[0]+''.join(sorted(w[1:-1],key=lambda x:__import__('random').random()))+w[-1]][len(w)>4] for w in\"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .\".split())" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment