|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""NB Samtale: Norwegian conversation speech corpus""" |
|
|
|
|
|
from collections import defaultdict |
|
from email.mime import audio |
|
from email.policy import default |
|
from importlib import metadata |
|
import io |
|
import json |
|
import os |
|
from re import split |
|
import tarfile |
|
from typing import List |
|
|
|
from huggingface_hub import hf_hub_url |
|
import datasets |
|
|
|
from datasets.packaged_modules.parquet.parquet import Parquet |
|
from datasets.tasks import AutomaticSpeechRecognition |
|
from datasets import ClassLabel |
|
|
|
|
|
|
|
|
|
|
|
_DESCRIPTION = """\ |
|
NB Samtale is a speech corpus made by the Language Bank at the National Library of Norway. |
|
The corpus contains orthographically transcribed speech from podcasts and recordings of live events at the National Library. |
|
The corpus is intended as an open source dataset for Automatic Speech Recognition (ASR) development, |
|
and is specifically aimed at improving ASR systems’ handle on conversational speech. |
|
""" |
|
|
|
_HOMEPAGE = "https://www.nb.no/sprakbanken/en/resource-catalogue/oai-nb-no-sbr-85/" |
|
|
|
_LICENSE = "CC-ZERO-license" |
|
|
|
_CITATION = """\ |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
_DATA_URL= "https://huggingface.co/datasets/Sprakbanken/nb_samtale/resolve/main/data" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def normalize_transcription(transcription: str, config="annotations"): |
|
"""Normalize transcriptions according to orthographic standards, or verbatim.""" |
|
|
|
if config == "orthographic": |
|
return transcription |
|
elif config == "verbatim": |
|
return transcription |
|
return transcription |
|
|
|
|
|
class NBSamtaleConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for NBSamtale""" |
|
|
|
def __init__(self, **kwargs): |
|
|
|
|
|
super(NBSamtaleConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs) |
|
|
|
|
|
class NBSamtale(datasets.GeneratorBasedBuilder): |
|
"""Norwegian conversational speech audio dataset with a total of 24 hours transcribed speech from 69 speakers. """ |
|
BUILDER_CONFIG_CLASS = NBSamtaleConfig |
|
|
|
BUILDER_CONFIGS = [ |
|
NBSamtaleConfig(name="annotations", description="Transcriptions contain original annotations, including hesitations, laughter, interruptions etc. See https://www.nb.no/sbfil/taledata/NB_Samtale_About_the_corpus.pdf section 'Transcriptions' for more information."), |
|
NBSamtaleConfig(name="orthographic", description="Transcriptions have been normalized and word forms that comply with the orthographic standard are chosen, even for dialect specific words, e.g. 'korsen'/'kossen' is replaced with 'hvordan' in bokmål, or 'korleis' in nynorsk."), |
|
NBSamtaleConfig(name="verbatim", description="Transcriptions are closer to the spoken words, dialectal word forms have been chosen instead of the standard orthographic word form. E.g. 'korsen' or 'kossen' would be kept, instead of the orthographic bokmål 'hvordan', or nynorsk 'korleis'."), |
|
|
|
|
|
] |
|
|
|
DEFAULT_CONFIG_NAME = "annotations" |
|
|
|
def _info(self): |
|
"""This method specifies the datasets.DatasetInfo object |
|
which contains informations and typings for the dataset. |
|
""" |
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
'source_file_id': datasets.Value(dtype='string'), |
|
'segment_id': datasets.Value(dtype='string'), |
|
'segment_order': datasets.Value(dtype='int64'), |
|
'duration': datasets.Value(dtype='float64'), |
|
'overlap_previous': datasets.Value(dtype='bool'), |
|
'overlap_next': datasets.Value(dtype='bool'), |
|
'speaker_id': datasets.Value(dtype='string'), |
|
'gender': ClassLabel(names=['f', 'm']), |
|
'dialect': ClassLabel(names=['e', 'n', 'sw', 't', 'w']), |
|
'orthography': ClassLabel(names=['bm', 'nn']), |
|
'source_type': ClassLabel(names=['live-event', 'podcast']), |
|
'file_name': datasets.Value(dtype='string'), |
|
'transcription': datasets.Value(dtype='string'), |
|
'audio': datasets.Audio(sampling_rate=16000, mono=True, decode=True), |
|
} |
|
), |
|
supervised_keys=None, |
|
homepage=_HOMEPAGE, |
|
license=_LICENSE, |
|
citation=_CITATION, |
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: |
|
"""Download data and extract to datasets.Splits""" |
|
dl_manager.download_config.ignore_url_params = True |
|
audio_path = {} |
|
split_type = { |
|
"train": datasets.Split.TRAIN, |
|
"test": datasets.Split.TEST, |
|
"validation": datasets.Split.VALIDATION, |
|
} |
|
for split in split_type: |
|
audio_path[split] = dl_manager.download([f"data/{split}_{lang}_1.tar.gz" for lang in ["bm", "nn"]]) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=split_type[split], |
|
gen_kwargs={ |
|
"local_extracted_archive": dl_manager.extract(audio_path[split]) if not dl_manager.is_streaming else None, |
|
"audio_files":[dl_manager.iter_archive(archive) for archive in audio_path[split]], |
|
"metadata": dl_manager.download_and_extract(f"data/{split}_metadata.jsonl"), |
|
} |
|
) for split in split_type |
|
] |
|
|
|
|
|
def _generate_examples(self, local_extracted_archive, audio_files, metadata): |
|
"""Loads the data files and extract the features.""" |
|
meta = {} |
|
with open(metadata, encoding="utf-8") as mf: |
|
datalines = mf.read().splitlines() |
|
for row in datalines: |
|
data = json.loads(row) |
|
audio_path = data["file_name"] |
|
data["transcription"] = normalize_transcription(data["transcription"], config=self.config.name) |
|
meta[audio_path] = data |
|
|
|
id_ = 0 |
|
|
|
for archive in audio_files: |
|
for path, audio_file in archive: |
|
if not path in meta: |
|
print(f"{path} not in metadata") |
|
else: |
|
result = dict(meta[path]) |
|
|
|
path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path |
|
result["audio"] = {"path": path, "bytes": audio_file.read()} |
|
yield id_, result |
|
id_ += 1 |
|
|