4
3

More than 3 years have passed since last update.

NLTKでnltk.download('wordnet')でResource not found.となってしまう

Posted at

はじめに

ゼロから作るDeepLearning2を使って勉強会を開こうと思い、復習をしていたました。
せっかくなので付録で説明されているwordnetを実装していたのですが、Colabで書籍通りに動かなかったのでまとめたいと思います。

古めのライブラリで利用する人は少ないかなと思いましたが、有名な書籍かつ初心者向けということで同じく詰まる人の助けになればと思います。

環境

  • Google Colabratory

問題

まず、nltkをインストールします。

!pip install nltk

その後、以下のコードを実行します。

import nltk
from nltk.corpus import wordnet

# carの類義語を調べる
wordnet.synsets('car')

すると、以下のエラーが発生します。

LookupError                               Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/nltk/corpus/util.py in __load(self)
     79             except LookupError as e:
---> 80                 try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
     81                 except LookupError: raise e

5 frames
LookupError: 
**********************************************************************
  Resource wordnet not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('wordnet')

  Searched in:
    - '/root/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - '/usr/nltk_data'
    - '/usr/lib/nltk_data'
**********************************************************************


During handling of the above exception, another exception occurred:

LookupError                               Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/nltk/data.py in find(resource_name, paths)
    671     sep = '*' * 70
    672     resource_not_found = '\n%s\n%s\n%s\n' % (sep, msg, sep)
--> 673     raise LookupError(resource_not_found)
    674 
    675 

LookupError: 
**********************************************************************
  Resource wordnet not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('wordnet')

  Searched in:
    - '/root/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - '/usr/nltk_data'
    - '/usr/lib/nltk_data'
**********************************************************************

wordnetの読み込むデータないようでした。
書籍通りではColabでは動作しないようです。

解決方法

wordnetにつかうデータをダウンロードすることで解決しました。

import nltk
from nltk.corpus import wordnet

# 必要なパッケージ、データ、ドキュメントのダウンロード (時間がかかる)
nltk.download('all')

wordnet.synsets('car')

おわりに

おそらくダウンロードで不要なものもダウンロードしているので、うまくダウンロードの設定を変えるとよいかもしれません。('all'を変更)

私は、書籍を試すだけでしたのでallを使いました。

最近はnltkを使うこともあまりないかと思いますので、詳しくは調べておりません。わかる方いましたらコメントお願いします。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3