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实现简单温度转换的方法
Mar 13 Python
Python实现SVN的目录周期性备份实例
Jul 17 Python
python学习 流程控制语句详解
Jun 01 Python
python 对txt中每行内容进行批量替换的方法
Jul 11 Python
Python3.4学习笔记之类型判断,异常处理,终止程序操作小结
Mar 01 Python
从0开始的Python学习014面向对象编程(推荐)
Apr 02 Python
python按行读取文件并找出其中指定字符串
Aug 08 Python
Atom Python 配置Python3 解释器的方法
Aug 28 Python
python实现删除列表中某个元素的3种方法
Jan 15 Python
Python DES加密实现原理及实例解析
Jul 17 Python
Python中logging日志记录到文件及自动分割的操作代码
Aug 05 Python
Jupyter Notebook 安装配置与使用详解
Jan 06 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 短链接算法收集与分析
2011/12/30 PHP
php实现的支持断点续传的文件下载类
2014/09/23 PHP
session 加入redis的实现代码
2016/07/15 PHP
如何正确配置Nginx + PHP
2016/07/15 PHP
php实现基于openssl的加密解密方法
2016/09/30 PHP
给大家分享几个常用的PHP函数
2017/01/15 PHP
thinkphp5框架实现的自定义扩展类操作示例
2019/05/16 PHP
PHP上传图片到数据库并显示的实例代码
2019/12/20 PHP
php 使用ActiveMQ发送消息,与处理消息操作示例
2020/02/23 PHP
window.location.hash 属性使用说明
2010/03/20 Javascript
网页中返回顶部代码(多种方法)另附注释说明
2013/04/24 Javascript
js获取input标签的输入值实现代码
2013/08/05 Javascript
判断滚动条到底部的JS代码
2013/11/04 Javascript
Egret引擎开发指南之运行项目
2014/09/03 Javascript
SpringMVC返回json数据的三种方式
2015/12/10 Javascript
jQuery.uploadify文件上传组件实例讲解
2016/09/23 Javascript
HTML5+jQuery实现搜索智能匹配功能
2017/03/24 jQuery
在ABP框架中使用BootstrapTable组件的方法
2017/07/31 Javascript
详解Vue+axios+Node+express实现文件上传(用户头像上传)
2018/08/10 Javascript
vue中接口域名配置为全局变量的实现方法
2018/09/20 Javascript
Node.js 如何利用异步提升任务处理速度
2019/01/07 Javascript
vscode调试node.js的实现方法
2020/03/22 Javascript
[01:34]传奇从这开始 2016国际邀请赛中国区预选赛震撼开启
2016/06/26 DOTA
python类型强制转换long to int的代码
2013/02/10 Python
Tensorflow卷积神经网络实例
2018/05/24 Python
Python使用jsonpath-rw模块处理Json对象操作示例
2018/07/31 Python
Linux下安装python3.6和第三方库的教程详解
2018/11/09 Python
Python3 关于pycharm自动导入包快捷设置的方法
2019/01/16 Python
Python使用socketServer包搭建简易服务器过程详解
2020/06/12 Python
python实现杨辉三角的几种方法代码实例
2021/03/02 Python
学生个人的自我评价分享
2013/11/05 职场文书
设计总监岗位职责
2013/12/07 职场文书
小学节能减排倡议书
2014/05/15 职场文书
大学生村官考核材料
2014/05/23 职场文书
锅炉工岗位职责
2015/02/13 职场文书
如何在Python中妥善使用进度条详解
2022/04/05 Python