官方文档: https://spacy.io/api

Spacy功能简介

可以用于进行分词,命名实体识别,词性识别等等,但是首先需要下载预训练模型

pip install --user spacy
python -m spacy download en_core_web_sm
pip install neuralcoref
pip install textacy

sentencizer

  • 将文章切分成句子,原理是Spacy通过将文章中某些单词的is_sent_start属性设置为True,来实现对文章的句子的切分,这些特殊的单词在规则上对应于句子的开头。
import spacy
nlp = spacy.load('en_core_web_sm')# 加载预训练模型 txt = "some text read from one paper ..."
doc = nlp(txt) for sent in doc.sents:
print(sent)
print('#'*50)

Tokenization

将句子切分成单词,英文中一般使用空格分隔

import spacy
nlp = spacy.load('en_core_web_sm') txt = "A magnetic monopole is a hypothetical elementary particle."
doc = nlp(txt)
tokens = [token for token in doc]
print(tokens)

Part-of-speech tagging

  • 词性标注,标注句子中每个单词的词性,是名词动词还是形容词。
pos = [token.pos_ for token in doc]
print(pos)
>>> ['DET', 'ADJ', 'NOUN', 'VERB', 'DET', 'ADJ', 'ADJ', 'NOUN', 'PUNCT']
# 对应于中文是 【冠词,形容词,名词,动词,冠词,形容词,形容词,名词,标点】
# 原始句子是 [A, magnetic, monopole, is, a, hypothetical, elementary, particle, .]

Lemmatization

  • 找到单词的原型,即词性还原,将am, is, are, have been 还原成be,复数还原成单数(cats -> cat),过去时态还原成现在时态 (had -> have)。在代码中使用 token.lemma_ 提取
lem = [token.lemma_ for token in doc]
print(lem)
>>> ['a', 'magnetic', 'monopole', 'be', 'a', 'hypothetical', 'elementary', 'particle', '.']

Stop words

  • 识别停用词,a,the等等。
stop_words = [token.is_stop for token in doc]
print(stop_words)
>>> [True, False, False, True, True, False, False, False, False]
# 可以看到,这个磁单极的例子中停用词有 a 和 is。

Dependency Parsing

依存分析,标记单词是主语,谓语,宾语还是连接词。程序中使用 token.dep_ 提取。

dep = [token.dep_ for token in doc]
print(dep)
>>> ['det', 'amod', 'nsubj', 'ROOT', 'det', 'amod', 'amod', 'attr', 'punct']
  • Spacy的依存分析采用了 ClearNLP 的依存分析标签 ClearNLP Dependency Labels。根据这个网站提供的标签字典,翻译成人话:[限定词, 形容词修饰, 名词主语, 根节点, 限定词, 形容词修饰, 形容词修饰, 属性, 标点]

Noun Chunks

  • 提取名词短语,程序中使用doc.noun_chunks获取。
noun_chunks = [nc for nc in doc.noun_chunks]
print(noun_chunks)
>>> [A magnetic monopole, a hypothetical elementary particle]

Named Entity Recognization

  • 命名实体识别,识别人名,地名,组织机构名,日期,时间,金额,事件,产品等等。程序中使用 doc.ents 获取。
txt = ''''European authorities fined Google a record $5.1 billion
on Wednesday for abusing its power in the mobile phone market and
ordered the company to alter its practices'
'''
doc = nlp(txt)
ners = [(ent.text, ent.label_) for ent in doc.ents]
print(ners)
>>> [('European', 'NORP'), ('Google', 'ORG'), ('$5.1 billion', 'MONEY'), ('Wednesday', 'DATE')]
更详细的命名实体简写列表。

https://upload-images.jianshu.io/upload_images/11452592-d7776c24334f0a94.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/720/format/webp

Coreference Resolution

  • 指代消解 ,寻找句子中代词 hesheit 所对应的实体。为了使用这个模块,需要使用神经网络预训练的指代消解系数,如果前面没有安装,可运行命令:pip install neuralcoref
txt = "My sister has a son and she loves him."

# 将预训练的神经网络指代消解加入到spacy的管道中
import neuralcoref
neuralcoref.add_to_pipe(nlp) doc = nlp(txt)
doc._.coref_clusters
>>> [My sister: [My sister, she], a son: [a son, him]]

Display

可视化。把这条功能单独列出来,是因为它太酷了。举几个简单的例子,第一个例子是对依存分析的可视化,

txt = '''In particle physics, a magnetic monopole is a
hypothetical elementary particle.'''
displacy.render(nlp(txt), style='dep', jupyter=True,\
options = {'distance': 90})
 
 
  • 第二个例子是对命名实体识别的可视化
from spacy import displacy
displacy.render(doc, style='ent', jupyter=True)
 
 

知识提取

这一部分使用了 textacy, 需要通过pip命令进行安装,textacy.extract 里面的 semistructured_statements() 函数可以提取主语是 Magnetic Monopole,谓语原型是 be 的所有事实。首先将维基百科上的关于磁单极的这篇介绍的文字拷贝到 magneti_monopole.txt 中。

import textacy.extract

nlp = spacy.load('en_core_web_sm')

with open("magnetic_monopole.txt", "r") as fin:
txt = fin.read() doc = nlp(txt)
statements = textacy.extract.semistructured_statements(doc, "monopole")
for statement in statements:
subject, verb, fact = statement
print(f" - {fact}")
  • 如果搜索Magnetic Monopole, 输出只有第三条,如果搜索 monopole, 结果如下:
- a singular solution of Maxwell's equation (because it requires removing the worldline from spacetime
- a [[topological defect]] in a compact U(1) gauge theory
- a new [[elementary particle]], and would violate [[Gauss's law for magnetism
import spacy
from spacy import displacy
nlp = spacy.load('en')
# nlp = spacy.load("en_core_web_sm")
filename = "test.txt"
document = open(filename,encoding="utf-8").read()
document = nlp(document)
# display.display()
#可视化
displacy.render(document,style="ent",jupyter=True)
displacy.render(document, style='dep', jupyter=True,\
options = {'distance': 90}) print([token.orth_ for token in document if not token.is_punct | token.is_space]) #分词
all_tags = {w.pos: w.pos_ for w in document} #词性标注 可以使用.pos_ 和 .tag_方法访问粗粒度POS标记和细粒度POS标记
print(all_tags)
labels = set([w.label_ for w in document.ents]) #实体识别
print([(i, i.label_, i.label) for i in document.ents])

spacy的更多相关文章

  1. spaCy is a library for advanced natural language processing in Python and Cython:spaCy 工业级自然语言处理工具

    spaCy is a library for advanced natural language processing in Python and Cython. spaCy is built on ...

  2. python 使用spaCy 进行NLP处理

    原文:http://mp.weixin.qq.com/s/sqa-Ca2oXhvcPHJKg9PuVg import spacy nlp = spacy.load("en_core_web_ ...

  3. Spacy 使用

    # 前提是必须安装: python -m spacy download ennlp = spacy.load('en')text = u"you are best. it is lemmat ...

  4. spaCy 并行分词

    spaCy 并行分词 在使用spacy的时候,感觉比nltk慢了许多,一直在寻找并行化的方案,好在找到了,下面给出spaCy并行化的分词方法使用示例: import spacy nlp = spacy ...

  5. 初识Spacy

    之所以想接触Spacy,是看到其自称为工业级的应用,所以想尝试下 windows下安装Spacy:     直接安装pip install spacy是会报错的     解决方法:     到 htt ...

  6. Sense2vec with spaCy and Gensim

    如果你在2015年做过文本分析项目,那么你大概率用的是word2vec模型.Sense2vec是基于word2vec的一个新模型,你可以利用它来获取更详细的.与上下文相关的词向量.本文主要介绍该模型的 ...

  7. NLTK vs SKLearn vs Gensim vs TextBlob vs spaCy

    Generally, NLTK is used primarily for general NLP tasks (tokenization, POS tagging, parsing, etc.) S ...

  8. spaCy 第二篇:语言模型

    spaCy处理文本的过程是模块化的,当调用nlp处理文本时,spaCy首先将文本标记化以生成Doc对象,然后,依次在几个不同的组件中处理Doc,这也称为处理管道.语言模型默认的处理管道依次是:tagg ...

  9. spaCy 第一篇:核心类型

    spaCy 是一个号称工业级的自然语言处理工具包,最核心的数据结构是Doc和Vocab.Doc对象包含Token的序列和Token的注释(Annotation),Vocab对象是spaCy使用的词汇表 ...

  10. Mac下,spacy配置

    pip3 install -U spacy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com python3 -m spa ...

随机推荐

  1. SAP生产订单没有目标成本的原因解释

    首先,OKV6察看一下目标成本的配置,默认是当期成本估算,见下图: 其次,没有目标成本的原因还可能是下列原因导致: 1.该物料没有成本估算和发布2.工艺路线维护日期晚于这个物料估算日期3.没有做CO1 ...

  2. react 富文本编辑器

    5大富文本编辑器今天,富文本编辑器被用于许多应用中,包括简单的博客和复杂的内容管理系统.然而,选择一个并不容易,因为有很多具有不同功能的编辑器. 因此,在这篇文章中,我将评估5个React的富文本编辑 ...

  3. Python自动化测试更新selenium的两种方式

    第一种手动实现: 来源 https://www.codeleading.com/article/73395290617/ import os import re import winreg impor ...

  4. Mysql数据库基础第二章:(五)分组查询

    Mysql数据库基础系列 软件下载地址 提取码:7v7u 数据下载地址 提取码:e6p9 mysql数据库基础第一章:(一)数据库基本概念 mysql数据库基础第一章:(二)mysql环境搭建 mys ...

  5. ABAP 报表的两种下钻功能

    在报表开发中往往会由需求要求跳转,SAP中提供了一些下钻的方式. 这里主要介绍两种 submit 和 call transaction submit 引用的是报表名称,以自开发报表居多 call tr ...

  6. Mac如何用鼠标快速锁屏

    锁屏谁不会啊?本来写这篇文章,感觉自己太多余,但用鼠标直接锁屏就有点小意思,Mac对于很多人来说非常模式,通常是商务.设计这类人事在使用,对于新手而言,它的功能过于隐藏,那么Mac要如何达到快速锁屏呢 ...

  7. postman导出Collection文件

    postman接口调用工具可以将曾经使用过的请求配置导出为文件保存,方法如下: 1.编写一个接口测试用例 2.按组分类 3.导出 参考来源: https://blog.csdn.net/IBLiplu ...

  8. 转载--文章(感谢陈晨博主分享) 关于 Json.net

    本文出自地址: http://www.cnblogs.com/freshman0216/p/4161800.html#undefined Newtonsoft.Json,一款.NET中开源的Json序 ...

  9. 使用python+poco+夜神模拟器进行自动化测试。

    https://blog.csdn.net/saint_228/article/details/84889017 网易最近出的一款自动化UI测试工具:Airtest 挺火的,还受到谷歌的推荐.我试着用 ...

  10. mangodb查询语句

    1.查询所有记录 db.userInfo.find(); 相当于: select* from userInfo; 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据.注意:键 ...