21自然语言处理基础技术工具篇之Stanfordcorenlp
Posted AI小白入门
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了21自然语言处理基础技术工具篇之Stanfordcorenlp相关的知识,希望对你有一定的参考价值。
Stanfordcorenlp简介
Stanford CoreNLP提供了一套人类语言技术工具。 支持多种自然语言处理基本功能,Stanfordcorenlp是它的一个python接口。
Stanfordcorenlp主要功能包括分词、词性标注、命名实体识别、句法结构分析和依存分析等等。
Stanfordcorenlp工具Demo
安装:pip install stanfordcorenlp
支持多种语言,这里记录一下中英文使用方法
from stanfordcorenlp import StanfordCoreNLP zh_model = StanfordCoreNLP(r'stanford-corenlp-full-2018-02-27', lang='zh') en_model = StanfordCoreNLP(r'stanford-corenlp-full-2018-02-27', lang='en')
zh_sentence = '我爱自然语言处理技术!' en_sentence = 'I love natural language processing technology!'
1.分词(Tokenize)
print ('Tokenize:', zh_model.word_tokenize(zh_sentence)) print ('Tokenize:', en_model.word_tokenize(en_sentence))
Tokenize: ['我爱', '自然', '语言', '处理', '技术', '!'] Tokenize: ['I', 'love', 'natural', 'language', 'processing', 'technology', '!']
2.词性标注(Part of Speech)
print ('Part of Speech:', zh_model.pos_tag(zh_sentence)) print ('Part of Speech:', en_model.pos_tag(en_sentence))
Part of Speech: [('我爱', 'NN'), ('自然', 'AD'), ('语言', 'NN'), ('处理', 'VV'), ('技术', 'NN'), ('!', 'PU')] Part of Speech: [('I', 'PRP'), ('love', 'VBP'), ('natural', 'JJ'), ('language', 'NN'), ('processing', 'NN'), ('technology', 'NN'), ('!', '.')]
3.命名实体识别(Named Entity)
print ('Named Entities:', zh_model.ner(zh_sentence)) print ('Named Entities:', en_model.ner(en_sentence))
Named Entities: [('我爱', 'O'), ('自然', 'O'), ('语言', 'O'), ('处理', 'O'), ('技术', 'O'), ('!', 'O')] Named Entities: [('I', 'O'), ('love', 'O'), ('natural', 'O'), ('language', 'O'), ('processing', 'O'), ('technology', 'O'), ('!', 'O')]
4.句法成分分析(Constituency Parse)
print ('Constituency Parsing:', zh_model.parse(zh_sentence) + "\n") print ('Constituency Parsing:', en_model.parse(en_sentence))
Constituency Parsing: (ROOT (IP (IP (NP (NN 我爱)) (ADVP (AD 自然)) (NP (NN 语言)) (VP (VV 处理) (NP (NN 技术)))) (PU !))) Constituency Parsing: (ROOT (S (NP (PRP I)) (VP (VBP love) (NP (JJ natural) (NN language) (NN processing) (NN technology))) (. !)))
5.依存句法分析(Dependency Parse)
print ('Dependency:', zh_model.dependency_parse(zh_sentence)) print ('Dependency:', en_model.dependency_parse(en_sentence))
Dependency: [('ROOT', 0, 4), ('nsubj', 4, 1), ('advmod', 4, 2), ('nsubj', 4, 3), ('dobj', 4, 5), ('punct', 4, 6)] Dependency: [('ROOT', 0, 2), ('nsubj', 2, 1), ('amod', 6, 3), ('compound', 6, 4), ('compound', 6, 5), ('dobj', 2, 6), ('punct', 2, 7)]
另外,代码我已经上传github:https://github.com/yuquanle/StudyForNLP/blob/master/NLPtools/StanfordcorenlpDemo.ipynb
更多个人笔记请关注:
知乎专栏:https://www.zhihu.com/people/yuquanle/columns
以上是关于21自然语言处理基础技术工具篇之Stanfordcorenlp的主要内容,如果未能解决你的问题,请参考以下文章