File size: 6,042 Bytes
d8100c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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 Studio automatic speech recognition dataset."""

import os
import glob

import datasets


_CITATION = """\
@article{korvzinek2017polish,
  title={Polish read speech corpus for speech tools and services},
  author={Kor{\v{z}}inek, Danijel and Marasek, Krzysztof and Brocki, {\L}ukasz and Wo{\l}k, Krzysztof},
  journal={arXiv preprint arXiv:1706.00245},
  year={2017}
}
"""

_DESCRIPTION = """\
The corpus consists of 317 speakers recorded in 554
sessions, where each session consists of 20 read sentences and 10 phonetically rich words. The size of
the audio portion of the corpus amounts to around 56 hours, with transcriptions containing 356674 words
from a vocabulary of size 46361.

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/audio.tar.gz"
_TRAIN_URL = "https://raw.githubusercontent.com/danijel3/ClarinStudioKaldi/master/local_clarin/train.sessions"
_TEST_URL = "https://raw.githubusercontent.com/danijel3/ClarinStudioKaldi/master/local_clarin/test.sessions"
_VALID_URL = "https://raw.githubusercontent.com/danijel3/ClarinStudioKaldi/master/local_clarin/dev.sessions"

class ClarinPLStudioASRConfig(datasets.BuilderConfig):
    """BuilderConfig for ClarinPLStudioASR."""

    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(ClarinPLStudioASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)


class ClarinPLStudio(datasets.GeneratorBasedBuilder):
    """ClarinPL Studio dataset."""

    BUILDER_CONFIGS = [
        ClarinPLStudioASRConfig(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):
        def get_sessions(path):
          sessions = []
          with open(path, 'r') as f:
            for line in f:
              sessions.append(line.strip())
            return sessions
        archive_path = dl_manager.download_and_extract(_DS_URL)
        train_sessions_path = dl_manager.download(_TRAIN_URL)
        test_sessions_path = dl_manager.download(_TEST_URL)
        valid_sessions_path = dl_manager.download(_VALID_URL)

        train_sessions = get_sessions(train_sessions_path)
        test_sessions = get_sessions(test_sessions_path)
        valid_sessions = get_sessions(valid_sessions_path)

        archive_path = os.path.join(archive_path, "audio")
        return [
            datasets.SplitGenerator(name="train", gen_kwargs={
                "archive_path": archive_path,
                "sessions": train_sessions
                }),
            datasets.SplitGenerator(name="test", gen_kwargs={
                "archive_path": archive_path,
                "sessions": test_sessions
                }),
            datasets.SplitGenerator(name="valid", gen_kwargs={
                "archive_path": archive_path,
                "sessions": valid_sessions
                }),
        ]

    def _generate_examples(self, archive_path, sessions):
        """Generate examples from a ClarinPL Studio archive_path."""
        def get_single_line(path):
          lines = []
          with open(path, 'r', encoding="utf-8") as f:
            for line in f:
              line = line.strip()
              lines.append(line)
          assert(len(lines) == 1)
          return lines[0]
        for session in sessions:
          session_path = os.path.join(archive_path, session)
          speaker = get_single_line(os.path.join(session_path, "spk.txt"))
          text_glob = os.path.join(session_path, "*.txt")
          for text_file in sorted(glob.glob(text_glob)):
            if text_file.endswith("spk.txt"):
              continue
            basename = os.path.basename(text_file)
            basename = basename.replace('.txt', '')
            key = f'{session}_{basename}'
            text = get_single_line(text_file)
            audio = text_file.replace('.txt', '.wav')
            example = {
                "id": key,
                "speaker_id": speaker,
                "file": audio,
                "text": text,
            }
            yield key, example