百度 LAC 的 python 使用

安裝依賴

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é)果:

['上海']
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容