言語処理100本ノック 2015 1日目
言語処理100本ノック(言語処理100本ノック 2015)を見つけたのでやる。使用した言語はPython2.7
途中上手くはてな記法が働いていないところがある
第1章: 準備運動
00. 文字列の逆順
文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.
プログラム
#coding: UTF-8 s = "stressed" print s[::-1]
実行結果
haruka@ubuntu:~/NLP100$ python 00.py
desserts
01. 「パタトクカシーー」
「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
プログラム
#coding: UTF-8 s = u"パタトクカシーー" print s[0:1] + s[2:3] + s[4:5] + s[6:7]
実行結果
haruka@ubuntu:~/NLP100$ python 01.py
パトカー
02. 「パトカー」+「タクシー」=「パタトクカシーー」
「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
プログラム
#coding: UTF-8 p = u'パトカー' t = u'タクシー' for i,j in (zip(p,t)): print "".join(i + j) ||< 実行結果 haruka@ubuntu:~/NLP100$ python 02.py パタ トク カシ ーー 03. 円周率 "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ. プログラム >|python| #coding: UTF-8 s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics." s = s.replace(',',"") s = s.replace('.',"") list =[] for i in s.split(): list.append(len(i)) print list
実行結果
haruka@ubuntu:~/NLP100$ python 03.py
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
05. n-gram
与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ.
プログラム
#coding: UTF-8
text = "I am an NLPer" def ngram(text,n): list = [] if len(text) >= n: for i in xrange(len(text) - n + 1): list.append(text[i:i+n]) return list print ngram(text,2)
実行結果
haruka@ubuntu:~/NLP100$ python 05.py
['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']
06. 集合
"paraparaparadise"と"paragraph"に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,'se'というbi-gramがXおよびYに含まれるかどうかを調べよ.
プログラム
#coding: UTF-8 tx1 = "paraparaparadise" tx2 = "paragraph" def ngram(text,n): list = [] if len(text) >= n: for i in xrange(len(text) - n + 1): list.append(text[i:i+n]) return list X = (ngram(tx1,2)) Y = (ngram(tx2,2)) print X,Y print "union:" + X.union(Y) print "intersection:" + X.intersection(Y) print "difference:" + X.difference(Y) ||< 実行結果 haruka@ubuntu:~/NLP100$ python 06.py ['pa', 'ar', 'ra', 'ap', 'pa', 'ar', 'ra', 'ap', 'pa', 'ar', 'ra', 'ad', 'di', 'is', 'se'] ['pa', 'ar', 'ra', 'ag', 'gr', 'ra', 'ap', 'ph'] Traceback (most recent call last): File "06.py", line 14, in <module> print "union:" + X.union(Y) 07. テンプレートによる文生成 引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ.さらに,x=12, y="気温", z=22.4として,実行結果を確認せよ. プログラム >|python| #coding: UTF-8 def template(x,y,z): print "%s時の%sは%s" % (x,y,z) template(12,"気温",22.4) ||< 実行結果 haruka@ubuntu:~/NLP100$ python 07.py 12時の気温は22.4 08. 暗号文 与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ. 英小文字ならば(219 - 文字コード)の文字に置換 その他の文字はそのまま出力 この関数を用い,英語のメッセージを暗号化・復号化せよ. プログラム >|python| #coding: UTF-8 text = "This is a pen" def cipher(text): ans = "" for i in text: if i.islower(): str = 219 - ord(i) ans += chr(str) else: ans += i return ans print cipher(text)
実行結果
haruka@ubuntu:~/NLP100$ python 08.py
Tsrh rh z kvm
第2章: UNIXコマンドの基礎
hightemp.txtは,日本の最高気温の記録を「都道府県」「地点」「℃」「日」のタブ区切り形式で格納したファイルである.以下の処理を行うプログラムを作成し,hightemp.txtを入力ファイルとして実行せよ.さらに,同様の処理をUNIXコマンドでも実行し,プログラムの実行結果を確認せよ.
10. 行数のカウント
行数をカウントせよ.確認にはwcコマンドを用いよ.
プログラム
#coding: UTF-8 f = open("hightemp.txt") lines = f.readlines() print len(lines) f.close
実行結果
haruka@ubuntu:~/NLP100$ python 10.py
24
コマンド確認
haruka@ubuntu:~/NLP100$ wc -l hightemp.txt
24 hightemp.txt
今日のまとめ
とりあえず100問ざっとみた。さっぱりいまは解ける気がしないけどなんとかなる気がする。
ここに載せてるものでも実行結果がおかしかったりエラーが出ているものがあるのでまずはそれを直す。
今日解けたのは00,01,03,07,08,10
1問解けたら絵をかいたふせんをはってく
今日のふせんしんちょく