博客
关于我
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 中的字符串、列表、元组和字典数据类型的特点和使用场景
查看>>
Python 中的属性访问器是什么?如何使用 @property 装饰器?
查看>>
Python 中的带宽限制
查看>>
Python 中的机器学习简介:多项式回归
查看>>
python读取grib2数据_用Python加载grib2文件
查看>>
Python 中的注意点_s2
查看>>
Python读取Excel的几种工具包(附Demo)
查看>>
Python 中的生成器是什么?
查看>>
Python 中的离线语音转文本
查看>>
Python读写json文件的简单实现
查看>>
Python 中的线程
查看>>
Python 中的继承机制是什么样的?
查看>>
python读写excel之xlrd&xlwt&xlutils组合
查看>>
Python 中的装饰器是什么?
查看>>
Python 中的装饰器是如何工作的,有哪些实际应用场景?
查看>>
python读excel
查看>>
Python 中读取 CSV 文件-ChatGPT4o作答
查看>>
Python 之 filecmp
查看>>
python请求html_使用Python请求获取HTML?
查看>>
Python 之匿名函数和偏函数
查看>>