テクニカルプア

備忘録と若干の補足

NLTKの準備で困ったこととその対処法

2013-03-06 追記

本文中ではpython2-nltk-gitを使っているが、結局素直にpython2-nltkを使うことにした。python2-nltk-gitでも不自由はなかったのだが。

 

 春休みなので本を読んでいる。

 昨日まではダラダラと「Linuxシステムプログラミング」を読んでいて、それがようやく読み終わったので今度は「入門 自然言語処理」を読むことにした。

 ちなみにpythonに関する知識は以前pythonが配布しているチュートリアルを適当に読み流した程度で終わっている。

 で、この本を読むにあたってNLTKのインストールが必要なのでいろいろとインストールした。本では推奨とか任意となってるパッケージも全部入れた。

$ yaourt -S nltk-data python2-nltk-git python2-numpy python2-matplotlib python2-networkx prover9

 python2-nltkというパッケージが既にバイナリで配布されているのだけれどなぜかAURのpython2-nltk-gitをつかってしまった。

 で、読み始める。生データを落としたりして本文に即してpythonインタプリタを叩いて遊んでたら以下の部分でエラーが起きた。

>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
/usr/lib/python2.7/site-packages/nltk/draw/__init__.py:14: UserWarning: nltk.draw package not loaded (please install Tkinter library).
  warnings.warn("nltk.draw package not loaded "
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/nltk/text.py", line 455, in dispersion_plot
    from nltk.draw import dispersion_plot
ImportError: cannot import name dispersion_plot

 どうやらTkinterとかいうのをimportすればいいらしい。だが

>>> import Tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 39, in 
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libtk8.6.so: cannot open shared object file: No such file or directory

となったので、おとなしくtkというのを入れる(最初python-pmwというのをAURから入れたのだがtkとtclが入っていればそれでよいらしい)。

sudo pacman -S tk tcl

で、

>>> import Tkinter
>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/nltk/text.py", line 455, in dispersion_plot。
    from nltk.draw import dispersion_plot
ImportError: cannot import name dispersion_plot

となったのだが正直何がなんだかわからなかったのでインタプリタを再起動し、もう一度必要なものをimportして試してみることにした。すると

>>> import nltk
>>> import Tkinter
>>> 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.
<<<略>>>
>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/nltk/text.py", line 456, in dispersion_plot
    dispersion_plot(self, words)
  File "/usr/lib/python2.7/site-packages/nltk/draw/dispersion.py", line 27, in dispersion_plot
    raise ValueError('The plot function requires the matplotlib package (aka pylab).'
ValueError: The plot function requires the matplotlib package (aka pylab).See http://matplotlib.sourceforge.net/

となった。冒頭でmatplotlibっぽいのはインストールしたはずなので import pylab を試してみると、

>>> import pylab
Traceback (most recent call last):
  File "<stdin&gt", line 1, in <module&gt
  File "/usr/lib/python2.7/site-packages/pylab.py", line 1, in <module&gt
    from matplotlib.pylab import *
  File "/usr/lib/python2.7/site-packages/matplotlib/pylab.py", line 265, in <module&gt
    from matplotlib.pyplot import *
  File "/usr/lib/python2.7/site-packages/matplotlib/pyplot.py", line 97, in <module&gt
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/usr/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module&gt
    from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
  File "/usr/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 16, in <module&gt
    raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.

となる。おとなしくpygtkをインストールする。

sudo pacman -S pygtk

 ここまでやったところで dispersion_plot() を試してみると、以下のようになって本に記載の通りの画像が出力された。

>>> import pylab
>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
Fontconfig warning: "/etc/fonts/conf.d/50-user.conf", line 9: reading configurations from ~/.fonts.conf is deprecated.
>>>

 なんかwarningとか言われてるけど気にしない。

参考