RitchieP commited on
Commit
0e739cb
·
verified ·
1 Parent(s): 25b264a

Upload VerbaLex_voice.py

Browse files
Files changed (1) hide show
  1. VerbaLex_voice.py +134 -0
VerbaLex_voice.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import os
3
+
4
+ import datasets
5
+ from tqdm import tqdm
6
+
7
+ from VerbaLex_Voice.accents import ACCENTS
8
+ from VerbaLex_Voice.release_stats import STATS
9
+
10
+ _HOMEPAGE = "https://huggingface.co/datasets/RitchieP/VerbaLex_voice"
11
+
12
+ _LICENSE = "https://choosealicense.com/licenses/apache-2.0/"
13
+
14
+ _BASE_URL = "https://huggingface.co/datasets/RitchieP/VerbaLex_voice/tree/main"
15
+
16
+ _AUDIO_URL = _BASE_URL + "audio/{accent}/{split}/{accent}_{split}.tar"
17
+
18
+ _TRANSCRIPT_URL = _BASE_URL + "transcript/{accent}/{split}.tsv"
19
+
20
+ _CITATION = """\
21
+ """
22
+
23
+
24
+ class VerbaLexVoiceConfig(datasets.BuilderConfig):
25
+ def __init__(self, name, version, **kwargs):
26
+ self.accent = kwargs.pop("accent", None)
27
+ self.num_speakers = kwargs.pop("num_speakers", None)
28
+ self.num_files = kwargs.pop("num_clips", None)
29
+ description = (
30
+ f"VerbaLex Voice english speech-to-text dataset in {self.accent} accent."
31
+ )
32
+
33
+ super(VerbaLexVoiceConfig, self).__init__(
34
+ name=name,
35
+ version=datasets.Version(version),
36
+ description=description,
37
+ **kwargs,
38
+ )
39
+
40
+
41
+ class VerbaLexVoiceDataset(datasets.GeneratorBasedBuilder):
42
+ """
43
+ VerbaLex is a dataset containing different English accents from non-native English speakers.
44
+ This dataset is created directly from the L2-Arctic dataset.
45
+ """
46
+ BUILDER_CONFIGS = [
47
+ VerbaLexVoiceConfig(
48
+ name=accent,
49
+ version=STATS["version"],
50
+ accent=ACCENTS[accent],
51
+ num_speakers=accent_stats["numOfSpeaker"],
52
+ num_files=accent_stats["numOfWavFiles"]
53
+ )
54
+ for accent, accent_stats in STATS["accents"].items()
55
+ ]
56
+
57
+ DEFAULT_CONFIG_NAME = "all"
58
+
59
+ def _info(self):
60
+ return datasets.DatasetInfo(
61
+ description=(
62
+ "VerbaLex Voice is a speech dataset focusing on accented English speech."
63
+ "It specifically targets speeches from speakers that is a non-native English speaker."
64
+ ),
65
+ features=datasets.Features(
66
+ {
67
+ "path": datasets.Value("string"),
68
+ "accent": datasets.Value("string"),
69
+ "sentence": datasets.Value("string"),
70
+ "audio": datasets.Audio(sampling_rate=44_100)
71
+ }
72
+ ),
73
+ supervised_keys=None,
74
+ homepage=_HOMEPAGE,
75
+ license=_LICENSE,
76
+ citation=_CITATION
77
+ )
78
+
79
+ def _split_generators(self, dl_manager):
80
+ """Returns SplitGenerators"""
81
+ accent = self.config.name
82
+
83
+ splits = ("train", "test")
84
+ audio_urls = {}
85
+ for split in splits:
86
+ audio_urls[split] = _AUDIO_URL.format(accent=accent, split=split)
87
+ archive_paths = dl_manager.download(audio_urls)
88
+ local_extracted_archive_paths = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else {}
89
+
90
+ meta_urls = {split: _TRANSCRIPT_URL.format(accent=accent, split=split) for split in splits}
91
+ meta_paths = dl_manager.download_and_extract(meta_urls)
92
+
93
+ split_names = {
94
+ "train": datasets.Split.TRAIN,
95
+ "test": datasets.Split.TEST
96
+ }
97
+ split_generators = []
98
+ for split in splits:
99
+ split_generators.append(
100
+ datasets.SplitGenerator(
101
+ name=split_names.get(split, split),
102
+ gen_kwargs={
103
+ "local_extracted_archive_paths": local_extracted_archive_paths.get(split),
104
+ "archives": [dl_manager.iter_archive(path) for path in archive_paths.get(split)],
105
+ "meta_path": meta_paths[split]
106
+ }
107
+ )
108
+ )
109
+
110
+ return split_generators
111
+
112
+ def _generate_examples(self, local_extracted_archive_paths, archives, meta_path):
113
+ data_fields = list(self._info().features.keys())
114
+ metadata = {}
115
+ with open(meta_path, encoding="UTF-8") as f:
116
+ reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
117
+ for row in tqdm(reader, desc="Reading metadata..."):
118
+ if not row["path"].endswith(".wav"):
119
+ row["path"] += ".wav"
120
+ for field in data_fields:
121
+ if field not in row:
122
+ row[field] = ""
123
+ metadata[row["path"]] = row
124
+
125
+ for i, audio_archive in enumerate(archives):
126
+ for path, file in audio_archive:
127
+ _, filename = os.path.split(path)
128
+ if filename in metadata:
129
+ result = dict(metadata[filename])
130
+ path = os.path.join(local_extracted_archive_paths[i],
131
+ path) if local_extracted_archive_paths else path
132
+ result["audio"] = {"path": path, "bytes": file.read()}
133
+ result["path"] = path
134
+ yield path, result