查找错误:从nltk.book导入*

5
在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]: 

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


3
尝试使用 nltk.download() 命令,它会打开一个面板。在那里,在 Corpora 中下载 gutenberg 书籍/语料库,然后再次尝试您的命令。 - Vaulstein
谢谢!它起作用了!已下载在错误信息中提到的语料库。以后我还需要再次下载未被下载的其他语料库吗? - Dakshila Kamalsooriya
4个回答

5
您在使用 nltk.book 时缺少了 Gutenberg 语料库,因此出现错误。 该错误是自说明的。
您需要使用 nltk.download() 下载语料库。 enter image description here 一旦语料库下载完成,请重新运行命令并检查是否再次出现错误。如果有,那么是另一个语料库的问题。请下载该语料库。 from nltk.book import * 不是首选方法,建议只导入您在代码中将要使用的语料库。 您可以使用 from nltk.corpus import gutenberg 替代。
请参阅链接进行参考。

谢谢!我只下载了每次出现错误的语料库。我还需要其他语料库吗? - Dakshila Kamalsooriya
这取决于您的应用程序及其使用方式。当您阅读nltk书籍时,您会意识到您不需要大部分语料库。一些必需的语料库包括brown、treebank、wordnet、words、conll2000、conll2002、ieer、gutenberg - Vaulstein

3

正如NLTK书籍所说,为了准备好使用该书,您需要打开nltk.download()弹出窗口,转到“集合”选项卡,并下载“Book”集合。这样做,您就可以毫无意外地阅读本书的其余部分。

顺便说一下,您也可以在Python控制台中执行nltk.download("book"),无需弹出窗口。


0

看起来它只在特定的位置搜索数据(如错误描述中所述)。尝试将nltk的内容复制到其中一个目录中(或创建一个目录),例如D:\ nltk_data这解决了我的问题(因为即使Guttenber已经下载,由于它没有在那个位置找到它,错误仍然会继续显示)

您收到的错误摘录:(这些是您可以选择放置nltk内容的目录之一,以便可以找到它们)

  • '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'

-1
也许你应该在以下目录中下载nltk_data包:

Screenshot 1


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接