NLTK和停用词失效 #lookuperror

68

我正在尝试启动一个情感分析的项目,我将使用停用词方法。我进行了一些研究,并发现nltk有停用词,但是当我执行命令时出现了错误。

我的做法是,为了知道nltk使用哪些单词(就像你可能在这里找到的http://www.nltk.org/book/ch02.html的4.1节中所发现的内容一样):

from nltk.corpus import stopwords
stopwords.words('english')

但是当我按下回车键时,我得到的是

---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
<ipython-input-6-ff9cd17f22b2> in <module>()
----> 1 stopwords.words('english')

C:\Users\Usuario\Anaconda\lib\site-packages\nltk\corpus\util.pyc in __getattr__(self, attr)
 66
 67     def __getattr__(self, attr):
---> 68         self.__load()
 69         # This looks circular, but its not, since __load() changes our
 70         # __class__ to something new:

C:\Users\Usuario\Anaconda\lib\site-packages\nltk\corpus\util.pyc in __load(self)
 54             except LookupError, e:
 55                 try: root = nltk.data.find('corpora/%s' % zip_name)
---> 56                 except LookupError: raise e
 57
 58         # Load the corpus.

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

因为这个问题,类似以下的事情无法正常运行(获得相同的错误):

>>> from nltk.corpus import stopwords
>>> stop = stopwords.words('english')
>>> sentence = "this is a foo bar sentence"
>>> print [i for i in sentence.split() if i not in stop]

你知道问题可能是什么吗?我必须使用西班牙语单词,你推荐另一种方法吗?我也考虑使用英语数据集和Goslate包。

谢谢阅读!

P.D.:我使用Ananconda。

7个回答

162

您的电脑似乎没有停用词语料库。

您需要启动 NLTK 下载器并下载所有所需数据。

打开 Python 控制台,然后执行以下操作:

>>> import nltk
>>> nltk.download()
showing info http://nltk.github.com/nltk_data/
在打开的 GUI 窗口中,只需按下“下载”按钮即可下载所有 corpus,或转到“语料库”选项卡并仅下载您需要/想要的语料库。

在 GUI 窗口中,只需按下“下载”按钮即可下载所有语料库,或进入“语料库”选项卡,仅下载您需要或想要的语料库。


87
如果您想避免使用图形用户界面并且知道想要下载什么:nltk.download("stopwords") - KLDavenport

17

我在Ubuntu终端尝试了tttthomasssss的答案,但不知道为什么GUI没有显示出来。所以我遵循了KLDavenport的评论,它起作用了。以下是摘要:

打开您的终端/命令行并键入python,然后

>>> import nltk .>>> nltk.download("stopwords")

这将在nltk_data下存储停用词语料库。对于我的情况,它是/home/myusername/nltk_data/corpora/stopwords

如果您需要另一个语料库,请访问nltk数据并查找其ID的语料库。然后像我们下载停用词语料库一样使用ID进行下载。


这很有效,但我感到惊讶的是这不是通过 pip 完成的。相反,你必须编写脚本来在每个环境中获取这些资源。 - Tim Hysniu

9
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
STOPWORDS = set(stopwords.words('english'))

3

1

import nltk

nltk.download()

  • 弹出一个GUI,在其中选择Corpora部分,选择所需的语料库。
  • 验证结果

1
您可以使用以下命令。
 import nltk

 nltk.download()

在按下回车键后,将弹出一个窗口,您可以从中下载所有所需的语料库和其他nltk工具。

0
import nltk
nltk.download()

当 GUI 提示时,点击下载按钮。这对我起作用了。(nltk.download('stopwords') 对我无效)


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