Use: pip install nlpcda
Open source is not easy, welcome to star?
pypi:https://pypi.org/project/nlpcda/
One-click Chinese data enhancement tool, supporting:
BIO data enhancementsimbert to generate similar sentences经过细节特殊处理,比如不改变年月日数字,尽量保证不改变原文语义。即使改变也能被猜出来、能被猜出来、能被踩出来、能被菜粗来、被菜粗、能菜粗来
文本to语音>语音recognition back to文本: generate speech on text based on fastspeech2, and voice recognition text based on wav2vec2example:
input: Xinhua News Agency Beijing News >
fastspeech2> x.wavx.wav >
wav2vec2> output: Xinhua set up Beijing news
Today is August 29th news > Today is August 29th news
I have 1234 apples > I have 1234 apples
nlpcda
️ If you simply score your accuracy scores, you will generally not get any score improvement with this package.
parameter:
It is the text file path, the content is as follows:
Entity 1
Entity 2
...
Entity n
from nlpcda import Randomword
test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击'''
smw = Randomword ( create_num = 3 , change_rate = 0.3 )
rs1 = smw . replace ( test_str )
print ( '随机实体替换>>>>>>' )
for s in rs1 :
print ( s )
'''
随机实体替换>>>>>>
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
这是个实体:长兴国际;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
这是个实体:浙江世宝;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
'''parameter:
It is the text file path, the content is as follows (separated by spaces):
Aa01A0 Humans are all humans
id2 Synonym b1 Synonym b2 ... Synonym bk
...
idn synonym n1 synonym n2
from nlpcda import Similarword
test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击'''
smw = Similarword ( create_num = 3 , change_rate = 0.3 )
rs1 = smw . replace ( test_str )
print ( '随机同义词替换>>>>>>' )
for s in rs1 :
print ( s )
'''
随机同义词替换>>>>>>
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数量增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;斯nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
'''
parameter:
It is the text file path, the content is as follows (t separated):
de del Dede De technet till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till till
...
Pinyin n word n1 word n2
from nlpcda import Homophone
test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击'''
smw = Homophone ( create_num = 3 , change_rate = 0.3 )
rs1 = smw . replace ( test_str )
print ( '随机近义字替换>>>>>>' )
for s in rs1 :
print ( s )
'''
随机近义字替换>>>>>>
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
这是个实体:58同城;今填是2020年3月8日11:40,天气晴朗,天气很不错,空气痕好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
鷓是个实体:58同乘;今天是2020年3月8日11:40,天迄晴朗,天气很不错,空气很儫,不差;这个nlpcad包,用于方便一键数据增强,犐有效增牆NLP模型的橎化性能、减少波动、抵抗对抗攻击
'''parameter:
from nlpcda import RandomDeleteChar
test_str = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击'''
smw = RandomDeleteChar ( create_num = 3 , change_rate = 0.3 )
rs1 = smw . replace ( test_str )
print ( '随机字删除>>>>>>' )
for s in rs1 :
print ( s )
'''
随机字删除>>>>>>
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气,不差;这个nlpcad包用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗
个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型泛化性能、减少波动、抵抗对抗
'''Enter the marked NER data directory, the marked file path that needs to be enhanced, and the number of enhanced, and you can enhance it with one click.
Ner class parameters:
Word 1 t TAG
Northt B-LOC
Beijingt I-LOC
Todayt O
Dayt O
Very t O
Hot O
. t O
Call function augment() parameter
example:
from nlpcda import Ner
ner = Ner ( ner_dir_name = 'ner_data' ,
ignore_tag_list = [ 'O' ],
data_augument_tag_list = [ 'P' , 'LOC' , 'ORG' ],
augument_size = 3 , seed = 0 )
data_sentence_arrs , data_label_arrs = ner . augment ( file_name = '0.txt' )
# 3条增强后的句子、标签 数据,len(data_sentence_arrs)==3
# 你可以写文件输出函数,用于写出,作为后续训练等
print ( data_sentence_arrs , data_label_arrs ) from nlpcda import CharPositionExchange
ts = '''这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击'''
smw = CharPositionExchange ( create_num = 3 , change_rate = 0.3 , char_gram = 3 , seed = 1 )
rs = smw . replace ( ts )
for s in rs :
print ( s )
'''
这是个实体:58同城;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,不差;这个nlpcad包,用于方便一键数据增强,可有效增强NLP模型的泛化性能、减少波动、抵抗对抗攻击
这实个是体:58城同;今天是2020年3月8日11:40,天气晴朗,天气很不错,空气很好,差不;这个nlpcad包,便用一数方增键强据于,增有效可强NLP模型性泛化的能、动少减波、抵对攻抗抗击
这是个体实:58城同;今是天2020年3月8日11:40,朗气晴天,天气很错不,空好很气,不差;个这nlpcad包,方便键一据增用数于强,可有效强增NLP模型的性化泛能、动减波少、抗抗击抵对攻
'''parameter:
It is the text file path, the content is as follows ((t) separated):
0 Zero
1 one ①
...
9 Nine Nine ⑨
from nlpcda import EquivalentChar
test_str = '''今天是2020年3月8日11:40,天气晴朗,天气很不错。'''
s = EquivalentChar ( create_num = 3 , change_rate = 0.3 )
# 添加等价字
s . add_equivalent_list ([ '看' , '瞅' ])
res = s . replace ( test_str )
print ( '等价字替换>>>>>>' )
for s in res :
print ( s )
'''
等价字替换>>>>>>
今天是2020年3月8日11:40,天气晴朗,天气很不错。
今天是二〇2〇年3月八日1①:4〇,天气晴朗,天气很不错。
今天是二0贰零年3月捌日11:40,天气晴朗,天气很不错
'''Used before use, add word participle effect
from nlpcda import Randomword
from nlpcda import Similarword
from nlpcda import Homophone
from nlpcda import RandomDeleteChar
from nlpcda import Ner
from nlpcda import CharPositionExchange
Randomword . add_word ( '小明' )
Randomword . add_words ([ '小明' , '小白' , '天地良心' ])
# Similarword,Homophone,RandomDeleteChar 同上1. Enhanced note of Baidu's Chinese-English translation interchange implementation:
Apply for your appid, secretKey: http://api.fanyi.baidu.com/api/trans
from nlpcda import baidu_translate
zh = '天气晴朗,天气很不错,空气很好'
# 申请你的 appid、secretKey
# 两遍洗数据法(回来的中文一般和原来不一样,要是一样,就不要了,靠运气?)
en_s = baidu_translate ( content = zh , appid = 'xxx' , secretKey = 'xxx' , t_from = 'zh' , t_to = 'en' )
zh_s = baidu_translate ( content = en_s , appid = 'xxx' , secretKey = 'xxx' , t_from = 'en' , t_to = 'zh' )
print ( zh_s )2. Enhancement of Google Translation Interchange Implementation
pip package: py-googletrans
Free Google Translation API, requires a wall-blocking and unstable
https://py-googletrans.readthedocs.io/en/latest
pip install googletrans
from googletrans import Translator
def googletrans ( content = '一个免费的谷歌翻译API' , t_from = 'zh-cn' , t_to = 'en' ):
translator = Translator ()
s = translator . translate ( text = content , dest = t_to , src = t_from )
return s . textSource: https://github.com/ZhuiyiTechnology/pretrained-models
Reference: https://github.com/ZhuiyiTechnology/simbert
Download any model in it, decompress it to any position and assign it to model_path variable:
| name | Training data size | Vocabulary size | Model size | Download address |
|---|---|---|---|---|
| SimBERT Tiny | 22 million similar sentence groups | 13685 | 26MB | Baidu Netdisk (1tp7) |
| SimBERT Small | 22 million similar sentence groups | 13685 | 49MB | Baidu Netdisk (nu67) |
| SimBERT Base | 22 million similar sentence groups | 13685 | 344MB | Baidu Netdisk (6xhq) |
parameter:
Environment reference (manual installation):
keras==2.3.1
bert4keras==0.7.7
# tensorflow==1.13.1
tensorflow-gpu==1.13.1
from nlpcda import Simbert
config = {
'model_path' : '/xxxx/chinese_simbert_L-12_H-768_A-12' ,
'CUDA_VISIBLE_DEVICES' : '0,1' ,
'max_len' : 32 ,
'seed' : 1
}
simbert = Simbert ( config = config )
sent = '把我的一个亿存银行安全吗'
synonyms = simbert . replace ( sent = sent , create_num = 5 )
print ( synonyms )
'''
[('我的一个亿,存银行,安全吗', 0.9871675372123718),
('把一个亿存到银行里安全吗', 0.9352194666862488),
('一个亿存银行安全吗', 0.9330801367759705),
('一个亿的存款存银行安全吗', 0.92387855052948),
('我的一千万存到银行安不安全', 0.9014463424682617)]
'''