Ericwang commited on
Commit
706a2b6
1 Parent(s): ea45383

Upload samromur_children_edited.py

Browse files
Files changed (1) hide show
  1. samromur_children_edited.py +166 -0
samromur_children_edited.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import defaultdict
2
+ import os
3
+ import json
4
+ import csv
5
+
6
+ import datasets
7
+
8
+ import torchaudio
9
+ import warnings
10
+
11
+ _NAME="samromur_children"
12
+ _VERSION="1.0.0"
13
+ _AUDIO_EXTENSIONS=".flac"
14
+
15
+ _DESCRIPTION = """
16
+ The Samrómur Children corpus contains more than 137000 validated speech-recordings uttered by Icelandic children.
17
+ """
18
+
19
+ _CITATION = """
20
+ @misc{menasamromurchildren2022,
21
+ title={Samrómur Children Icelandic Speech 1.0},
22
+ ldc_catalog_no={LDC2022S11},
23
+ DOI={https://doi.org/10.35111/frrj-qd60},
24
+ author={Hernández Mena, Carlos Daniel and Borsky, Michal and Mollberg, David Erik and Guðmundsson, Smári Freyr and Hedström, Staffan and Pálsson, Ragnar and Jónsson, Ólafur Helgi and Þorsteinsdóttir, Sunneva and Guðmundsdóttir, Jóhanna Vigdís and Magnúsdóttir, Eydís Huld and Þórhallsdóttir, Ragnheiður and Guðnason, Jón},
25
+ publisher={Reykjavík University}
26
+ journal={Linguistic Data Consortium, Philadelphia},
27
+ year={2019},
28
+ url={https://catalog.ldc.upenn.edu/LDC2022S11},
29
+ }
30
+ """
31
+
32
+ _HOMEPAGE = "https://catalog.ldc.upenn.edu/LDC2022S11"
33
+
34
+ _LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/"
35
+
36
+ _BASE_DATA_DIR = "corpus/"
37
+ _METADATA_TRAIN = os.path.join(_BASE_DATA_DIR,"files","metadata_train.tsv")
38
+ _METADATA_TEST = os.path.join(_BASE_DATA_DIR,"files", "metadata_test.tsv")
39
+ _METADATA_DEV = os.path.join(_BASE_DATA_DIR,"files", "metadata_dev.tsv")
40
+
41
+ _TARS_TRAIN = os.path.join(_BASE_DATA_DIR,"files","tars_train.paths")
42
+ _TARS_TEST = os.path.join(_BASE_DATA_DIR,"files", "tars_test.paths")
43
+ _TARS_DEV = os.path.join(_BASE_DATA_DIR,"files", "tars_dev.paths")
44
+
45
+ class SamromurChildrenConfig(datasets.BuilderConfig):
46
+ """BuilderConfig for Samromur Children"""
47
+
48
+ def __init__(self, name, **kwargs):
49
+ name=_NAME
50
+ super().__init__(name=name, **kwargs)
51
+
52
+ class SamromurChildren(datasets.GeneratorBasedBuilder):
53
+ """Samrómur Children Icelandic Speech 1.0"""
54
+
55
+ VERSION = datasets.Version(_VERSION)
56
+ BUILDER_CONFIGS = [
57
+ SamromurChildrenConfig(
58
+ name=_NAME,
59
+ version=datasets.Version(_VERSION),
60
+ )
61
+ ]
62
+
63
+ def _info(self):
64
+ features = datasets.Features(
65
+ {
66
+ "audio_id": datasets.Value("string"),
67
+ "audio": datasets.Audio(sampling_rate=16000),
68
+ "speaker_id": datasets.Value("string"),
69
+ "gender": datasets.Value("string"),
70
+ "age": datasets.Value("string"),
71
+ "duration": datasets.Value("float32"),
72
+ "normalized_text": datasets.Value("string"),
73
+ }
74
+ )
75
+ return datasets.DatasetInfo(
76
+ description=_DESCRIPTION,
77
+ features=features,
78
+ homepage=_HOMEPAGE,
79
+ license=_LICENSE,
80
+ citation=_CITATION,
81
+ )
82
+
83
+ def _split_generators(self, dl_manager):
84
+
85
+ metadata_train=dl_manager.download_and_extract(_METADATA_TRAIN)
86
+ metadata_test=dl_manager.download_and_extract(_METADATA_TEST)
87
+ metadata_dev=dl_manager.download_and_extract(_METADATA_DEV)
88
+
89
+ tars_train=dl_manager.download_and_extract(_TARS_TRAIN)
90
+ tars_test=dl_manager.download_and_extract(_TARS_TEST)
91
+ tars_dev=dl_manager.download_and_extract(_TARS_DEV)
92
+
93
+ hash_tar_files=defaultdict(dict)
94
+ with open(tars_train,'r') as f:
95
+ hash_tar_files['train']=[path.replace('\n','') for path in f]
96
+
97
+ with open(tars_test,'r') as f:
98
+ hash_tar_files['test']=[path.replace('\n','') for path in f]
99
+
100
+ with open(tars_dev,'r') as f:
101
+ hash_tar_files['dev']=[path.replace('\n','') for path in f]
102
+
103
+ hash_meta_paths={"train":metadata_train,"test":metadata_test,"dev":metadata_dev}
104
+ audio_paths = dl_manager.download(hash_tar_files)
105
+
106
+ splits=["train","dev","test"]
107
+ local_extracted_audio_paths = (
108
+ dl_manager.extract(audio_paths) if not dl_manager.is_streaming else
109
+ {
110
+ split:[None] * len(audio_paths[split]) for split in splits
111
+ }
112
+ )
113
+
114
+ return [
115
+ datasets.SplitGenerator(
116
+ name=datasets.Split.TRAIN,
117
+ gen_kwargs={
118
+ "audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["train"]],
119
+ "local_extracted_archives_paths": local_extracted_audio_paths["train"],
120
+ "metadata_paths": hash_meta_paths["train"],
121
+ }
122
+ ),
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.VALIDATION,
125
+ gen_kwargs={
126
+ "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["dev"]],
127
+ "local_extracted_archives_paths": local_extracted_audio_paths["dev"],
128
+ "metadata_paths": hash_meta_paths["dev"],
129
+ }
130
+ ),
131
+ datasets.SplitGenerator(
132
+ name=datasets.Split.TEST,
133
+ gen_kwargs={
134
+ "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["test"]],
135
+ "local_extracted_archives_paths": local_extracted_audio_paths["test"],
136
+ "metadata_paths": hash_meta_paths["test"],
137
+ }
138
+ ),
139
+ ]
140
+
141
+ def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
142
+
143
+ features = ["speaker_id","gender","age","duration","normalized_text"]
144
+
145
+ with open(metadata_paths) as f:
146
+ metadata = {x["audio_id"]: x for x in csv.DictReader(f, delimiter="\t")}
147
+
148
+ for audio_archive, local_extracted_archive_path in zip(audio_archives, local_extracted_archives_paths):
149
+ for audio_filename, audio_file in audio_archive:
150
+ #audio_id = audio_filename.split(os.sep)[-1].split(_AUDIO_EXTENSIONS)[0]
151
+ audio_id =os.path.splitext(os.path.basename(audio_filename))[0]
152
+ path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path else audio_filename
153
+
154
+ # Load the audio file using torchaudio
155
+ waveform, sample_rate = torchaudio.load(path)
156
+
157
+ # Check if the waveform is empty
158
+ if waveform.numel() == 0:
159
+ warnings.warn(f"Empty audio file: {audio_id}")
160
+ continue
161
+
162
+ yield audio_id, {
163
+ "audio_id": audio_id,
164
+ **{feature: metadata[audio_id][feature] for feature in features},
165
+ "audio": {"path": path, "bytes": audio_file.read()},
166
+ }