Datasets:

Languages:
Thai
ArXiv:
License:
holylovenia commited on
Commit
4feb9d4
1 Parent(s): 8476ae0

Upload asr_stidusc.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. asr_stidusc.py +178 -0
asr_stidusc.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 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
+
16
+
17
+ import os
18
+ from pathlib import Path
19
+ from typing import Dict, List, Tuple
20
+
21
+ import datasets
22
+
23
+ from seacrowd.utils import schemas
24
+ from seacrowd.utils.configs import SEACrowdConfig
25
+ from seacrowd.utils.constants import Licenses, Tasks
26
+
27
+ # no bibtex citation
28
+ _CITATION = ""
29
+ _DATASETNAME = "asr_stidusc"
30
+ _DESCRIPTION = """\
31
+ This open-source dataset consists of 4.56 hours of transcribed Thai scripted
32
+ speech focusing on daily use sentences, where 5,431 utterances contributed by
33
+ ten speakers were contained.
34
+ """
35
+
36
+ _HOMEPAGE = "https://magichub.com/datasets/thai-scripted-speech-corpus-daily-use-sentence/"
37
+ _LANGUAGES = ["tha"]
38
+ _LICENSE = Licenses.CC_BY_NC_ND_4_0.value
39
+ _LOCAL = False
40
+ _URLS = {
41
+ _DATASETNAME: "https://magichub.com/df/df.php?file_name=Thai_Scripted_Speech_Corpus_Daily_Use_Sentence.zip",
42
+ }
43
+ _SUPPORTED_TASKS = [Tasks.SPEECH_RECOGNITION]
44
+
45
+ _SOURCE_VERSION = "1.0.0"
46
+ _SEACROWD_VERSION = "2024.06.20"
47
+
48
+
49
+ class ASRSTIDuSCDataset(datasets.GeneratorBasedBuilder):
50
+ """ASR-STIDuSC consists transcribed Thai scripted speech focusing on daily use sentences"""
51
+
52
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
53
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
54
+
55
+ SEACROWD_SCHEMA_NAME = "sptext"
56
+
57
+ BUILDER_CONFIGS = [
58
+ SEACrowdConfig(
59
+ name=f"{_DATASETNAME}_source",
60
+ version=SOURCE_VERSION,
61
+ description=f"{_DATASETNAME} source schema",
62
+ schema="source",
63
+ subset_id=_DATASETNAME,
64
+ ),
65
+ SEACrowdConfig(
66
+ name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA_NAME}",
67
+ version=SEACROWD_VERSION,
68
+ description=f"{_DATASETNAME} SEACrowd schema",
69
+ schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
70
+ subset_id=_DATASETNAME,
71
+ ),
72
+ ]
73
+
74
+ DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
75
+
76
+ def _info(self) -> datasets.DatasetInfo:
77
+
78
+ if self.config.schema == "source":
79
+ features = datasets.Features(
80
+ {
81
+ "id": datasets.Value("string"),
82
+ "channel": datasets.Value("string"),
83
+ "uttrans_id": datasets.Value("string"),
84
+ "speaker_id": datasets.Value("string"),
85
+ "transcription": datasets.Value("string"),
86
+ "path": datasets.Value("string"),
87
+ "audio": datasets.Audio(sampling_rate=16_000),
88
+ "speaker_gender": datasets.Value("string"),
89
+ "speaker_age": datasets.Value("int64"),
90
+ "speaker_region": datasets.Value("string"),
91
+ "speaker_device": datasets.Value("string"),
92
+ }
93
+ )
94
+
95
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
96
+ features = schemas.speech_text_features
97
+
98
+ return datasets.DatasetInfo(
99
+ description=_DESCRIPTION,
100
+ features=features,
101
+ homepage=_HOMEPAGE,
102
+ license=_LICENSE,
103
+ citation=_CITATION,
104
+ )
105
+
106
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
107
+ """Returns SplitGenerators."""
108
+
109
+ data_paths = {
110
+ _DATASETNAME: Path(dl_manager.download_and_extract(_URLS[_DATASETNAME])),
111
+ }
112
+
113
+ return [
114
+ datasets.SplitGenerator(
115
+ name=datasets.Split.TRAIN,
116
+ gen_kwargs={
117
+ "filepath": data_paths[_DATASETNAME],
118
+ "split": "train",
119
+ },
120
+ )
121
+ ]
122
+
123
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
124
+ """Yields examples as (key, example) tuples."""
125
+
126
+ # read UTTRANSINFO file
127
+ # columns: channel, uttrans_id, speaker_id, prompt (empty field), transcription
128
+ uttransinfo_filepath = os.path.join(filepath, "UTTRANSINFO.txt")
129
+ with open(uttransinfo_filepath, "r", encoding="utf-8") as uttransinfo_file:
130
+ uttransinfo_data = uttransinfo_file.readlines()
131
+ uttransinfo_data = uttransinfo_data[1:] # remove header
132
+ uttransinfo_data = [s.strip("\n").split("\t") for s in uttransinfo_data]
133
+
134
+ # read SPKINFO file
135
+ # columns: channel, speaker_id, gender, age, region, device
136
+ spkinfo_filepath = os.path.join(filepath, "SPKINFO.txt")
137
+ with open(spkinfo_filepath, "r", encoding="utf-8") as spkinfo_file:
138
+ spkinfo_data = spkinfo_file.readlines()
139
+ spkinfo_data = spkinfo_data[1:] # remove header
140
+ spkinfo_data = [s.strip("\n").split("\t") for s in spkinfo_data]
141
+ for i, s in enumerate(spkinfo_data):
142
+ if s[2] == "M":
143
+ s[2] = "male"
144
+ elif s[2] == "F":
145
+ s[2] = "female"
146
+ else:
147
+ s[2] = None
148
+ # dictionary of metadata of each speaker
149
+ spkinfo_dict = {s[1]: {"speaker_gender": s[2], "speaker_age": int(s[3]), "speaker_region": s[4], "speaker_device": s[5]} for s in spkinfo_data}
150
+
151
+ for i, sample in enumerate(uttransinfo_data):
152
+ wav_path = os.path.join(filepath, "WAV", sample[2], sample[1])
153
+
154
+ if self.config.schema == "source":
155
+ example = {
156
+ "id": str(i),
157
+ "channel": sample[0],
158
+ "uttrans_id": sample[1],
159
+ "speaker_id": sample[2],
160
+ "transcription": sample[4],
161
+ "path": wav_path,
162
+ "audio": wav_path,
163
+ "speaker_gender": spkinfo_dict[sample[2]]["speaker_gender"],
164
+ "speaker_age": spkinfo_dict[sample[2]]["speaker_age"],
165
+ "speaker_region": spkinfo_dict[sample[2]]["speaker_region"],
166
+ "speaker_device": spkinfo_dict[sample[2]]["speaker_device"],
167
+ }
168
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
169
+ example = {
170
+ "id": str(i),
171
+ "speaker_id": sample[2],
172
+ "path": wav_path,
173
+ "audio": wav_path,
174
+ "text": sample[4],
175
+ "metadata": {"speaker_age": spkinfo_dict[sample[2]]["speaker_age"], "speaker_gender": spkinfo_dict[sample[2]]["speaker_gender"]},
176
+ }
177
+
178
+ yield i, example