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


Posted in Python onDecember 24, 2019

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

# -*- 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 动态获取当前运行的类名和函数名的方法
Apr 15 Python
举例讲解Python中的list列表数据结构用法
Mar 12 Python
Python对多属性的重复数据去重实例
Apr 18 Python
python 实现对文件夹中的图像连续重命名方法
Oct 25 Python
python 通过麦克风录音 生成wav文件的方法
Jan 09 Python
Python实现钉钉发送报警消息的方法
Feb 20 Python
Python使用matplotlib实现交换式图形显示功能示例
Sep 06 Python
Python接口自动化判断元素原理解析
Feb 24 Python
django中的数据库迁移的实现
Mar 16 Python
django 利用Q对象与F对象进行查询的实现
May 15 Python
Python基于pandas绘制散点图矩阵代码实例
Jun 04 Python
用python查找统一局域网下ip对应的mac地址
Jan 13 Python
Python迷宫生成和迷宫破解算法实例
Dec 24 #Python
Python3 A*寻路算法实现方式
Dec 24 #Python
python logging添加filter教程
Dec 24 #Python
python打印异常信息的两种实现方式
Dec 24 #Python
numpy实现神经网络反向传播算法的步骤
Dec 24 #Python
python异常处理和日志处理方式
Dec 24 #Python
Python 音频生成器的实现示例
Dec 24 #Python
You might like
PHP+MySQL 手工注入语句大全 推荐
2009/10/30 PHP
PHP实现微信公众号验证Token的示例代码
2019/12/16 PHP
Javascript 中介者模式实例
2009/12/16 Javascript
33个优秀的 jQuery 图片展示插件分享
2012/03/14 Javascript
12种不宜使用的Javascript语法整理
2013/11/04 Javascript
JQuery操作iframe父页面与子页面的元素与方法(实例讲解)
2013/11/20 Javascript
Javascript验证上传图片大小[前台处理]
2014/07/18 Javascript
Javascript基础教程之switch语句
2015/01/18 Javascript
javascript实现博客园页面右下角返回顶部按钮
2015/02/22 Javascript
JavaScript中实现map功能代码分享
2015/06/11 Javascript
jQuery预加载图片常用方法
2015/06/15 Javascript
javascript常用函数(2)
2015/11/05 Javascript
jQuery ajax时间差导致的变量赋值问题分析
2016/01/22 Javascript
jquery获取form表单input元素值的简单实例
2016/05/30 Javascript
详解vue-cli + webpack 多页面实例配置优化方法
2017/07/13 Javascript
Vue组件的使用教程详解
2018/01/05 Javascript
js中apply和Math.max()函数的问题及区别介绍
2018/03/27 Javascript
Vue.js 父子组件通信的十种方式
2018/10/30 Javascript
Python实现求最大公约数及判断素数的方法
2015/05/26 Python
图文讲解选择排序算法的原理及在Python中的实现
2016/05/04 Python
使用Python实现从各个子文件夹中复制指定文件的方法
2018/10/25 Python
使用PyTorch将文件夹下的图片分为训练集和验证集实例
2020/01/08 Python
如何基于Python Matplotlib实现网格动画
2020/07/20 Python
2020版Python学习路线图(附学习资料)
2020/09/15 Python
python定义具名元组实例操作
2021/02/28 Python
小学生自我评价范例
2013/09/24 职场文书
土木工程专业自荐信
2013/10/04 职场文书
行政部岗位职责范本
2014/03/13 职场文书
事业单位竞聘上岗实施方案
2014/03/28 职场文书
车间核算员岗位职责
2014/07/01 职场文书
校长师德师风自我剖析材料
2014/09/29 职场文书
2014年物业公司工作总结
2014/11/22 职场文书
小学优秀班主任材料
2014/12/17 职场文书
不同意离婚答辩状
2015/05/22 职场文书
2019年最新七夕唯美祝福语(60条)
2019/07/22 职场文书
详解Mysql 函数调用优化
2021/04/07 MySQL