File size: 4,615 Bytes
5627ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a420e
5627ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a420e
 
5627ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a420e
 
 
 
5627ebc
 
 
 
 
 
 
 
 
 
 
 
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
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