LookupError:从 nltk.book 导入*

Dak*_*iya 5 python import ipython nltk anaconda

在 iPython 控制台中,我from nltk.book import输入了几个 LookupErrors。下面显示了我得到的代码。

*** 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.
---------------------------------------------------------------------------
LookupError Traceback (most recent call last)
<ipython-input-3-8446809acbd4> in <module>()
 ----> 1 from nltk.book import*

C:\Users\dell\Anaconda\lib\site-packages\nltk-3.0.3-py2.7.egg\nltk\book.py in <module>()
 20 print("Type: 'texts()' or 'sents()' to list the materials.")
 21 
---> 22 text1 = Text(gutenberg.words('melville-moby_dick.txt'))
 23 print("text1:", text1.name)
 24 

 C:\Users\dell\Anaconda\lib\site-packages\nltk-3.0.3-py2.7.egg\nltk\corpus\util.pyc in __getattr__(self, attr)
 97             raise AttributeError("LazyCorpusLoader object has no attribute '__bases__'")
 98 
 ---> 99         self.__load()
100         # This looks circular, but its not, since __load() changes our
101         # __class__ to something new:

 C:\Users\dell\Anaconda\lib\site-packages\nltk-3.0.3-py2.7.egg\nltk\corpus\util.pyc in __load(self)
 62             except LookupError as e:
 63                 try: root = nltk.data.find('corpora/%s' % zip_name)
 ---> 64                 except LookupError: raise e
 65 
 66         # Load the corpus.

 LookupError: 
 **********************************************************************
 Resource u'corpora/gutenberg' not found.  Please use the NLTK
 Downloader to obtain the resource:  >>> nltk.download()
 Searched in:
- 'C:\\Users\\dell/nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'C:\\Users\\dell\\Anaconda\\nltk_data'
- 'C:\\Users\\dell\\Anaconda\\lib\\nltk_data'
- 'C:\\Users\\dell\\AppData\\Roaming\\nltk_data'
**********************************************************************

In [4]: 
Run Code Online (Sandbox Code Playgroud)

我能知道为什么会出现这些错误吗?

Vau*_*ein 5

您缺少Gutenberg中的语料库nltk.book,因此出现错误。该错误是自我描述的。

您需要使用nltk.download()来下载语料库。 在此输入图像描述

下载语料库后,重新运行命令并检查是否再次出现错误。如果是的话,那将是另一个语料库。也下载该语料库。

from nltk.book import *不是首选方法,建议仅导入您将在代码中使用的语料库。你可以用from nltk.corpus import gutenberg它来代替。

请参阅链接上的参考