在PC上搭建 StanfordCoreNLP + Python 開發環境,步驟如下:
1. 下載NLP工具包
下載地址: https://share.weiyun.com/5UJ1Gdi
將下載好的stanford-corenlp-full-2018-10-05.zip
放置於電腦的D盤
2. 安裝 stanfordcorenlp (Python版接口)
使用清華的鏡像安裝stanfordcorenlp,速度會快些。
pip install stanfordcorenlp -i https://pypi.tuna.tsinghua.edu.cn/simple
3. 下載JDK
下載地址:https://share.weiyun.com/5HRNRDK
安裝下載好的jdk-8u231-windows-x64.exe
,一直都選擇默認的下一步
4. 編寫Python腳本
# 導入NLP類
from stanfordcorenlp import StanfordCoreNLP
# 生成nlp對象
nlp = StanfordCoreNLP(r'D:\stanford-corenlp-full-2018-10-05')
# 待分析的句子
sentence = 'Stanford CoreNLP provides a set of human language technology tools.'
# 分詞,將句子打散
print('Tokenize:\n', nlp.word_tokenize(sentence))
print('-' * 60)
# 生成語法樹
print('Constituency Parsing:\n', nlp.parse(sentence))
print('-' * 60)
# 顯示依存關系
print('Dependency Parsing:\n', nlp.dependency_parse(sentence))
nlp.close()
也可以在IDLE中逐行輸入代碼:
5. 結果分析
Python輸出的結果和在線版本是一致的。
在線版給出的是可視化的結果:
Python代碼給出的是文本格式的結果,例如('det', 5, 4),5是set的索引,4是a的索引。
相關鏈接