1.調用LTP的API
- 進入哈工大語言云進行註冊,獲得key
-
# -*- coding: utf-8 -*- import urllib2 url_get_base = "http://api.ltp-cloud.com/analysis/?" api_key = '*********' # 輸入註冊API_KEY # 待分析的文本 text = "國務院總理李克強調研上海外高橋時提出,支持上海積極探索新機制。" format0 = 'plain' # 結果格式,有xml、json、conll、plain(不可改成大寫) pattern = 'ws' # 指定分析模式,有ws、pos、ner、dp、sdp、srl和all #分詞 result = urllib2.urlopen("%sapi_key=%s&text=%s&format=%s&pattern=%s" % (url_get_base, api_key, text, format0, 'ws')) content = result.read().strip() print content print '*'*60 #詞性標註 result1 = urllib2.urlopen("%sapi_key=%s&text=%s&format=%s&pattern=%s" % (url_get_base, api_key, text, format0, 'pos')) content1 = result1.read().strip() print content1 print '*'*60 #命名實體識別 result2 = urllib2.urlopen("%sapi_key=%s&text=%s&format=%s&pattern=%s" % (url_get_base, api_key, text, format0, 'ner')) content2 = result2.read().strip() print content2 print '*'*60 #依存句法分析 result3 = urllib2.urlopen("%sapi_key=%s&text=%s&format=%s&pattern=%s" % (url_get_base, api_key, text, format0, 'dp')) content3 = result3.read().strip() print content3 print '*'*60 #語義依存分析 result4 = urllib2.urlopen("%sapi_key=%s&text=%s&format=%s&pattern=%s" % (url_get_base, api_key, text, format0, 'sdp')) content4 = result4.read().strip() print content4 print '*'*60 #語義角色標註 result1 = urllib2.urlopen("%sapi_key=%s&text=%s&format=%s&pattern=%s" % (url_get_base, api_key, text, format0, 'srl')) content1 = result1.read().strip() print content1 print '*'*60
顯示結果:
國務院 總理 李克強 調研 上海 外高橋 時 提出 , 支持 上海 積極 探索 新 機制 。
************************************************************
國務院_ni 總理_n 李克強_nh 調研_v 上海_ns 外高橋_ns 時_n 提出_v ,_wp 支持_v 上海_ns 積極_a 探索_v 新_a 機制_n 。_wp
************************************************************
[國務院]Ni 總理 [李克強]Nh 調研 [上海 外高橋]Ns 時 提出 , 支持 [上海]Ns 積極 探索 新 機制 。
************************************************************
國務院_0 總理_1 ATT
總理_1 李克強_2 ATT
李克強_2 調研_3 SBV
調研_3 時_6 ATT
上海_4 外高橋_5 ATT
外高橋_5 調研_3 VOB
時_6 提出_7 ADV
提出_7 -1 HED
,_8 提出_7 WP
支持_9 提出_7 COO
上海_10 探索_12 SBV
積極_11 探索_12 ADV
探索_12 支持_9 VOB
新_13 機制_14 ATT
機制_14 探索_12 VOB
。_15 提出_7 WP
************************************************************
國務院_0 總理_1 Nmod
總理_1 調研_3 Agt
李克強_2 總理_1 Nmod
調研_3 提出_7 dTime
上海_4 外高橋_5 Nmod
外高橋_5 調研_3 Dir
時_6 調研_3 mTime
提出_7 -1 Root
,_8 提出_7 mPunc
支持_9 提出_7 ePurp
上海_10 探索_12 Agt
積極_11 探索_12 Mann
探索_12 支持_9 dCont
新_13 機制_14 Feat
機制_14 探索_12 Prod
。_15 支持_9 mPunc
************************************************************
[國務院 總理 李克強 調研 上海 外高橋 時]TMP [提出]v , [支持 上海 積極 探索 新 機制]A1 。
國務院 總理 李克強 調研 上海 外高橋 時 提出 , [支持]v [上海]A1 積極 探索 新 機制 。
************************************************************