birgermoell
commited on
Commit
•
598246f
1
Parent(s):
ff4dbcd
Updated script
Browse files- speechdat.py +129 -0
speechdat.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2021 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""Speech Dat dataset"""
|
16 |
+
|
17 |
+
import datasets
|
18 |
+
import json
|
19 |
+
import os
|
20 |
+
from datasets.tasks import AutomaticSpeechRecognition
|
21 |
+
from pathlib import Path
|
22 |
+
|
23 |
+
_DESCRIPTION = """\
|
24 |
+
Speechdat dataset
|
25 |
+
"""
|
26 |
+
|
27 |
+
_HOMEPAGE = ""
|
28 |
+
|
29 |
+
_LICENSE = ""
|
30 |
+
|
31 |
+
class SpeechDatConfig(datasets.BuilderConfig):
|
32 |
+
"""BuilderConfig for CommonVoice."""
|
33 |
+
|
34 |
+
def __init__(self, name, **kwargs):
|
35 |
+
"""
|
36 |
+
Args:
|
37 |
+
data_dir: `string`, the path to the folder containing the files in the
|
38 |
+
downloaded .tar
|
39 |
+
citation: `string`, citation for the data set
|
40 |
+
url: `string`, url for information about the data set
|
41 |
+
**kwargs: keyword arguments forwarded to super.
|
42 |
+
"""
|
43 |
+
|
44 |
+
self.date_of_snapshot = kwargs.pop("date", None)
|
45 |
+
self.size = kwargs.pop("size", None)
|
46 |
+
description = f"Speech Dat dataset"
|
47 |
+
super(SpeechDatConfig, self).__init__(
|
48 |
+
name=name, **kwargs
|
49 |
+
)
|
50 |
+
|
51 |
+
|
52 |
+
class SpeechDat(datasets.GeneratorBasedBuilder):
|
53 |
+
|
54 |
+
DEFAULT_WRITER_BATCH_SIZE = 1000
|
55 |
+
BUILDER_CONFIGS = [
|
56 |
+
SpeechDatConfig(
|
57 |
+
name="SpeechDat",
|
58 |
+
)
|
59 |
+
]
|
60 |
+
|
61 |
+
def _info(self):
|
62 |
+
features = datasets.Features(
|
63 |
+
{
|
64 |
+
"path": datasets.Value("string"),
|
65 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
66 |
+
"sentence": datasets.Value("string"),
|
67 |
+
}
|
68 |
+
)
|
69 |
+
|
70 |
+
return datasets.DatasetInfo(
|
71 |
+
description=_DESCRIPTION,
|
72 |
+
features=features,
|
73 |
+
supervised_keys=None,
|
74 |
+
homepage=_HOMEPAGE,
|
75 |
+
license=_LICENSE,
|
76 |
+
task_templates=[
|
77 |
+
AutomaticSpeechRecognition(audio_file_path_column="path", transcription_column="sentence")
|
78 |
+
],
|
79 |
+
)
|
80 |
+
|
81 |
+
def _split_generators(self, dl_manager):
|
82 |
+
"""Returns SplitGenerators."""
|
83 |
+
|
84 |
+
manual_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
|
85 |
+
path_to_data = "/".join(["wav"])
|
86 |
+
|
87 |
+
return [
|
88 |
+
datasets.SplitGenerator(
|
89 |
+
name=datasets.Split.TRAIN,
|
90 |
+
gen_kwargs={
|
91 |
+
"data_dir": manual_dir
|
92 |
+
},
|
93 |
+
)
|
94 |
+
]
|
95 |
+
|
96 |
+
def _generate_examples(self, data_dir):
|
97 |
+
"""Yields examples."""
|
98 |
+
data_fields = list(self._info().features.keys())
|
99 |
+
|
100 |
+
def get_single_line(path):
|
101 |
+
lines = []
|
102 |
+
with open(path, 'r', encoding="utf-8") as f:
|
103 |
+
for line in f:
|
104 |
+
line = line.strip()
|
105 |
+
lines.append(line)
|
106 |
+
if len(lines) == 1:
|
107 |
+
return lines[0]
|
108 |
+
elif len(lines) == 0:
|
109 |
+
return None
|
110 |
+
else:
|
111 |
+
return " ".join(lines)
|
112 |
+
|
113 |
+
data_path = Path(data_dir)
|
114 |
+
for wav_file in data_path.glob("*.wav"):
|
115 |
+
text_file = Path(str(wav_file).replace(".wav", ".svo"))
|
116 |
+
if not text_file.is_file():
|
117 |
+
continue
|
118 |
+
text_line = get_single_line(text_file)
|
119 |
+
if text_line is None or text_line == "":
|
120 |
+
continue
|
121 |
+
with open(wav_file, "rb") as wav_data:
|
122 |
+
yield str(wav_file), {
|
123 |
+
"path": str(wav_file),
|
124 |
+
"sentence": text_line,
|
125 |
+
"audio": {
|
126 |
+
"path": str(wav_file),
|
127 |
+
"bytes": wav_data.read()
|
128 |
+
}
|
129 |
+
}
|