【问题标题】:LookupError: from nltk.book import*LookupError: 从 nltk.book 导入*
【发布时间】:2015-06-18 07:20:21
【问题描述】:

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

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

【问题讨论】:

  • 尝试使用nltk.dowwnload() 这将打开一个面板。从Corpora 下载gutenberrg book/corpora 并再次尝试您的命令
  • 谢谢!有效!错误消息中提到的已下载语料库。我是否需要其他语料库,但后来没有再次下载?

标签: python import ipython nltk anaconda


【解决方案1】:

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

您需要使用nltk.download() 下载语料库。

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

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

参见link上的参考

【讨论】:

  • 谢谢!我只下载了每次出现在错误中的语料库。我还需要其他语料库吗?
  • 这取决于您的应用程序及其使用情况。当您阅读 nltk 书时,您会意识到您不需要大部分语料库。一些需要的语料库是brown, treebank, wordnet, words, conll2000, conll2002, ieer, gutenberg
【解决方案2】:

正如 NLTK 书籍所说,准备使用书籍的方法是打开nltk.download() 弹出窗口,转到“收藏”选项卡,然后下载“书籍”收藏。这样做,您就可以毫无意外地阅读本书的其余部分。

顺便说一句,您可以通过执行nltk.download("book")从python控制台执行相同的操作,而无需弹出窗口

【讨论】:

    【解决方案3】:

    似乎它仅在特定位置搜索数据(如错误描述中所述)。 尝试将 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'

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-24
        • 2019-04-09
        • 2012-03-21
        • 2015-07-28
        • 2018-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多