python 利用已有Ner模型進(jìn)行數(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模型進(jìn)行數(shù)據(jù)清洗合并代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 用Python實(shí)現(xiàn)網(wǎng)易云音樂的數(shù)據(jù)進(jìn)行數(shù)據(jù)清洗和可視化分析
- Python數(shù)據(jù)清洗工具之Numpy的基本操作
- python實(shí)現(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ù)清洗之?dāng)?shù)據(jù)合并、轉(zhuǎn)換、過濾、排序
- Python?八個(gè)數(shù)據(jù)清洗實(shí)例代碼詳解
相關(guān)文章
神經(jīng)網(wǎng)絡(luò)(BP)算法Python實(shí)現(xiàn)及應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)(BP)算法及簡單應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04python兩個(gè)_多個(gè)字典合并相加的實(shí)例代碼
這篇文章主要介紹了python兩個(gè)_多個(gè)字典合并相加,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12使用Python進(jìn)行情感分析并可視化展示結(jié)果
情感分析是一種通過自然語言處理技術(shù)來識(shí)別、提取和量化文本中的情感傾向的方法,Python在這一領(lǐng)域有著豐富的庫和工具,本文將介紹如何使用Python進(jìn)行情感分析,并通過可視化展示結(jié)果,需要的朋友可以參考下2024-05-05Python面向?qū)ο蟮某绦蛟O(shè)計(jì)詳情
這篇文章主要介紹了Python面向?qū)ο蟮某绦蛟O(shè)計(jì)詳情,面向?qū)ο蟮某绦蛟O(shè)計(jì)在Python中具有非常重要的地位,熟練的使用面向?qū)ο缶幊棠軌驗(yàn)槲覀兊腜ython編程提供很多的便利之處,希望您閱讀完本文后能夠有所收獲2022-01-01python數(shù)據(jù)可視化matplotlib繪制折線圖示例
這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06python通過pil模塊將raw圖片轉(zhuǎn)換成png圖片的方法
這篇文章主要介紹了python通過pil模塊將raw圖片轉(zhuǎn)換成png圖片的方法,實(shí)例分析了Python中pil模塊的使用技巧,并Image.fromstring函數(shù)進(jìn)行了較為詳盡的分析說明,需要的朋友可以參考下2015-03-03Python定時(shí)任務(wù)APScheduler原理及實(shí)例解析
這篇文章主要介紹了Python定時(shí)任務(wù)APScheduler原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05