holylovenia commited on
Commit
a7225ff
1 Parent(s): 02a79ba

Upload indspeech_news_ethnicsr.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. indspeech_news_ethnicsr.py +213 -0
indspeech_news_ethnicsr.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from typing import Dict, List, Tuple
3
+
4
+ import datasets
5
+ import json
6
+ import os
7
+
8
+ from nusacrowd.utils import schemas
9
+ from nusacrowd.utils.configs import NusantaraConfig
10
+ from nusacrowd.utils.constants import Tasks
11
+ from zipfile import ZipFile
12
+
13
+ _CITATION = """\
14
+ @inproceedings{sani-cocosda-2012,
15
+ title = "Towards Language Preservation: Preliminary Collection and Vowel Analysis of {I}ndonesian Ethnic Speech Data",
16
+ author = "Sani, Auliya and Sakti, Sakriani and Neubig, Graham and Toda, Tomoki and Mulyanto, Adi and Nakamura, Satoshi",
17
+ booktitle = "Proc. Oriental COCOSDA",
18
+ year = "2012",
19
+ pages = "118--122"
20
+ address = "Macau, China"
21
+ }
22
+ """
23
+
24
+ _LOCAL = False
25
+ _LANGUAGES = ["sun", "jav"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
26
+ _DATASETNAME = "indspeech_news_ethnicsr"
27
+
28
+ _DESCRIPTION = """
29
+ INDspeech_NEWS_EthnicSR is a collection of Indonesian ethnic speech corpora for Javanese and Sundanese for Indonesian ethnic speech recognition. It was developed in 2012 by the Nara Institute of Science and Technology (NAIST, Japan) in collaboration with the Bandung Institute of Technology (ITB, Indonesia) [Sani et al., 2012].
30
+ """
31
+
32
+ _HOMEPAGE = "https://github.com/s-sakti/data_indsp_news_ethnicsr"
33
+
34
+ _LICENSE = "CC-BY-NC-SA 4.0"
35
+
36
+ _URLS = {
37
+ _DATASETNAME: "https://github.com/s-sakti/data_indsp_news_ethnicsr/archive/refs/heads/main.zip",
38
+ }
39
+
40
+ _SUPPORTED_TASKS = [Tasks.SPEECH_RECOGNITION]
41
+ _SOURCE_VERSION = "1.0.0"
42
+ _NUSANTARA_VERSION = "1.0.0"
43
+
44
+
45
+ class IndSpeechNewsEthnicSR(datasets.GeneratorBasedBuilder):
46
+ """INDspeech_NEWS_EthnicSR is a collection of Indonesian ethnic speech corpora for Javanese and Sundanese for Indonesian ethnic speech recognition. It was developed in 2012 by the Nara Institute of Science and Technology (NAIST, Japan) in collaboration with the Bandung Institute of Technology (ITB, Indonesia) [Sani et al., 2012]."""
47
+
48
+
49
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
50
+ NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)
51
+
52
+ BUILDER_CONFIGS = []
53
+
54
+ for fold_id in ["overlap", "nooverlap"]:
55
+ for fold_name in ['jv', "su"]:
56
+ BUILDER_CONFIGS.extend(
57
+ [NusantaraConfig(
58
+ name=f"indspeech_news_ethnicsr_{fold_name}_{fold_id}_source",
59
+ version=_SOURCE_VERSION,
60
+ description="indspeech_news_ethnicsr source schema",
61
+ schema="source",
62
+ subset_id=f"indspeech_news_ethnicsr_{fold_name}_{fold_id}"
63
+ ),
64
+ NusantaraConfig(
65
+ name=f"indspeech_news_ethnicsr_{fold_name}_{fold_id}_nusantara_sptext",
66
+ version=_SOURCE_VERSION,
67
+ description="indspeech_news_ethnicsr Nusantara schema",
68
+ schema="nusantara_sptext",
69
+ subset_id=f"indspeech_news_ethnicsr_{fold_name}_{fold_id}"
70
+ ),]
71
+ )
72
+
73
+ DEFAULT_CONFIG_NAME = "indspeech_news_ethnicsr_jv_nooverlap_source"
74
+
75
+ def _info(self) -> datasets.DatasetInfo:
76
+
77
+ if self.config.schema == "source":
78
+
79
+ features = datasets.Features(
80
+ {
81
+ "id": datasets.Value("string"),
82
+ "speaker_id": datasets.Value("string"),
83
+ "path": datasets.Value("string"),
84
+ "audio": datasets.Audio(sampling_rate=16_000),
85
+ "text": datasets.Value("string"),
86
+ }
87
+ )
88
+
89
+ elif self.config.schema == "nusantara_sptext":
90
+ features = schemas.speech_text_features
91
+
92
+ return datasets.DatasetInfo(
93
+ description=_DESCRIPTION,
94
+ features=features,
95
+ homepage=_HOMEPAGE,
96
+ license=_LICENSE,
97
+ citation=_CITATION,
98
+ task_templates=[datasets.AutomaticSpeechRecognition(audio_column="audio", transcription_column="text")],
99
+ )
100
+
101
+
102
+ def _get_fold_name_id(self):
103
+ subset_id = self.config.subset_id
104
+ subset_id_list = subset_id.split('_')
105
+ fold_name = subset_id_list[-2]
106
+ fold_id = subset_id_list[-1]
107
+ if fold_id == "overlap":
108
+ fold_id = 1
109
+ elif fold_id == "nooverlap":
110
+ fold_id = 2
111
+ return fold_name, fold_id
112
+
113
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
114
+ fold_name, fold_id = self._get_fold_name_id()
115
+ if fold_name == 'su':
116
+ fold_name1 = "Sunda"
117
+ fold_name2 = 'Snd'
118
+
119
+ else:
120
+ fold_name1 = 'Jawa'
121
+ fold_name2 = 'Jaw'
122
+
123
+ urls = _URLS[_DATASETNAME]
124
+ data_dir = Path(dl_manager.download_and_extract(urls))
125
+ # print("data_dir", data_dir)
126
+ text_file = os.path.join(data_dir, f"data_indsp_news_ethnicsr-main/{fold_name1}/text/transcript.txt")
127
+ wav_folder = os.path.join(data_dir, f"data_indsp_news_ethnicsr-main/{fold_name1}/speech/16kHz/")
128
+ train_list = os.path.join(data_dir, f"data_indsp_news_ethnicsr-main/{fold_name1}/lst/dataset{fold_id}_train_news_{fold_name2}.lst")
129
+ test_list = os.path.join(data_dir, f"data_indsp_news_ethnicsr-main/{fold_name1}/lst/dataset{fold_id}_test_news_{fold_name2}.lst")
130
+
131
+ #unzip
132
+ for speaker_id in range(1, 11):
133
+ speaker_id = "%03d" % (speaker_id)
134
+ zip_file = os.path.join(wav_folder, f"{fold_name2}{speaker_id}.zip")
135
+ out_folder = os.path.join(wav_folder, f"{fold_name2}{speaker_id}")
136
+ if not os.path.exists(out_folder):
137
+ with ZipFile(zip_file, 'r') as f:
138
+ f.extractall(out_folder)
139
+
140
+ return [
141
+ datasets.SplitGenerator(
142
+ name=datasets.Split.TRAIN,
143
+
144
+ gen_kwargs={
145
+ "wav_folder": wav_folder,
146
+ "text_path": text_file,
147
+ "split": "train",
148
+ "fold_name": fold_name,
149
+ "file_list": train_list,
150
+ },
151
+ ),
152
+
153
+ datasets.SplitGenerator(
154
+ name=datasets.Split.TEST,
155
+ gen_kwargs={
156
+ "wav_folder": wav_folder,
157
+ "text_path": text_file,
158
+ "split": "test",
159
+ "fold_name": fold_name,
160
+ "file_list": test_list,
161
+ },
162
+ )
163
+ ]
164
+
165
+
166
+ def _generate_examples(self, wav_folder: Path, text_path: Path, split: str, fold_name: str, file_list: Path) -> Tuple[int, Dict]:
167
+ if fold_name == 'su':
168
+ fold_name2 = 'Snd'
169
+ else:
170
+ fold_name2 = 'Jaw'
171
+
172
+ id2text = {}
173
+ with open(text_path, "r", encoding='unicode_escape') as f:
174
+ for text_idx, line in enumerate(f.readlines()):
175
+ id2text.update({"%04d" % (text_idx + 1):line.strip()})
176
+
177
+
178
+ wave_list = []
179
+ with open(file_list) as f:
180
+ for l in f.readlines():
181
+ audio_id = l.strip()[:-4]
182
+ speaker_id = audio_id.split('_')[0][-3:]
183
+ text_id = audio_id.split('_')[-1]
184
+ text = id2text[text_id]
185
+
186
+ wav_path = os.path.join(wav_folder, audio_id.split('_')[0], l.strip())
187
+ if not os.path.exists(wav_path):
188
+ print('no exisit wav_path', wav_path)
189
+ assert os.path.exists(wav_path)
190
+
191
+ if self.config.schema == "source":
192
+ ex = {
193
+ "id": audio_id,
194
+ "speaker_id": speaker_id,
195
+ "path": wav_path,
196
+ "audio": wav_path,
197
+ "text": text,
198
+ }
199
+ yield audio_id, ex
200
+
201
+ elif self.config.schema == "nusantara_sptext":
202
+ ex = {
203
+ "id": audio_id,
204
+ "speaker_id": speaker_id,
205
+ "path": wav_path,
206
+ "audio": wav_path,
207
+ "text": text,
208
+ "metadata": {
209
+ "speaker_age": None,
210
+ "speaker_gender": audio_id.split("_")[1],
211
+ }
212
+ }
213
+ yield audio_id, ex