Stanford CoreNLP依存關係分析、詞性標註及句子主語分析(使用Python) 超詳細截圖手把手教學,新手友好

前言

Stanford CoreNLP的源代碼是使用Java寫的,提供了Server方式進行交互。stanfordcorenlp是一個對Stanford CoreNLP進行了封裝的Python工具包,GitHub地址,使用非常方便。本文以stanfordcorenlp接口爲例(本文所用版本爲Stanford CoreNLP 3.9.1),講解Python調用StanfordCoreNLP的使用方法。

安裝依賴

1:下載安裝JDK 1.8及以上版本。 
2:下載Stanford CoreNLP文件,解壓。 
3:處理中文還需要下載中文的模型jar文件,然後放到stanford-corenlp-full-2018-02-27根目錄下即可(注意一定要下載這個文件,否則它默認是按英文來處理的)。

常用接口

StanfordCoreNLP官網給出了python調用StanfordCoreNLP的接口。

Python packages using the Stanford CoreNLP server

These packages use the Stanford CoreNLP server that we’ve developed over the last couple of years. You should probably use one of them.

  • stanfordcorenlp by Lynten Guo. A Python wrapper to Stanford CoreNLP server, version 3.8.0. Also: PyPI page.
  • pycorenlp, A Python wrapper for Stanford CoreNLP by Smitha Milli that uses the new CoreNLP v3.6+ server. Available on PyPI.
  • corenlp-pywrap by Sherin Thomas also uses the new CoreNLP v3.6+ server. Python 3.x (only). Also: PyPI page.
  • Stanford CoreNLP Python Interface: A reference implementation of a Python interface to the Stanford CoreNLP server. By Arun Chaganty. PyPI page.
  • pynlp ,A (Pythonic) Python wrapper for Stanford CoreNLP by Sina. PyPI page.
  • NLTK since version 3.2.3 has a new interface to Stanford CoreNLP using the StanfordCoreNLPServer. Among other places, see instructions on using the dependency parser and the code for this module, and if you poke around the documentation, you can find equivalent interfaces to other CoreNLP components; for example here is Stanford CoreNLP NER. Much of the work for this was done by Dmitrijs Milajevs.

1.下載NLP相關包:

網址: https://stanfordnlp.github.io/CoreNLP/index.html 
需要下載的包看下圖:

2.準備jar包

將下載下來的stanford-corenlp-full-2016-10-31解壓並將下載中文的jar文件
stanford-chinese-corenlp-2016-10-31-models.jar放到該目錄下。

3.在Python中安裝stanfordcorenlp

使用pip安裝stanfordcorenlp:

簡單使用命令:pip install stanfordcorenlp 
選擇USTC鏡像安裝(安裝速度很快,畢竟國內鏡像):pip install stanfordcorenlp -i http://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn

如果使用Pycharm也可以再設置中安裝stanfordcorenlp

 

4.在Python環境下調用stanfordcorenlp:

Simple usage

from stanfordcorenlp import StanfordCoreNLP

nlp = StanfordCoreNLP(r'G:\JavaLibraries\stanford-corenlp-full-2018-02-27')
#這裏改成你stanford-corenlp所在的目錄
sentence = 'Guangdong University of Foreign Studies is located in Guangzhou.'
print 'Tokenize:', nlp.word_tokenize(sentence)
print 'Part of Speech:', nlp.pos_tag(sentence)
print 'Named Entities:', nlp.ner(sentence)
print 'Constituency Parsing:', nlp.parse(sentence)
print 'Dependency Parsing:', nlp.dependency_parse(sentence)

nlp.close() # Do not forget to close! The backend server will consume a lot memery.

 

Output format:

# Tokenize
[u'Guangdong', u'University', u'of', u'Foreign', u'Studies', u'is', u'located', u'in', u'Guangzhou', u'.']

#Part of Speech

[(u'Guangdong', u'NNP'), (u'University', u'NNP'), (u'of', u'IN'), (u'Foreign', u'NNP'), (u'Studies', u'NNPS'), (u'is', u'VBZ'), (u'located', u'JJ'), (u'in', u'IN'), (u'Guangzhou', u'NNP'), (u'.', u'.')]

# Named Entities
 [(u'Guangdong', u'ORGANIZATION'), (u'University', u'ORGANIZATION'), (u'of', u'ORGANIZATION'), (u'Foreign', u'ORGANIZATION'), (u'Studies', u'ORGANIZATION'), (u'is', u'O'), (u'located', u'O'), (u'in', u'O'), (u'Guangzhou', u'LOCATION'), (u'.', u'O')]

# Constituency Parsing
 (ROOT
  (S
    (NP
      (NP (NNP Guangdong) (NNP University))
      (PP (IN of)
        (NP (NNP Foreign) (NNPS Studies))))
    (VP (VBZ is)
      (ADJP (JJ located)
        (PP (IN in)
          (NP (NNP Guangzhou)))))
    (. .)))

#Dependency Parsing
[(u'ROOT', 0, 7), (u'compound', 2, 1), (u'nsubjpass', 7, 2), (u'case', 5, 3), (u'compound', 5, 4), (u'nmod', 2, 5), (u'auxpass', 7, 6), (u'case', 9, 8), (u'nmod', 7, 9), (u'punct', 7, 10)]

如果要分析中文句子可以使用下面的代碼:

# _*_coding:utf-8_*_

# Other human languages support, e.g. Chinese

sentence = '清華大學位於北京。'

with StanfordCoreNLP(r'G:\JavaLibraries\stanford-corenlp-full-2018-02-27', lang='zh') as nlp:
    print(nlp.word_tokenize(sentence))
    print(nlp.pos_tag(sentence))
    print(nlp.ner(sentence))
    print(nlp.parse(sentence))
    print(nlp.dependency_parse(sentence))

5.啓動CoreNLP服務器命令:

使用過命令

java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000

在windows中,再cmd中進入到Stanford CoreNLP目錄中執行該命令

現在我們可以使用如下的代碼來調用這個server:

# coding=utf-8

from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost', port=9000)
#這裏改成了我們server的地址
sentence = 'Functions shall be declared at file scope.'
print(nlp.word_tokenize(sentence))
print(nlp.pos_tag(sentence))
print(nlp.ner(sentence))
print(nlp.parse(sentence))
print(nlp.dependency_parse(sentence))
nlp.close()

或者直接在瀏覽器中訪問:

輸入句子就能得到直觀的結果:

6.分詞結果及詞性標註:

NNS、VB的意思請見文末的附錄

7.依存關係:

上面標註的nsubjpass表示被動的名詞主語。可以得到主語是Functions。

附錄

ROOT:要處理文本的語句
IP:簡單從句
NP:名詞短語
VP:動詞短語
PU:斷句符,通常是句號、問號、感嘆號等標點符號
LCP:方位詞短語
PP:介詞短語
CP:由‘的’構成的表示修飾性關係的短語
DNP:由‘的’構成的表示所屬關係的短語
ADVP:副詞短語
ADJP:形容詞短語
DP:限定詞短語
QP:量詞短語
NN:常用名詞
NR:固有名詞
NT:時間名詞
PN:代詞
VV:動詞
VC:是
CC:表示連詞
VE:有
VA:表語形容詞
AS:內容標記(如:了)
VRD:動補複合詞
CD: 表示基數詞
DT: determiner 表示限定詞
EX: existential there 存在句
FW: foreign word 外來詞
IN: preposition or conjunction, subordinating 介詞或從屬連詞
JJ: adjective or numeral, ordinal 形容詞或序數詞
JJR: adjective, comparative 形容詞比較級
JJS: adjective, superlative 形容詞最高級
LS: list item marker 列表標識
MD: modal auxiliary 情態助動詞
PDT: pre-determiner 前位限定詞
POS: genitive marker 所有格標記
PRP: pronoun, personal 人稱代詞
RB: adverb 副詞
RBR: adverb, comparative 副詞比較級
RBS: adverb, superlative 副詞最高級
RP: particle 小品詞 
SYM: symbol 符號
TO:”to” as preposition or infinitive marker 作爲介詞或不定式標記 
WDT: WH-determiner WH限定詞
WP: WH-pronoun WH代詞
WP$: WH-pronoun, possessive WH所有格代詞
WRB:Wh-adverb WH副詞
 
關係表示
abbrev: abbreviation modifier,縮寫
acomp: adjectival complement,形容詞的補充;
advcl : adverbial clause modifier,狀語從句修飾詞
advmod: adverbial modifier狀語
agent: agent,代理,一般有by的時候會出現這個
amod: adjectival modifier形容詞
appos: appositional modifier,同位詞
attr: attributive,屬性
aux: auxiliary,非主要動詞和助詞,如BE,HAVE SHOULD/COULD等到
auxpass: passive auxiliary 被動詞
cc: coordination,並列關係,一般取第一個詞
ccomp: clausal complement從句補充
complm: complementizer,引導從句的詞好重聚中的主要動詞
conj : conjunct,連接兩個並列的詞。
cop: copula。系動詞(如be,seem,appear等),(命題主詞與謂詞間的)連繫
csubj : clausal subject,從主關係
csubjpass: clausal passive subject 主從被動關係
dep: dependent依賴關係
det: determiner決定詞,如冠詞等
dobj : direct object直接賓語
expl: expletive,主要是抓取there
infmod: infinitival modifier,動詞不定式
iobj : indirect object,非直接賓語,也就是所以的間接賓語;
mark: marker,主要出現在有“that” or “whether”“because”, “when”,
mwe: multi-word expression,多個詞的表示
neg: negation modifier否定詞
nn: noun compound modifier名詞組合形式
npadvmod: noun phrase as adverbial modifier名詞作狀語
nsubj : nominal subject,名詞主語
nsubjpass: passive nominal subject,被動的名詞主語
num: numeric modifier,數值修飾
number: element of compound number,組合數字
parataxis: parataxis: parataxis,並列關係
partmod: participial modifier動詞形式的修飾
pcomp: prepositional complement,介詞補充
pobj : object of a preposition,介詞的賓語
poss: possession modifier,所有形式,所有格,所屬
possessive: possessive modifier,這個表示所有者和那個’S的關係
preconj : preconjunct,常常是出現在 “either”, “both”, “neither”的情況下
predet: predeterminer,前綴決定,常常是表示所有
prep: prepositional modifier
prepc: prepositional clausal modifier
prt: phrasal verb particle,動詞短語
punct: punctuation,這個很少見,但是保留下來了,結果當中不會出現這個
purpcl : purpose clause modifier,目的從句
quantmod: quantifier phrase modifier,數量短語
rcmod: relative clause modifier相關關係
ref : referent,指示物,指代
rel : relative
root: root,最重要的詞,從它開始,根節點
tmod: temporal modifier
xcomp: open clausal complement
xsubj : controlling subject 掌控者
中心語爲謂詞
  subj — 主語
 nsubj — 名詞性主語(nominal subject) (同步,建設)
   top — 主題(topic) (是,建築)
npsubj — 被動型主語(nominal passive subject),專指由“被”引導的被動句中的主語,一般是謂詞語義上的受事 (稱作,鎳)
 csubj — 從句主語(clausal subject),中文不存在
 xsubj — x主語,一般是一個主語下面含多個從句 (完善,有些)
中心語爲謂詞或介詞   
   obj — 賓語
  dobj — 直接賓語 (頒佈,文件)
  iobj — 間接賓語(indirect object),基本不存在
 range — 間接賓語爲數量詞,又稱爲與格 (成交,元)
  pobj — 介詞賓語 (根據,要求)
  lobj — 時間介詞 (來,近年)
中心語爲謂詞
  comp — 補語
 ccomp — 從句補語,一般由兩個動詞構成,中心語引導後一個動詞所在的從句(IP) (出現,納入)
 xcomp — x從句補語(xclausal complement),不存在   
 acomp — 形容詞補語(adjectival complement)
 tcomp — 時間補語(temporal complement) (遇到,以前)
lccomp — 位置補語(localizer complement) (佔,以上)
       — 結果補語(resultative complement)
中心語爲名詞
   mod — 修飾語(modifier)
  pass — 被動修飾(passive)
  tmod — 時間修飾(temporal modifier)
 rcmod — 關係從句修飾(relative clause modifier) (問題,遇到)
 numod — 數量修飾(numeric modifier) (規定,若干)
ornmod — 序數修飾(numeric modifier)
   clf — 類別修飾(classifier modifier) (文件,件)
  nmod — 複合名詞修飾(noun compound modifier) (浦東,上海)
  amod — 形容詞修飾(adjetive modifier) (情況,新)
advmod — 副詞修飾(adverbial modifier) (做到,基本)
  vmod — 動詞修飾(verb modifier,participle modifier)
prnmod — 插入詞修飾(parenthetical modifier)
   neg — 不定修飾(negative modifier) (遇到,不)
   det — 限定詞修飾(determiner modifier) (活動,這些)
 possm — 所屬標記(possessive marker),NP
  poss — 所屬修飾(possessive modifier),NP
  dvpm — DVP標記(dvp marker),DVP (簡單,的)
dvpmod — DVP修飾(dvp modifier),DVP (採取,簡單)
  assm — 關聯標記(associative marker),DNP (開發,的)
assmod — 關聯修飾(associative modifier),NP|QP (教訓,特區)
  prep — 介詞修飾(prepositional modifier) NP|VP|IP(採取,對)
 clmod — 從句修飾(clause modifier) (因爲,開始)
 plmod — 介詞性地點修飾(prepositional localizer modifier) (在,上)
   asp — 時態標詞(aspect marker) (做到,了)
partmod– 分詞修飾(participial modifier) 不存在
   etc — 等關係(etc) (辦法,等)
中心語爲實詞
  conj — 聯合(conjunct)
   cop — 系動(copula) 雙指助動詞????
    cc — 連接(coordination),指中心詞與連詞 (開發,與)
其它
  attr — 屬性關係 (是,工程)
cordmod– 並列聯合動詞(coordinated verb compound) (頒佈,實行)
  mmod — 情態動詞(modal verb) (得到,能)
    ba — 把字關係
tclaus — 時間從句 (以後,積累)
       — semantic dependent
   cpm — 補語化成分(complementizer),一般指“的”引導的CP (振興,的)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章