import refrom pyltp import namedentityrecognizer
from pyltp import sementicrolelabeller
from pyltp import parser
from pyltp import postagger
def ltp_segmentor(sentence):""" 分割字串 """
segmentor = segmentor()
cws_model_path = '..\\ltp_data\\cws.model'
lexicon_path = '..\\ltp_data\\lexicon.txt'
segmentor.load_with_lexicon(cws_model_path, lexicon_path)
segmentor.load(cws_model_path)
words = segmentor.segment(sentence)
segmentor.release()
return list(words)
def extract_data():
parser = parser() # 初始化模型
postagger = postagger() # 詞性標註
labeller = sementicrolelabeller() # 語義校色標註
recognizer = namedentityrecognizer() # 命名實體識別
model_path = '..\\ltp_data\\pos.model'
lexicon_path = '..\\ltp_data\\poslexicon.txt'
postagger.load_with_lexicon(model_path, lexicon_path) # 載入自定義詞性表
labeller.load('..\\ltp_data\\pisrl_win.model') # 載入模型
recognizer.load('..\\ltp_data\\ner.model')
postagger.load('..\\ltp_data\\pos.model')
parser.load('..\\ltp_data\\parser.model')
content = "#•江都建設集團南京分公司南鋼專案部安全生產規章制度不落實,作業現場安全管理缺失,安全操作規程不認真執行"
text = re.sub("[#•]", "", content) # 對語句進行預處理
words = sc_fun.ltp_segmentor(text) # 分詞
postags = postagger.postag(words)
arcs = parser.parse(words, postags)
netags = recognizer.recognize(words, postags) # 命名實體識別
print(list(netags))
rely_id = [arc.head for arc in arcs]
relation = [arc.relation for arc in arcs] # 關係
heads = ['root' if id == 0 else words[id - 1] for id in rely_id]
roles = labeller.label(words, postags, arcs)
for i in range(len(words)):
print(i, relation[i], (words[i], heads[i]), postags[i])
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() # 釋放模型
parser.release()
postagger.release()
recognizer.release()
if __name__ == '__main__':結果:extract_data()
['b-ni', 'i-ni', 'i-ni', 'i-ni', 'i-ni', 'i-ni', 'e-ni', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o','o', 'o', 'o', 'o', 'o', 'o']分開封裝一樣的:0 att ('江都', '集團') ns
1 att ('建設', '集團') v
[11, 'a1:(0,9)adv:(10,10)']
[24, 'a1:(19,21)adv:(22,23)']
def ltp_segmentor(sentence):""" 分割字串 """
segmentor = segmentor()
segmentor.load('..\\ltp_data\\cws.model')
words = segmentor.segment(sentence)
segmentor.release()
return list(words)
def ltp_parser(words, postags):
parser = parser()
parser.load('..\\ltp_data\\parser.model')
arcs = parser.parse(words, postags)
parser.release()
return list(arcs)
def ltp_postags(words):
postagger = postagger()
model_path = '..\\ltp_data\\pos.model'
lexicon_path = '..\\ltp_data\\poslexicon.txt'
postagger.load_with_lexicon(model_path, lexicon_path) # 載入自定義詞性表
postagger.load(model_path)
postags = postagger.postag(words)
postagger.release()
return list(postags)
php分頁和具體使用
conn mysql connect localhost root password or die 資料庫鏈結錯誤 mysql select db test conn mysql query set names gbk 使用gbk中文編碼 content str replace content ht...
MongoDB之具體使用(二)
mongodb之具體使用 一 改 語法 db.collection.update 查詢條件 要更新的內容 覆蓋修改 db.user.update 部分修改 db.user.update 新增一條 db.user.update 修改多條 db.user.update 修改內嵌文件 db.user.up...
Google hacking介紹及具體使用
一 google hacking googgoogle hacking是利用谷歌搜尋的強大,來在網際網路中搜尋到我們需要的資訊。利用google搜尋我們xia資訊。利用googlesou 搜尋我們想要的想要的資訊,需要配合谷歌搜尋引擎的一些語法 基本搜尋 邏輯與 and 邏輯或 or 邏輯非 萬用字...