在windows的cmd命令行中使用pip安装:
pip install nltk
python环境/编译器中
import nltk
nltk.download()
弹出一个自动的可交互下载框
选择all packages=>download
但是速度很慢,据说需要两天可以完全下载
记录下 download directory的路径位置,打开该路径文件夹
如果找不到可以在以下地方寻找:
可以看到有下载好的文件
打开某个文件夹,可以看到下面有zip文件和解压缩后的文件
如果用nltk.download() 没有成功下载所有文件,重新运行该语句的时候总会报错“丢失链接、无法连接”等问题
https://github.com/nltk/nltk_data
可以直接download整个工程
或者单独下载某个包的zip文件
https://github.com/nltk/nltk_data/tree/gh-pages/packages
或者
nltk.download(‘punkt’)
ps:可能也会丢失连接
将下载的zip文件放到本机对应的文件夹路径下
并解压缩即可
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()
如果运行示例报错,去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.csdn.net/sinat_34328764/article/details/94830948
https://blog.csdn.net/qiang12qiang12/article/details/81254595
https://www.osgeo.cn/nltk/data/
https://wing2south.com/post/speedup-ntlk-data-download/
https://blog.csdn.net/qq_43376013/article/details/102883773
https://blog.csdn.net/weixin_44574186/article/details/90748946