提示会让我们下载某些包,但很可能提示下载失败
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:/msys/1.0/home/nltk_data'
- 'C:\\Users\\16662\\AppData\\Local\\Programs\\Python\\Python37\\nltk_data'
- 'C:\\Users\\16662\\AppData\\Local\\Programs\\Python\\Python37\\share\\nltk_data'
- 'C:\\Users\\16662\\AppData\\Local\\Programs\\Python\\Python37\\lib\\nltk_data'
- 'C:\\Users\\16662\\AppData\\Roaming\\nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- ''
一般照做提示
>>> import nltk
>>> nltk.download('punkt')
但大概率拒绝连接,这时候就得去访问nltk_data的github
https://github.com/nltk/nltk_data
用git clone或者其他手段下载下来,打开里面的package
把package里的对应文件(如果保守点所有文件也行),放在
Searched in:
- 'C:/msys/1.0/home/nltk_data'
- 'C:\\Users\\16662\\AppData\\Local\\Programs\\Python\\Python37\\nltk_data'
- 'C:\\Users\\16662\\AppData\\Local\\Programs\\Python\\Python37\\share\\nltk_data'
- 'C:\\Users\\16662\\AppData\\Local\\Programs\\Python\\Python37\\lib\\nltk_data'
- 'C:\\Users\\16662\\AppData\\Roaming\\nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
这里的其中一个目录下,记住要把zip文件解压缩,下面是递归解压缩的python代码供参考
import os
import zipfile
# 定义要处理的根目录
root_dir = os.path.expanduser('~/miniconda3/envs/amphion/share/nltk_data/')
# 递归遍历目录及其子目录
for root, dirs, files in os.walk(root_dir):
for file in files:
if file.endswith('.zip'):
zip_file_path = os.path.join(root, file)
try:
# 打开压缩包
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
# 解压到当前目录
zip_ref.extractall(root)
print(f"已解压 {zip_file_path}")
except zipfile.BadZipFile:
print(f"无法解压 {zip_file_path},可能是损坏的压缩包。")