命名实体识别(一)_Python3爬取新闻并将文本存入MySQL

Python3爬取新闻并存入MySQL

Python3爬取新闻并存入MySQL

项目内容:

1.爬取某新闻正文

2.将新闻正文存入Mysql

本文以两篇人民网新闻为例

一、 爬取新闻正文

# -*- coding:utf-8 -*-
import lxml.html
import tushare as ts
from sphinx.util import requests
import pymysql
pymysql.install_as_MySQLdb()
import mysqldbda
from sqlalchemy import create_engine
def reptile(web,xpath1,xpath2):
    selector = lxml.html.fromstring(requests.get(web).content.decode('GBK'))
    site = selector.xpath(xpath1)
    selector_html = []
    for i in range(len(</
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
针对医疗数据进行命名实体识别是自然语言处理中的一个热门问题。下面介绍一种基于 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'}] ``` 表示文本中包含了两个问题实体

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值