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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Untitled2.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1Jy8fwFO774TM_FTwK-0to2L0qHoUAT-U
|
8 |
+
"""
|
9 |
+
|
10 |
+
# -*- coding: utf-8 -*-
|
11 |
+
"""MGB2.ipynb
|
12 |
+
Automatically generated by Colaboratory.
|
13 |
+
Original file is located at
|
14 |
+
https://colab.research.google.com/drive/15ejoy2EWN9bj2s5ORQRZb5aTmFlcgA9d
|
15 |
+
"""
|
16 |
+
|
17 |
+
import datasets
|
18 |
+
import os
|
19 |
+
|
20 |
+
|
21 |
+
_DESCRIPTION = "MGB2 speech recognition dataset AR"
|
22 |
+
_HOMEPAGE = "https://arabicspeech.org/mgb2/"
|
23 |
+
_LICENSE = "MGB-2 License agreement"
|
24 |
+
_CITATION = """@misc{https://doi.org/10.48550/arxiv.1609.05625,
|
25 |
+
doi = {10.48550/ARXIV.1609.05625},
|
26 |
+
|
27 |
+
url = {https://arxiv.org/abs/1609.05625},
|
28 |
+
|
29 |
+
author = {Ali, Ahmed and Bell, Peter and Glass, James and Messaoui, Yacine and Mubarak, Hamdy and Renals, Steve and Zhang, Yifan},
|
30 |
+
|
31 |
+
keywords = {Computation and Language (cs.CL), FOS: Computer and information sciences, FOS: Computer and information sciences},
|
32 |
+
|
33 |
+
title = {The MGB-2 Challenge: Arabic Multi-Dialect Broadcast Media Recognition},
|
34 |
+
|
35 |
+
publisher = {arXiv},
|
36 |
+
|
37 |
+
year = {2016},
|
38 |
+
|
39 |
+
copyright = {arXiv.org perpetual, non-exclusive license}
|
40 |
+
}
|
41 |
+
"""
|
42 |
+
_DATA_ARCHIVE_ROOT = "Data/archives/"
|
43 |
+
_DATA_URL = {
|
44 |
+
"test": _DATA_ARCHIVE_ROOT + "mgb2_wav.test.zip",
|
45 |
+
"dev": _DATA_ARCHIVE_ROOT + "mgb2_wav.dev.zip",
|
46 |
+
"train": _DATA_ARCHIVE_ROOT + "mgb2_wav.train.zip",
|
47 |
+
|
48 |
+
#"train": [_DATA_ARCHIVE_ROOT + f"mgb2_wav_{x}.train.tar.gz" for x in range(48)], # we have 48 archives
|
49 |
+
}
|
50 |
+
_TEXT_URL = {
|
51 |
+
"test": _DATA_ARCHIVE_ROOT + "mgb2_txt.test.zip",
|
52 |
+
"dev": _DATA_ARCHIVE_ROOT + "mgb2_txt.dev.zip",
|
53 |
+
"train": _DATA_ARCHIVE_ROOT + "mgb2_txt.train.zip",
|
54 |
+
}
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
def absoluteFilePaths(directory):
|
59 |
+
for dirpath,_,filenames in os.walk(directory):
|
60 |
+
for f in filenames:
|
61 |
+
yield os.path.abspath(os.path.join(dirpath, f))
|
62 |
+
|
63 |
+
|
64 |
+
class MGDB2Dataset(datasets.GeneratorBasedBuilder):
|
65 |
+
def _info(self):
|
66 |
+
return datasets.DatasetInfo(
|
67 |
+
description=_DESCRIPTION,
|
68 |
+
features=datasets.Features(
|
69 |
+
{
|
70 |
+
"path": datasets.Value("string"),
|
71 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
72 |
+
"sentence": datasets.Value("string"),
|
73 |
+
}
|
74 |
+
),
|
75 |
+
supervised_keys=None,
|
76 |
+
homepage=_HOMEPAGE,
|
77 |
+
license=_LICENSE,
|
78 |
+
citation=_CITATION,
|
79 |
+
)
|
80 |
+
|
81 |
+
def _split_generators(self, dl_manager):
|
82 |
+
wav_archive = dl_manager.download(_DATA_URL)
|
83 |
+
txt_archive = dl_manager.download(_TEXT_URL)
|
84 |
+
test_dir = "dataset/test"
|
85 |
+
dev_dir = "dataset/dev"
|
86 |
+
train_dir = "dataset/train"
|
87 |
+
|
88 |
+
|
89 |
+
print("Starting write datasets.........................................................")
|
90 |
+
|
91 |
+
|
92 |
+
if dl_manager.is_streaming:
|
93 |
+
print("from streaming.........................................................")
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
return [
|
99 |
+
datasets.SplitGenerator(
|
100 |
+
name=datasets.Split.TEST,
|
101 |
+
gen_kwargs={
|
102 |
+
"path_to_txt": test_dir + "/txt",
|
103 |
+
"path_to_wav": test_dir + "/wav",
|
104 |
+
"wav_files": dl_manager.iter_archive(wav_archive['test']),
|
105 |
+
"txt_files": dl_manager.iter_archive(txt_archive['test']),
|
106 |
+
},
|
107 |
+
),
|
108 |
+
datasets.SplitGenerator(
|
109 |
+
name=datasets.Split.VALIDATION,
|
110 |
+
gen_kwargs={
|
111 |
+
"path_to_txt": dev_dir + "/txt",
|
112 |
+
"path_to_wav": dev_dir + "/wav",
|
113 |
+
"wav_files": dl_manager.iter_archive(wav_archive['dev']),
|
114 |
+
"txt_files": dl_manager.iter_archive(txt_archive['dev']),
|
115 |
+
},
|
116 |
+
),
|
117 |
+
datasets.SplitGenerator(
|
118 |
+
name=datasets.Split.TRAIN,
|
119 |
+
gen_kwargs={
|
120 |
+
"path_to_txt": train_dir + "/txt",
|
121 |
+
"path_to_wav": train_dir + "/wav",
|
122 |
+
"wav_files": dl_manager.iter_archive(wav_archive['train']),
|
123 |
+
"txt_files": dl_manager.iter_archive(txt_archive['train']),
|
124 |
+
},
|
125 |
+
),
|
126 |
+
]
|
127 |
+
else:
|
128 |
+
print("from non streaming.........................................................")
|
129 |
+
|
130 |
+
|
131 |
+
dstZipFileName=txt_archive['test']
|
132 |
+
|
133 |
+
sz=os.path.getsize(dstZipFileName)
|
134 |
+
|
135 |
+
print("file size=",sz)
|
136 |
+
|
137 |
+
|
138 |
+
#test_txt_files=dl_manager.extract(txt_archive['test']);
|
139 |
+
|
140 |
+
#flist=os.listdir(test_txt_files)
|
141 |
+
|
142 |
+
#print(flist)
|
143 |
+
|
144 |
+
#f = open(test_txt_files, 'r')
|
145 |
+
#file_contents = f.read()
|
146 |
+
#print (file_contents)
|
147 |
+
#f.close()
|
148 |
+
|
149 |
+
return [
|
150 |
+
datasets.SplitGenerator(
|
151 |
+
name=datasets.Split.TEST,
|
152 |
+
gen_kwargs={
|
153 |
+
"path_to_txt": test_dir + "/txt",
|
154 |
+
"path_to_wav": test_dir + "/wav",
|
155 |
+
"wav_files": absoluteFilePaths(dl_manager.extract(wav_archive['test'])),
|
156 |
+
"txt_files": absoluteFilePaths(dl_manager.extract(txt_archive['test'])),
|
157 |
+
"data_type":2,
|
158 |
+
},
|
159 |
+
),
|
160 |
+
datasets.SplitGenerator(
|
161 |
+
name=datasets.Split.VALIDATION,
|
162 |
+
gen_kwargs={
|
163 |
+
"path_to_txt": dev_dir + "/txt",
|
164 |
+
"path_to_wav": dev_dir + "/wav",
|
165 |
+
"wav_files": absoluteFilePaths(dl_manager.extract(wav_archive['dev'])),
|
166 |
+
"txt_files": absoluteFilePaths(dl_manager.extract(txt_archive['dev'])),
|
167 |
+
"data_type":1,
|
168 |
+
},
|
169 |
+
),
|
170 |
+
datasets.SplitGenerator(
|
171 |
+
name=datasets.Split.TRAIN,
|
172 |
+
gen_kwargs={
|
173 |
+
"path_to_txt": train_dir + "/txt",
|
174 |
+
"path_to_wav": train_dir + "/wav",
|
175 |
+
"wav_files": absoluteFilePaths(dl_manager.extract(wav_archive['train'])),
|
176 |
+
"txt_files": absoluteFilePaths(dl_manager.extract(txt_archive['train'])),
|
177 |
+
"data_type":0,
|
178 |
+
},
|
179 |
+
),
|
180 |
+
]
|
181 |
+
print("end of generation.........................................................")
|
182 |
+
|
183 |
+
|
184 |
+
#0 --> train
|
185 |
+
#1--> validation
|
186 |
+
#2-->test
|
187 |
+
|
188 |
+
def _generate_examples(self, path_to_txt, path_to_wav, wav_files, txt_files,data_type):
|
189 |
+
"""
|
190 |
+
This assumes that the text directory alphabetically precedes the wav dir
|
191 |
+
The file names for wav and text seem to match and are unique
|
192 |
+
We can use them for the dictionary matching them
|
193 |
+
"""
|
194 |
+
|
195 |
+
print("start of generate examples.........................................................")
|
196 |
+
|
197 |
+
print("txt file names............................",txt_files)
|
198 |
+
print("wav_files names....................................",wav_files)
|
199 |
+
|
200 |
+
examples = {}
|
201 |
+
id_ = 0
|
202 |
+
# need to prepare the transcript - wave map
|
203 |
+
for item in txt_files:
|
204 |
+
|
205 |
+
|
206 |
+
#print("copying txt file...............",item)
|
207 |
+
|
208 |
+
if type(item) is tuple:
|
209 |
+
# iter_archive will return path and file
|
210 |
+
path, f = item
|
211 |
+
txt = f.read().decode(encoding="utf-8").strip()
|
212 |
+
else:
|
213 |
+
# extract will return path only
|
214 |
+
path = item
|
215 |
+
with open(path, encoding="utf-8") as f:
|
216 |
+
txt = f.read().strip()
|
217 |
+
|
218 |
+
#if os.path.exists(path_to_txt)==False:
|
219 |
+
# os.makedirs(path_to_txt)
|
220 |
+
#if path.find(path_to_txt) > -1:
|
221 |
+
# construct the wav path
|
222 |
+
# which is used as an identifier
|
223 |
+
wav_path = os.path.split(path)[1].replace("_utf8", "").replace(".txt", ".wav").strip()
|
224 |
+
#print(wav_path)
|
225 |
+
examples[wav_path] = {
|
226 |
+
"sentence": txt,
|
227 |
+
"path": wav_path,
|
228 |
+
}
|
229 |
+
|
230 |
+
#for wf in wav_files:
|
231 |
+
#print(wf)
|
232 |
+
for item in wav_files:#wf:
|
233 |
+
#print(item)
|
234 |
+
if type(item) is tuple:
|
235 |
+
path, f = item
|
236 |
+
wav_data = f.read()
|
237 |
+
else:
|
238 |
+
path = item
|
239 |
+
with open(path, "rb") as f:
|
240 |
+
wav_data = f.read()
|
241 |
+
#if os.path.exists(path_to_wav)==False:
|
242 |
+
# os.makedirs(path_to_wav)
|
243 |
+
#if path.find(path_to_wav) > -1:
|
244 |
+
wav_path = os.path.split(path)[1].strip()
|
245 |
+
if not (wav_path in examples):
|
246 |
+
print("wav file mismatch:",wav_path)
|
247 |
+
continue
|
248 |
+
audio = {"path": path, "bytes": wav_data}
|
249 |
+
yield id_, {**examples[wav_path], "audio": audio}
|
250 |
+
id_ += 1
|