NLTK使用Stanford parser方法,可类推Stanford其他nlp工具

一、环境变量的方法
  • 安装最新的nltk(3.1),否则没有包含stanford模型
  • 下载parser的stanford-parser.jar以及模型stanford-parser-models.jar
  • 设置环境变量,将stanford-parser.jar的路径加入calsspath,创建一个STANFORD_MODELS,将stanford-parser-models.jar路径加入
  • 在python中查看一下环境变量是否生效
import  os
print  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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值