博客
关于我
python 利用已有Ner模型进行数据清洗合并
阅读量:796 次
发布时间:2023-03-07

本文共 1425 字,大约阅读时间需要 4 分钟。

import kashgarifrom data_tools import clean_dataimport reimport tqdmdef cut_text(text, length):    textArr = re.findall(f'.{length}', text)    remainder = text[len(textArr) * length:]    if remainder:        textArr.append(remainder)    return textArrdef clean_data(source_file, target_file, ner_model):    data = DataReader().read_conll_format_file(source_file)        for i, (text, labels) in enumerate(data):        if len(text) <= 100:            prediction = ner_model.predict([text])            ner = prediction[0]        else:            chunks = cut_text(''.join(text), 100)            predictions = []            for chunk in chunks:                prediction = ner_model.predict([chunk])                predictions.extend(prediction[0])            ner = predictions                    for j, (token, label) in enumerate(zip(text, labels)):            if ner[j].startswith('B') or ner[j].startswith('I'):                if labels[j] == 'O':                    labels[j] = ner[j]        with open(target_file, 'a', encoding='utf-8') as f:        f.write('\n'.join(            [f"{token} {label}" for token, label in zip(text, labels)]        ))    data, _ = DataReader().read_conll_format_file(source_file)    print(f"数据清洗完成:{data == original_data}, 数据标签长度一致")
import kashgarifrom data_tools import clean_datatime_ner = kashgari.utils.load_model('time_ner.h5')clean_data('./data/example.dev', 'example.dev', time_ner)

转载地址:http://kjofk.baihongyu.com/

你可能感兴趣的文章
python nltk nltk_data 离线安装,chatterbot
查看>>
python numba 转灰度图_使用NumPy、Numba的简单使用(二)
查看>>
python numpy矩阵索引_python – 在2D numpy ndarray或numpy矩阵中获取前N个值的索引
查看>>
python os.system
查看>>
Python os.system执行多条语句,os.system的返回值以及与os.popen的区别
查看>>
Python os和sys模块
查看>>
python os文件/目录
查看>>
Python Package 之 Faker(随机姓名、电话)
查看>>
python运算符优先级
查看>>
Python Panda TIME 系列重新采样
查看>>
python pandas TimeStamps到夏令时的本地时间字符串
查看>>
Python pandas 数据清洗与数据绘图实战
查看>>
Python Pandas 用顶行替换标题
查看>>
Python pandas 通过 dt 访问器有效地将日期时间转换为时间戳
查看>>
Python Pandas-从DataFrame按类别绘制多个条形图
查看>>
Python Pandas:每月或每周拆分 TimeSerie
查看>>
python pandas中融化的对面
查看>>
python pandas从时间序列中提取唯一日期
查看>>
Python PyQt5 将不再显示此消息复选框添加到 QMessageBox
查看>>
Python PyQt5:如何使用 PyQt5 显示错误消息
查看>>