欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

斯坦福 CoreNLP 工具的使用

最编程 2024-06-03 22:11:22
...

简介

Stanford CoreNLP是使用Java开发的进行自然语言处理的工具。支持多种语言接口,Stanfordcorenlp是它的一个python接口。

Stanfordcorenlp主要功能包括分词、词性标注、命名实体识别、句法结构分析和依存分析等。可处理中文、英文、法语、德语、西班牙语等。

下面以中文、英文为例演示。

环境

  1. macOS
  2. python3

安装

代码语言:javascript
复制
python3 -m pip install stanfordcorenlp --user

注:由于我有python2和python3,-m指定使用pip安装到python3环境下。

模型下载地址: https://nlp.stanford.edu/software/corenlp-backup-download.html

下载下面的zip包,解压,放到一个目录下(比如/Downloads/app下面)。 http://nlp.stanford.edu/software/stanford-corenlp-full-2018-02-27.zip

下面这个中文jar下载完成放到上面解压的目录下。 http://nlp.stanford.edu/software/stanford-chinese-corenlp-2018-02-27-models.jar

导包

代码语言:javascript
复制
>>> from stanfordcorenlp import StanfordCoreNLP

# 中文模型
>>> zh_model = StanfordCoreNLP(r'/Downloads/app/stanford-corenlp-full-2018-02-27', lang='zh')

# 英文模型
>>> en_model = StanfordCoreNLP(r'/Downloads/app/stanford-corenlp-full-2018-02-27', lang='en')

# 中文句子
>>> zh = '中国2020年GDP破百万亿!'

# 英文句子
>>> en = 'China is GDP will exceed 100 trillion yuan by 2020!'

分词

代码语言:javascript
复制
>>> print('Tokenize:', zh_model.word_tokenize(zh))
Tokenize: ['中国', '2020年', 'GDP', '破', '百万亿', '!']

>>> print('Tokenize:', en_model.word_tokenize(en))
Tokenize: ['China', 'is', 'GDP', 'will', 'exceed', '100', 'trillion', 'yuan', 'by', '2020', '!']

词性标注

代码语言:javascript
复制
>>> print('Part of Speech:', zh_model.pos_tag(zh))
Part of Speech: [('中国', 'NR'), ('2020年', 'NT'), ('GDP', 'NN'), ('破', 'VV'), ('百万亿', 'CD'), ('!', 'PU')]

>>> print('Part of Speech:', en_model.pos_tag(en))
Part of Speech: [('China', 'NNP'), ('is', 'VBZ'), ('GDP', 'NNP'), ('will', 'MD'), ('exceed', 'VB'), ('100', 'CD'), ('trillion', 'CD'), ('yuan', 'NN'), ('by', 'IN'), ('2020', 'CD'), ('!', '.')]
词性解释
代码语言:javascript
复制
CC   :	 conjunction, coordinatin 表示连词
CD   :	 numeral, cardinal 表示基数词
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 情态助动词
NN   :	 noun, common, singular or mass
NNS  :	 noun, common, plural
NNP  :	 noun, proper, singular
NNPS :	 noun, proper, plural
PDT  :	 pre-determiner 前位限定词
POS  :	 genitive marker 所有格标记
PRP  :	 pronoun, personal 人称代词
PRP$ :	 pronoun, possessive 所有格代词
RB   :	 adverb 副词
RBR  :	 adverb, comparative 副词比较级
RBS  :	 adverb, superlative 副词*
RP   :	 particle 小品词
SYM  :	 symbol 符号
TO   :	 "to" as preposition or infinitive marker 作为介词或不定式标记
UH   :	 interjection 插入语
VB   :	 verb, base form
VBD  :	 verb, past tense
VBG  :	 verb, present participle or gerund
VBN  :	 verb, past participle
VBP  :	 verb, present tense, not 3rd person singular
VBZ  :	 verb, present tense,3rd person singular
WDT  :	 WH-determiner WH限定词
WP   :	 WH-pronoun WH代词
WP$  :	 WH-pronoun, possessive WH所有格代词
WRB  :	 Wh-adverb WH副词

命名实体识别

代码语言:javascript
复制
>>> print('NER:', zh_model.ner(zh))
NER: [('中国', 'COUNTRY'), ('2020年', 'DATE'), ('GDP', 'O'), ('破', 'O'), ('百万亿', 'NUMBER'), ('!', 'O')]

>>> print('NER:', en_model.ner(en))
NER: [('China', 'COUNTRY'), ('is', 'O'), ('GDP', 'O'), ('will', 'O'), ('exceed', 'O'), ('100', 'MONEY'), ('trillion', 'MONEY'), ('yuan', 'MONEY'), ('by', 'O'), ('2020', 'DATE'), ('!', 'O')]

句法成分分析

代码语言:javascript
复制
>>> print('Constituency Parsing:', zh_model.parse(zh))
Constituency Parsing: (ROOT
  (IP
    (IP
      (NP (NR 中国))
      (NP (NT 2020年))
      (NP (NN GDP))
      (VP (VV 破)
        (QP (CD 百万亿))))
    (PU !)))

>>> print('Constituency Parsing:', en_model.parse(en))
Constituency Parsing: (ROOT
  (S
    (NP (NNP China))
    (VP (VBZ is)
      (NP
        (NP (NNP GDP))
        (SBAR
          (S
            (VP (MD will)
              (VP (VB exceed)
                (NP
                  (QP (CD 100) (CD trillion))
                  (NN yuan))
                (PP (IN by)
                  (NP (CD 2020)))))))))
    (. !)))
词性解释
代码语言:javascript
复制
ROOT :	要处理文本的语句
IP   :	简单从句
NP   :	名词短语
VP   :	动词短语
PU   :	断句符,通常是句号、问号、感叹号等标点符号
LCP  :	方位词短语
PP   :	介词短语
CP   :	由‘的’构成的表示修饰性关系的短语
DNP  :	由‘的’构成的表示所属关系的短语
ADVP :	副词短语
ADJP :	形容词短语
DP   :	限定词短语
QP   :	量词短语
NN   :	常用名词
NT   :	时间名词
PN   :	代词
VV   :	动词
VC   :	是
CC   :	表示连词
VE   :	有
VA   :	表语形容词
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副词

依存句法分析

代码语言:javascript
复制
>>> print('Dependency:', zh_model.dependency_parse(zh))
Dependency: [('ROOT', 0, 4), ('nmod:assmod', 3, 1), ('compound:nn', 3, 2), ('nsubj', 4, 3), ('nmod:range', 4, 5), ('punct', 4, 6)]

>>> print('Dependency:', en_model.dependency_parse(en))
Dependency: [('ROOT', 0, 3), ('nsubj', 3, 1), ('cop', 3, 2), ('aux', 5, 4), ('dep', 3, 5), ('compound', 7, 6), ('nummod', 8, 7), ('dobj', 5, 8), ('case', 10, 9), ('nmod', 5, 10), ('punct', 3, 11)]
依存关系说明
代码语言:javascript
复制
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,组合数字
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 掌控者