nltk for biginer
TRANSCRIPT
![Page 1: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/1.jpg)
にこにこテキストマイニング勉強会
初めての NLTKーツイートでの累積頻度ー
早川 敦士
![Page 3: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/3.jpg)
AGENDA
●自己紹介●NLTKについて●ツイートの取得●MeCabでツイートの分かち書き●NLTKを用いて累積頻度プロット●参考資料
![Page 4: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/4.jpg)
AGENDA
●自己紹介●NLTKについて●ツイートの取得●MeCabでツイートの分かち書き●NLTKを用いて累積頻度プロット●参考資料
![Page 5: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/5.jpg)
自己紹介
早川 敦士電気通信大学電気通信学部システム工学科 二年
TwitterID: @gepuro
テキストマイニング初心者プログラミング初心者
![Page 6: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/6.jpg)
AGENDA
●自己紹介●NLTKについて●ツイートの取得●MeCabでツイートの分かち書き●NLTKを用いて累積頻度プロット●参考資料
![Page 7: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/7.jpg)
NLTKについて
Natural Language Toolkitの略
ペンシルバニア大学で、コンピュータ言語学コーパスの一部として作成
●単純化●一貫性●拡張性
●モジュール性
を目標にデザインされた。
![Page 8: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/8.jpg)
NLTKについて
単純化
退屈で面倒な作業を少なくするユーザーへ NLPの実用知識を与える
直感的なフレームワーク
![Page 9: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/9.jpg)
NLTKについて
一貫性
一貫したインターフェイスとデータ構造推測しやすいメソッド名
![Page 10: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/10.jpg)
NLTKについて
拡張性
新しいソフトウェアモジュールが容易に対応可能であるような構造
![Page 11: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/11.jpg)
NLTKについて
モジュール性
ツールキットの他の部分の理解を必要なしで、独立して利用できるコンポーネント
![Page 12: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/12.jpg)
NLTKについて
要は、使いやすいように
作った
![Page 13: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/13.jpg)
AGENDA
●自己紹介●NLTKについて●ツイートの取得●MeCabでツイートの分かち書き●NLTKを用いて累積頻度プロット●参考資料
![Page 14: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/14.jpg)
ツイートの取得
ぺちゃくちゃ Reader で取得Windowsで使用可能です。
python-twitterなるものがあるそうだけど、知識不足の為、断念。一度目の挫折 orz
![Page 15: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/15.jpg)
ツイートの取得
まずは、ダウンロード。しみず工房 http://pcbase.web.infoseek.co.jp/
過去の投稿を取得後、csvファイルに出力
![Page 16: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/16.jpg)
ツイートの取得
csvファイルには、
●何番目のツイートか●発言ユーザー●ツイート内容
●ツイートの投稿時間●謎の数字
![Page 17: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/17.jpg)
ツイートの取得
今回、使用した情報は、
自分のツイート内容です。投稿時間等の他の情報は使用しませんでした。
![Page 18: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/18.jpg)
ツイートの取得
Pythonで csvを使おうとしたら苦戦しました。↓
Google Documentを使用した。
二度目の挫折 orz
![Page 19: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/19.jpg)
ツイートの取得
必要な無いツイート以外の内容を削除したのち、tcv形式で出力しました。
これで、とりあえず一段落。
![Page 20: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/20.jpg)
AGENDA
●自己紹介●NLTKについて●ツイートの取得●MeCabでツイートの分かち書き●NLTKを用いて累積頻度プロット●参考資料
![Page 21: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/21.jpg)
MeCabで分かち書き
分かち書きするなら、MeCabでしょ!
![Page 22: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/22.jpg)
MeCabで分かち書き
という訳で、MeCabのインストール
$ sudo apt-get install mecab
Ubuntu10.10で動作確認
![Page 23: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/23.jpg)
MeCabで分かち書き
$ mecab -Owakati input.tcv -o twitter.txt
これで分かち書きされる。
![Page 24: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/24.jpg)
AGENDA
●自己紹介●NLTKについて●ツイートの取得●MeCabでツイートの分かち書き●NLTKを用いて累積頻度プロット●参考資料
![Page 25: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/25.jpg)
NLTKを用いて累積頻度プロット
まずは、インストールを、
$ sudo apt-get install python-nltk
参考書を見ながら、プログラムを書いてみました。
![Page 26: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/26.jpg)
NLTKを用いて累積頻度プロット
#!/usr/bin/pythonimport sysimport nltkfrom nltk.book import *from nltk.corpus import PlaintextCorpusReaderreload(sys)sys.setdefaultencoding('utf-8')
corpus_root = '/home/dedicatus545/Dropbox/Documents/nlp/'wordlist = PlaintextCorpusReader(corpus_root,'twitter.txt',encoding='utf-8')
fdist = FreqDist(wordlist.words('twitter.txt'))fdist.plot(40,cumulative=True)
![Page 27: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/27.jpg)
NLTKを用いて累積頻度プロット
![Page 28: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/28.jpg)
NLTKを用いて累積頻度プロット
文字化け!!
![Page 29: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/29.jpg)
NLTKを用いて累積頻度プロット
しかも、文字化けだけで無く、 y軸が割合では無く、
カウントで表示されている。
三度目の挫折 orz
![Page 30: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/30.jpg)
NLTKを用いて累積頻度プロット
しかし、ここは譲ることができない。という訳で、
文字化けの修正と、 y軸を割合で表示できるようにしました。
![Page 31: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/31.jpg)
NLTKを用いて累積頻度プロット
どうやら、NLTKツールキットの内部的な問題なので、ライブラリにパッチを当ててみました。
![Page 32: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/32.jpg)
NLTKを用いて累積頻度プロット
グラフ出力のメソッドは、/usr/lib/pymodules/python2.6/nltk/probability.py
を参照してます。
![Page 33: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/33.jpg)
NLTKを用いて累積頻度プロット
変更した内容は、asciiコードでの処理を utf-8に変更
累積の割合で表示できるように、引数を追加フォントの指定による文字化けの回避です。
http://www.mma.club.uec.ac.jp/~hayakawa/nltk_probability_plot.txt
に掲載しました。
![Page 34: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/34.jpg)
NLTKを用いて累積頻度プロット
#!/usr/bin/pythonimport nltkfrom nltk.book import *from nltk.corpus import PlaintextCorpusReader
corpus_root = '/home/dedicatus545/Dropbox/Documents/nlp/'wordlist = PlaintextCorpusReader(corpus_root,'twitter.txt',encoding='utf-8')
fdist = FreqDist(wordlist.words('twitter.txt'))fdist.plot(40,cumulative=True,percent=True) #上位 40単語でプロット
を実行しました。
![Page 35: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/35.jpg)
NLTKを用いて累積頻度プロット
![Page 36: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/36.jpg)
NLTKを用いて累積頻度プロット
助詞や句読点で大半を占めていることが分かる。自分のツイートの特徴的なものが
出てこなかった。
![Page 37: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/37.jpg)
NLTKを用いて累積頻度プロット
そこで、上位 100語を出力してみた。fdist.tabulate(100)で表示される。
![Page 38: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/38.jpg)
NLTKを用いて累積頻度プロット
特徴的な語は、
Http,今日、何、時間、僕、勉強、 R、 fkhr、mlka、けど、良い、アニメ、明日
頻度順
![Page 39: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/39.jpg)
NLTKを用いて累積頻度プロット
アニメ!
![Page 40: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/40.jpg)
NLTKを用いて累積頻度プロット
しかし、アニメより、勉強という語の方が出現頻度が高くて、
安心しました。
![Page 41: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/41.jpg)
AGENDA
●自己紹介●NLTKについて●ツイートの取得●MeCabでツイートの分かち書き●NLTKを用いて累積頻度プロット●参考資料
![Page 42: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/42.jpg)
参考資料
出版 O'RELLY 入門 自然言語処理
Steven Bird, Ewan Klein, Edward Loper 著萩原 正人、中山 敬広、水野 貴明 訳
![Page 43: Nltk for biginer](https://reader033.vdocuments.pub/reader033/viewer/2022052400/55991c5f1a28abde478b4803/html5/thumbnails/43.jpg)
おわり
ご清聴ありがとうございました。