導入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下載地址