【python 报错】NLTK stopword找不到

问题描述

使用NLTK时,出现stopword资源找不到。

解决方法

在网上找到公开资源,下载文件后,解压到相应地址。

解决方法博客链接

  • 查看地址路径:
from nltk import data
print(data.path)

在这里插入图片描述
将下载的文件解压到下面目录里,没有文件夹的新建文件夹:

  • 我在解压时候没有这个目录,找到在/Users/mac/opt/anaconda3文件
  • 在目录下新建一个nltk_data文件夹;
  • 再在nltk_data里建corpora文件夹,将stopword拉进去。
    在这里插入图片描述

过程记录

这里记录自己的解决过程。

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

报错内容:

---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/nltk/corpus/util.py in __load(self)
     85                 try:
---> 86                     root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
     87                 except LookupError:

~/opt/anaconda3/lib/python3.7/site-packages/nltk/data.py in find(resource_name, paths)
    700     resource_not_found = '\n%s\n%s\n%s\n' % (sep, msg, sep)
--> 701     raise LookupError(resource_not_found)
    702 

LookupError: 
**********************************************************************
  Resource stopwords not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('stopwords')
  
  For more information see: https://www.nltk.org/data.html

  Attempted to load corpora/stopwords.zip/stopwords/

  Searched in:
    - '/Users/mac/nltk_data'
    - '/Users/mac/opt/anaconda3/nltk_data'
    - '/Users/mac/opt/anaconda3/share/nltk_data'
    - '/Users/mac/opt/anaconda3/lib/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
**********************************************************************


During handling of the above exception, another exception occurred:

LookupError                               Traceback (most recent call last)
<ipython-input-18-a8339b7b1fb5> in <module>
      1 from nltk.corpus import stopwords
----> 2 stop = stopwords.words('english')
      3 train['stopwords']=train['tweet'].apply(lambda x: len([x for x in x.split() if x in stop]))
      4 train[['tweet','stopwords']].head()

~/opt/anaconda3/lib/python3.7/site-packages/nltk/corpus/util.py in __getattr__(self, attr)
    121             raise AttributeError("LazyCorpusLoader object has no attribute '__bases__'")
    122 
--> 123         self.__load()
    124         # This looks circular, but its not, since __load() changes our
    125         # __class__ to something new:

~/opt/anaconda3/lib/python3.7/site-packages/nltk/corpus/util.py in __load(self)
     86                     root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
     87                 except LookupError:
---> 88                     raise e
     89 
     90         # Load the corpus.

~/opt/anaconda3/lib/python3.7/site-packages/nltk/corpus/util.py in __load(self)
     81         else:
     82             try:
---> 83                 root = nltk.data.find('{}/{}'.format(self.subdir, self.__name))
     84             except LookupError as e:
     85                 try:

~/opt/anaconda3/lib/python3.7/site-packages/nltk/data.py in find(resource_name, paths)
    699     sep = '*' * 70
    700     resource_not_found = '\n%s\n%s\n%s\n' % (sep, msg, sep)
--> 701     raise LookupError(resource_not_found)
    702 
    703 

LookupError: 
**********************************************************************
  Resource stopwords not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('stopwords')
  
  For more information see: https://www.nltk.org/data.html

  Attempted to load corpora/stopwords

  Searched in:
    - '/Users/mac/nltk_data'
    - '/Users/mac/opt/anaconda3/nltk_data'
    - '/Users/mac/opt/anaconda3/share/nltk_data'
    - '/Users/mac/opt/anaconda3/lib/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
**********************************************************************

根据返回的信息,输入代码下载,依旧失败

import nltk
nltk.download('stopwords')

在这里插入图片描述

直接去网站下载:http://www.nltk.org/nltk_data/
显示找不到网页,可能被墙掉了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值