自然语言处理:基于PCFG的中文句法分析样例

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
['我', '准备', '坐地铁', '去', '虹桥', '火车站', '接', '你', '。']

在这里插入图片描述

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值