哈工大LTP使用

2021-10-05 19:41:44 字數 2372 閱讀 8980

# coding=utf-8

from pyltp import sentencesplitter

from pyltp import segmentor

from pyltp import postagger

from pyltp import namedentityrecognizer

from pyltp import parser

from nltk import dependencygraph

path =

'c:\\users\\28612\\desktop\\sentimentrecommendation\\tool\\ltp\\ltp_data_v3.4.0\\'

defdraw

(words, postags, arcs)

: par_result =

''for i in

range

(len

(words)):

if arcs[i]

.head ==0:

arcs[i]

.relation =

"root"

par_result +=

"\t"

+ words[i]

+"("

+ arcs[i]

.relation +

")"+

"\t"

+ postags[i]

+"\t"

+str

( arcs[i]

.head)

+"\t"

+ \ arcs[i]

.relation +

"\n"

conlltree = dependencygraph(par_result)

# 轉換為依存句法圖

tree = conlltree.tree(

)# 構建樹結構

tree.draw(

)# 顯示輸出的樹

# 分句

sents = sentencesplitter.split(

'元芳你怎麼看?我就趴視窗上看唄!'

)paragraph1 =

'速度非常快。'

# 分詞

sengentor = segmentor(

)sengentor.load(path +

'cws.model'

)words = sengentor.segment(paragraph1)

sengentor.release(

)# 詞性標註

postagger = postagger(

)postagger.load(path +

'pos.model'

)postags = postagger.postag(words)

postagger.release(

)# 命名實體識別

recognizer = namedentityrecognizer(

)recognizer.load(path +

'ner.model'

)netags = recognizer.recognize(words, postags)

recognizer.release(

)# 句法分析

parser = parser(

)parser.load(path +

'parser.model'

)arcs = parser.parse(words, postags)

parser.release(

)print

(list

(words)

)print

(list

(postags)

)print

("\t"

.join(

"%d:%s"

%(arc.head, arc.relation)

for arc in arcs)

)draw(words, postags, arcs)

words =

list

(words)

postags =

list

(postags)

words.insert(0,

'root'

)postags.insert(0,

'a')

arcs =

[(arc.head, arc.relation)

for arc in arcs]

print

(words)

print

(postags)

print

(arcs)

哈工大 2009 孟婆湯

description 男從戎,女守家。一夜,狼煙四起,男戰死沙場。從此一道黃泉,兩地離別。最後,女終於在等待中老去逝去。逝去的最後是換盡一生等到的相逢和團圓。某日兩人至奈何橋前,服下孟婆湯。每滴孟婆湯都有強度不一的藥效,設一碗孟婆湯共n滴 0 n 100000 其中第i滴 0 i n 用b i 表...

哈工大對抗甲流

隨著哈爾濱部分高校發現甲流確診病例,隨著黑龍江大學封校,氣氛為之一緊,可是沒有執行一天的查證制度隨著時間的流逝而成了擺設。大家說 流感很遠,十一很近。但是十一也很遠了,學校發布公告,十一長假期間,除了十月一日外,其餘時間正常上課,不允許離開哈爾濱市。發現私自離開的,需要休學一年。本部的決定影響了哈工...

哈工大mooc測試1

在分組交換網路中,主要取決於網路擁塞程度的時間延遲是 伺服器端套接字繫結的ip位址是 通告smtp伺服器端開始傳送郵件內容的smtp命令是 下列tcp ip應用層協議中,可以使用傳輸層無連線服務的是 假設採用p2p方式為1000個客戶分發檔案f,檔案f初始位於某伺服器上 伺服器介入internet鏈...