ぴろの狂人日記

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

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

前回の続きです。
(前回:

piroshhh.hatenablog.com



nltk.download()とコマンドを打ってもうまくいかないというお話でした。

これは次のようなコマンドでも代替できるようです。

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


そうすると、次のような処理が始まります。

[nltk_data] Downloading collection 'book'
[nltk_data]    |
[nltk_data]    | Downloading package abc to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package abc is already up-to-date!
[nltk_data]    | Downloading package brown to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package brown is already up-to-date!
[nltk_data]    | Downloading package chat80 to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package chat80 is already up-to-date!
[nltk_data]    | Downloading package cmudict to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package cmudict is already up-to-date!
[nltk_data]    | Downloading package conll2000 to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package conll2000 is already up-to-date!
[nltk_data]    | Downloading package conll2002 to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package conll2002 is already up-to-date!
[nltk_data]    | Downloading package dependency_treebank to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package dependency_treebank is already up-to-date!
[nltk_data]    | Downloading package genesis to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package genesis is already up-to-date!
[nltk_data]    | Downloading package gutenberg to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package gutenberg is already up-to-date!
[nltk_data]    | Downloading package ieer to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package ieer is already up-to-date!
[nltk_data]    | Downloading package inaugural to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package inaugural is already up-to-date!
[nltk_data]    | Downloading package movie_reviews to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package movie_reviews is already up-to-date!
[nltk_data]    | Downloading package nps_chat to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package nps_chat is already up-to-date!
[nltk_data]    | Downloading package names to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package names is already up-to-date!
[nltk_data]    | Downloading package ppattach to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package ppattach is already up-to-date!
[nltk_data]    | Downloading package reuters to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Package reuters is already up-to-date!
[nltk_data]    | Downloading package senseval to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/senseval.zip.
[nltk_data]    | Downloading package state_union to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/state_union.zip.
[nltk_data]    | Downloading package stopwords to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/stopwords.zip.
[nltk_data]    | Downloading package swadesh to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/swadesh.zip.
[nltk_data]    | Downloading package timit to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/timit.zip.
[nltk_data]    | Downloading package treebank to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/treebank.zip.
[nltk_data]    | Downloading package toolbox to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/toolbox.zip.
[nltk_data]    | Downloading package udhr to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/udhr.zip.
[nltk_data]    | Downloading package udhr2 to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/udhr2.zip.
[nltk_data]    | Downloading package unicode_samples to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/unicode_samples.zip.
[nltk_data]    | Downloading package webtext to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/webtext.zip.
[nltk_data]    | Downloading package wordnet to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/wordnet.zip.
[nltk_data]    | Downloading package wordnet_ic to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/wordnet_ic.zip.
[nltk_data]    | Downloading package words to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/words.zip.
[nltk_data]    | Downloading package maxent_treebank_pos_tagger to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping taggers/maxent_treebank_pos_tagger.zip.
[nltk_data]    | Downloading package maxent_ne_chunker to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping chunkers/maxent_ne_chunker.zip.
[nltk_data]    | Downloading package universal_tagset to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping taggers/universal_tagset.zip.
[nltk_data]    | Downloading package punkt to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping tokenizers/punkt.zip.
[nltk_data]    | Downloading package book_grammars to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping grammars/book_grammars.zip.
[nltk_data]    | Downloading package city_database to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping corpora/city_database.zip.
[nltk_data]    | Downloading package tagsets to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |   Unzipping help/tagsets.zip.
[nltk_data]    | Downloading package panlex_swadesh to
[nltk_data]    |     /Users/hiroshi/nltk_data...
[nltk_data]    |
[nltk_data]  Done downloading collection book
hiroshi-no-MacBook-Air:~ hiroshi$


では、次にそのまま、

hiroshi-no-MacBook-Air:~ hiroshi$ from nltk.book import *             
 #       $のあとにいきなり「from nltk.book import *」と打ってもうまくはいかない。


としても。。。

from: can't read /var/mail/nltk.book
hiroshi-no-MacBook-Air:~ hiroshi$


と返されうまくいきません。

今度はこうしてみます。

hiroshi-no-MacBook-Air:~ hiroshi$ python3


そうすると、こうなります。

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を押す。
>>> text1               #    続けてtext1と打ってreturnを押す。


それでも、やはりこうなってしまい失敗。

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


今度は、こうしてみます。

>>> 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
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」と打って

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


となり、なんとか成功しました。

まとめとしては、「python3 -m nltk.downloader book」を行ったあとは

  1. python3  とコマンドを打つ。
  2. import nltk とコマンドを打つ。
  3. from nltk.book import * とコマンドを打つ。
  4. 最後にtext1と打って、上記のように表示されれば成功。

となります。