オライリー・ジャパンさんより、「入門自然言語処理」という本をご献本いただいた。人生初の献本なのですごくうれしい。

書評を書くという約束でいただいているので、さっそく読み始める…のだけれど、この本は実習問題がたくさん掲載されているので、手を動かさずにただ読んでもあまり意味がない。そこで、サンプルを動かす準備をしてみた。


インストールするもの

  • python本体
  • NLTK (Natural Language Toolkit)
  • サンプルテキスト

python本体

NLTKが対応している中で一番新しい2.6系を使うことにする。

CentOS 5.4
% python -V
Python 2.4.3

うーむ。あんまり時間かけたくないので、この記事を参考にyumでpython26としてインストールした。

RPM置き場はこちら。記事の時点とはファイル名が変わってるので注意。

% wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/i386/epel-release-1-1.ius.el5.noarch.rpm
% wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/i386/ius-release-1.0-6.ius.el5.noarch.rpm
% sudo rpm -i epel-release-1-1.ius.el5.noarch.rpm
% sudo rpm -i ius-release-1.0-6.ius.el5.noarch.rpm
% sudo yum install python26-devel
MacOSX

最近MacBook ProのHDDをSSDに換装して、Snow Leopardをクリーンインストールした。そのときにMacPortsからHomebrewに乗り換えたので、ここではHomebrewでインストールする道を探る。

% brew info python
python 2.7

おっと。NLTKは2.4〜2.6のサポートなので、2.7をインストールするわけにもいかない。

ググったらこちらの記事がヒットした。自分でFormulaを書いて、gistにあげている。おおこれはありがたい…と思ったんだけど、インストールされるバージョンが2.6.5。2.6系は現在2.6.6になっているので、Forkして修正してみた。

% cd /usr/local/Library/Formula
% curl "https://gist.github.com/raw/727879/65c5b6f9c686ddbf0e5c399f745daa7ef7a5ba12/python2.6%20for%20homebrew" > python2.6.rb
% brew install python2.6
% python -V
Python 2.6.1

…あれ?

% which python
/usr/bin/python

homebrewは/usr/local/binにいろいろインストールするので、PATHの順番を変えてやればいいか。~/.zshrcもしくは~/.bashrcあたりに…

export PATH=/usr/local/bin:$PATH

…を追記する。Homebrewのインストール時にやっておくべき作業かな。

% source ~/.zshrc
% which python
/usr/local/bin/python
% python -V
Python 2.6.6

よしよし。…まあ、もともとシステムには2.6.1が入ってたのでそっちを使ってもよかったような気がしないでもないけど。

NLTK

「Download」ページの手順に従ってソースからインストール。まずはPyYAMLから。

PyYAML

ダウンロードページはここ

% wget http://pyyaml.org/download/pyyaml/PyYAML-3.09.tar.gz
% tar zxvf PyYAML-3.09.tar.gz
% cd PyYAML-3.09/
% sudo python26 setup.py install
NLTK

続いてNLTK本体。ダウンロードリンクとインストール手順はここにある。

% wget http://nltk.googlecode.com/files/nltk-2.0b9.tar.gz
% tar zxvf nltk-2.0b9.tar.gz
% cd nltk-2.0b9/
% sudo python26 setup.py install
サンプルテキスト

NLTKに付属のダウンローダでサンプルテキストを入手する。

% python26
>>> import nltk
>>> nltk.download()

MacOSだとGUIのダウンローダが立ち上がるので、「book」を選択して「Download」。

CentOSはコンソールで作業続行。

NLTK Downloader
---------------------------------------------------------------------------
    d) Download      l) List      c) Config      h) Help      q) Quit
---------------------------------------------------------------------------
Downloader> l

Packages:
/usr/lib/python2.6/site-packages/nltk/__init__.py:588: DeprecationWarning: object.__new__() takes no parameters
  [ ] maxent_ne_chunker... ACE Named Entity Chunker (Maximum entropy)
  [ ] abc................. Australian Broadcasting Commission 2006
  [ ] brown............... Brown Corpus

(snip)

Collections:
  [ ] all-corpora......... All the corpora
  [ ] all................. All packages
  [ ] book................ Everything used in the NLTK Book

([*] marks installed packages)

---------------------------------------------------------------------------
    d) Download      l) List      c) Config      h) Help      q) Quit
---------------------------------------------------------------------------
Downloader> d

Download which package (l=list; x=cancel)?
  Identifier> book
    Downloading collection 'book'
       |
       | Downloading package 'brown' to /home/kwappa/nltk_data...
       |   Unzipping corpora/brown.zip.
       | Downloading package 'chat80' to /home/kwappa/nltk_data...

(snip)
       | Downloading package 'book_grammars' to
       |     /home/kwappa/nltk_data...
       |   Unzipping grammars/book_grammars.zip.
       | Downloading package 'tagsets' to /home/kwappa/nltk_data...
       |
     Done downloading collection 'book'

データは ~/nltk_data/ 以下に格納される。

% du --max-depth=1 -h nltk_data/
64K     nltk_data/grammars
237M    nltk_data/corpora
23M     nltk_data/tokenizers
18M     nltk_data/chunkers
14M     nltk_data/taggers
24K     nltk_data/help
290M    nltk_data/

本にはさらっと「約100MB」と書いてあるが、この記事の時点で300MBに迫る勢い。ディスクスペースには気をつけよう。

動作確認

>>> 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>
>>> text1.concordance("monstrous")
Building index...
Displaying 11 of 11 matches:
ong the former , one was of a most monstrous size . ... This came towards us ,
ON OF THE PSALMS . " Touching that monstrous bulk of the whale or ork we have r
ll over with a heathenish array of monstrous clubs and spears . Some were thick
d as you gazed , and wondered what monstrous cannibal and savage could ever hav
that has survived the flood ; most monstrous and most mountainous ! That Himmal
they might scout at Moby Dick as a monstrous fable , or still worse and more de
th of Radney .'" CHAPTER 55 Of the monstrous Pictures of Whales . I shall ere l
ing Scenes . In connexion with the monstrous pictures of whales , I am strongly
ere to enter upon those still more monstrous stories of them which are to be fo
ght have been rummaged out of this monstrous cabinet there is no telling . But
of Whale - Bones ; for Whales of a monstrous size are oftentimes cast up dead u

まとめ

今回はweb開発が目的じゃないのでインストール手順は微妙だけど(pipとか準備してないし)、「入門自然言語処理」を読み始めるための準備としては十分だろうと思う。

積ん読している「初めてのコンピュータサイエンス」もPythonが教材なので、読み進めるためにはちょうどいい機会だった。

ちなみに職場のLT大会でも「自然言語処理やってみようよ」的な話をした。気軽に手を出せるようになったのはとてもいいことだと思う。

入門自然言語処理入門 from Hiromu Shioya