This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.py
282 lines (224 loc) · 7.07 KB
/
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import glob
import re
import logging
import unicodedata
import codecs
import numpy as np
import scipy.io.wavfile as wav
from python_speech_features import mfcc
def read_text_file(path):
"""
Read text from file
Args:
path: string.
Path to text file.
Returns:
string.
Read text.
"""
with codecs.open(path, encoding="utf-8") as file:
return file.read()
def normalize_text(text, remove_apostrophe=True):
"""
Normalize given text.
Args:
text: string.
Given text.
remove_apostrophe: bool.
Whether to remove apostrophe in given text.
Returns:
string.
Normalized text.
"""
# Convert unicode characters to ASCII.
result = unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode()
# Remove apostrophes.
if remove_apostrophe:
result = result.replace("'", "")
return re.sub("[^a-zA-Z']+", ' ', result).strip().lower()
def read_text_files(dir, extensions=['txt']):
"""
Read text files.
Args:
dir: string.
Data directory.
extensions: list of strings.
File extensions.
Returns:
files: array of texts.
"""
if not os.path.isdir(dir):
logging.error("Text files directory %s is not found.", dir)
return None
if not all(isinstance(extension, str) for extension in extensions):
logging.error("Variable 'extensions' is not a list of strings.")
return None
# Get files list.
files_paths_list = []
for extension in extensions:
file_glob = os.path.join(dir, '*.' + extension)
files_paths_list.extend(glob.glob(file_glob))
# Read files.
files = []
for file_path in files_paths_list:
file = read_text_file(file_path)
file = normalize_text(file)
files.append(file)
files = np.array(files)
return files
def read_audio_files(dir, extensions=['wav']):
"""
Read audio files.
Args:
dir: string.
Data directory.
extensions: list of strings.
File extensions.
Returns:
files: array of audios.
"""
if not os.path.isdir(dir):
logging.error("Audio files directory %s is not found.", dir)
return None
if not all(isinstance(extension, str) for extension in extensions):
logging.error("Variable 'extensions' is not a list of strings.")
return None
# Get files list.
files_paths_list = []
for extension in extensions:
file_glob = os.path.join(dir, '*.' + extension)
files_paths_list.extend(glob.glob(file_glob))
# Read files.
files = []
for file_path in files_paths_list:
audio_rate, audio_data = wav.read(file_path)
file = mfcc(audio_data, samplerate=audio_rate)
files.append(file)
files = np.array(files)
return files
def make_char_array(text, space_token='<space>'):
"""
Make text as char array. Replace spaces with space token.
Args:
text: string.
Given text.
space_token: string.
Text which represents space char.
Returns:
string array.
Split text.
"""
result = np.hstack([space_token if x == ' ' else list(x) for x in text])
return result
def sparse_tuples_from_sequences(sequences, dtype=np.int32):
"""
Create a sparse representations of inputs.
Args:
sequences: a list of lists of type dtype where each element is a sequence
Returns:
A tuple with (indices, values, shape)
"""
indexes = []
values = []
for n, sequence in enumerate(sequences):
indexes.extend(zip([n] * len(sequence), range(len(sequence))))
values.extend(sequence)
indexes = np.asarray(indexes, dtype=np.int64)
values = np.asarray(values, dtype=dtype)
shape = np.asarray([len(sequences), np.asarray(indexes).max(0)[1] + 1], dtype=np.int64)
return indexes, values, shape
def sequence_decoder(sequence, first_index=(ord('a') - 1)):
"""
Read text files.
Args:
sequence: list of int.
Encoded sequence
first_index: int.
First index (usually index of 'a').
Returns:
decoded_text: string.
"""
decoded_text = ''.join([chr(x) for x in np.asarray(sequence) + first_index])
# Replacing blank label to none.
decoded_text = decoded_text.replace(chr(ord('z') + 1), '')
# Replacing space label to space.
decoded_text = decoded_text.replace(chr(ord('a') - 1), ' ')
return decoded_text
def texts_encoder(texts, first_index=(ord('a') - 1), space_index=0, space_token='<space>'):
"""
Encode texts to numbers.
Args:
texts: list of texts.
Data directory.
first_index: int.
First index (usually index of 'a').
space_index: int.
Index of 'space'.
space_token: string.
'space' representation.
Returns:
array of encoded texts.
"""
result = []
for text in texts:
item = make_char_array(text, space_token)
item = np.asarray([space_index if x == space_token else ord(x) - first_index for x in item])
result.append(item)
return np.array(result)
def standardize_audios(inputs):
"""
Standardize audio inputs.
Args:
inputs: array of audios.
Audio files.
Returns:
decoded_text: array of audios.
"""
result = []
for i in range(inputs.shape[0]):
item = np.array((inputs[i] - np.mean(inputs[i])) / np.std(inputs[i]))
result.append(item)
return np.array(result)
def get_sequence_lengths(inputs):
"""
Get sequence length of each sequence.
Args:
inputs: list of lists where each element is a sequence.
Returns:
array of sequence lengths.
"""
result = []
for input in inputs:
result.append(len(input))
return np.array(result, dtype=np.int64)
def make_sequences_same_length(sequences, sequences_lengths, default_value=0.0):
"""
Make sequences same length for avoiding value
error: setting an array element with a sequence.
Args:
sequences: list of sequence arrays.
sequences_lengths: list of int.
default_value: float32.
Default value of newly created array.
Returns:
result: array of with same dimensions [num_samples, max_length, num_features].
"""
# Get number of sequnces.
num_samples = len(sequences)
max_length = np.max(sequences_lengths)
# Get shape of the first non-zero length sequence.
sample_shape = tuple()
for s in sequences:
if len(s) > 0:
sample_shape = np.asarray(s).shape[1:]
break
# Create same sizes array
result = (np.ones((num_samples, max_length) + sample_shape) * default_value)
# Put sequences into new array.
for idx, sequence in enumerate(sequences):
result[idx, :len(sequence)] = sequence
return result