Datasets:

File size: 7,673 Bytes
9a4b2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Copyright 2023 KBLab and The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
import datasets
import pandas as pd


_CITATION = """\
@misc{rekathati2023finding,
  author = {Rekathati, Faton},
  title = {The KBLab Blog: Finding Speeches in the Riksdag's Debates},
  url = {https://kb-labb.github.io/posts/2023-02-15-finding-speeches-in-the-riksdags-debates/},
  year = {2023}
}
"""

_DESCRIPTION = """\
RixVox is a speech dataset comprised of speeches from the Swedish Parliament (the Riksdag). Audio from speeches have been aligned with official transcripts, on the sentence level, using aeneas. 
Speaker metadata is available for each observation, including the speaker's name, gender, party, birth year and electoral district. The dataset contains a total of 5493 hours of speech. 
An observation may consist of one or several sentences (up to 30 seconds in duration).
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""

_LICENSE = "CC BY 4.0"

_N_SHARDS = {"train": 126, "dev": 2, "test": 2}

_BASE_PATH = "data/"
_META_URL = _BASE_PATH + "{split}_metadata.parquet"
_DATA_URL = _BASE_PATH + "{split}/{split}_{shard_idx}.tar.gz"


# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
class Rixvox(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    DEFAULT_CONFIG_NAME = "all"

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="train", version=VERSION, description="Training set of the RixVox dataset. 5383 hours of speech."
        ),
        datasets.BuilderConfig(
            name="dev", version=VERSION, description="Development set of the RixVox dataset. 52 hours of speech."
        ),
        datasets.BuilderConfig(
            name="test", version=VERSION, description="Test set of the RixVox dataset. 59 hours of speech."
        ),
    ]

    def _info(self):

        features = datasets.Features(
            {
                "dokid": datasets.Value("string"),
                "anforande_nummer": datasets.Value("int16"),
                "observation_nr": datasets.Value("int16"),
                "audio": datasets.features.Audio(sampling_rate=16_000),
                "text": datasets.Value("string"),
                "debatedate": datasets.Value("date64"),
                "speaker": datasets.Value("string"),
                "party": datasets.Value("string"),
                "gender": datasets.Value("string"),
                "birthyear": datasets.Value("int64"),
                "electoral_district": datasets.Value("string"),
                "intressent_id": datasets.Value("string"),
                "speaker_from_id": datasets.Value("bool"),
                "speaker_audio_meta": datasets.Value("string"),
                "start": datasets.Value("float64"),
                "end": datasets.Value("float64"),
                "duration": datasets.Value("float64"),
                "bleu_score": datasets.Value("float64"),
                "filename": datasets.Value("string"),
                # "path": datasets.Value("string"),
                "speaker_total_hours": datasets.Value("float64"),
                # These are the features of your dataset like images, labels ...
            }
        )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
        splits = ["train", "dev", "test"]

        if self.config.name == "all":
            archive_urls = {
                split: [_DATA_URL.format(split=split, shard_idx=idx) for idx in range(0, _N_SHARDS[split])]
                for split in splits
            }
            meta_urls = {split: [_META_URL.format(split=split)] for split in splits}

        else:
            archive_urls = {
                self.config.name: [
                    _DATA_URL.format(split=self.config.name, shard_idx=idx)
                    for idx in range(0, _N_SHARDS[self.config.name])
                ]
            }
            meta_urls = {self.config.name: _META_URL[self.config.name]}

        archive_paths = dl_manager.download(archive_urls)
        local_extracted_archives = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else {}
        meta_paths = dl_manager.download(meta_urls)

        split_generators = []
        split_names = {
            "train": datasets.Split.TRAIN,
            "dev": datasets.Split.VALIDATION,
            "test": datasets.Split.TEST,
        }

        if self.config.name == "all":
            for split in splits:
                split_generators.append(
                    datasets.SplitGenerator(
                        name=split_names.get(split),
                        gen_kwargs={
                            "local_extracted_archive_paths": local_extracted_archives.get(split),
                            "archives": [dl_manager.iter_archive(path) for path in archive_paths.get(split)],
                            "meta_paths": meta_paths[split],
                        },
                    ),
                )
        else:
            split_generators.append(
                datasets.SplitGenerator(
                    name=split_names.get(self.config.name),
                    gen_kwargs={
                        "local_extracted_archive_paths": local_extracted_archives.get(self.config.name),
                        "archives": [dl_manager.iter_archive(path) for path in archive_paths.get(self.config.name)],
                        "meta_paths": meta_paths[self.config.name],
                    },
                ),
            )

        return split_generators

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(
        self,
        local_extracted_archives,
        archive_iters,
        meta_paths,
    ):

        if self.config.name == "all":
            data = []
            for meta_path in meta_paths.values():
                data.append(pd.read_parquet(meta_path))

            df_meta = pd.concat(data)
        else:
            df_meta = pd.read_parquet(meta_path[self.config.name])

        df_meta = df_meta.set_index("filename")

        for i, audio_archive in enumerate(archive_iters):
            for filename, file in audio_archive:
                if filename not in df_meta.index:
                    continue

                result = dict(df_meta.loc[filename])
                path = (
                    os.path.join(local_extracted_archives[i], filename)
                    if local_extracted_archives is not None
                    else filename
                )
                result["audio"] = {"path": path, "bytes": file.read()}
                result["path"] = path if local_extracted_archives else filename

                yield path, result