1、下载
https://nlp.stanford.edu/software/lex-parser.shtml#Download
解压缩
复制stanford-parser.jar和stanford-parser-3.9.2-models.jar到python程序文件同目录下
2、python程序
#coding=utf-8
# 分词
import jieba
# PCFG句法分析
from nltk.parse import stanford
import os
if __name__ == '__main__':
string = '我准备坐地铁去虹桥火车站接你。'
seg_list = jieba.cut(string, cut_all=False, HMM=True)
seg_str = ' '.join(seg_list)
print(seg_str)
root = './'
parser_path = root + 'stanford-parser.jar'
model_path = root + 'stanford-parser-3.9.2-models.jar'
# 指定JDK路径
if not os.environ.get('JAVA_HOME'):
JAVA_HOME = '/usr/lib/jvm/jdk1.8'
os.environ['JAVA_HOME'] = JAVA_HOME
# PCFG模型路径
pcfg_path = 'edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz'
parser = stanford.StanfordParser(
path_to_jar=parser_path,
path_to_models_jar=model_path,
model_path=pcfg_path
)
sentence = parser.raw_parse(seg_str)
for line in sentence:
print(line.leaves())
line.draw()
输出
runfile('D:/Users/chengyq/py/chapter-6/PCFG.py', wdir='D:/Users/chengyq/py/chapter-6')
我 准备 坐地铁 去 虹桥 火车站 接 你 。
D:/Users/chengyq/py/chapter-6/PCFG.py:32: DeprecationWarning: The StanfordParser will be deprecated
Please use nltk.parse.corenlp.CoreNLPParser instead.
model_path=pcfg_path
['我', '准备', '坐地铁', '去', '虹桥', '火车站', '接', '你', '。']