SpaCy 是一个收费的开源库,用于 Python 中的高级自然语言解决包含但不限于词性标注、dependency parsing、NER和类似度计算。它可帮忙构建解决和了解大量文本的应用程序可用于多种方向,例如信息提取、自然语言了解或为深度学习提供文本预处理。
SpaCy 诞生于2014年年中(并且到当初这么多年了,它仍然放弃着继续的更新),号称“Industrial-Strength Natural Language Processing in Python”,spaCy里大量应用了 Cython 来进步相干模块的性能,这个区别于学术性质更浓的Python NLTK,因而具备了业界利用的理论价值。
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-01 2.3225e-02 -1.8158e-02 6.7192e-03# -1.3877e-01 1.7708e-01 1.7709e-01 2.5882e+00 -3.5179e-01 -1.7312e-01# 4.3285e-01 -1.0708e-01 1.5006e-01 -1.9982e-01 -1.9093e-01 1.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 doc2print(doc1.similarity(doc2))# 0.7799485853415737# Similarity of doc1 and doc3print(doc1.similarity(doc3))# 0.6210606690259671
https://www.overfit.cn/post/61a6c8dc080c4249917a44921923b6f2
以上就是spaCy的次要性能,心愿对你有所帮忙