看了网上很多博文介绍stanford-corenlp 安装使用。总觉得乱,不够傻瓜式。通过鄙人一下午的试错,我写一下mini教程。
- 下载 stanford-corenlp
brew install stanford-corenlp
用Homebrew下载,没那么多啰嗦。
- 下载models
手动下载(我用folx),保存到/usr/local/Cellar/stanford-corenlp/3.9.1/libexec
(一般是这个地址,参考你的homebrew安装的路径,或者有什么特别的配置)
我一开始装在/usr/local/Cellar/stanford-corenlp/3.9.1/,不对的!
-
下载Python封装
pip install stanfordcorenlp
-
使用
源文件
#!/usr/bin/env python3
from stanfordcorenlp import StanfordCoreNLP
sentence = '我爱你.'
with StanfordCoreNLP(r'/usr/local/Cellar/stanford-corenlp/3.9.1/libexec', lang='zh') as nlp:
print ('Tokenize:', nlp.word_tokenize(sentence))
print ('Part of Speech:', nlp.pos_tag(sentence))
print ('Named Entities:', nlp.ner(sentence))
print ('Constituency Parsing:', nlp.parse(sentence))#语法树
print ('Dependency Parsing:', nlp.dependency_parse(sentence))#依存句法
运行
sudo path/to/corenlptest.py
Tokenize: ['我', '爱', '你', '.']
Part of Speech: [('我', 'PN'), ('爱', 'VV'), ('你', 'PN'), ('.', 'PU')]
Named Entities: [('我', 'O'), ('爱', 'O'), ('你', 'O'), ('.', 'O')]
Constituency Parsing: (ROOT
(IP
(NP (PN 我))
(VP (VV 爱)
(NP (PN 你)))
(PU .)))
Dependency Parsing: [('ROOT', 0, 2), ('nsubj', 2, 1), ('dobj', 2, 3), ('punct', 2, 4)]
注意:没有sudo 会报PermissionError
网友评论