报错详情
$ import nltk
>>> nltk.download()
[nltk_data] Error loading : <urlopen error [Errno 54] Connection reset
[nltk_data] by peer>
解决方案
离线安装NLTK Data文件
1.下载NLTK Data
Github https://github.com/nltk/nltk_data/tree/gh-pages 下载得到nltk_data-gh-pages.zip文件
整包都下载(需要的是其中的package)
2.将package文件夹内容移到nltk_data下目录
2.1 查找nltk的搜索目录
即nltk_data的理论路径
>>> import nltk
>>> nltk.find('.')
获取
Searched in:
- '/Users/Rachel1900/nltk_data'
- '/Library/Frameworks/Python.framework/Versions/3.8/nltk_data'
- '/Library/Frameworks/Python.framework/Versions/3.8/share/nltk_data'
- '/Library/Frameworks/Python.framework/Versions/3.8/lib/nltk_data'
- '/usr/share/nltk_data'
- '/usr/local/share/nltk_data'
- '/usr/lib/nltk_data'
- '/usr/local/lib/nltk_data'
以上任意一个均可;或直接参考报错页面的安装存储路径
由于nltk.download()命令运行失败,nltk_data目录目前实际并不存在
2.2 创建nltk_data目录
$ mkdir nltk_data
或将packages文件夹重新命名为nltk_data
2.3 package中内容移动至的nltk_data目录
3.测试
>>> from nltk.book import *
返回:
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.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
成功
参考
离线下载安装NLTK的nltk_data数据包
离线下载安装 NLTK 的 nltk_data 模块