5分钟NLP|5分钟NLP - SpaCy速查表

SpaCy 是一个免费的开源库,用于 Python 中的高级自然语言处理包括但不限于词性标注、dependency parsing、NER和相似度计算。它可帮助构建处理和理解大量文本的应用程序可用于多种方向,例如信息提取、自然语言理解或为深度学习提供文本预处理。
SpaCy 诞生于2014年年中(并且到现在这么多年了,它依然保持着持续的更新),号称“Industrial-Strength Natural Language Processing in Python”,spaCy里大量使用了 Cython 来提高相关模块的性能,这个区别于学术性质更浓的Python NLTK,因此具有了业界应用的实际价值。
spaCy 简介 5分钟NLP|5分钟NLP - SpaCy速查表
文章图片

SpaCy 目前为各种语言提供与训练的模型和处理流程,并可以作为单独的 Python 模块安装。例如下面就是下载与训练的en_core_web_sm 的示例。

python -m spacy download en_core_web_sm

请根据任务和你的文本来选择与训练的模型。小的默认流程(即以 sm 结尾的流程)总是一个好的开始。
标记化 标记化包括将文本分割成单词、标点符号等。这是通过应用特定于每种语言的规则来完成的。
import spacynlp = spacy.load("en_core_web_sm") doc = nlp("The cat is on the table") for token in doc: print(token.text)# The # cat # is # on # the # table

词性标注 POS(词性)标记是指根据词的定义及其上下文对文本中的词进行分类,使其与特定的词性相对应。
import spacynlp = spacy.load("en_core_web_sm") doc = nlp("The cat is on the table") for token in doc: print(f"{token.text} --- POS: {token.pos_}, {token.tag_}")# The --- POS: DET, DT # cat --- POS: NOUN, NN # is --- POS: AUX, VBZ # on --- POS: ADP, IN # the --- POS: DET, DT # table --- POS: NOUN, NN

pos_ 属性包含简单的 UPOS 词性标记,而 tag_ 属性包含详细的 POS 标记。
dependency parsing dependency parsing(依赖解析)包括分配句法依赖标签,描述各个标记之间的关系,如主题或对象。
import spacynlp = spacy.load("en_core_web_sm") doc = nlp("The cat is on the table") for token in doc: print(f"{token.text} --- dependency label: {token.dep_}")# The --- dependency label: det # cat --- dependency label: nsubj # is --- dependency label: ROOT # on --- dependency label: prep # the --- dependency label: det # table --- dependency label: pobj

停用词 停用词是一种语言中最常见的词,在 NLP 任务中经常被忽略,因为它们通常对句子没有什么意义。
import spacynlp = spacy.load("en_core_web_sm") doc = nlp("The cat is on the table") for token in doc: print(f"{token.text} --- is stopword: {token.is_stop}")# The --- is stopword: True # cat --- is stopword: False # is --- is stopword: True # on --- is stopword: True # the --- is stopword: True # table --- is stopword: False

词形还原 词形还原(Lemmatization)指定单词的基本形式。例如,“was”的词根是“be”,“dogs”的词根是“dog”。
import spacynlp = spacy.load("en_core_web_sm") doc = nlp("The cat is on the table") for token in doc: print(f"{token.text} --- lemma: {token.lemma_}")# The --- lemma: the # cat --- lemma: cat # is --- lemma: be # on --- lemma: on # the --- lemma: the # table --- lemma: table

命名实体识别 (NER) 命名实体识别是指在文本中标记命名的“真实世界”对象,例如人、公司或位置。
import spacynlp = spacy.load("en_core_web_sm") doc = nlp("Elon Musk cofounded the electronic-payment firm PayPal and formed SpaceX.")for ent in doc.ents: print(ent.text, ent.start_char, ent.end_char, ent.label_)# Elon Musk 0 9 PERSON # PayPal 48 54 ORG

词嵌入 词嵌入是文本的学习表示(通常是数字向量),其中具有相同含义的词具有相似的表示。
为了使它们紧凑和快速,spaCy 的小型处理管道包(所有以 sm 结尾的包)不附带词向量,只包含上下文敏感的张量。这意味着只能可以使用similarity() 方法来比较句子和单词,并且结果不会那么好,并且单个标记不会分配任何向量。所以为了使用真实的词向量,你需要下载一个更大的管道包。
python -m spacy download en_core_web_md

下面就可以使用 spaCy 获得词嵌入。
import spacynlp = spacy.load("en_core_web_md") tokens = nlp("The cat is on the aofafgag")vectors = [] for token in tokens: print(token.text, token.has_vector, token.is_oov) vectors.append(token.vector)# The True False # cat True False # is True False # on True False # the True False # aofafgag False Trueprint(vectors[0])# [ 2.7204e-01 -6.2030e-02 -1.8840e-012.3225e-02 -1.8158e-026.7192e-03 # -1.3877e-011.7708e-011.7709e-012.5882e+00 -3.5179e-01 -1.7312e-01 #4.3285e-01 -1.0708e-011.5006e-01 -1.9982e-01 -1.9093e-011.1871e+00 #...

句子相似度 spaCy可以计算句子之间的相似性。这是通过对每个句子中单词的词嵌入进行平均,然后使用相似度度量计算相似度来完成的。
import spacynlp = spacy.load("en_core_web_md")# make sure to use larger package! doc1 = nlp("I like salty fries and hamburgers.") doc2 = nlp("Fast food tastes very good.") doc3 = nlp("Where is the cat.")# Similarity of doc1 and doc2 print(doc1.similarity(doc2))# 0.7799485853415737# Similarity of doc1 and doc3 print(doc1.similarity(doc3))# 0.6210606690259671

https://www.overfit.cn/post/61a6c8dc080c4249917a44921923b6f2
【5分钟NLP|5分钟NLP - SpaCy速查表】以上就是spaCy的主要功能,希望对你有所帮助

    推荐阅读