本系列文章主要結合Python語言實現知識圖譜構建相關工程,具有一定創新性和實用性,非常希望各位博友交流討論,相互促進成長。第一篇文章主要介紹哈工大pyltp工具,包括安裝過程、中文分詞等;第二篇文章主要講解詞性標注、實體識別、依存句法分析和語義角色標注。
知識圖譜系列文章:
[知識圖譜實戰篇] 一.數據抓取之Python3抓取JSON格式的電影實體
[知識圖譜實戰篇] 二.Json+Seaborn可視化展示電影實體
[知識圖譜實戰篇] 三.Python提取JSON數據、HTML+D3構建基本可視化布局
[知識圖譜實戰篇] 四.HTML+D3+CSS繪制關系圖譜
[知識圖譜實戰篇] 五.HTML+D3添加鼠標響應事件顯示相關節點及邊
[知識圖譜實戰篇] 六.HTML+D3實現點擊節點顯示相關屬性及屬性值
[知識圖譜實戰篇] 七.HTML+D3實現關系圖譜搜索功能
[知識圖譜實戰篇] 八.HTML+D3繪制時間軸線及顯示實體
環境下載地址:https://download.csdn.net/download/eastmount/11226539
原文地址:
[Python知識圖譜] 一.哈工大pyltp安裝及中文分句、中文分詞、導入詞典基本用法
一.詞性標注
詞性標注(Part-Of-Speech tagging, POS tagging)也被稱為語法標注(grammatical tagging)或詞類消疑(word-category disambiguation),是語料庫語言學(corpus linguistics)中將語料庫內單詞的詞性按其含義和上下文內容進行標記的文本數據處理技術。
pyltp詞性標注與分詞模塊相同,將詞性標注任務建模為基于詞的序列標注問題。對于輸入句子的詞序列,模型給句子中的每個詞標注一個標識詞邊界的標記。在LTP中,采用的北大標注集。
完整代碼
# -*- coding: utf-8 -*-
from
pyltp
import
SentenceSplitter
from
pyltp
import
Segmentor
from
pyltp
import
Postagger
from
pyltp
import
NamedEntityRecognizer
ldir
=
'AgriKG\\ltp\\cws.model'
#分詞模型
dicdir
=
'word'
#外部字典
text
=
"貴州財經大學要舉辦大數據比賽嗎?"
#中文分詞
segmentor
=
Segmentor
(
)
#初始化實例
segmentor
.
load_with_lexicon
(
ldir
,
'word'
)
#加載模型
words
=
segmentor
.
segment
(
text
)
#分詞
print
(
text
)
print
(
' '
.
join
(
words
)
)
#分詞拼接
words
=
list
(
words
)
#轉換list
print
(
u
"分詞:"
,
words
)
segmentor
.
release
(
)
#釋放模型
#詞性標注
pdir
=
'AgriKG\\ltp\\pos.model'
pos
=
Postagger
(
)
#初始化實例
pos
.
load
(
pdir
)
#加載模型
postags
=
pos
.
postag
(
words
)
#詞性標注
postags
=
list
(
postags
)
print
(
u
"詞性:"
,
postags
)
pos
.
release
(
)
#釋放模型
data
=
{
"words"
:
words
,
"tags"
:
postags
}
print
(
data
)
輸出結果如下圖所示,“貴州”詞性為“ns”(地理名詞 ),“財經”詞性為“n”(一般名詞),“舉辦”詞性為“v”(動詞),“嗎”詞性為“u”(助詞),“?”詞性為“wp”(標點)。
貴州財經大學要舉辦大數據比賽嗎?
貴州 財經 大學 要 舉辦 大數據 比賽 嗎 ?
分詞
:
[
'貴州'
,
'財經'
,
'大學'
,
'要'
,
'舉辦'
,
'大數據'
,
'比賽'
,
'嗎'
,
'?'
]
詞性
:
[
'ns'
,
'n'
,
'n'
,
'v'
,
'v'
,
'n'
,
'v'
,
'u'
,
'wp'
]
{
'words'
:
[
'貴州'
,
'財經'
,
'大學'
,
'要'
,
'舉辦'
,
'大數據'
,
'比賽'
,
'嗎'
,
'?'
]
,
'tags'
:
[
'ns'
,
'n'
,
'n'
,
'v'
,
'v'
,
'n'
,
'v'
,
'u'
,
'wp'
]
}
具體詞性為:
Tag Description Example
a adjective:形容詞 美麗
b other noun
-
modifier:其他的修飾名詞 大型
,
西式
c conjunction:連詞 和
,
雖然
d adverb:副詞 很
e exclamation:感嘆詞 哎
g morpheme 茨
,
甥
h prefix:前綴 阿
,
偽
i idiom:成語 百花齊放
j abbreviation:縮寫 公檢法
k suffix:后綴 界
,
率
m number:數字 一
,
第一
n general noun:一般名詞 蘋果
nd direction noun:方向名詞 右側
nh person name:人名 杜甫
,
湯姆
ni organization name:公司名 保險公司,中國銀行
nl location noun:地點名詞 城郊
ns geographical name:地理名詞 北京
nt temporal noun:時間名詞 近日
,
明代
nz other proper noun:其他名詞 諾貝爾獎
o onomatopoeia:擬聲詞 嘩啦
p preposition:介詞 在
,
把,與
q quantity:量詞 個
r pronoun:代詞 我們
u auxiliary:助詞 的
,
地
v verb:動詞 跑
,
學習
wp punctuation:標點 ,。!
ws foreign words:國外詞 CPU
x non
-
lexeme:不構成詞 萄
,
翱
z descriptive words 描寫,敘述的詞 瑟瑟,匆匆
二.命名實體識別
命名實體識別(Named Entity Recognition,簡稱NER),又稱作“專名識別”,是指識別文本中具有特定意義的實體,主要包括人名、地名、機構名、專有名詞等。命名實體識別是信息提取、問答系統、句法分析、機器翻譯、面向Semantic Web的元數據標注等應用領域的重要基礎工具,在自然語言處理技術走向實用化的過程中占有重要地位。
在哈工大Pyltp中,NE識別模塊的標注結果采用O-S-B-I-E標注形式,其含義如下(參考):
![[Python知識圖譜] 二.哈工大pyltp詞性標注、命名實體識別、依存句法分析和語義角色標注_第1張圖片](https://img.it610.com/image/info8/ea250330c54141b885f75f3e35c52e55.jpg)
LTP中的NE 模塊識別三種NE,分別為人名(Nh)、機構名(Ni)、地名(Ns)。
完整代碼
# -*- coding: utf-8 -*-
from
pyltp
import
SentenceSplitter
from
pyltp
import
Segmentor
from
pyltp
import
Postagger
from
pyltp
import
NamedEntityRecognizer
ldir
=
'AgriKG\\ltp\\cws.model'
#分詞模型
dicdir
=
'word'
#外部字典
text
=
"貴州財經大學要舉辦大數據比賽嗎?"
#中文分詞
segmentor
=
Segmentor
(
)
#初始化實例
segmentor
.
load_with_lexicon
(
ldir
,
'word'
)
#加載模型
words
=
segmentor
.
segment
(
text
)
#分詞
print
(
text
)
print
(
' '
.
join
(
words
)
)
#分詞拼接
words
=
list
(
words
)
#轉換list
print
(
u
"分詞:"
,
words
)
segmentor
.
release
(
)
#釋放模型
#詞性標注
pdir
=
'AgriKG\\ltp\\pos.model'
pos
=
Postagger
(
)
#初始化實例
pos
.
load
(
pdir
)
#加載模型
postags
=
pos
.
postag
(
words
)
#詞性標注
postags
=
list
(
postags
)
print
(
u
"詞性:"
,
postags
)
pos
.
release
(
)
#釋放模型
data
=
{
"words"
:
words
,
"tags"
:
postags
}
print
(
data
)
print
(
" "
)
#命名實體識別
nermodel
=
'AgriKG\\ltp\\ner.model'
reg
=
NamedEntityRecognizer
(
)
#初始化命名實體實例
reg
.
load
(
nermodel
)
#加載模型
netags
=
reg
.
recognize
(
words
,
postags
)
#對分詞、詞性標注得到的數據進行實體標識
netags
=
list
(
netags
)
print
(
u
"命名實體識別:"
,
netags
)
#實體識別結果
data
=
{
"reg"
:
netags
,
"words"
:
words
,
"tags"
:
postags
}
print
(
data
)
reg
.
release
(
)
#釋放模型
輸出結果如下圖所示,識別出的三個命名實體分別是:“貴州”(B-Ni)表示一個NE開始-機構名,“財經”(I-Ni)表示一個NE中間-機構名,“大學”(E-Ni)表示一個NE結束-機構名。
PS:雖然導入指定詞典,但“貴州財經大學”分詞仍然被分割,后續研究中。
![[Python知識圖譜] 二.哈工大pyltp詞性標注、命名實體識別、依存句法分析和語義角色標注_第2張圖片](https://img.it610.com/image/info8/3f99f48bdfeb45068532b8dbb1f88dad.jpg)
三.依存句法分析
依存句法是由法國語言學家L.Tesniere最先提出。它將句子分析成一棵依存句法樹,描述出各個詞語之間的依存關系。也即指出了詞語之間在句法上的搭配關系,這種搭配關系是和語義相關聯的。如下圖所示:
![[Python知識圖譜] 二.哈工大pyltp詞性標注、命名實體識別、依存句法分析和語義角色標注_第3張圖片](https://img.it610.com/image/info8/325eb0ee35474c929e4d84ee687960a8.jpg)
哈工大Pyltp的依存句法關系如下圖所示。
參考:https://ltp.readthedocs.io/zh_CN/latest/appendix.html
![[Python知識圖譜] 二.哈工大pyltp詞性標注、命名實體識別、依存句法分析和語義角色標注_第4張圖片](https://img.it610.com/image/info8/7357d4cd679c47ca97b076d69278ca17.jpg)
完整代碼
# -*- coding: utf-8 -*-
from
pyltp
import
SentenceSplitter
from
pyltp
import
Segmentor
from
pyltp
import
Postagger
from
pyltp
import
Parser
from
pyltp
import
NamedEntityRecognizer
ldir
=
'AgriKG\\ltp\\cws.model'
#分詞模型
dicdir
=
'word'
#外部字典
text
=
"貴州財經大學要舉辦大數據比賽嗎?"
#中文分詞
segmentor
=
Segmentor
(
)
#初始化實例
segmentor
.
load_with_lexicon
(
ldir
,
'word'
)
#加載模型
words
=
segmentor
.
segment
(
text
)
#分詞
print
(
text
)
print
(
' '
.
join
(
words
)
)
#分詞拼接
words
=
list
(
words
)
#轉換list
print
(
u
"分詞:"
,
words
)
segmentor
.
release
(
)
#釋放模型
#詞性標注
pdir
=
'AgriKG\\ltp\\pos.model'
pos
=
Postagger
(
)
#初始化實例
pos
.
load
(
pdir
)
#加載模型
postags
=
pos
.
postag
(
words
)
#詞性標注
postags
=
list
(
postags
)
print
(
u
"詞性:"
,
postags
)
pos
.
release
(
)
#釋放模型
data
=
{
"words"
:
words
,
"tags"
:
postags
}
print
(
data
)
print
(
" "
)
#命名實體識別
nermodel
=
'AgriKG\\ltp\\ner.model'
reg
=
NamedEntityRecognizer
(
)
#初始化命名實體實例
reg
.
load
(
nermodel
)
#加載模型
netags
=
reg
.
recognize
(
words
,
postags
)
#對分詞、詞性標注得到的數據進行實體標識
netags
=
list
(
netags
)
print
(
u
"命名實體識別:"
,
netags
)
#實體識別結果
data
=
{
"reg"
:
netags
,
"words"
:
words
,
"tags"
:
postags
}
print
(
data
)
reg
.
release
(
)
#釋放模型
print
(
" "
)
#依存句法分析
parmodel
=
'AgriKG\\ltp\\parser.model'
parser
=
Parser
(
)
#初始化命名實體實例
parser
.
load
(
parmodel
)
#加載模型
arcs
=
parser
.
parse
(
words
,
postags
)
#句法分析
#輸出結果
print
(
words
)
print
(
"\t"
.
join
(
"%d:%s"
%
(
arc
.
head
,
arc
.
relation
)
for
arc
in
arcs
)
)
rely_id
=
[
arc
.
head
for
arc
in
arcs
]
# 提取依存父節點id
relation
=
[
arc
.
relation
for
arc
in
arcs
]
# 提取依存關系
heads
=
[
'Root'
if
id
==
0
else
words
[
id
-
1
]
for
id
in
rely_id
]
# 匹配依存父節點詞語
for
i
in
range
(
len
(
words
)
)
:
print
(
relation
[
i
]
+
'('
+
words
[
i
]
+
', '
+
heads
[
i
]
+
')'
)
parser
.
release
(
)
#釋放模型
輸出結果如下所示,其中ATT表示定中關系,如“貴州-大學”、“財經-大學”;SBV表示主謂關系,如“大學-舉辦”;ADV表示狀中結果“要-舉辦”;HED表示核心關系“舉辦-Root”,即“舉辦大數據”。
![[Python知識圖譜] 二.哈工大pyltp詞性標注、命名實體識別、依存句法分析和語義角色標注_第5張圖片](https://img.it610.com/image/info8/fa308dae9d1b4678848443029f811664.jpg)
補充:arc.head表示依存弧的父節點詞的索引,arc.relation表示依存弧的關系。arc.head中的ROOT節點的索引是0,第一個詞開始的索引依次為1、2、3。
四.語義角色標注
該部分代碼僅供博友們參考,作者還在深入研究中。
#語義角色標注
from
pyltp
import
SementicRoleLabeller
srlmodel
=
'AgriKG\\ltp\\pisrl.model'
labeller
=
SementicRoleLabeller
(
)
#初始化實例
labeller
.
load
(
srlmodel
)
#加載模型
words
=
[
'元芳'
,
'你'
,
'怎么'
,
'看'
]
postags
=
[
'nh'
,
'r'
,
'r'
,
'v'
]
arcs
=
parser
.
parse
(
words
,
postags
)
#依存句法分析
#arcs使用依存句法分析的結果
roles
=
labeller
.
label
(
words
,
postags
,
arcs
)
#語義角色標注
# 打印結果
for
role
in
roles
:
print
(
role
.
index
,
""
.
join
(
[
"%s:(%d,%d)"
%
(
arg
.
name
,
arg
.
range
.
start
,
arg
.
range
.
end
)
for
arg
in
role
.
arguments
]
)
)
labeller
.
release
(
)
#釋放模型
輸出結果如下:
3 A0:(1,1)ADV:(2,2)
上面的例子,由于結果輸出一行,所以“元芳你怎么看”有一組語義角色。 其謂詞索引為3,即“看”。這個謂詞有三個語義角色,范圍分別是(0,0)即“元芳”,(1,1)即“你”,(2,2)即“怎么”,類型分別是A0、A0、ADV。
希望這篇基礎性文章對你有所幫助,如果有錯誤或不足之處,還請海涵。
你好!六月。
這些年通過CSDN認識了很多志同道合的朋友,也有很多編程領域的大佬,每天都感覺自己差距還很大,優秀的很多。很高興認識這些素未謀面的新朋友。靡不有初,鮮克有終,一起加油。coding~
(By:Eastmount 2019-06-17 中午12點寫于花溪 https://blog.csdn.net/Eastmount )
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
