carlosdanielhernandezmena commited on
Commit
07badb9
1 Parent(s): 7f6427a

Upload ravnursson_asr.py

Browse files
Files changed (1) hide show
  1. ravnursson_asr.py +151 -0
ravnursson_asr.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import defaultdict
2
+ import os
3
+ import json
4
+ import csv
5
+
6
+ import datasets
7
+
8
+ _NAME="ravnursson_asr"
9
+ _VERSION="1.0.0"
10
+ _AUDIO_EXTENSIONS=".flac"
11
+
12
+ _DESCRIPTION = """
13
+ The corpus \"RAVNURSSON FAROESE SPEECH AND TRANSCRIPTS\" (or RAVNURSSON Corpus for short) is a collection of speech recordings with transcriptions intended for Automatic Speech Recognition (ASR) applications in the language that is spoken at the Faroe Islands (Faroese). It was curated at the Reykjavík University (RU) in 2022.
14
+ """
15
+
16
+ _CITATION = """
17
+ @misc{carlosmenaravnursson2022,
18
+ title={Ravnursson Faroese Speech and Transcripts},
19
+ author={Hernandez Mena, Carlos Daniel and Simonsen, Annika},
20
+ year={2022},
21
+ url={http://hdl.handle.net/20.500.12537/276},
22
+ }
23
+ """
24
+
25
+ _HOMEPAGE = "http://hdl.handle.net/20.500.12537/276"
26
+
27
+ _LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/"
28
+
29
+ _BASE_DATA_DIR = "corpus/"
30
+ _METADATA_TRAIN = os.path.join(_BASE_DATA_DIR,"files","metadata_train.tsv")
31
+ _METADATA_TEST = os.path.join(_BASE_DATA_DIR,"files", "metadata_test.tsv")
32
+ _METADATA_DEV = os.path.join(_BASE_DATA_DIR,"files", "metadata_dev.tsv")
33
+
34
+ _TARS_TRAIN = os.path.join(_BASE_DATA_DIR,"files","tars_train.paths")
35
+ _TARS_TEST = os.path.join(_BASE_DATA_DIR,"files", "tars_test.paths")
36
+ _TARS_DEV = os.path.join(_BASE_DATA_DIR,"files", "tars_dev.paths")
37
+
38
+ class RavnurssonAsrConfig(datasets.BuilderConfig):
39
+ """BuilderConfig for Ravnursson Corpus"""
40
+
41
+ def __init__(self, name, **kwargs):
42
+ name=_NAME
43
+ super().__init__(name=name, **kwargs)
44
+
45
+ class RavnurssonAsr(datasets.GeneratorBasedBuilder):
46
+ """Ravnursson Faroese Speech and Transcripts"""
47
+
48
+ VERSION = datasets.Version(_VERSION)
49
+ BUILDER_CONFIGS = [
50
+ RavnurssonAsrConfig(
51
+ name=_NAME,
52
+ version=datasets.Version(_VERSION),
53
+ )
54
+ ]
55
+
56
+ def _info(self):
57
+ features = datasets.Features(
58
+ {
59
+ "audio_id": datasets.Value("string"),
60
+ "audio": datasets.Audio(sampling_rate=16000),
61
+ "speaker_id": datasets.Value("string"),
62
+ "gender": datasets.Value("string"),
63
+ "age": datasets.Value("string"),
64
+ "duration": datasets.Value("float32"),
65
+ "normalized_text": datasets.Value("string"),
66
+ "dialect": datasets.Value("string"),
67
+ }
68
+ )
69
+ return datasets.DatasetInfo(
70
+ description=_DESCRIPTION,
71
+ features=features,
72
+ homepage=_HOMEPAGE,
73
+ license=_LICENSE,
74
+ citation=_CITATION,
75
+ )
76
+
77
+ def _split_generators(self, dl_manager):
78
+
79
+ metadata_train=dl_manager.download_and_extract(_METADATA_TRAIN)
80
+ metadata_test=dl_manager.download_and_extract(_METADATA_TEST)
81
+ metadata_dev=dl_manager.download_and_extract(_METADATA_DEV)
82
+
83
+ tars_train=dl_manager.download_and_extract(_TARS_TRAIN)
84
+ tars_test=dl_manager.download_and_extract(_TARS_TEST)
85
+ tars_dev=dl_manager.download_and_extract(_TARS_DEV)
86
+
87
+ hash_tar_files=defaultdict(dict)
88
+ with open(tars_train,'r') as f:
89
+ hash_tar_files['train']=[path.replace('\n','') for path in f]
90
+
91
+ with open(tars_test,'r') as f:
92
+ hash_tar_files['test']=[path.replace('\n','') for path in f]
93
+
94
+ with open(tars_dev,'r') as f:
95
+ hash_tar_files['dev']=[path.replace('\n','') for path in f]
96
+
97
+ hash_meta_paths={"train":metadata_train,"test":metadata_test,"dev":metadata_dev}
98
+ audio_paths = dl_manager.download(hash_tar_files)
99
+
100
+ splits=["train","dev","test"]
101
+ local_extracted_audio_paths = (
102
+ dl_manager.extract(audio_paths) if not dl_manager.is_streaming else
103
+ {
104
+ split:[None] * len(audio_paths[split]) for split in splits
105
+ }
106
+ )
107
+
108
+ return [
109
+ datasets.SplitGenerator(
110
+ name=datasets.Split.TRAIN,
111
+ gen_kwargs={
112
+ "audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["train"]],
113
+ "local_extracted_archives_paths": local_extracted_audio_paths["train"],
114
+ "metadata_paths": hash_meta_paths["train"],
115
+ }
116
+ ),
117
+ datasets.SplitGenerator(
118
+ name=datasets.Split.VALIDATION,
119
+ gen_kwargs={
120
+ "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["dev"]],
121
+ "local_extracted_archives_paths": local_extracted_audio_paths["dev"],
122
+ "metadata_paths": hash_meta_paths["dev"],
123
+ }
124
+ ),
125
+ datasets.SplitGenerator(
126
+ name=datasets.Split.TEST,
127
+ gen_kwargs={
128
+ "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["test"]],
129
+ "local_extracted_archives_paths": local_extracted_audio_paths["test"],
130
+ "metadata_paths": hash_meta_paths["test"],
131
+ }
132
+ ),
133
+ ]
134
+
135
+ def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
136
+
137
+ features = ["speaker_id","gender","age","duration","normalized_text","dialect"]
138
+
139
+ with open(metadata_paths) as f:
140
+ metadata = {x["audio_id"]: x for x in csv.DictReader(f, delimiter="\t")}
141
+
142
+ for audio_archive, local_extracted_archive_path in zip(audio_archives, local_extracted_archives_paths):
143
+ for audio_filename, audio_file in audio_archive:
144
+ audio_id = audio_filename.split(os.sep)[-1].split(_AUDIO_EXTENSIONS)[0]
145
+ path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path else audio_filename
146
+
147
+ yield audio_id, {
148
+ "audio_id": audio_id,
149
+ **{feature: metadata[audio_id][feature] for feature in features},
150
+ "audio": {"path": path, "bytes": audio_file.read()},
151
+ }