ぴろの狂人日記

2014年から頑張ってブログを継続しようと思います。「継続と習慣」を今年の目標にしたので、頑張って更新を続けようと思います。おいおいはレビューや数学や認知科学などについて記事を書いていければと思っています

Macでpython3の環境構築7---NLTKのインストール1---

今回はNLTKをインストールしていきます。
まず、NLTK自体のインストールはすぐできます。
次のようにコマンドを打ちます。


hiroshi-no-MacBook-Air:~ hiroshi$ sudo pip3 install -u nltk          #       「sudo  pip3 install -u nltk」と打ってreturnを押す。


そうすると、パスワードを求められるので入力します。今までも出てきたので同じことの繰り返しになりますが、打っても入力できているようには見えませんが、きちんと認識はされています。


Password:          #  自分のPCのパスワードを入力。入力できているようには見えないけど、きちんと認識はされています。


そうすると以下のような処理がはしり、NLTKのインストールは終わりです。


The directory '/Users/hiroshi/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The directory '/Users/hiroshi/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting nltk
  Downloading nltk-3.0.5.tar.gz (1.0MB)
    100% |████████████████████████████████| 1.0MB 325kB/s
Requirement already up-to-date: six>=1.9.0 in /usr/local/lib/python3.4/site-packages (from nltk)
Building wheels for collected packages: nltk
  Running setup.py bdist_wheel for nltk
  Stored in directory: /Users/hiroshi/Library/Caches/pip/wheels/f6/ef/3d/039e21095c9845e948931918734aa6c05df4d72f09a3132c69
Successfully built nltk
Installing collected packages: nltk
Successfully installed nltk-3.0.5

hiroshi-no-MacBook-Air:~ hiroshi$ 


これで、NLTKのインストールは完了。
このあと、サンプルデータのインストールになりますが、これが少しやっかいかも。

続いて、次のようにコマンドを打ちます。


hiroshi-no-MacBook-Air:~ hiroshi$ python3        #「python3」と打ってreturnを押す。


そうすると次のようになり、また入力の待ち状態になります。


Python 3.4.3 (default, Aug 11 2015, 08:57:25)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>          #入力の待ち状態


そこにまたコマンドをうちます。


>>> import nltk       #「import nltk」と打ってreturnを押す。
>>> nltk.download()     #そうするとまた入力待ちになるので、続けて「nltk.download()」と打ってreturnを押す。


そうすると、
ターミナルの方は、


showing info http://www.nltk.org/nltk_data/


と表示されると同時に 新しい画面が立ち上がります。

で、上の画面はallが選ばれていますが、bookを選んで左下のDownloadを押します。
そうすると、/User/hiroshi/の中に、nltk_dataというフォルダが作成されます。
これでうまくいくはずなのですが、エラーが出てしまいました。

そのとき、ターミナル上は、こうなっています。


Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1533, in __call__
    return self.func(*args)
  File "/usr/local/lib/python3.4/site-packages/nltk/downloader.py", line 1569, in _download
    return self._download_threaded(*e)
  File "/usr/local/lib/python3.4/site-packages/nltk/downloader.py", line 1836, in _download_threaded
    assert self._download_msg_queue == []
AssertionError
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1533, in __call__
    return self.func(*args)
  File "/usr/local/lib/python3.4/site-packages/nltk/downloader.py", line 1569, in _download
    return self._download_threaded(*e)
  File "/usr/local/lib/python3.4/site-packages/nltk/downloader.py", line 1836, in _download_threaded
    assert self._download_msg_queue == []
AssertionError


このinvalid argumentというのをそのまま検索してみると、引数が不正、無効、とのこと。
一体なんのことやら。。。
わかる方がいたら教えてください。

よくわからないので、新しく出た画面の方はバツとじをしてしまいました。
そうすると、Trueとターミナル上には表示されます。


#####先ほどの続きから。中略。########
  File "/usr/local/lib/python3.4/site-packages/nltk/downloader.py", line 1569, in _download
    return self._download_threaded(*e)
  File "/usr/local/lib/python3.4/site-packages/nltk/downloader.py", line 1836, in _download_threaded
    assert self._download_msg_queue == []
AssertionError
True        #バツとじをするTrueと表示される。


全然うまくいっている感じはしないけれど、(そもそもエラーでたし)念のためうまくいっているかどうか確認をすることに。
「from nltk.book import *」と打って、「text1」と打って、「<Text: Moby Dick by Herman Melville 1851>」と表示されればうまくいったかどうかの確認らしい。

先ほどのところでTrueとでたあとはまた入力待ちのような状態になっているので、次ようにまた打ち込みます。


>>> from nltk.book import *       #    「from nltk.book import *」と打ってreturnを押す。


そうするとつぎのようになります。


*** 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
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/nltk/corpus/util.py", line 63, in __load
    try: root = nltk.data.find('corpora/%s' % zip_name)
  File "/usr/local/lib/python3.4/site-packages/nltk/data.py", line 624, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource 'corpora/webtext.zip/webtext/' not found.  Please use
  the NLTK Downloader to obtain the resource:  >>> nltk.download()
  Searched in:
    - '/Users/hiroshi/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
**********************************************************************

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.4/site-packages/nltk/book.py", line 34, in 
    text6 = Text(webtext.words('grail.txt'), name="Monty Python and the Holy Grail")
  File "/usr/local/lib/python3.4/site-packages/nltk/corpus/util.py", line 99, in __getattr__
    self.__load()
  File "/usr/local/lib/python3.4/site-packages/nltk/corpus/util.py", line 64, in __load
    except LookupError: raise e
  File "/usr/local/lib/python3.4/site-packages/nltk/corpus/util.py", line 61, in __load
    root = nltk.data.find('corpora/%s' % self.__name)
  File "/usr/local/lib/python3.4/site-packages/nltk/data.py", line 624, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource 'corpora/webtext' not found.  Please use the NLTK
  Downloader to obtain the resource:  >>> nltk.download()
  Searched in:
    - '/Users/hiroshi/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
**********************************************************************
>>>


やはりうまくいっている感じは全然しないけれど、続けます。


>>> text8     #「text8」と打ってreturnを押す。


が、やはり


Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'text8' is not defined


となってしまいます。「NameError: name 'text8' is not defined」とか言われてるしね。。。
text5とかtext1とかでやっても同じです。

しらべてみると、「nltk.download()」のくだりは別の方法でもできるらしい。
結局今回のエラーについて原因はよくわからないままでしたが、あとまわしにして別の方法で、試してみたいと思います。それは次回。