pyhanlp中文依存句法应用

欢迎使用pyhanlp分析句子 ^ ^

安装

// pyhanlp安装 ^.^
pip install pyhanlp

安装简单粗暴,第一次使用的时候需要下载一些乱七八糟的东西所以要等一会儿
要注意 pyhanlp 目前不能支持 python3.9 及以上版本,如果你是和我一样用 python3.9 或者 python3.10 的大冤种需要重建个虚拟环境或者重装 python 哦 ^ ^

分词

如果没什么特殊需求使用 p y h a n l p pyhanlp pyhanlp 的分词就够了,示例如下:

// pyhanlp分词示例 ^.^
from pyhanlp import *

sen = '傲来雾,花果香,定海一棒万妖朝。'

# 带词性的分词
sen_semgent = HanLP.segment(sen)
print(sen_semgent)

# 输出
# [/a,/vf,/n,/w, 花果/n,/a,/w, 定海/nz,/m,/ng,/m,/n,/tg,/w]

句法依存分析

句法依存分析的示例如下:

// pyhanlp分词示例 ^.^
from pyhanlp import *

sen = '傲来雾,花果香,定海一棒万妖朝。'

# 依存句法分析
sen_denp = HanLP.parseDependency(sen)
print(sen_denp)

# 输出
# 1       傲      		傲      	a       a       _       3       定中关系			_       	_
# 2       来      		来      	u       u       _       1		右附加关系		_       	_
# 3       雾     		雾      	n       n       _       0       核心关系			_       	_
# 4       ,    		,      	wp      w       _       3       标点符号			_       	_
# 5       花果香  		花果香  	n   	n   	_   	3       并列关系			_       	_
# 6       ,      		,      	wp      w       _       5       标点符号			_       	_
# 7       定海一棒       定海一棒	ns      ns      _		8		定中关系			_       	_
# 8       万妖朝  		万妖朝  	nt      t       _   	5       并列关系			_       	_
# 9       。      		。      	wp      w       _       3       标点符号			_       	_

依存句法输出内容的类型是 < c l a s s ′ j p y p e . j c l a s s . c o m . h a n k c s . h a n l p . c o r p u s . d e p e n d e n c y . C o N l l . C o N L L S e n t e n c e ′ > <class 'jpype._jclass.com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence'> <classjpype.jclass.com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence>,把这个‘表’的列名补全应该是这样:

// pyhanlp依存句法分析输出 ^.^

# 输出
# ID	 FORM 		   LEMMA 	 CPOSTAG  POSTAG  FEATS   HEAD 		DEPREL 		  PHEAD 	 PDEPREL
# 1       傲      		傲      	a       a       _       3       定中关系			_       	_
# 2       来      		来      	u       u       _       1		右附加关系		_       	_
# 3       雾     		雾      	n       n       _       0       核心关系			_       	_
# 4       ,    		,      	wp      w       _       3       标点符号			_       	_
# 5       花果香  		花果香  	n   	n   	_   	3       并列关系			_       	_
# 6       ,      		,      	wp      w       _       5       标点符号			_       	_
# 7       定海一棒       定海一棒	ns      ns      _		8		定中关系			_       	_
# 8       万妖朝  		万妖朝  	nt      t       _   	5       并列关系			_       	_
# 9       。      		。      	wp      w       _       3       标点符号			_       	_

如果想把特定的属性输出的话,可以先将整体转成 L i s t List List后再选对应列,实例如下:

// pyhanlp依存句法分析拆解示例 ^.^
from pyhanlp import *

sen = '傲来雾,花果香,定海一棒万妖朝。'

sen_denp = HanLP.parseDependency(sen)
sen_denp_list = list(sen_denp)

for i in sen_denp_list:
	print(i.LEMMA, i.POSTAG, i.DEPREL) 

# 输出
# 傲 a 定中关系
# 来 u 右附加关系
# 雾 n 核心关系
# , w 标点符号
# 花果香 n 并列关系
# , w 标点符号
# 定海一棒 ns 定中关系
# 万妖朝 t 并列关系
# 。 w 标点符号

句法依存关系可视化

建议使用南京大学的Dependency Viewer,方便快捷好用。(不知道为什么这两天没下下来)
指路:http://nlp.nju.edu.cn/tanggc/tools/DependencyViewer.html
p y h a n l p pyhanlp pyhanlp 句子依存保存成txt文件再导入就可以使用啦。

// pyhanlp依存句法分析 + nju dependency viewer ^.^
from pyhanlp import *

sen = '傲来雾,花果香,定海一棒万妖朝。'

sen_denp = HanLP.parseDependency(sen)
fout = open('xxxxxx/sen_dependency.txt','a',encoding='utf-8')
print(sen_denp. file=fout)

把 sen_dependency.txt 文件导入到 Dependency Viewer 就好啦。

还有一些手动的可视化方法,比如使用 n e t w o r k x networkx networkx 或者 g r a p h v i z graphviz graphviz 手动画图,作者实在是嫌麻烦就偷懒不写辣 ^. ^

For Future Use

如果大家伙还想更好使用 pyhanlp 可以去它的 github 看看哈,
指路: https://github.com/hankcs/pyhanlp

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值