安装:
1.下载
处理中文还需要下载中文的模型jar文件,然后放到stanford-corenlp-full-2018-02-27根目录下即可
2.准备jar包
将下载下来的stanford-corenlp-full-2016-10-31解压并将下载中文的jar文件
stanford-chinese-corenlp-2016-10-31-models.jar放到该目录下。
3.安装
安装Stanford CoreNLP工具
!pip install stanfordcorenlp
使用
1.代码引入
from stanfordcorenlp import StanfordCoreNLP
nlp= StanfordCoreNLP(r'D:\Soft\tool\stanford-corenlp-4.5.7', lang='zh')
sentence = '浙江大学有七个校区。'
print(nlp.word_tokenize(sentence))
print(nlp.pos_tag(sentence))
print(nlp.ner(sentence))
print(nlp.parse(sentence))
print(nlp.dependency_parse(sentence))
nlp.close()
(1)导入
nlp = StanfordCoreNLP(r’D:\Soft\tool\stanford-corenlp-4.5.7’)
nlp= StanfordCoreNLP(r'D:\Soft\tool\stanford-corenlp-4.5.7', lang='zh')
读入的地址是文件夹地址,不指定语言的话默认是英语。
(2)分词 Tokenization
nlp.word_tokenize
(3)词性标注 Part-of-speech Tagging
nlp.pos_tag(sentence)
(4)实体识别 NERCombinerAnnotator
nlp.ner(sentence)
(5)句法成分分析 Constituency Parsing
nlp.parse(sentence)
(5)依存句法分析
nlp.dependency_parse(sentence)
更多用法见Full List Of Annotators - CoreNLP
2.命令行开启服务器
命令行输入,开启 CoreNLP服务器
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 7000 -timeout 15000
程序内调用
# Use an existing server
nlp = StanfordCoreNLP('http://localhost', port=7000)
在浏览器访问http://localhost:7000/