-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_utils.py
185 lines (157 loc) · 5.81 KB
/
data_utils.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
import numpy as np
def prepare_data_for_cnn(seqs_x, opt):
maxlen=opt.maxlen
filter_h=opt.filter_shape
lengths_x = [len(s) for s in seqs_x]
# print lengths_x
if maxlen != None:
new_seqs_x = []
new_lengths_x = []
for l_x, s_x in zip(lengths_x, seqs_x):
if l_x < maxlen:
new_seqs_x.append(s_x)
new_lengths_x.append(l_x)
lengths_x = new_lengths_x
seqs_x = new_seqs_x
if len(lengths_x) < 1 :
return None, None
pad = filter_h -1
x = []
for rev in seqs_x:
xx = []
for i in xrange(pad):
xx.append(0)
for idx in rev:
xx.append(idx)
while len(xx) < maxlen + 2*pad:
xx.append(0)
x.append(xx)
x = np.array(x,dtype='int32')
return x
def prepare_data_for_rnn(seqs_x, opt, is_add_GO = True):
maxlen=opt.maxlen
lengths_x = [len(s) for s in seqs_x]
# print lengths_x
if maxlen != None:
new_seqs_x = []
new_lengths_x = []
for l_x, s_x in zip(lengths_x, seqs_x):
if l_x < maxlen:
new_seqs_x.append(s_x)
new_lengths_x.append(l_x)
lengths_x = new_lengths_x
seqs_x = new_seqs_x
if len(lengths_x) < 1 :
return None, None
n_samples = len(seqs_x)
maxlen_x = np.max(lengths_x)
x = np.zeros(( n_samples, opt.sent_len)).astype('int32')
for idx, s_x in enumerate(seqs_x):
if is_add_GO:
x[idx, 0] = 1 # GO symbol
x[idx, 1:lengths_x[idx]+1] = s_x
else:
x[idx, :lengths_x[idx]] = s_x
return x
def get_minibatches_idx(n, minibatch_size, shuffle=False):
idx_list = np.arange(n, dtype="int32")
if shuffle:
np.random.shuffle(idx_list)
minibatches = []
minibatch_start = 0
for i in range(n // minibatch_size):
minibatches.append(idx_list[minibatch_start:
minibatch_start + minibatch_size])
minibatch_start += minibatch_size
# if (minibatch_start != n):
# # Make a minibatch out of what is left
# minibatches.append(idx_list[minibatch_start:])
return zip(range(len(minibatches)), minibatches)
def add_noise(sents, opt):
if opt.substitution == 's':
sents_permutated= substitute_sent(sents, opt)
elif opt.substitution == 'p':
sents_permutated= permutate_sent(sents, opt)
elif opt.substitution == 'a':
sents_permutated= add_sent(sents, opt)
elif opt.substitution == 'd':
sents_permutated= delete_sent(sents, opt)
elif opt.substitution == 'm':
sents_permutated= mixed_noise_sent(sents, opt)
elif opt.substitution == 'sc':
sents_permutated = substitute_sent_char(sents, opt)
else:
sents_permutated= sents
return sents_permutated
def permutate_sent(sents, opt):
sents_p = []
for ss in range(len(sents)):
sent_temp = sents[ss][:]
if len(sent_temp) <= 1:
sents_p.append(sent_temp)
continue
idx_s= np.random.choice(len(sent_temp)-1, size=opt.permutation, replace=True)
temp = sent_temp[idx_s[0]]
for ii in range(opt.permutation-1):
sent_temp[idx_s[ii]] = sent_temp[idx_s[ii+1]]
sent_temp[idx_s[opt.permutation-1]] = temp
sents_p.append(sent_temp)
return sents_p
def substitute_sent(sents, opt):
# substitute single word
sents_p = []
for ss in range(len(sents)):
sent_temp = sents[ss][:]
if len(sent_temp) <= 1:
sents_p.append(sent_temp)
continue
idx_s= np.random.choice(len(sent_temp)-1, size=opt.permutation, replace=True)
for ii in range(opt.permutation):
sent_temp[idx_s[ii]] = np.random.choice(opt.n_words)
sents_p.append(sent_temp)
return sents_p
def delete_sent(sents, opt):
# substitute single word
sents_p = []
for ss in range(len(sents)):
sent_temp = sents[ss][:]
if len(sent_temp) <= 1:
sents_p.append(sent_temp)
continue
idx_s= np.random.choice(len(sent_temp)-1, size=opt.permutation, replace=True)
for ii in range(opt.permutation):
sent_temp[idx_s[ii]] = -1
sents_p.append([s for s in sent_temp if s!=-1])
return sents_p
def add_sent(sents, opt):
# substitute single word
sents_p = []
for ss in range(len(sents)):
sent_temp = sents[ss][:]
if len(sent_temp) <= 1:
sents_p.append(sent_temp)
continue
idx_s= np.random.choice(len(sent_temp)-1, size=opt.permutation, replace=True)
for ii in range(opt.permutation):
sent_temp.insert(idx_s[ii], np.random.choice(opt.n_words))
sents_p.append(sent_temp[:opt.maxlen])
return sents_p
def mixed_noise_sent(sents, opt):
sents = delete_sent(sents, opt)
sents = add_sent(sents, opt)
sents = substitute_sent(sents, opt)
return sents
def substitute_sent_char(sents, opt):
# substitute single word
sents_p = []
for ss in range(len(sents)):
sent_temp = sents[ss][:]
if len(sent_temp) <= 1:
sents_p.append(sent_temp)
continue
permute_choice = [ic for ic in range(len(sent_temp)) if sent_temp[ic] != 1]
idx_s= np.random.choice(permute_choice, size=int(opt.permutation * (len(permute_choice))), replace=True)
for ii in range(len(idx_s)):
sent_temp[idx_s[ii]] = np.random.choice(list(range(2,28)))
sents_p.append(sent_temp)
return sents_p