python 利用已有Ner模型进行数据清洗合并代码

yipeiwu_com5年前Python基础

我就废话不多说了,直接上代码吧!

# -*- 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), '数据清洗完成')       
# -*- 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模型进行数据清洗合并代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python单向循环链表原理与实现方法示例

python单向循环链表原理与实现方法示例

本文实例讲述了python单向循环链表原理与实现方法。分享给大家供大家参考,具体如下: 单向循环链表 单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指...

python pyheatmap包绘制热力图

利用python pyheatmap包绘制热力图,供大家参考,具体内容如下 import matplotlib.pyplot as plt from pyheatmap.heatma...

对python以16进制打印字节数组的方法详解

对python以16进制打印字节数组的方法详解

一、问题描述 如果直接用print打印bytes的话,有时候会直接显示ascii对应的字符,看起来很蛋疼。 二、运行效果 上面一行是直接用print打印的结果,很明显,第一个字节0x7...

pytorch方法测试——激活函数(ReLU)详解

测试代码: import torch import torch.nn as nn #inplace为True,将会改变输入的数据 ,否则不会改变原输入,只会产生新的输出 m = n...

日常整理python执行系统命令的常见方法(全)

具体内容如下: 1 os.system 例如 ipython中运行如下命令,返回运行状态status os.system('cat /etc/passwdqc.conf') min=di...