-
Notifications
You must be signed in to change notification settings - Fork 13
/
dataset_handler.py
240 lines (193 loc) · 7.37 KB
/
dataset_handler.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
#! /usr/bin/env python
# encoding: utf-8
from __future__ import unicode_literals
import io
import json
import os
from abc import ABCMeta, abstractproperty, abstractmethod
from copy import deepcopy
from future.utils import iteritems, itervalues
from pathlib import Path
class Dataset(object):
__metaclass__ = ABCMeta
"""
Abstract data set class to inherit from when implementing a new
data set handler
"""
@classmethod
@abstractmethod
def from_dir(cls, _dir):
"""
Instantiates a dataset from a folder
"""
raise NotImplementedError
@abstractproperty
def training_dataset(self):
raise NotImplementedError
@abstractproperty
def test_dataset(self):
raise NotImplementedError
@abstractmethod
def get_audio_file(self, text):
raise NotImplementedError
class TrainTestDataset(Dataset):
"""Interface for train/test metrics
"""
def __init__(self, data_list, training_dataset, test_dataset):
self.data = data_list
self._training_dataset = training_dataset
self._test_dataset = test_dataset
self._normalized_training_dataset = None
self._normalized_test_dataset = None
self.language = self._training_dataset["language"]
# text -> wav mapping
self.audio_corpus = {}
for entry in self.data:
text = entry['text']
wav_file = entry['path_file']
self.audio_corpus[text] = wav_file
# clean up the dataset by removing utterances that don't have an audio
self._test_dataset = keep_only_utterances_with_audio(
self._test_dataset, self.audio_corpus
)
# wav -> text mapping
self.wav_to_text = {
Path(wav).name: text for text, wav in self.audio_corpus.items()
}
# labels
self.utterances_labels = retrieve_utterances_labels(self._test_dataset)
@classmethod
def from_dir(cls, _dir):
metadata_path = os.path.join(_dir, "metadata.json")
json_data = load_json(metadata_path)
training_dataset_path = os.path.join(_dir, "training_dataset.json")
training_dataset = load_json(training_dataset_path)
test_dataset_path = os.path.join(_dir, "test_dataset.json")
test_dataset = load_json(test_dataset_path)
for entry in json_data:
entry["path_file"] = os.path.join(_dir, entry["path_file"])
return cls(json_data, training_dataset, test_dataset)
def get_audio_file(self, text):
wav = self.audio_corpus.get(text)
if wav is None:
raise KeyError("Text {} is absent from audio dataset".format(text))
return wav
def get_labels_from_text(self, text):
labels = self.utterances_labels.get(text)
if labels is None:
raise KeyError("Text {} is absent from audio dataset".format(text))
return labels
def get_labels_from_wav(self, wav):
text = self.wav_to_text.get(wav)
if text is None:
raise KeyError("File {} does not exist".format(wav))
return self.utterances_labels.get(text)
def get_transcript(self, wav):
text = self.wav_to_text.get(wav)
if text is None:
raise KeyError("File {} does not exist".format(wav))
return text
@property
def training_dataset(self):
return self._training_dataset
@property
def test_dataset(self):
return self._test_dataset
class CrossValDataset(Dataset):
"""Interface for cross validation metrics
"""
def __init__(self, config, dataset, audio_corpus):
self.config = config
# text -> wav mapping
self.audio_corpus = {}
for sentence, wav_file in iteritems(audio_corpus):
self.audio_corpus[sentence] = wav_file
# clean up the data set by removing utterances that don't have an audio
self._dataset = keep_only_utterances_with_audio(
dataset, self.audio_corpus
)
# wav -> text mapping
self.wav_to_text = {
Path(wav).name: text for text, wav in self.audio_corpus.items()
}
# labels
self.utterances_labels = retrieve_utterances_labels(self._dataset)
@classmethod
def from_dir(cls, _dir):
config = load_json(os.path.join(_dir, "config.json"))
dataset = load_json(os.path.join(_dir, config['dataset']))
speech_corpus_dir = os.path.join(_dir, config['speech_corpus'])
metadata = load_json(os.path.join(speech_corpus_dir, "metadata.json"))
audio_corpus = {
item['text']: os.path.abspath(
os.path.join(speech_corpus_dir, 'audio', item['filename'])) for
item in itervalues(metadata)
}
return cls(config, dataset, audio_corpus)
def get_audio_file(self, text):
wav = self.audio_corpus.get(text)
if wav is None:
raise KeyError("Text {} is absent from audio dataset".format(text))
return wav
def get_transcript(self, wav):
text = self.wav_to_text.get(wav)
if text is None:
raise KeyError("File {} does not exist".format(wav))
return text
def get_labels_from_text(self, text):
labels = self.utterances_labels.get(text)
if labels is None:
raise KeyError("Text {} is absent from audio dataset".format(text))
return labels
def get_labels_from_wav(self, wav):
text = self.wav_to_text.get(wav)
if text is None:
raise KeyError("File {} does not exist".format(wav))
return self.utterances_labels.get(text)
@property
def training_dataset(self):
return self._dataset
@property
def test_dataset(self):
raise TypeError(
"CrossValDataset is not meant to be used with train test metrics"
)
def load_json(filename, encoding='utf-8'):
"""
Load the content of filename
"""
with io.open(filename, 'r', encoding=encoding) as _file:
return json.load(_file)
def retrieve_utterances_labels(dataset):
utterances_labels = {}
for intent, intent_data in iteritems(dataset['intents']):
for idx, utt in enumerate(intent_data['utterances']):
sentence = "".join(chunk['text'] for chunk in utt['data'])
slots = [chunk for chunk in utt['data'] if 'entity' in chunk]
utterances_labels[sentence] = {
"text": sentence,
"intent": intent,
"slots": slots
}
return utterances_labels
def keep_only_utterances_with_audio(dataset, audio_corpus):
cleaned_up_dataset = deepcopy(dataset)
n_skipped = 0
total = 0
for intent, intent_data in iteritems(dataset['intents']):
cleaned_up_dataset['intents'][intent]['utterances'] = []
for idx, utt in enumerate(intent_data['utterances']):
total += 1
sentence = "".join(chunk['text'] for chunk in utt['data'])
if sentence in audio_corpus:
cleaned_up_dataset['intents'][intent]['utterances'].append(utt)
else:
n_skipped += 1
print(
"Skipping sentence {} from dataset because it does not "
"have an audio file".format(sentence)
)
print(
"{} utterancces skipped out of {}".format(n_skipped, total)
)
return cleaned_up_dataset