python 利用已有Ner模型進(jìn)行數(shù)據(jù)清洗合并代碼
我就廢話不多說(shuō)了,直接上代碼吧!
# -*- 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ù)清洗合并代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 用Python實(shí)現(xiàn)網(wǎng)易云音樂(lè)的數(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é))
- 對(duì)python數(shù)據(jù)清洗容易遇到的函數(shù)-re.sub bytes string詳解
- python數(shù)據(jù)清洗系列之字符串處理詳解
- python 數(shù)據(jù)清洗之?dāng)?shù)據(jù)合并、轉(zhuǎn)換、過(guò)濾、排序
- Python?八個(gè)數(shù)據(jù)清洗實(shí)例代碼詳解
相關(guān)文章
使用Anaconda創(chuàng)建Python指定版本的虛擬環(huán)境的教程詳解
由于工作的需要和學(xué)習(xí)的需要,需要?jiǎng)?chuàng)建不同Python版本的虛擬環(huán)境,所以這篇文章主要為大家詳細(xì)介紹了如何使用Anaconda創(chuàng)建Python指定版本的虛擬環(huán)境,需要的可以參考下2024-03-03
python 模擬創(chuàng)建seafile 目錄操作示例
這篇文章主要介紹了python 模擬創(chuàng)建seafile 目錄操作,結(jié)合實(shí)例形式詳細(xì)分析了Python模擬創(chuàng)建seafile 目錄相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
Python使用pylab庫(kù)實(shí)現(xiàn)繪制直方圖功能示例
這篇文章主要介紹了Python使用pylab庫(kù)實(shí)現(xiàn)繪制直方圖功能,結(jié)合實(shí)例形式分析了Python數(shù)據(jù)讀取、遍歷以及基于pylab庫(kù)繪制直方圖的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
python爬蟲(chóng)爬取微博評(píng)論案例詳解
這篇文章主要介紹了python爬蟲(chóng)爬取微博評(píng)論,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
pandas的相關(guān)系數(shù)與協(xié)方差實(shí)例
今天小編就為大家分享一篇pandas的相關(guān)系數(shù)與協(xié)方差實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
tensorflow 動(dòng)態(tài)獲取 BatchSzie 的大小實(shí)例
這篇文章主要介紹了tensorflow 動(dòng)態(tài)獲取 BatchSzie 的大小實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python 根據(jù)列表批量下載網(wǎng)易云音樂(lè)的免費(fèi)音樂(lè)
這篇文章主要介紹了python 根據(jù)列表下載網(wǎng)易云音樂(lè)的免費(fèi)音樂(lè),幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-12-12

