【自然语言处理】安装NLTK并下载数据包的方法
1. 安装nltk
cmd中:
pip install nltk
2. 下载nltk数据包
python环境/编译器中
import nltk
nltk.download()
弹出一个自动的可交互下载框
选择all packages
download
但是速度很慢,据说需要两天可以完全下载
3. 补充下载失败的文件
记录下 download directory的路径位置,打开该路径文件夹
可以看到有下载好的文件
打开某个文件夹,可以看到下面有zip文件和解压缩后的文件
如果用nltk.download() 没有成功下载所有文件,重新运行该语句的时候总会报错“丢失链接、无法连接”等问题
去github下载文件
https://github.com/nltk/nltk_data
可以直接download整个工程
或者单独下载某个包的zip文件
https://github.com/nltk/nltk_data/tree/gh-pages/packages
或者
nltk.download(‘punkt’)
ps:可能也会丢失连接
将下载的zip文件放到本机对应的文件夹路径下
并解压缩即可
4. nltk使用示例代码
eg1:
import nltk
sen = 'hello, how are you?'
res = nltk.word_tokenize(sen) #分词
print(res)
eg2:
text = "hello, how are you? I'm from China"
tokens = nltk.word_tokenize(text) #分词
tagged = nltk.pos_tag(tokens) #词性标注
entities = nltk.chunk.ne_chunk(tagged) #命名实体识别
a1=str(entities) #将文件转换为字符串
file_object = open('out.txt', 'w')
file_object.write(a1) #写入到文件中
file_object.close( )
print(entities)
# 语法解析树
from nltk.corpus import treebank
t = treebank.parsed_sents('wsj_0001.mrg')[0]
t.draw()
4. tips
如果运行示例报错,去github下载对应的加粗位置路径下的相应工具包再解压缩到本机即可
报错示例:
Traceback (most recent call last):
File "D:\Users\xxxxx\AppData\Local\Anaconda3\lib\site-packages\nltk\corpus\util.py", line 80, in __load
try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
File "D:\Users\xxxxx\AppData\Local\Anaconda3\lib\site-packages\nltk\data.py", line 653, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource 'corpora/treebank.zip/treebank/combined/' not found.
Please use the NLTK Downloader to obtain the resource: >>>
nltk.download()
Searched in:
- 'D:\\Users\\xxxxx/nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'D:\\Users\\xxxxx\\pData\\Local\\Anaconda3\\nltk_data'
- 'D:\\Users\\xxxxx\\AppData\\Local\\Anaconda3\\lib\\nltk_data'
- 'D:\\Users\\xxxxx\\AppData\\Roaming\\nltk_data'
**********************************************************************
During handling of the above exception, another exception occurred:
按报错提示,需要下载 corpora/treebank.zip
5.参考资料:
https://github.com/nltk/nltk_data
https://www.cnblogs.com/guo7533/p/8695812.html
https://blog.****.net/sinat_34328764/article/details/94830948
https://blog.****.net/qiang12qiang12/article/details/81254595
https://www.osgeo.cn/nltk/data/
https://wing2south.com/post/speedup-ntlk-data-download/
https://blog.****.net/qq_43376013/article/details/102883773
https://blog.****.net/weixin_44574186/article/details/90748946
上一篇: 使用和安装python的nltk库
下一篇: NLTK简介和示例使用
推荐阅读
-
IOS UI 自动化测试实践:pyhton-wda 环境设置篇-Xcode 版本:10.1iphone 版本:12.0.1OS 版本:10.13.6 实践开始 创建一个新目录并从 git 下载 WDA 项目代码。 git clone https://github.com/facebook/WebDriverAgent 并运行初始化脚本。 ./Scripts/bootstrap.sh 出现以下错误信息:原因:Carthage 需要下载相关的依赖项,而这些依赖项并未在本地安装。 解决方法通过 brew 下载并安装依赖项: brew install carthage 下载成功并初始化脚本后,出现以下错误:原因:需要 npm 来打包响应 js 文件,而机器上未安装该文件。 解决方案:通过 brew 下载并安装 npm(注:brew 真的是个好东西):brew install npm 安装成功后,继续初始化脚本。/Scripts/bootstrap.sh Xcode 相关操作
-
使用Python库nltk进行自然语言处理的安装方法
-
【自然语言处理】安装NLTK并下载数据包的方法
-
【小沐探索自然语言处理】NLTK库在Python中的安装及入门教程