clarinpl_sejmsenat / clarinpl_sejmsenat.py
jimregan's picture
Fix path in download (#2)
418f6d9
# coding=utf-8
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
# Copyright 2021 Jim O'Regan
#
# 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.
# Lint as: python3
"""ClarinPL Sejm/Senat automatic speech recognition dataset."""
import os
import datasets
_CITATION = """\
@article{marasek2014system,
title={System for automatic transcription of sessions of the {P}olish {S}enate},
author={Marasek, Krzysztof and Kor{\v{z}}inek, Danijel and Brocki, {\L}ukasz},
journal={Archives of Acoustics},
volume={39},
number={4},
pages={501--509},
year={2014}
}
"""
_DESCRIPTION = """\
A collection of 97 hours of parliamentary speeches published on the ClarinPL website
Note that in order to limit the required storage for preparing this dataset, the audio
is stored in the .wav format and is not converted to a float32 array. To convert the audio
file to a float32 array, please make use of the `.map()` function as follows:
```python
import soundfile as sf
def map_to_array(batch):
speech_array, _ = sf.read(batch["file"])
batch["speech"] = speech_array
return batch
dataset = dataset.map(map_to_array, remove_columns=["file"])
```
"""
_URL = "https://mowa.clarin-pl.eu/"
_DS_URL = "http://mowa.clarin-pl.eu/korpusy/parlament/parlament.tar.gz"
class ClarinPLSejmSenatASRConfig(datasets.BuilderConfig):
"""BuilderConfig for ClarinPLSejmSenatASR."""
def __init__(self, **kwargs):
"""
Args:
data_dir: `string`, the path to the folder containing the files in the
downloaded .tar
citation: `string`, citation for the data set
url: `string`, url for information about the data set
**kwargs: keyword arguments forwarded to super.
"""
super(ClarinPLSejmSenatASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
class ClarinPLSejmSenat(datasets.GeneratorBasedBuilder):
"""ClarinPL Sejm/Senat dataset."""
BUILDER_CONFIGS = [
ClarinPLSejmSenatASRConfig(name="clean", description="'Clean' speech."),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"file": datasets.Value("string"),
"text": datasets.Value("string"),
"speaker_id": datasets.Value("string"),
"id": datasets.Value("string"),
}
),
supervised_keys=("file", "text"),
homepage=_URL,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
archive_path = dl_manager.download_and_extract(_DS_URL)
archive_path = os.path.join(archive_path, "SejmSenat")
audio_path = os.path.join(archive_path, "audio")
return [
datasets.SplitGenerator(name="train", gen_kwargs={
"archive_path": os.path.join(archive_path, "train"),
"audio_path": audio_path
}),
datasets.SplitGenerator(name="test", gen_kwargs={
"archive_path": os.path.join(archive_path, "test"),
"audio_path": audio_path
}),
]
def _generate_examples(self, archive_path, audio_path):
"""Generate examples from a ClarinPL Sejm/Senat archive_path."""
with open(os.path.join(archive_path, "text"), "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
key, transcript = line.split(" ", 1)
parts = key.split('-')
dir = '-'.join(parts[0:2])
audio_file = f'{parts[2]}.wav'
example = {
"id": key,
"speaker_id": parts[0],
"file": os.path.join(audio_path, dir, audio_file),
"text": transcript,
}
yield key, example