安装stanfordnlp以及简单使用

请注意:不是stanfordcorenlp
如果直接在CMD下,pip install stanfordnlp,我遇到了这个错误:在这里插入图片描述
于是我直接下载torch也遇到了错误,所以我在conda下,建立了一个虚拟环境,
conda create -n pytorch python=3.6 //创建虚拟环境
activate pytorch // 激活虚拟环境
conda install torch // 安装torch
由于我的电脑配置不高,没有下载GPU版本,经过一段时间的等待,下载完成之后。
pip install stanfordnlp (这里纯属试一下为什么一般的cmd不行, 然后就成功了)

stanfordnlp的起步很简单,官网也有相关的教程https://stanfordnlp.github.io/stanfordnlp/installation_usage.html#getting-started

import stanfordnlp
sentence = ""  # 句子
stanfordnlp.download("en")  # 你要下载的语言

然后会提示你是否下载语料包,输入y,然后定义语料包保存路径
经过一段时间的等待(反正我是等了很久)。
之后使用只需要:

import stanfordnlp
sentence = ""  # 句子
nlp = stanfordnlp.Pipeline(lang='en')
doc = nlp(sentence)
print(doc.sentences[0].print_dependencies())

# 如果要输出pos结果
print(word.upos for sent in doc.sentences for word in sent.words)
发布了12 篇原创文章 · 获赞 3 · 访问量 2050

猜你喜欢

转载自blog.csdn.net/weixin_40902563/article/details/100714972