简体   繁体   English

LookupError:来自nltk.book import *

[英]LookupError: from nltk.book import*

In iPython console I typed from nltk.book import and I got several LookupErrors. 在iPython控制台中,我输入from nltk.book import ,我得到了几个LookupErrors。 Below shows the code I got. 下面显示了我得到的代码。

*** 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]: 

Can i know why I get these errors? 我能知道为什么会收到这些错误吗?

Your missing the Gutenberg corpora in nltk.book , hence the error. 您在nltk.book遗漏了Gutenberg语料库,因此错误。 The error is self descriptive. 该错误是自我描述性的。

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

Once the corpora is downloaded, re-run your command and check if the error comes up again. 下载语料库后,重新运行命令并检查错误是否再次出现。 If it does, it would be for another corpora. 如果确实如此,那将是另一个语料库。 Download that corpora too. 也下载该语料库。

from nltk.book import * is not the preferred method, it is advisable to only import the corpora which you would be using in your code. from nltk.book import *不是首选方法,建议只导入您将在代码中使用的语料库。 You could use from nltk.corpus import gutenberg instead. 您可以使用from nltk.corpus import gutenberg代替。

See reference on link 请参阅链接参考

As the NLTK book says, the way to prepare for working with the book is to open up the nltk.download() pop-up, turn to the tab "Collections", and download the "Book" collection. 正如NLTK书中所说,准备使用本书的方法是打开nltk.download()弹出窗口,转到“Collections”选项卡,然后下载“Book”集。 Do it and you can read the rest of the book with no surprises. 这样做,你可以阅读本书的其余部分,没有任何意外。

Incidentally you can do the same from the python console, without the pop-ups, by executing nltk.download("book") 顺便说一句,你可以通过执行nltk.download("book")从python控制台执行相同的操作,而不需要弹出窗口

Seems it searches for the data only at specific places (like mentioned in the error description). 似乎它仅在特定位置搜索数据(如错误描述中所述)。 Try copying the content of nltk into one of those directories (or create one) such as D:\\nltk_data This solved the issue for me (because the error would continue to show up even if the Guttenber was already downloaded since it did not find it at that place) 尝试将nltk的内容复制到其中一个目录(或创建一个)中,例如D:\\ nltk_data 这解决了我的问题 (因为即使已经下载了Guttenber,错误也会继续出现,因为它没有找到它在那个地方)

An excerpt from the error you get: (these are the directories among which you can choose where to place the nltk content so that it can be found) 你得到的错误的摘录:(这些是你可以选择在哪里放置nltk内容以便找到它的目录)

  • 'C:\\Users\\dell/nltk_data' 'C:\\用户\\ DELL / nltk_data'
  • 'C:\\nltk_data' 'C:\\ nltk_data'
  • 'D:\\nltk_data' 'd:\\ nltk_data'
  • 'E:\\nltk_data' 'E:\\ nltk_data'
  • 'C:\\Users\\dell\\Anaconda\\nltk_data' 'C:\\用户\\ DELL \\阿纳康达\\ nltk_data'
  • 'C:\\Users\\dell\\Anaconda\\lib\\nltk_data' 'C:\\用户\\ DELL \\阿纳康达\\ lib中\\ nltk_data'
  • 'C:\\Users\\dell\\AppData\\Roaming\\nltk_data' 'C:\\用户\\ DELL \\应用程序数据\\漫游\\ nltk_data'

Maybe you should download the nltk_data package in the following directory: 也许您应该在以下目录中下载nltk_data包:

截图1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM