STANFORDPARSER(NLP工具——stanford Parser使用手册[通俗易懂])

发布时间:2025-12-10 19:18:41 浏览次数:3

NLP工具——stanford Parser使用手册[通俗易懂]-

NLP工具——stanford Parser使用手册[通俗易懂]Preface:工作两年多了,陆续接触过蛮多工具,时常没有时间整理整理,最近接触得多了,整理整理自己接触到的NLP这块工具环境:macOS anaconda2目录一、下载安装资源二、使用运行配置及栗子分词及POS命名实体识别句法分析依存句法一、下载 安装 pipinstallstanfordcorenlp 资源 下载模…

Preface:工作两年多了,陆续接触过蛮多工具,时常没有时间整理整理,最近接触得多了,整理整理自己接触到的NLP这块工具

环境:

macOSanaconda2

目录

一、下载

安装

资源

二、使用

运行配置及栗子

分词及POS

命名实体识别

句法分析

依存句法


一、下载

安装

pip installstanfordcorenlp

资源

下载模型及文件:wget http://nlp.stanford.edu/software/stanford-corenlp-full-2018-10-05.zip下载中文jar包:https://stanfordnlp.github.io/CoreNLP/download.html解压stanford-corenlp-full-2018-10-05.zip,并将stanford-chinese-corenlp-2018-10-05-models.jar,放入文件夹中

二、使用

运行配置及栗子

demo
from stanfordcorenlp import StanfordCoreNLPpath = '/Users/shifengmac/Downloads/myselfdoc/selflearning/stanford-corenlp-full-2018-10-05'nlp = StanfordCoreNLP(path, lang='zh')s = '''今天上海的天气很好'''token = nlp.word_tokenize(s)postag = nlp.pos_tag(s)ner = nlp.ner(s)parse = nlp.parse(s)dependencyParse = nlp.dependency_parse(s)print ' '.join(token)print '|'.join([','.join(i) for i in postag])print '|'.join([','.join(i) for i in ner])print parsefor i, begin, end in dependencyParse:    print i, '-'.join([str(begin), token[begin-1]]), '-'.join([str(end),token[end-1]])# python2.7条件下import sysreload(sys)sys.setdefaultencoding('utf-8')us = unicode(s)print ' '.join(nlp.word_tokenize(us))nlp.close()'''#结果:今天 上海 的 天气 很好今天,NT|上海,NR|的,DEG|天气,NN|很好,VV今天,DATE|上海,STATE_OR_PROVINCE|的,O|天气,O|很好,O(ROOT  (IP    (NP (NT 今天))    (NP      (DNP        (NP (NR 上海))        (DEG 的))      (NP (NN 天气)))    (VP (VV 很好))))ROOT 0-很好 5-很好nmod:tmod 5-很好 1-今天nmod:assmod 4-天气 2-上海case 2-上海 3-的nsubj 5-很好 4-天气今天 上'''
说明stanford-corenlp-full-2018-10-05文件夹下需要stanford-chinese-corenlp-2018-10-05-models.jar,代码才可以跑中文文本查看StanfordCoreNLP函数,其默认内存4g,可自行改动memory参数Init signature: StanfordCoreNLP(self, path_or_host, port=None, memory=’4g’, lang=’en’, timeout=1500, quiet=True, logging_level=30)源代码路径:/anaconda2/lib/python2.7/site-packages/stanfordcorenlp/corenlp.py注意点及坑:执行有权限限制时:sudo ipython进入ipython,或者sudo python demo.py执行文件在python2.7下,输入文本需要为str编码格式,不可unicode,识别有问题

分词及POS

调用:nlp.word_tokenize(s)、nlp.pos_tag(s)缺点:不能加载自己的分词词典,毕竟主要针对英文,未考虑中文的分词工具。词性解释:
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 massNNS  : noun, common, pluralNNP  : noun, proper, singularNNPS : noun, proper, pluralPDT  : 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 formVBD  : verb, past tenseVBG  : verb, present participle or gerundVBN  : verb, past participleVBP  : verb, present tense, not 3rd person singularVBZ  : verb, present tense,3rd person singularWDT  : WH-determiner WH限定词WP   : WH-pronoun WH代词WP$  : WH-pronoun, possessive WH所有格代词WRB  : Wh-adverb WH副词

命名实体识别

调用:ner = nlp.ner(s)优点:能够识别多种ner,包括:FACILITY、ORGANIZATION、NUMBER,分别为设施名、组织名、数字识别并处理实体的demo:
from stanfordcorenlp import StanfordCoreNLPpath = '/Users/shifengmac/Downloads/myselfdoc/selflearning/stanford-corenlp-full-2018-10-05'nlp = StanfordCoreNLP(path, lang='zh')s = '''“滴水湖”引进了中国航海博物馆、上海天文馆、滴水湖皇冠假日酒店、豪生国际酒店、大润发购物中心等一批功能性项目'''ner = nlp.ner(s)indexWordNer = {}wordindex = 0wordTag   = ''for lineIdx, line in enumerate(ner):word = line[0]tag  = line[1]if tag != 'O':wordindex += 1 if wordindex==1:begin = lineIdxwordTag = tagelse:if wordindex != 0:key = (begin, lineIdx-1)wordner = ''.join([i[0] for i in ner[begin:lineIdx]])value = (wordner, wordTag)indexWordNer[key] = valuewordindex = 0indexWordNerSorted = sorted(indexWordNer.items(), key=lambda e:e[0][0])for key, value in indexWordNerSorted:print key, '|'.join(value)'''#识别结果:(词的分词起始下标,结束下标),实体词|词的类型(5, 7) 中国航海博物馆|FACILITY(9, 10) 上海天文馆|ORGANIZATION(19, 21) 豪生国际酒店|ORGANIZATION(28, 28) 一|NUMBER'''

句法分析

调用:nlp.parse(s)使用:提取chunk,也即短语、词组之类的;如NP(名词短语)两个词之间的距离,也即一个树的两个叶子节点之间的路径词性解释
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副词

依存句法

调用demo:
from stanfordcorenlp import StanfordCoreNLPpath = '/Users/shifengmac/Downloads/myselfdoc/selflearning/stanford-corenlp-full-2018-10-05'nlp = StanfordCoreNLP(path, lang='zh')s = '''“滴水湖”引进了中国航海博物馆、上海天文馆、滴水湖皇冠假日酒店、豪生国际酒店、大润发购物中心等一批功能性项目'''token = nlp.word_tokenize(s)dependencyParse = nlp.dependency_parse(s)for i, begin, end in dependencyParse:    print i, '-'.join([str(begin), token[begin-1]]), '-'.join([str(end),token[end-1]])'''# 结果:ROOT 0-项目 4-引进punct 2-滴水湖 1-“nsubj 4-引进 2-滴水湖punct 2-滴水湖 3-”aux:asp 4-引进 5-了compound:nn 8-博物馆 6-中国compound:nn 8-博物馆 7-航海conj 10-上海 8-博物馆punct 10-上海 9-、dobj 4-引进 10-上海dep 10-上海 11-天文馆punct 11-天文馆 12-、compound:nn 14-湖 13-滴水compound:nn 15-皇 14-湖compound:nn 18-酒店 15-皇compound:nn 18-酒店 16-冠compound:nn 18-酒店 17-假日root 12-、 18-酒店punct 18-酒店 19-、root 19-、 20-豪生compound:nn 22-酒店 21-国际conj 27-中心 22-酒店punct 27-中心 23-、amod 25-润发 24-大compound:nn 27-中心 25-润发compound:nn 27-中心 26-购物compound:nn 32-项目 27-中心etc 27-中心 28-等nummod 32-项目 29-一mark:clf 29-一 30-批amod 32-项目 31-功能性dobj 20-豪生 32-项目'''
使用:主体词挖掘:挖掘表示该文本的主体词,作为该文本的标签词。如demo中的文本,有很多词/实体:天文馆、博物馆、大润发等,但实际上该文本讲得是“滴水湖”,给该文本打上“博物馆”的标签就扯淡了。主要提取:名词短语、形容词短语等,即nsubj,amod、nmod:assmod、nmod:topic等依存关系说明:
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,主要是抓取thereinfmod    : 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 modifierprepc     : prepositional clausal modifierprt       : phrasal verb particle,动词短语punct     : punctuation,这个很少见,但是保留下来了,结果当中不会出现这个purpcl    : purpose clause modifier,目的从句quantmod  : quantifier phrase modifier,数量短语rcmod     : relative clause modifier相关关系ref       : referent,指示物,指代rel       : relativeroot      : root,最重要的词,从它开始,根节点tmod      : temporal modifierxcomp     : open clausal complementxsubj     : controlling subject 掌控者

三、其他

交互式链接:http://nlp.stanford.edu:8080/parser/index.jsp缺点:较慢

四、参考

资源链接:文件包下载中文jar包下载链接安装教程StanfordNLP官方教程Stanford Parser教程stanford-cornlp的githubStanford Parser的使用——进行词法语法分析python︱六款中文分词模块尝试:jieba、THULAC、SnowNLP、pynlpir、CoreNLP、pyLTP

编辑时间:2019-03-09

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477