nlp spacy_使用spacy nlp第4部分命名实体识别器

nlp spacy

Named Entity Recognition is the most important, or I would say, the starting step in Information Retrieval. Information Retrieval is the technique to extract important and useful information from unstructured raw text documents. Named Entity Recognition NER works by locating and identifying the named entities present in unstructured text into the standard categories such as person names, locations, organizations, time expressions, quantities, monetary values, percentage, codes etc. Spacy comes with an extremely fast statistical entity recognition system that assigns labels to contiguous spans of tokens.

命名实体识别是最重要的,或者我想说这是信息检索中的起始步骤。 信息检索是从非结构化原始文本文档中提取重要和有用信息的技术。 命名实体识别NER的工作原理是将非结构化文本中存在的命名实体定位并识别为标准类别,例如人名,位置,组织,时间表达,数量,货币价值,百分比,代码等。Spacy带有非常快速的统计实体为标签的连续范围分配标签的识别系统。

Spacy Installation and Basic Operations | NLP Text Processing Library | Part 1

Spacy安装和基本操作NLP文本处理库| 第1部分

Spacy provides an option to add arbitrary classes to entity recognition systems and update the model to even include the new examples apart from already defined entities within the model.

Spacy提供了一个选项,可以向实体识别系统添加任意类,并更新模型,以包括模型中已定义的实体之外的新示例。

Spacy has the ‘ner’ pipeline component that identifies token spans fitting a predetermined set of named entities. These are available as the ‘ents’ property of a Doc object.

Spacy具有“内部”管道组件,该组件标识适合预定集合的命名实体的令牌范围。 这些可用作Doc对象的'ents'属性。

# Perform standard imports import spacy nlp = spacy.load('en_core_web_sm')# Write a function to display basic entity info: def show_ents(doc): if doc.ents: for ent in doc.ents: print(ent.text+' - ' +str(ent.start_char) +' - '+ str(ent.end_char) +' - '+ent.label_+ ' - '+str(spacy.explain(ent.label_))) else: print('No named entities found.')doc1 = nlp("Apple is looking at buying U.K. startup for $1 billion") show_ents(doc1)
Image for post
doc2 = nlp(u'May I go to Washington, DC next May to see the Washington Monument?') show_ents(doc2)
Image for post

Here we see tokens combine to form the entities next May and the Washington Monument

在这里,我们看到代币结合在一起,形成了next May的实体和the Washington Monument

doc3 = nlp(u'Can I please borrow 500 dollars from you to buy some Microsoft stock?') for ent in doc3.ents: print(ent.text, ent.start, ent.end, ent.start_char, ent.end_char, ent.label_)
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spacy是一个功能强大的Python自然语言处理库,用于进行命名实体识别。它可以自动识别人名、地名、组织名称等实体,并将它们分类到预定义的实体类型中。但是,Spacy默认提供的实体类型可能不足以满足我们的具体需求。因此,我们需要自定义Spacy命名实体识别,以便它可以识别我们所需的实体类型。 下面是如何自定义Spacy命名实体识别,以识别中文实体: 1. 安装Spacy和中文语言模型 首先,需要安装Spacy和中文语言模型。可以使用以下命令进行安装: ``` !pip install spacy !pip install -U spacy[cuda110] !pip install jieba !pip install pkuseg !pip install pandas !pip install numpy !pip install scikit-learn !pip install tqdm !pip install matplotlib !pip install seaborn !pip install pyecharts !pip install openpyxl ``` 2. 创建一个新的实体类型 要创建一个新的实体类型,首先需要创建一个空的语言模型。然后,在语言模型中添加新的实体类型。以下是如何创建一个新的实体类型: ``` import spacy # 创建一个新的实体类型 nlp = spacy.load('zh_core_web_sm') nlp.entity.add_label('MY_ENTITY') ``` 这将创建一个新的实体类型“MY_ENTITY”。 3. 训练命名实体识别 要训练命名实体识别,需要准备一些已标注的语料库。在本例中,我们将使用一个包含1000个中文文本的语料库。每个文本都已经标注了我们要识别的实体类型。 以下是如何准备数据: ``` import pandas as pd # 读取已标注的语料库 df = pd.read_excel('corpus.xlsx') # 将语料库转换为Spacy的训练数据格式 TRAIN_DATA = [] for i, row in df.iterrows(): text = row['text'] entities = [] for ent in row['entities']: start, end, label = ent entities.append((start, end, 'MY_ENTITY')) TRAIN_DATA.append((text, {'entities': entities})) ``` 然后,我们可以使用Spacy的训练API来训练命名实体识别。以下是训练命名实体识别的代码: ``` import random from tqdm import tqdm from spacy.util import minibatch, compounding # 训练命名实体识别 def train_ner(nlp, train_data, n_iter=10): # 获取命名实体识别组件 ner = nlp.get_pipe('ner') # 添加新的实体标签 for _, annotations in train_data: for ent in annotations.get('entities'): ner.add_label(ent[2]) # 禁用其它管道组件 other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner'] with nlp.disable_pipes(*other_pipes): # 只训练命名实体识别组件 optimizer = nlp.begin_training() for i in range(n_iter): # 随机化训练数据 random.shuffle(train_data) # 使用minibatch训练 batches = minibatch(train_data, size=compounding(4.0, 32.0, 1.001)) for batch in tqdm(batches, desc='Training', leave=False): texts, annotations = zip(*batch) nlp.update(texts, annotations, sgd=optimizer) return nlp # 训练命名实体识别 nlp = spacy.load('zh_core_web_sm') nlp.entity.add_label('MY_ENTITY') nlp = train_ner(nlp, TRAIN_DATA) ``` 4. 测试命名实体识别 训练完成后,我们可以使用命名实体识别来识别新的文本中的实体。以下是如何使用命名实体识别来测试文本: ``` # 测试命名实体识别 text = '钢铁侠是美国漫威漫画公司的一名超级英雄。' doc = nlp(text) # 输出识别结果 for ent in doc.ents: print(ent.text, ent.label_) ``` 输出结果应该是: ``` 钢铁侠 MY_ENTITY 美国漫威漫画公司 MY_ENTITY 超级英雄 MY_ENTITY ``` 这表明命名实体识别成功地识别了“钢铁侠”、“美国漫威漫画公司”和“超级英雄”这三个实体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值