一、环境变量的方法
- 安装最新的nltk(3.1),否则没有包含stanford模型
- 下载parser的stanford-parser.jar以及模型stanford-parser-models.jar
- 设置环境变量,将stanford-parser.jar的路径加入calsspath,创建一个STANFORD_MODELS,将stanford-parser-models.jar路径加入
- 在python中查看一下环境变量是否生效
import osprint os . environ ( 'calsspath' )如果不生效,重启一下编译环境(PyDev)
- python代码
二、不用环境变量的方法from nltk.parse import stanford
if __name__ == '__main__':parser = stanford.StanfordParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")s = parser.raw_parse("Targeted fiscal year 2015 voluntary programs, with eligibility limited by both grade and Air Force specialty codes, will be offered to help properly shape the force.")
for line in s:for t in line:print(t)
- 直接代码,设置相关包的绝对路径
from nltk.parse.stanford import StanfordParser
if __name__ == '__main__':stanford_parser_dir = 'D:/workspace/stanford-parser-full-2015-04-20/'eng_model_path = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"my_path_to_models_jar = stanford_parser_dir + "stanford-parser-3.5.2-models.jar"my_path_to_jar = stanford_parser_dir + "stanford-parser.jar"
parser=StanfordParser(model_path=eng_model_path, path_to_models_jar=my_path_to_models_jar, path_to_jar=my_path_to_jar)
s = parser.raw_parse("Targeted fiscal year 2015 voluntary programs, with eligibility limited by both grade and Air Force specialty codes, will be offered to help properly shape the force.")for line in s:for t in line:print(t)