近期接手一个项目,时关于深度学习文本分类,我用的是keras深度学习框架LSTM模型,学习前首先得对数据进行处理,由于文本类别比较多有96类,并且有部分类别数据量比较少,这将会影响算法的拟合,我首先想到的是过采样的方法,但是过采样对文本数据无用,所以得先进行词向量的转换,转换完之后我发现准确率还是上不去,后来看了一下word2vec原理,是按照输入的文本数据进行分词统计,在计算词向量,所以这样利用过采样出来的结果,不能够代表就是与原文本类似的文本,所以这种方法pass掉。之后就找到文本数据增强的方法,下面我把我用到的给分享一下。看代码:


# !/usr/bin/env python
# -*- coding: utf-8 -*-import jieba
import re
import synonyms
import random
from random import shufflerandom.seed(2019)# 停用词列表,默认使用哈工大停用词表
f = open('stop_word.txt', encoding='utf-8')
stop_words = list()
for stop_word in f.readlines():stop_words.append(stop_word[:-1])# 考虑到与英文的不同,暂时搁置
# 文本清理
'''
import re
def get_only_chars(line):#1.清除所有的数字
'''########################################################################
# 同义词替换
# 替换一个语句中的n个单词为其同义词
########################################################################
def synonym_replacement(words, n):new_words = words.copy()random_word_list = list(set([word for word in words if word not in stop_words]))random.shuffle(random_word_list)num_replaced = 0for random_word in random_word_list:synonyms = get_synonyms(random_word)if len(synonyms) >= 1:synonym = random.choice(synonyms)new_words = [synonym if word == random_word else word for word in new_words]num_replaced += 1if num_replaced >= n:breaksentence = ' '.join(new_words)new_words = sentence.split(' ')return new_wordsdef get_synonyms(word):return synonyms.nearby(word)[0]########################################################################
# 随机插入
# 随机在语句中插入n个词
########################################################################
def random_insertion(words, n):new_words = words.copy()for _ in range(n):add_word(new_words)return new_wordsdef add_word(new_words):synonyms = []counter = 0while len(synonyms) < 1:random_word = new_words[random.randint(0, len(new_words) - 1)]synonyms = get_synonyms(random_word)counter += 1if counter >= 10:returnrandom_synonym = random.choice(synonyms)random_idx = random.randint(0, len(new_words) - 1)new_words.insert(random_idx, random_synonym)########################################################################
# Random swap
# Randomly swap two words in the sentence n times
########################################################################def random_swap(words, n):new_words = words.copy()for _ in range(n):new_words = swap_word(new_words)return new_wordsdef swap_word(new_words):random_idx_1 = random.randint(0, len(new_words) - 1)random_idx_2 = random_idx_1counter = 0while random_idx_2 == random_idx_1:random_idx_2 = random.randint(0, len(new_words) - 1)counter += 1if counter > 3:return new_wordsnew_words[random_idx_1], new_words[random_idx_2] = new_words[random_idx_2], new_words[random_idx_1]return new_words########################################################################
# 随机删除
# 以概率p删除语句中的词
########################################################################
def random_deletion(words, p):if len(words) == 1:return wordsnew_words = []for word in words:r = random.uniform(0, 1)if r > p:new_words.append(word)if len(new_words) == 0:rand_int = random.randint(0, len(words) - 1)return [words[rand_int]]return new_words########################################################################
# EDA函数
def eda_func(sentence, alpha_sr=0.25, alpha_ri=0.25, alpha_rs=0.25, p_rd=0.25, num_aug=12):seg_list = jieba.cut(text_func(sentence))seg_list = " ".join(seg_list)words = list(seg_list.split())num_words = len(words)augmented_sentences = []num_new_per_technique = int(num_aug / 4)n_sr = max(1, int(alpha_sr * num_words))n_ri = max(1, int(alpha_ri * num_words))n_rs = max(1, int(alpha_rs * num_words))# print(words, "\n")# 同义词替换srfor _ in range(num_new_per_technique):a_words = synonym_replacement(words, n_sr)augmented_sentences.append(''.join(a_words))# 随机插入rifor _ in range(num_new_per_technique):a_words = random_insertion(words, n_ri)augmented_sentences.append(''.join(a_words))## 随机交换rsfor _ in range(num_new_per_technique):a_words = random_swap(words, n_rs)augmented_sentences.append(''.join(a_words))### 随机删除rdfor _ in range(num_new_per_technique):a_words = random_deletion(words, p_rd)augmented_sentences.append(''.join(a_words))# print(augmented_sentences)shuffle(augmented_sentences)if num_aug >= 1:augmented_sentences = augmented_sentences[:num_aug]else:keep_prob = num_aug / len(augmented_sentences)augmented_sentences = [s for s in augmented_sentences if random.uniform(0, 1) < keep_prob]# augmented_sentences.append(seg_list)return augmented_sentences##
# 测试用例
# print(len(eda_func(sentence=text_func("知识图谱本质上是语义网络,是一种基于图的数据结构,由节点和边组成"))))

这里有四种方法,随机替换,随机插值,随机交换,随机删除,都是用其近义词换掉里面的某一个词,具体换几个词,可以在eda函数里面修改,这个可以根据自己的情况来改。。。

利用eda函数对文本数据进行增强相关推荐

  1. mysql shell可视化_shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中...

    shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...

  2. 处理DataFrame数据——利用Groupby函数分类汇总数据并转为矩阵

    问题 我的手头有35万条数据,包含三个属性:时间.数量和ID.我现在需要以时间.ID分类,对数量进行求和统计(分类汇总):然后以time列为行(x轴),ID为列(y轴),num为值建立矩阵. 读取数据 ...

  3. 利用Excel函数对 重复数据进行编号解决方法

    效果要求: 问题描述: 根据不同的类别(含重复数据)进行不同的编号排序,如上图所示 用法: sumproduct 在Sumproduct函数的适用范围,在给定的几组数组中,然后把数组间对应的元素相乘, ...

  4. 利用dbms_backup_restore函数来恢复数据文件

    本实验对数据文件丢失,控制文件丢失,但是RMAN的备份信息和备份日志存在的时候使用. 控制文件的丢失恢复参看http://luoping.blog.51cto.com/534596/294164. 下 ...

  5. SQLServer中利用NTILE函数对数据进行分组的一点使用

    本文出处:http://www.cnblogs.com/wy123/p/6908377.html NTILE函数可以按照指定的排序规则,对数据按照指定的组数(M个对象,按照某种排序分N个组)进行分组, ...

  6. 【STM32学习】——USART串口数据包HEX/文本数据包收发流程串口收发HEX/文本数据包实操

    文章目录 前言 一.数据包格式(江科大规定) 1.HEX数据包 2.文本数据包 3.两者对比 二.数据包收发流程 1.HEX数据包接收(只演示固定包长) 2.文本数据包接收(只演示可变包长) 三.实操 ...

  7. 你好,C++(26)如何与函数内部进行数据交换?5.1.3 函数参数的传递

    5.1.3  函数参数的传递 我们知道,函数是用来完成某个功能的相对独立的一段代码.函数在完成这个功能的时候,往往需要外部数据的支持,这时就需要在调用这个函数时向它传递所需要的数据它才能完成这个功能获 ...

  8. 文本数据增强二(EDA、同义词替换-新增-交换-删除-生成同义句)

    一.中文文本数据增强 (中文.同义句生成.enhance.augment.text.data.nlp.样本不均衡.语料不够.数据不足.扩充增加),相较于图片,中文文本数据强的效果似乎没那么靠谱(效果没 ...

  9. 人机交互系统(3.1)——NLP文本数据增强方法

    一.数据增强的背景和应用场景 随着AI技术的逐步发展,更好的神经网络模型对数据规模的要求也逐步提升.而在分类任务中,若不同类别数据量相差很大,模型则会出现过拟合现象,严重影响预测的正确性. 从广义上来 ...

最新文章

  1. Linux下su与su -命令的区别
  2. 物联网可应用于十大行业嘛?
  3. 对Web页面元素的绝对唯一引用方法
  4. config中自定义配置
  5. nacos云环境集群部署
  6. 华为交换机vlan配置
  7. Loader之一:基本原理
  8. ORACLE ROLLUP CUBE
  9. html背景定位,css background-position center left right top bottom代表意思
  10. debian安装java jdk_Linux(Centos、Debian)之安装Java JDK及注意事项(转)
  11. python 数据类_python数据类
  12. 【电子签章】HTML格式合同转化成PDF文件 已下载
  13. Git commit至Gitee报错‘remote: error: hook declined to update refs/heads/master‘的解决方法
  14. FTP多路径递归下载
  15. jsjavaScriptDate的时间格式转换,直接粘贴就可以使用
  16. C++基于MFC编程——课程管理系统
  17. VB编程操作AutoCAD线型
  18. 1.为什么要使用token,token是什么?
  19. 编译原理实验,赋值语句的语法分析程序设计
  20. 如何从阿里云官方镜像站下载centos并安装

热门文章

  1. 1000 以内的水仙花数java
  2. HtmlEncode是做什么的?
  3. 计算机类普刊有哪些,基础数学类的容易发表的普刊有哪些
  4. Java实现 幸运数字
  5. 韶音科技2021研究员面试经验
  6. 利用有道翻译Api实现英文翻译功能
  7. 模板字符串(` `)
  8. Zbush建模笔记_036_Zsketch 创建初始模型(与Z球建模配合使用)
  9. 生信学习笔记:fastp质控处理生成的report结果解读
  10. 作为一名IT狗,天天加班,快变秃子了,我决定去植发……