python nltk下载_【NLP】NLTK的安装和数据包的下载

1. 安装nltk

cmd中:

pip install nltk

2. 下载nltk数据包

python环境/编译器中

import nltk

nltk.download()

弹出一个自动的可交互下载框

选择all packages

download

但是速度很慢,据说需要两天可以完全下载

3. 补充下载失败的文件

记录下 download directory的路径位置,打开该路径文件夹

可以看到有下载好的文件

打开某个文件夹,可以看到下面有zip文件和解压缩后的文件

如果用nltk.download() 没有成功下载所有文件,重新运行该语句的时候总会报错“丢失链接、无法连接”等问题

去github下载文件

可以直接download整个工程

或者单独下载某个包的zip文件

或者

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.参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值