-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
291 lines (219 loc) · 8.36 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
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
import numpy as np
import sys
import os
import csv
from scipy import misc
import collections
import tensorflow as tf
from tensorflow.contrib import rnn
import pickle
import _pickle as cPickle
try:
import cPickle as pickle
except ImportError:
import pickle
train_path = "./data/mfcc/train.ark"
test_path = "./data/mfcc/test.ark"
label_path = "./label/train.lab"
test_path = "./data/mfcc/test.ark"
prepro_dir = "./prepo/"
map48_dir = "./data/phones/48phone_char.map"
map48to39_dir = "./data/phones/48_39.map"
testlabel = "./testlabel.txt"
testlabelmap = "./data/label/testlabelmap.txt"
filename = "./hw1.spydata"
model_dir = "./model/"
batch_size = 128
pointer = 0
#file = open(map48_dir, 'r')
##for r_index, row in file.readlines():
#r_array_map = []
#
#for r_index, row in enumerate(file.readlines()):
## if (r_index < 10):
## print(row, end='')
# r_temp = row.split('\t')
# r_array_map.append(r_temp)
#######################################################
def load_train_data(train_path, label_path, map48_dir, map48to39_dir, testlabelmap):
#######################################################
## open the file of training data and delete the name of ppl
file = open(train_path, 'r')
#for r_index, row in file.readlines():
t_array_rnn = []
r_array = []
for r_index, row in enumerate(file.readlines()):
temprnn = row.split()[0]
t_array_rnn.append(temprnn)
r_temp = row.split()
for k in range(1, len(r_temp),1):
r_temp[k] = float(r_temp[k])
r_array.append(r_temp)
for i in range(0, len(r_array),1):
del r_array[i][0]
## separate every ppl's array from id 1 to the end so we got 3698 ppl (3698,?,39)
t_array_final = []
flag_final = False
temp_final = []
for m in range(0,len(t_array_rnn),1):
temprnnn = t_array_rnn[m].split('_')
if (temprnnn[2] == '1'):
flag_final = True
if (flag_final == True):
t_array_final.append(temp_final)
temp_final = []
flag_final = False
temp_final.append(r_array[m])
if (m == len(t_array_rnn)-1):
t_array_final.append(temp_final)
del t_array_final[0]
######################################################
## add the file of map 48 convert 39 and 48 phone and ENG letters
file = open(map48_dir, 'r')
#for r_index, row in file.readlines():
r_array_map = []
for r_index, row in enumerate(file.readlines()):
# if (r_index < 10):
# print(row, end='')
r_temp = row.split('\t')
r_array_map.append(r_temp)
file39 = open(map48to39_dir, 'r')
#for r_index, row in file.readlines():
r_array_map39 = []
for r_index39, row39 in enumerate(file39.readlines()):
# if (r_index < 10):
# print(row, end='')
r_temp39 = row39.split('\t')
r_array_map39.append(r_temp39)
#convert labelmap to 48 char
file = open(testlabelmap, 'r')
#for r_index, row in file.readlines():
r_label_map = []
for r_index, row in enumerate(file.readlines()):
# if (r_index < 10):
# print(row, end='')
r_temp = row.split('\n')[0]
# r_temp[1] = int('0')
# r_temp = int(r_temp)
r_label_map.append(r_temp)
pre_48char = [] #convert integer 0-47 to 48 char
for i in range(0,len(r_label_map),1):
for j in range(0,len(r_array_map),1):
if (r_label_map[i] == r_array_map[j][1]):
pre_48char.append(r_array_map[j][0])
## convert 48 phones to 39 phones
pre_48char39 = []
for e in range(0,len(pre_48char),1):
for f in range(0,len(r_array_map39),1):
if (pre_48char[e] == r_array_map39[f][0]):
pre_48char39.append(r_array_map39[f][1])
# get the 48 phones but only have 39 phones (some phones are duplicated)
rmap39 = []
for a in range(0,len(r_array_map39)):
temp39 = r_array_map39[a][1]
temp39a = temp39.split('\n')[0]
rmap39.append(temp39a)
############################################
#convert 48 char to 39 char
a = rmap39
map39_afterpre = []
temp_final1 = []
rmap39a1 = []
rmapc = 0
## convert 48 phone but only have 39 phones into only 39 phone without same phones
for aa in range(0,len(rmap39),1):
for bb in range(0,len(map39_afterpre),1):
if (rmap39[aa] == map39_afterpre[bb]):
rmapc = rmapc + 1
if (rmapc < 1):
map39_afterpre.append(rmap39[aa])
rmapc = 0
else:
rmapc = 0
mapxx = map39_afterpre
##turn the 39 phones into the number of its index
pre_39final = []
for cc in range(0,len(pre_48char39),1):
for dd in range(0,len(map39_afterpre),1):
temp39 = pre_48char39[cc]
temp39a = temp39.split('\n')[0]
if (temp39a == map39_afterpre[dd]):
pre_39final.append(int(dd))
#############################################
#convert label to be like array formation
##(alian ppl's id with different lens sentences of label)
pmap = 0
ch = 0
label_array_final1 = []
for ee in range(0,len(t_array_final),1):
ch = len(t_array_final[ee])
tempmap = pre_39final[pmap:pmap+ch]
pmap = pmap + ch
# pmap = pmap + 1
label_array_final1.append(tempmap)
####################################
#pedding
##calculate max len of array
max_final = len(t_array_final[0])
for ff in range(0,len(t_array_final),1):
if(max_final < len(t_array_final[ff])):
max_final = len(t_array_final[ff])
#pedding for array to make every sentence has same length
##we use the max length that every sentence padds to be like max length
t_array_f = t_array_final
t_array_f1 = []
pedd_f = []
for gg1 in range(0,len(t_array_f[0][0]),1):
temppeddf = float(1000.0)
pedd_f.append(temppeddf)
for gg in range(0,len(t_array_final),1):
changefinal = max_final - len(t_array_final[gg])
for hh in range(0,changefinal,1):
temparrayf = t_array_f[gg]
temparrayf.append(pedd_f)
t_array_f1.append(temparrayf)
#pedding for label to make every sentence has same length
##we use the max length that every sentence padds to be like max length
t_label_f = label_array_final1
label_array_f1 = []
pedd_flabel = []
temppeddf = int(100)
pedd_flabel = temppeddf
temparrayflabel = []
for hh in range(0,len(t_label_f),1):
changefinallabel = max_final - len(t_label_f[hh])
for ii in range(0,changefinallabel,1):
temparrayflabel = t_label_f[hh]
temparrayflabel.append(pedd_flabel)
label_array_f1.append(temparrayflabel)
################################################
#processing the array and label to be the input shape of RNN model
x = t_array_f1
xinput = np.array(x)
y = label_array_f1
#yinput = np.array(y)
#yinput = np.array(y1)
#del y1[0][48:97]
tempp = []
#for k in range(0,49,1):
# tempp1 = int(0)
# tempp.append(tempp1)
y1 = []
temppp = []
# we use one hot encoding to the labels (let it to be like (00001000000000))
for i in range(0,len(y),1):
for j in range(0,len(y[0]),1):
for k in range(0,40,1):
tempp1 = int(0)
tempp.append(tempp1)
if (y[i][j]<39):
tempp[y[i][j]] = int(1)
else:
tempp[39] = int(1)
temppp.append(tempp)
tempp = []
y1.append(temppp)
temppp = []
yinput = np.array(y1)
return xinput, yinput
############################################