#!/usr/bin/env python
# -*- coding:utf-8 -*-
from nltk.parseimport *
from pyltpimport *
words ="我 有 一只 小 毛驴".split(" ")
postagger = Postagger()
postagger.load(r"D:\NLP\ltp_data_v3.4.0\pos.model")#加载词性标注模块
postags = postagger.postag(words)
parse = Parser()
parse.load(r"D:\NLP\ltp_data_v3.4.0\parser.model")#加载句法解析模块
arcs = parse.parse(words, postags)#句法解析
arclen =len(arcs)
conll =""
for iin range(arclen):
if arcs[i].head ==0:
arcs[i].relation ="ROOT"
conll +="\t" + words[i] +"(" + postags[i] +")" +"\t" + postags[i] +"\t" +str(arcs[i].head) +"\t" + arcs[i].relation +"\n"
print(conll)
conlltree = DependencyGraph(conll)#转换为依存句法图
tree = conlltree.tree()#构建树结构
tree.draw()#显示输出的树
网友评论