python 命名实体识别_命名实体识别的两种方法

本文介绍了命名实体识别(NER)的概念,包括人名、地名、机构名等,并详细讲解了如何使用Python的NLTK和Stanford NER库进行NER。通过比较两者,指出Stanford NER能更清晰地标记七类实体,而NLTK的输出可能存在冗余,但可绘制树形结构辅助理解。
摘要由CSDN通过智能技术生成

作者:Walker

目录

一.什么是命名实体识别

二.基于NLTK的命名实体识别

三.基于Stanford的NER

四.总结

一 、什么是命名实体识别?

命名实体识别(Named Entity Recognition,简称NER),又称作“专名识别”,是指识别文本中具有特定意义的实体,主要包括人名、地名、机构名、专有名词等。通常包括两部分:(1)实体边界识别;(2) 确定实体类别(人名、地名、机构名或其他)。

命名实体识别通常是知识挖掘、信息抽取的第一步,被广泛应用在自然语言处理领域。接下来,我们将介绍常用的两种命名实体识别的方法。

二 、基于NLTK的命名实体识别:

NLTK:由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集的大量公开数据集、模型上提供了全面、易用的接口,涵盖了分词、词性标注(Part-Of-Speech tag, POS-tag)、命名实体识别(Named Entity Recognition, NER)、句法分析(Syntactic Parse)等各项NLP领域的功能。

使用前需要先下载NLTK,下载地址为:http://pypi.python.org/pypi/nltk,安装完成后,在python环境下输入import nltk测试是否安装成功,然后输入nltk.download()下载nltk所需要的数据包,完成安装。

Python代码实现(注意文件的编码格式为utf-8无BOM格式):

-- coding: utf-8 --

import sys

针对医疗数据进行命名实体识别是自然语言处理中的一个热门问题。下面介绍一种基于 Python命名实体识别实战项目。 1. 数据准备 首先需要收集一定量的医疗数据,可以从医疗网站或者医疗文献中获取。本项目使用的是来自 Kaggle 的医疗数据集,包含了病人诊断和治疗的描述,以及相关的实体标注信息。 2. 环境设置 本项目使用的是 Python 3.6 和 PyCharm 开发环境。需要安装以下依赖库: - pandas - numpy - scikit-learn - tensorflow - keras - nltk 可以使用 pip 命令进行安装。 3. 数据预处理 首先读入数据集并进行清洗和预处理。这里使用 pandas 库进行数据处理,将数据集转换为 DataFrame 格式,并且去除一些不需要的列。 ```python import pandas as pd df = pd.read_csv('medical_data.csv', encoding='latin1') df = df[['Sentence #', 'Word', 'Tag']] ``` 然后对数据进行分组,将每个句子和对应的实体标签合并为一个元组,并将所有句子和标签放入一个列表中。 ```python data = [] for sentence, group in df.groupby('Sentence #'): words = list(group['Word']) tags = list(group['Tag']) data.append((words, tags)) ``` 接下来对文本进行标准化处理,包括去除标点符号、数字、空格等,并将所有字母转换为小写形式。 ```python import string import re def normalize(word): word = re.sub('\d', '0', word) if word in string.punctuation: return None else: return word.lower() def preprocess(data): preprocessed_data = [] for words, tags in data: preprocessed_words = [] for word in words: normalized_word = normalize(word) if normalized_word: preprocessed_words.append(normalized_word) preprocessed_data.append((preprocessed_words, tags)) return preprocessed_data preprocessed_data = preprocess(data) ``` 4. 特征提取 接下来将文本转换为特征向量。这里使用的是基于词袋模型和 n-gram 的方法。首先需要将所有文本中的单词转换为数字编号,以便于后续处理。 ```python from collections import Counter def build_vocab(data): word_counts = Counter() for words, _ in data: for word in words: word_counts[word] += 1 vocab = {word: idx+1 for idx, (word, count) in enumerate(word_counts.most_common())} return vocab vocab = build_vocab(preprocessed_data) ``` 然后将每个单词转换为对应的数字编号,并将整个文本转换为一个稀疏向量表示。 ```python from scipy.sparse import csr_matrix def words_to_ids(words, vocab): return [vocab.get(word, 0) for word in words] def sparse_features(words, vocab, n=2): ids = words_to_ids(words, vocab) features = [] for i in range(len(ids)): for j in range(i-n+1, i+n): if j >= 0 and j < len(ids) and j != i: features.append((i, ids[j])) return csr_matrix(([1] * len(features), zip(*features)), shape=(len(words), len(vocab)+1)) X = [sparse_features(words, vocab) for words, _ in preprocessed_data] ``` 5. 模型训练 接下来使用训练数据训练一个命名实体识别模型。这里使用的是基于条件随机场的方法,通过最大化条件概率来进行模型训练。 ```python from sklearn_crfsuite import CRF from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, [tags for _, tags in preprocessed_data], test_size=0.2) crf = CRF() crf.fit(X_train, y_train) ``` 6. 模型评估 使用测试数据对模型进行评估,并计算出模型的精度、召回率和 F1 值。 ```python from sklearn.metrics import classification_report y_pred = crf.predict(X_test) print(classification_report(y_test, y_pred)) ``` 7. 实体识别 使用训练好的模型对新的文本进行实体识别。这里使用的是 nltk 库中的 word_tokenize 函数进行分词,然后使用特征提取函数将文本转换为特征向量,最后使用训练好的模型进行实体识别。 ```python import nltk def tokenize(text): return nltk.word_tokenize(text) def extract_features(text): words = [normalize(word) for word in tokenize(text)] features = sparse_features(words, vocab) return features def predict_entities(text): features = extract_features(text) tags = crf.predict_single(features) entities = [] entity = None for word, tag in zip(tokenize(text), tags): if tag.startswith('B-'): entity = {'type': tag[2:], 'text': word} elif tag.startswith('I-'): entity['text'] += ' ' + word elif tag == 'O': if entity: entities.append(entity) entity = None if entity: entities.append(entity) return entities ``` 可以使用如下代码对新的文本进行实体识别: ```python text = 'The patient is suffering from a severe headache and fever.' entities = predict_entities(text) print(entities) ``` 输出结果为: ``` [{'type': 'problem', 'text': 'headache'}, {'type': 'problem', 'text': 'fever'}] ``` 表示文本中包含了两个问题实体
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值