spacy库的使用说明
1.安装
2.用法
2.1 word tokenize(doc: token)
2.2 英文断句(doc.sents: sent)
2.3 词干化(doc: token, token_lemma_, token_lemma)
2.4 词性标注(doc: token,token.pos_,token.pos)
2.5 命名实体识别(doc.ents:ent, ent.label_, ent.label)
2.6 名词短语提取(doc.noun_chunks)
2.7 基于词向量计算两个单词的相似度 (doc[index_i].similarity(doc[index_j]))
1.安装
见另一篇python spacy安装问题末尾总结。
2.用法
spaCy 是一个Python自然语言处理工具包,诞生于2014年年中,号称“Industrial-Strength Natural Language Processing in Python”,是具有工业级强度的Python NLP工具包。spaCy里大量使用了 Cython 来提高相关模块的性能,这个区别于学术性质更浓的Python NLTK,因此具有了业界应用的实际价值。
import spacy
nlp = spacy.load(en_core_web_em)
官方文档见spacy(https://spacy.io/usage/linguistic-features)
主要支持英语和德语。
功能包括word tokenize, 英文断句,词干化,词性标注,命名实体识别,名词短语提取,相似度计算……
2.1 word tokenize(doc: token)
将英文单词和标点符号都分离出来,如果含有中文,则中文以多个文字之间的空格分词。
In [3]: test_doc = nlp(u"it's word tokenize test for spacy")
In [4]: print(test_doc)
it's word tokenize test for spacy
In [5]: for token in test_doc:
print(token)
...:
it
's
word
tokenize
test
for
spacy
test_doc是 spacy.tokens.doc.Doc 对象。
2.2 英文断句(doc.sents: sent)
In [6]: test_doc = nlp(u'Natura