安裝依賴
pip install paddlepaddle
pip install paddlehub
使用
使用 lac 進(jìn)行詞性標(biāo)注
import paddlehub as hub
lac = hub.Module(name='lac')
test_text =["被告人李楊洋應(yīng)于2019年8月30日賠償原告平安在線責(zé)任有限公司2000萬(wàn)元。"]
inputs = {"text": test_text}
results = lac.lexical_analysis(data=inputs)
輸出結(jié)果:
[{'word': ['被告人', '李楊洋', '應(yīng)', '于', '2019年8月30日', '賠償', '原告', '平安在線責(zé)任有限公司', '2000萬(wàn)元', '。'], 'tag': ['n', 'PER', 'v', 'p', 'TIME', 'v', 'n', 'ORG', 'm', 'w']}]
得到命名實(shí)體識(shí)別結(jié)果
import paddlehub as hub
import numpy as np
def get_model():
lac = hub.Module(name='lac')
return lac
def get_lac(text):
inputs = {"text": [text]}
lac = get_model()
res = lac.lexical_analysis(data=inputs)
tag = res[0]['tag']
word = res[0]['word']
return tag, word
def get_entity(text, label):
'''
label參數(shù)可以為
'PER' : 人名
'LOC' : 地名
'ORG' : 機(jī)構(gòu)名
'TIME' : 時(shí)間
'''
res = []
tag, word = get_lac(text)
tag = np.array(tag)
indexs = np.where(tag == label)[0]
for index in indexs:
res.append(word[index])
return res
print(get_entity('上海自來(lái)水來(lái)自海上', 'LOC'))
輸出結(jié)果:
['上海']