导入pyhanlp包,并调用依存句法分析方法。
具体hanlp的一些常用方法等,可参考另一篇文章 Hanlp安装使用详解
from pyhanlp import *
para_sen = "马伊琍与文章宣布离婚,华为是背后的赢家。"
sentence = HanLP.parseDependency(para_sen)
print(sentence)
# 输出依存文法的结果 txt文件,在windows系统下的 Dependency Viewer.exe 打开文件
path = "/Users/aaaa/Documents/Work/aa.txt"
with open(path, "w", encoding='utf-8') as f:
f.write(str(sentence))
print("path:%s" % (path))
输出结果

将输出的txt文件在Dependency Viewer中打开显示如下: DependencyViewer.exe下载地址
DependencyViewer官方: http://nlp.nju.edu.cn/tanggc/tools/DependencyViewer.html

本文介绍如何使用HanLP进行依存句法分析,通过实例演示了从导入pyhanlp包到调用依存句法分析方法的全过程,并展示了分析结果的输出方式。
582





