言語処理100本ノック 2015 5日目
言語処理100本ノックを見つけたのでやる。環境はPython2.7 + Ubuntu15.10です。 いたらぬ点もありますがよろしくお願いします
もう解けてるやつ 00,01,02,03,04,05,06,07,08,10,11,12,13,14,15,17,18,20,21,22,24 まだ解けてないやつ たくさん
第1章: 準備運動
09. Typoglycemia
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする.適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .")を与え,その実行結果を確認せよ.
#coding: UTF-8 import random text = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ." ans = [] anslist = [] for i in text.split(): if len(i) <= 4: anslist.append(i) else: naka = list(i[1:-1]) random.shuffle(naka) anslist.append(i[0:1] + "".join(naka) + i[-1]) print " ".join(anslist)
実行結果 haruka@ubuntu:~/NLP100$ python nlp09.py I cnoud'lt bveeile that I culod altacluy undatnresd what I was rdnieag : the peaomehnnl pweor of the human mind
第2章: UNIXコマンドの基礎
hightemp.txtは,日本の最高気温の記録を「都道府県」「地点」「℃」「日」のタブ区切り形式で格納したファイルである.以下の処理を行うプログラムを作成し,hightemp.txtを入力ファイルとして実行せよ.さらに,同様の処理をUNIXコマンドでも実行し,プログラムの実行結果を確認せよ.
19.各行の1コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べる
各行の1列目の文字列の出現頻度を求め,その高い順に並べて表示せよ.確認にはcut, uniq, sortコマンドを用いよ.
#coding: UTF-8 ans = [] dic = {} f = open("hightemp.txt","r") for line in f: ans.append(unicode(line,"utf-8").split()[0]) if line in ans: dic[line] += 1 else: dic[line] = 1 sorted(dic) print dic f.close()
実行結果 haruka@ubuntu:~/NLP100$ python nlp19.py nlp19.py:9: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if line in ans: {'\xe5\xb1\xb1\xe5\xbd\xa2\xe7\x9c\x8c\t\xe9\xb6\xb4\xe5\xb2\xa1\t39.9\t1978-08-03\n': 1, '\xe5\xb1\xb1\xe6\xa2\xa8\xe7\x9c\x8c\t\xe5\x8b\x9d\xe6\xb2\xbc\t40.5\t2013-08-10\n': 1, '\xe5\xb2\x90\xe9\x98\x9c\xe7\x9c\x8c\t\xe5\xa4\x9a\xe6\xb2\xbb\xe8\xa6\x8b\t40.9\t2007-08-16\n': 1, '\xe5\xb1\xb1\xe6\xa2\xa8\xe7\x9c\x8c\t\xe7\x94\xb2\xe5\xba\x9c\t40.7\t2013-08-10\n': 1, '\xe5\x92\x8c\xe6\xad\x8c\xe5\xb1\xb1\xe7\x9c\x8c\t\xe3\x81\x8b\xe3\x81\xa4\xe3\x82\x89\xe3\x81\x8e\t40.6\t1994-08-08\n': 1, '\xe6\x84\x9b\xe7\x9f\xa5\xe7\x9c\x8c\t\xe6\x84\x9b\xe8\xa5\xbf\t40.3\t1994-08-05\n': 1, '\xe5\xb1\xb1\xe6\xa2\xa8\xe7\x9c\x8c\t\xe5\xa4\xa7\xe6\x9c\x88\t39.9\t1990-07-19\n': 1, '\xe7\xbe\xa4\xe9\xa6\xac\xe7\x9c\x8c\t\xe9\xa4\xa8\xe6\x9e\x97\t40.3\t2007-08-16\n': 1, '\xe9\x9d\x99\xe5\xb2\xa1\xe7\x9c\x8c\t\xe4\xbd\x90\xe4\xb9\x85\xe9\x96\x93\t40.2\t2001-07-24\n': 1, '\xe5\xb1\xb1\xe5\xbd\xa2\xe7\x9c\x8c\t\xe9\x85\x92\xe7\x94\xb0\t40.1\t1978-08-03\n': 1, '\xe5\xb1\xb1\xe5\xbd\xa2\xe7\x9c\x8c\t\xe5\xb1\xb1\xe5\xbd\xa2\t40.8\t1933-07-25\n': 1, '\xe5\x9f\xbc\xe7\x8e\x89\xe7\x9c\x8c\t\xe9\xb3\xa9\xe5\xb1\xb1\t39.9\t1997-07-05\n': 1, '\xe9\x9d\x99\xe5\xb2\xa1\xe7\x9c\x8c\t\xe5\xa4\xa9\xe7\xab\x9c\t40.6\t1994-08-04\n': 1, '\xe5\x8d\x83\xe8\x91\x89\xe7\x9c\x8c\t\xe7\x89\x9b\xe4\xb9\x85\t40.2\t2004-07-20\n': 1, '\xe5\xa4\xa7\xe9\x98\xaa\xe5\xba\x9c\t\xe8\xb1\x8a\xe4\xb8\xad\t39.9\t1994-08-08\n': 1, '\xe5\x9f\xbc\xe7\x8e\x89\xe7\x9c\x8c\t\xe8\xb6\x8a\xe8\xb0\xb7\t40.4\t2007-08-16\n': 1, '\xe6\x84\x9b\xe5\xaa\x9b\xe7\x9c\x8c\t\xe5\xae\x87\xe5\x92\x8c\xe5\xb3\xb6\t40.2\t1927-07-22\n': 1, '\xe7\xbe\xa4\xe9\xa6\xac\xe7\x9c\x8c\t\xe4\xb8\x8a\xe9\x87\x8c\xe8\xa6\x8b\t40.3\t1998-07-04\n': 1, '\xe9\xab\x98\xe7\x9f\xa5\xe7\x9c\x8c\t\xe6\xb1\x9f\xe5\xb7\x9d\xe5\xb4\x8e\t41\t2013-08-12\n': 1, '\xe5\x9f\xbc\xe7\x8e\x89\xe7\x9c\x8c\t\xe7\x86\x8a\xe8\xb0\xb7\t40.9\t2007-08-16\n': 1, '\xe6\x84\x9b\xe7\x9f\xa5\xe7\x9c\x8c\t\xe5\x90\x8d\xe5\x8f\xa4\xe5\xb1\x8b\t39.9\t1942-08-02\n': 1, '\xe5\xb2\x90\xe9\x98\x9c\xe7\x9c\x8c\t\xe7\xbe\x8e\xe6\xbf\x83\t40\t2007-08-16\n': 1, '\xe7\xbe\xa4\xe9\xa6\xac\xe7\x9c\x8c\t\xe5\x89\x8d\xe6\xa9\x8b\t40\t2001-07-24\n': 1, '\xe5\x8d\x83\xe8\x91\x89\xe7\x9c\x8c\t\xe8\x8c\x82\xe5\x8e\x9f\t39.9\t2013-08-11\n': 1}
コマンド確認 haruka@ubuntu:~/NLP100$ cut -f1 "hightemp.txt" | sort | uniq -c | sort -r 3 山梨県 3 山形県 3 埼玉県 3 群馬県 2 千葉県 2 静岡県 2 岐阜県 2 愛知県 1 和歌山県 1 大阪府 1 高知県 1 愛媛県
第3章:正規表現
Wikipediaの記事を以下のフォーマットで書き出したファイルjawiki-country.json.gzがある.
1行に1記事の情報がJSON形式で格納される 各行には記事名が"title"キーに,記事本文が"text"キーの辞書オブジェクトに格納され,そのオブジェクトがJSON形式で書き出される ファイル全体はgzipで圧縮される 以下の処理を行うプログラムを作成せよ. ※"jawiki-country.json"でエラーをだしすぎたあまりいつのまにかスワップファイルとなってしまっていたので以下では"jawiki-countrys.json"で処理しています。
23.セクション構造
記事中に含まれるセクション名とそのレベル(例えば"== セクション名 =="なら1)を表示せよ.
#coding: UTF-8 import re import sys print sys.getdefaultencoding() with open("jawiki-uks.txt","r") as f: for line in f.readlines(): text = re.match(r"(?P<number>=*)(?P<section>.*)=+$",line) if text is not None: print (int(line.count("=")/2),line.encode("utf-8"))
実行結果
haruka@ubuntu:~/NLP100$ python nlp23.py
ascii
Traceback (most recent call last):
File "nlp23.py", line 11, in
25.テンプレートの抽出
記事中に含まれる「基礎情報」テンプレートのフィールド名と値を抽出し,辞書オブジェクトとして格納せよ.
#coding: UTF-8 import re dic = {} with open("jawiki-uks.txt","r") as f: for line in f.readlines(): text = re.search("\|(?P<field>.*) \s=\s (?P<name>.*)\|",line) if text: dic[text.group("field")] = text.group("name") print dic
実行結果 haruka@ubuntu:~/NLP100$ python nlp25.py {}
第4章:形態素解析
夏目漱石の小説『吾輩は猫である』の文章(neko.txt)をMeCabを使って形態素解析し,その結果をneko.txt.mecabというファイルに保存せよ.このファイルを用いて,以下の問に対応するプログラムを実装せよ.
なお,問題37, 38, 39はmatplotlibもしくはGnuplotを用いるとよい.
30.形態素解析結果の読み込み
形態素解析結果(neko.txt.mecab)を読み込むプログラムを実装せよ.ただし,各形態素は表層形(surface),基本形(base),品詞(pos),品詞細分類1(pos1)をキーとするマッピング型に格納し,1文を形態素(マッピング型)のリストとして表現せよ.第4章の残りの問題では,ここで作ったプログラムを活用せよ.
#coding: UTF-8 res = [] with open("neko.txt.mecab","r") as f: for i in f: i.replace("\t",",") i.split(",") surface = i[0] base = i[1] pos = i[2] pos1 = i[6] res.append({ surface,base,pos,pos1 }) print res
今日のまとめ ファイル名を"(問題番号).py"から"nlp(問題番号).py"に変更しました。 今日解けたのは9, 今日のしんちょくはなし
言語処理100本ノック 2015のリンク 言語処理100本ノック 2015 1日目 言語処理100本ノック 2015 2日目 言語処理100本ノック 2015 3日目 言語処理100本ノック 2015 4日目