python 利用已有Ner模型進行數(shù)據(jù)清洗合并代碼
更新時間:2019年12月24日 19:56:12 作者:gmHappy
今天小編就為大家分享一篇python 利用已有Ner模型進行數(shù)據(jù)清洗合并代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,直接上代碼吧!
# -*- coding: utf-8 -*- from kashgari.corpus import DataReader import re from tqdm import tqdm def cut_text(text, lenth): textArr = re.findall('.{' + str(lenth) + '}', text) textArr.append(text[(len(textArr) * lenth):]) return textArr def clean_data(source_file, target_file, ner_model): data_x, data_y = DataReader().read_conll_format_file(source_file) with tqdm(total=len(data_x)) as pbar: for idx, text_array in enumerate(data_x): if len(text_array) <= 100: ners = ner_model.predict([text_array]) ner = ners[0] else: texts = cut_text(''.join(text_array), 100) ners = [] for text in texts: ner = ner_model.predict([[char for char in text]]) ners = ners + ner[0] ner = ners # print('[-----------------------', idx, len(data_x)) # print(data_y[idx]) # print(ner) for jdx, t in enumerate(text_array): if ner[jdx].startswith('B') or ner[jdx].startswith('I') : if data_y[idx][jdx] == 'O': data_y[idx][jdx] = ner[jdx] # print(data_y[idx]) # print('-----------------------]') pbar.update(1) f = open(target_file, 'a', encoding="utf-8") for idx, text_array in enumerate(data_x): if idx != 0: f.writelines(['\n']) for jdx, t in enumerate(text_array): text = t + ' ' + data_y[idx][jdx] if idx == 0 and jdx == 0: text = text else: text = '\n' + text f.writelines([text]) f.close() data_x2, data_y2 = DataReader().read_conll_format_file(source_file) print(data_x == data_x2, len(data_y) == len(data_y2), '數(shù)據(jù)清洗完成')
# -*- coding: utf-8 -*- import kashgari from data_tools import clean_data time_ner = kashgari.utils.load_model('time_ner.h5') clean_data('./data/example.dev', 'example.dev', time_ner)
以上這篇python 利用已有Ner模型進行數(shù)據(jù)清洗合并代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 用Python實現(xiàn)網(wǎng)易云音樂的數(shù)據(jù)進行數(shù)據(jù)清洗和可視化分析
- Python數(shù)據(jù)清洗工具之Numpy的基本操作
- python實現(xiàn)數(shù)據(jù)清洗(缺失值與異常值處理)
- python3常用的數(shù)據(jù)清洗方法(小結(jié))
- 8段用于數(shù)據(jù)清洗Python代碼(小結(jié))
- 對python數(shù)據(jù)清洗容易遇到的函數(shù)-re.sub bytes string詳解
- python數(shù)據(jù)清洗系列之字符串處理詳解
- python 數(shù)據(jù)清洗之數(shù)據(jù)合并、轉(zhuǎn)換、過濾、排序
- Python?八個數(shù)據(jù)清洗實例代碼詳解
相關(guān)文章
神經(jīng)網(wǎng)絡(luò)(BP)算法Python實現(xiàn)及應(yīng)用
這篇文章主要為大家詳細介紹了Python實現(xiàn)神經(jīng)網(wǎng)絡(luò)(BP)算法及簡單應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04python數(shù)據(jù)可視化matplotlib繪制折線圖示例
這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06python通過pil模塊將raw圖片轉(zhuǎn)換成png圖片的方法
這篇文章主要介紹了python通過pil模塊將raw圖片轉(zhuǎn)換成png圖片的方法,實例分析了Python中pil模塊的使用技巧,并Image.fromstring函數(shù)進行了較為詳盡的分析說明,需要的朋友可以參考下2015-03-03Python定時任務(wù)APScheduler原理及實例解析
這篇文章主要介紹了Python定時任務(wù)APScheduler原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2020-05-05