# 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{rekathati2023rixvox:, author = {Rekathati, Faton}, title = {The KBLab Blog: RixVox: A Swedish Speech Corpus with 5500 Hours of Speech from Parliamentary Debates}, url = {https://kb-labb.github.io/posts/2023-03-09-rixvox-a-swedish-speech-corpus/}, 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" 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("date32"), "speaker": datasets.Value("string"), "party": datasets.Value("string"), "gender": datasets.Value("string"), "birth_year": 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"] meta_urls = {split: [_META_URL.format(split=split)] for split in splits} archive_urls = { split: [_DATA_URL.format(split=split, shard_idx=idx) for idx in range(0, _N_SHARDS[split])] for split in splits } 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, } 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), "archive_iters": [dl_manager.iter_archive(path) for path in archive_paths.get(split)], "meta_paths": meta_paths[split], }, ), ) return split_generators # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` def _generate_examples( self, local_extracted_archive_paths, archive_iters, meta_paths, ): key = 0 data = [] for meta_path in meta_paths: data.append(pd.read_parquet(meta_path)) df_meta = pd.concat(data) df_meta = df_meta.set_index("filename", drop=False) # Column contains NAType, so we convert to object type column and NAType to None values. df_meta["birth_year"] = df_meta["birth_year"].astype("object").where(df_meta["birth_year"].notnull(), None) 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_archive_paths[i], filename) if local_extracted_archive_paths is not None else filename ) result["audio"] = {"path": path, "bytes": file.read()} result["path"] = path if local_extracted_archive_paths else filename yield key, result key += 1