OpenBible_Swahili / OpenBible_Swahili.py
w11wo's picture
Update Dataloader for Clean Versions
b2a420e
from pathlib import Path
import json
import datasets
logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """Verse-level segmented audio of
[Swahili: Biblica® Open Kiswahili Contemporary Version (Neno)](https://open.bible/bibles/swahili-biblica-audio-bible/).
Aligned, cleaned, and segmented using [MMS](https://arxiv.org/abs/2305.13516).
"""
_HOMEPAGE_URL = "https://github.com/bookbot-hive/OpenBible-TTS"
_CITATION = ""
_DATA_URL = "https://huggingface.co/datasets/bookbot/OpenBible_Swahili/resolve/main/data"
_LICENSE = "https://creativecommons.org/licenses/by-sa/4.0/"
BOOKS = [
"GEN",
"EXO",
"LEV",
"NUM",
"DEU",
"JOS",
"JDG",
"RUT",
"1SA",
"2SA",
"1KI",
"2KI",
"1CH",
"2CH",
"EZR",
"NEH",
"EST",
"JOB",
"PSA",
"PRO",
"ECC",
"SNG",
"ISA",
"JER",
"LAM",
"EZK",
"DAN",
"HOS",
"JOL",
"AMO",
"OBA",
"JON",
"MIC",
"NAM",
"HAB",
"ZEP",
"HAG",
"ZEC",
"MAL",
"MAT",
"MRK",
"LUK",
"JHN",
"ACT",
"ROM",
"1CO",
"2CO",
"GAL",
"EPH",
"PHP",
"COL",
"1TH",
"2TH",
"1TI",
"2TI",
"TIT",
"PHM",
"HEB",
"JAS",
"1PE",
"2PE",
"1JN",
"2JN",
"3JN",
"JUD",
"REV",
]
class OpenBibleSwahiliConfig(datasets.BuilderConfig):
"""BuilderConfig for OpenBibleSwahili"""
def __init__(self, name, version):
super(OpenBibleSwahiliConfig, self).__init__(
name=name,
version=version,
description=_DESCRIPTION,
)
class OpenBible_Swahili(datasets.GeneratorBasedBuilder):
DEFAULT_CONFIG_NAME = "default"
BUILDER_CONFIGS = [
OpenBibleSwahiliConfig(
name=book,
version=datasets.Version("1.1.0"),
)
for book in BOOKS + [f"{_book}_clean" for _book in BOOKS] + ["default", "clean"]
]
def _info(self):
features = datasets.Features(
{
"id": datasets.Value("string"),
"verse_id": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=44_100),
"verse_text": datasets.Value("string"),
"transcript": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
homepage=_HOMEPAGE_URL,
citation=_CITATION,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
if self.config.name == "default":
archive_paths = [dl_manager.download_and_extract(f"{_DATA_URL}/{book}.tar.gz") for book in BOOKS]
elif self.config.name == "clean":
archive_paths = [dl_manager.download_and_extract(f"{_DATA_URL}/{book}_clean.tar.gz") for book in BOOKS]
else:
archive_paths = [dl_manager.download_and_extract(f"{_DATA_URL}/{self.config.name}.tar.gz")]
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"archive_paths": archive_paths,
},
)
]
def _generate_examples(self, archive_paths):
id_ = 0
for archive_path in archive_paths:
path = Path(archive_path)
json_path = list(path.rglob("*.json"))[0]
with open(json_path) as f:
data = json.load(f)
for datum in data:
book = datum["verseNumber"].split()[0]
chapter, verse_number = datum["verseNumber"].split()[1].split(":")
book_chapter = f"{book}_{chapter.zfill(3)}"
book_chapter_verse = f"{book_chapter}_{verse_number.zfill(3)}"
audio_path = (path / book / book_chapter / book_chapter_verse).with_suffix(".wav")
# skip if audio file does not exist, e.g. audio not included in clean data after filtering
if not audio_path.exists():
continue
transcript_path = audio_path.with_suffix(".txt")
with open(transcript_path) as f:
transcript = f.read().strip()
yield id_, {
"id": book_chapter_verse,
"verse_id": datum["verseNumber"],
"audio": str(audio_path),
"verse_text": datum["verseText"],
"transcript": transcript,
}
id_ += 1