-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdata_aug.py
335 lines (293 loc) · 11.2 KB
/
data_aug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# -*- coding: utf-8 -*
"""
:py:data augmentation tools
"""
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import argparse
import json
import logging
import numpy as np
import six
import collections
from functools import reduce
from tqdm import tqdm
log = logging.getLogger(__name__)
stream_hdl = logging.StreamHandler(stream=sys.stderr)
formatter = logging.Formatter(fmt='[%(levelname)s] %(asctime)s [%(filename)12s:%(lineno)5d]: %(message)s')
stream_hdl.setFormatter(formatter)
log.addHandler(stream_hdl)
log.setLevel(logging.DEBUG)
def build_unk_parser(args):
"""
build unk parser
"""
log.info('building unk parser')
max_span_len = 10
p = 0.2
span_lens = range(1, max_span_len + 1)
span_len_dist = [p * (1 - p) ** (i - 1) for i in span_lens]
span_len_dist = [x / sum(span_len_dist) for x in span_len_dist]
log.debug('span len dist:')
avg_span_len = 0.
for k, v in zip(span_lens, span_len_dist):
log.debug('\t%d: %f' % (k, v))
avg_span_len += k * v
log.debug('avg span len: %f' % avg_span_len)
def unk_parser(tokens):
"""
unk parser
"""
ret, i = [], 0
while i < len(tokens):
span_len = np.random.choice(span_lens, p=span_len_dist)
span_len = min(span_len, len(tokens) - len(ret))
if np.random.rand() < 0.15:
ret += [args.unk_token] * span_len
else:
ret += tokens[i: i + span_len]
i += span_len
ret = ''.join(ret)
return ret
log.info('done')
return unk_parser
def build_trucate_parser(args):
"""
build truncate parser
"""
log.info('building truncate parser')
max_span_len = 10
p = 0.2
span_lens = range(1, max_span_len + 1)
span_len_dist = [p * (1 - p) ** (i - 1) for i in span_lens]
span_len_dist = [x / sum(span_len_dist) for x in span_len_dist]
log.debug('span len dist:')
avg_span_len = 0.
for k, v in zip(span_lens, span_len_dist):
log.debug('\t%d: %f' % (k, v))
avg_span_len += k * v
log.debug('avg span len: %f' % avg_span_len)
def truncate_parser(tokens):
"""
truncate parser
"""
ret, i = [], 0
while i < len(tokens):
span_len = np.random.choice(span_lens, p=span_len_dist)
span_len = min(span_len, len(tokens) - len(ret))
if np.random.rand() < 0.15:
pass
else:
ret += tokens[i: i + span_len]
i += span_len
ret = ''.join(ret)
return ret
log.info('done')
return truncate_parser
def build_pos_dict(field_list):
"""
build pos dict for pos parser
"""
from LAC import LAC
lac = LAC(mode='lac')
pos_dict = {}
for i in field_list:
#piece, tag = lac.lexer(i.strip(), return_tag=True)
piece, tag = lac.run(i.strip())
for p, t in zip(piece, tag):
pos_dict.setdefault(t, []).append(p)
return pos_dict
def build_pos_replace_parser(args):
"""
build pos_replace parser
"""
from LAC import LAC
lac = LAC(mode='lac')
log.info('building pos replace parser')
def pos_replace_parser(tokens, pos_dict):
"""
pos replace parser
"""
tokens = tokens.strip()
#piece, tag = lac.lexer(tokens, return_tag=True)
# piece, tag = lac.run(tokens, return_tag=True)
piece, tag = lac.run(tokens)
ret = []
for p, t in zip(piece, tag):
if np.random.rand() < 0.15:
p = np.random.choice(pos_dict[t])
ret.append(p)
ret = ''.join(ret)
return ret
log.info('done')
return pos_replace_parser
def build_w2v_replace_parser(args):
"""
build w2v_replace parser
"""
import re
from gensim.models import KeyedVectors
import gensim
from LAC import LAC
lac = LAC(mode='seg')
bin_file = "./vec2.bin"
if os.path.exists(bin_file):
log.debug('loading word2vec....')
word2vec = KeyedVectors.load_word2vec_format(bin_file)
log.debug('done loading word2vec....')
else:
log.debug('loading word2vec from txt....')
tmp_file = './vec2.txt'
#4.0以上版本用法
word2vec = gensim.models.KeyedVectors.load_word2vec_format(tmp_file, binary=False)
#word2vec.save_word2vec_format(bin_file)
log.debug('done loading word2vec....')
pat = re.compile('[a-zA-Z0-9]+')
def w2v_parser(tokens):
"""
w2v parser
"""
ret = []
#for i in lac.lexer(tokens):
for i in lac.run(tokens):
if np.random.rand() < 0.15 and i in word2vec.index_to_key:
candidate = word2vec.similar_by_word(i, topn=3)
t = np.random.choice([c for c, p in candidate])
if six.PY3:
t = t.strip()
elif six.PY2:
t = t.strip().decode("utf8")
if pat.match(t):
t = '%s ' % t
ret.append(t)
else:
ret.append(i)
ret = ''.join(ret)
return ret
return w2v_parser
builders = {
"unk": build_unk_parser,
"truncate": build_trucate_parser,
"pos_replace": build_pos_replace_parser,
"w2v_replace": build_w2v_replace_parser,
}
def build_parser(args):
"""
build parser
"""
selected_funcs, probs, selected_func_names = [], [], []
for func_name in builders:
p = args.__dict__[func_name]
print("args.dict", args.__dict__)
#return
if p > 0.:
log.info('using %s with prob %.2f' % (func_name, p))
probs.append(p)
func = builders[func_name](args)
selected_funcs.append(func)
selected_func_names.append(func_name)
probs = np.array(probs)
probs /= probs.sum()
def choose_parser():
"""
choose parser
"""
f = np.random.choice(selected_funcs, p=probs)
#print(f)
return f
return choose_parser, selected_func_names
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='main')
parser.add_argument("input", type=str)
parser.add_argument("output", type=str)
parser.add_argument("-n", "--aug_times", type=int, default=4)
parser.add_argument("-c", "--column_number", type=str, default='1')
parser.add_argument("-u", "--unk", type=float, default=0.25)
parser.add_argument("-t", "--truncate", type=float, default=0.25)
parser.add_argument("-r", "--pos_replace", type=float, default=0.25)
parser.add_argument("-w", "--w2v_replace", type=float, default=0.25)
if six.PY2:
parser.add_argument("--unk_token", type=unicode, default='\U0001f604')
elif six.PY3:
parser.add_argument("--unk_token", type=str, default='\U0001f604')
args = parser.parse_args()
col_nums = args.column_number.split(',')
col_nums = list(map(int, col_nums))
choose_parser, selected_func_names = build_parser(args)
data_files = os.listdir(args.input)
assert len(data_files) > 0, "%s is an empty directory" % args.input
mkdirlambda =lambda x: os.makedirs(x) if not os.path.exists(x) else True
mkdirlambda(args.output)
counter = collections.defaultdict(lambda:0)
for data_file in data_files:
input_file_path = os.path.join(args.input, data_file)
print("input_file_path", input_file_path)
input_file_name, suffix = os.path.splitext(data_file)
print(input_file_name, suffix)
output_file_name = '{0}_aug{1}' . format(input_file_name, suffix)
output_file_path = os.path.join(args.output, output_file_name)
fields_list = []
for i in range(len(col_nums)):
fields_list.append([])
pos_dict = []
if "pos_replace" in selected_func_names:
if six.PY3:
with open(input_file_path, encoding='UTF-8') as input_file:
for l in input_file.readlines():
cols = l.strip().split('\t')
for j in col_nums:
fields_list[j - 1].append(cols[j - 1])
elif six.PY2:
with open(input_file_path) as input_file:
for l in input_file.readlines():
cols = l.strip().decode("utf8").split('\t')
for j in col_nums:
fields_list[j - 1].append(cols[j - 1])
#print(np.array(fields_list).shape)
for j in col_nums:
pos_dict.append(build_pos_dict(fields_list[j - 1]))
if six.PY3:
with open(input_file_path, 'r', encoding='UTF-8') as input_file:
with open(output_file_path, 'w', encoding='UTF-8') as output_file:
for i, l in enumerate(input_file.readlines()):
parser = choose_parser()
#print(parser.__name__ == "pos_replace_parser")
if i % 1000 == 0:
log.debug('parsing line %d' % i)
print(l.strip(), file=output_file)
for k in range(args.aug_times):
cols = l.strip().split('\t')
for j in col_nums:
if parser.__name__ == "pos_replace_parser":
cols[j - 1] = parser(cols[j - 1], pos_dict[j - 1])
counter[parser.__name__] += 1
else:
cols[j - 1] = parser(cols[j - 1])
counter[parser.__name__] += 1
new_line = '\t'.join(cols)
print(new_line, file=output_file)
elif six.PY2:
with open(input_file_path) as input_file:
with open(output_file_path, 'w') as output_file:
for i, l in enumerate(input_file.readlines()):
parser = choose_parser()
#print(parser.__name__ == "pos_replace_parser")
if i % 1000 == 0:
log.debug('parsing line %d' % i)
print(l.strip(), file=output_file)
for k in range(args.aug_times):
cols = l.strip().decode("utf8").split('\t')
for j in col_nums:
if parser.__name__ == "pos_replace_parser":
cols[j - 1] = parser(cols[j - 1], pos_dict[j - 1])
counter[parser.__name__] += 1
else:
cols[j - 1] = parser(cols[j - 1])
counter[parser.__name__] += 1
new_line = '\t'.join(cols)
print(new_line.encode("utf8"), file=output_file)
print("counter", counter)