博客
关于我
python 利用已有Ner模型进行数据清洗合并
阅读量:798 次
发布时间: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 | bashplotlib,一个有趣的Python库!
查看>>
python | bentoml,一个超级厉害的 模型部署 Python 库!
查看>>
python调用jar包的模块_python调用jar包
查看>>
python | black,一个神奇的 代码格式化工具 Python 库!
查看>>
python | bleach,一个超强的 Python 库!
查看>>
python | cartopy,一个有趣的 Python 库!
查看>>
python | cloud-init,一个实用的 云计算 Python 库!
查看>>
python | code2flow,一个神奇的 Python 库!
查看>>
python | cudf,一个超实用的 Python 库!
查看>>
python | daphne,一个非常nice的 Python 库!
查看>>
python调用halcon
查看>>
python | doit,一个非常实用的 Python 库!
查看>>
python | easyocr,一个超厉害的 关于OCR的 Python 库!
查看>>
python | fastFM,一个高级的 Python 库!
查看>>
python | feature_engine,一个实用的 Python 库!
查看>>
python | filelock,一个超酷的 Python 库!
查看>>
python | fire,一个强大的 Python 库!
查看>>
python | flanker,一个神奇的 Python 库!
查看>>
python | flower,一个强大的 Python 库!
查看>>
python | funcy,一个超强的 提供函数式编程工具 Python 库!
查看>>