入門自然言語処理のための準備

入門自然言語処理の読書会に参加するために本屋で購入してきました。

入門自然言語処理

元々言語解析には興味があったので、これを機に勉強しようと思います。
最近はちょっとした流行になっているらしく、今自然言語処理を知っておくとモテるらしい。
やらない手はないよね!


必要なソフトウェアとしてPythonは当然として、NLTKが必要となるので早速ダウンロード。

http://www.nltk.org/download

今回はMacにインストールを行うのでMac OS Xの欄。

  1. MacPython
  2. PyYAML
  3. NLTK

をダウンロードし、インストールを行う。

準備が整ったらPythonインタプリタを起動し、下記のコマンドを打つ。

>>> import nltk
>>> import nltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named nltk

あれ?
NLTKが見つからない。
インストールが失敗しているのかな。
NLTKのダウンロード元を参照するとトラブルシューティングについて記載があった。

Troubleshooting

It is possible that the NLTK installer gives an error message like "Errors occurred. Try installing again." This may indicate that the NLTK installer could not locate a suitable version of Python on your machine, or that you have more than one suitable version of Python installed. In this case, open a terminal window, type cd /tmp/nltk-installer and then type sudo python setup.py install.

なるほど。直接インストールすればいいのか。
早速実行。

cd /tmp/nltk-installer
sudo python setup.py install

これでインストール処理が行われていく。

では改めて。

>>> import nltk
>>> nltk.download()

無事にimportも通り、downloadをコールすることでNLTK DownloaderのGUIが起動できた。
後はFullInstallでOK。

インストールが完了したらNLTKのBookモジュールをロードする。

>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
>>> 

ロードが完了したら早速参照してみる。

>>> text1
<Text: Moby Dick by Herman Melville 1851>

これでNLTKを使用する準備ができた。