已解决(nltk加载词库报错问题)Resource punkt not found. Please use the NLTK Downloader to obtain the resource:
报错代码
粉丝群里面的一个小伙伴想用nltk加载词库,在网上复制别人的代码运行的时候发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下:
import nltk
words = nltk.word_tokenize('Python is a widely used programming language.')
print(nltk.pos_tag(words))
我们可以看到他导包没有任何问题(已经安装了nltk模块):
但是还是发生了报错,报错信息内容如下:
```python
D:\Python3.8\python.exe E:/Python/test2.py
Traceback (most recent call last):
File "E:/Python/test2.py", line 3, in <module>
words = nltk.word_tokenize('Python is a widely used programming language.')
File "D:\Python3.8\lib\site-packages\nltk\tokenize\__init__.py", line 129, in word_tokenize
sentences = [text] if preserve_line else sent_tokenize(text, language)
File "D:\Python3.8\lib\site-packages\nltk\tokenize\__init__.py", line 106, in sent_tokenize
tokenizer = load(f"tokenizers/punkt/{language}.pickle")
File "D:\Python3.8\lib\site-packages\nltk\data.py", line 750, in load
opened_resource = _open(resource_url)
File "D:\Python3.8\lib\site-packages\nltk\data.py", line 876, in _open
return find(path_, path + [""]).open()
File "D:\Python3.8\lib\site-packages\nltk\data.py", line 583, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource punkt not found.
Please use the NLTK Downloader to obtain the resource:
>>> import nltk
>>> nltk.download('punkt')
For more information see: https://www.nltk.org/data.html
Attempted to load tokenizers/punkt/english.pickle
Searched in:
- 'C:\\Users\\Administrator/nltk_data'
- 'D:\\Python3.8\\nltk_data'
- 'D:\\Python3.8\\share\\nltk_data'
- 'D:\\Python3.8\\lib\\nltk_data'
- 'C:\\Users\\Administrator\\AppData\\Roaming\\nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- ''
**********************************************************************
报错翻译
报错信息翻译:
未找到资源punkt。
请使用NLTK下载器获取资源:
报错原因
报错原因:安装nltk库的时候不会自动安装词库,按照下面的方式手动安装即可,博主已经把词库下载好了,根据下面自行免费下载领取!!!
解决方法
当时我查阅资料得知,可以自己下载这个数据。根据报错信息提示放到以下任一目录即可(注意:每个人的目录不一定)
不论选择哪个路径,注意的是:在安装之前,必须新建两层文件夹:nltk_data、tokenizers
再次运行程序就可以了
博主已经下好了NLTK 常用的三个词库:停用词词库、词性词库。。。关注文末公众号回复:nltk_data
,即可领取!!! 下载后放到自己的报错信息的指定文件路径下即可!
关注文末公众号回复:nltk_data
帮忙解决
本文已收录于:《告别Bug》专栏
本专栏用于记录学习和工作中遇到的各种疑难Bug问题,以及粉丝群里小伙伴提出的各种问题,文章形式:报错代码 + 报错翻译 + 报错原因 + 解决方法,包括程序安装、运行程序过程中等等问题,订阅专栏+关注博主后如遇到其他问题可私聊帮忙解决!!!