File size: 7,907 Bytes
bcbf8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d68eb67
66e36e4
 
411c1bc
 
 
02b437f
 
 
fa4d750
 
45f3c9c
66e36e4
bcbf8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e25059c
66e36e4
411c1bc
02b437f
fa4d750
2d74c91
bcbf8ed
02b437f
bcbf8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66e36e4
 
411c1bc
 
02b437f
 
fa4d750
 
ae6fcfb
c77b571
bcbf8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33ae0f6
 
ac1d3a9
33ae0f6
 
0f586a1
ac1d3a9
0f586a1
bcbf8ed
ac1d3a9
bcbf8ed
 
 
c77b571
ac1d3a9
c77b571
 
bcbf8ed
 
 
 
 
c77b571
ac1d3a9
c77b571
 
bcbf8ed
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
# coding=utf-8
# Copyright 2021 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
""" Common Voice Dataset"""

import json
import os
from copy import deepcopy
import re
import unicodedata
from more_itertools import windowed
import datasets

_CITATION = """\
"""

_DESCRIPTION = """\
coraalを音声認識した誤り訂正用データセット
"""
_HOMEPAGE = ""
_LICENSE = ""

URLS = {
    "v1": {
        "text": "https://huggingface.co/datasets/Padomin/coraal-asr/resolve/main/coraal-asr.tar.gz",
    },
    "v2": {
        "text": "https://huggingface.co/datasets/Padomin/coraal-asr/resolve/main/coraal-asr-v2.tar.gz",
    },
    "ctc-large": {
        "text": "https://huggingface.co/datasets/Padomin/coraal-asr/resolve/main/coraal-ctc-large.tar.gz",
    },
    "xlsr": {
        "text": "https://huggingface.co/datasets/Padomin/coraal-asr/resolve/main/coraal-xlsr.tar.gz",
    },
    "whisper-small": {
        "text": "https://huggingface.co/datasets/Padomin/coraal-asr/resolve/main/coraal-whisper-small.tar.gz",
    }
}


class coraal_asr_config(datasets.BuilderConfig):
    def __init__(self, n_fronts=0, n_bodies=1, n_rears=0, front_prefix='front:\n', body_prefix='body:\n', rear_prefix='rear:\n', **kwargs):
        super(coraal_asr_config, self).__init__(**kwargs)
        self.n_fronts = n_fronts
        self.n_bodies = n_bodies
        self.n_rears = n_rears
        self.front_prefix = front_prefix
        self.body_prefix = body_prefix
        self.rear_prefix = rear_prefix

class coraal_asr(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("0.2.0")
    BUILDER_CONFIGS = [
        coraal_asr_config(name="v1", version=VERSION),
        coraal_asr_config(name="v2", version=VERSION),
        coraal_asr_config(name="ctc-large", version=VERSION),
        coraal_asr_config(name="xlsr", version=VERSION),
        coraal_asr_config(name="whisper-small", version=VERSION),
        coraal_asr_config(name="ctc-large-oracle", version=VERSION),
    ]
    DEFAULT_CONFIG_NAME = "ctc-large"  # It's not mandatory to have a default configuration. Just use one if it make sense.
    BUILDER_CONFIG_CLASS = coraal_asr_config

    def _info(self):
        feature_dict = {
                "text": datasets.Value("string"),
                "text_asr": datasets.Value("string"),
                "src": datasets.Value("string"),
                "tgt": datasets.Value("string"),
                "id": datasets.Value("string")
        }

        features = datasets.Features(feature_dict)
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        if "v1" in self.config.name:
            urls = deepcopy(URLS["v1"])
        if "v2" in self.config.name:
            urls = deepcopy(URLS["v2"])
        if "ctc-large" in self.config.name:
            urls = deepcopy(URLS["ctc-large"])
        if "xlsr" in self.config.name:
            urls = deepcopy(URLS["xlsr"])
        if "whisper-small" in self.config.name:
            urls = deepcopy(URLS["whisper-small"])
        if "ctc-large-oracle" in self.config.name:
            urls = deepcopy(URLS["ctc-large"])
            
        dl_path = dl_manager.download_and_extract(urls)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": os.path.join(dl_path["text"], "train.jsonl"),
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": os.path.join(dl_path["text"], "test.jsonl"),
                    "split": "test",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "filepath": os.path.join(dl_path["text"], "validation.jsonl"),
                    "split": "validation",
                },
            ),
        ]

    def _generate_examples(self, filepath, split):
        """Yields examples."""
        id_ = 0
        with open(filepath, encoding="utf-8") as f:
            for line in f:
                doc = json.loads(line)
                utterances = doc['utterances']
                # divide text and asr
                texts_asr = [utt['asr'] for utt in utterances]
                texts = [utt['text'] for utt in utterances]
                # window considering front and rear contexts
                if split == "train":
                    windowed_texts_asr = windowed([''] * self.config.n_fronts + texts_asr + [''] * self.config.n_rears, self.config.n_bodies + self.config.n_fronts + self.config.n_rears)
                    windowed_oracles = windowed([''] * self.config.n_fronts + texts + [''] * self.config.n_rears, self.config.n_bodies + self.config.n_fronts + self.config.n_rears)
                    windowed_texts = windowed(texts, self.config.n_bodies)
                else:
                    windowed_texts_asr = windowed([''] * self.config.n_fronts + texts_asr + [''] * self.config.n_rears, self.config.n_bodies + self.config.n_fronts + self.config.n_rears, fillvalue='', step=self.config.n_bodies)
                    windowed_oracles = windowed([''] * self.config.n_fronts + texts + [''] * self.config.n_rears, self.config.n_bodies + self.config.n_fronts + self.config.n_rears, fillvalue='', step=self.config.n_bodies)
                    windowed_texts = windowed(texts, self.config.n_bodies, fillvalue='', step=self.config.n_bodies)
                
                for text_asr, text, oracle, utt in zip(windowed_texts_asr, windowed_texts, windowed_oracles, utterances):
                    src = ''
                    if self.config.n_fronts > 0:
                        src += self.config.front_prefix
                        if "oracle" in self.config.name:
                            src += '\n'.join(oracle[:self.config.n_fronts])
                        else:
                            src += '\n'.join(text_asr[:self.config.n_fronts])
                        src += '\n'
                    src += self.config.body_prefix
                    src += '\n'.join(text_asr[self.config.n_fronts:self.config.n_fronts + self.config.n_bodies])
                    if self.config.n_rears > 0:
                        src += '\n' + self.config.rear_prefix
                        if "oracle" in self.config.name:
                            src += '\n'.join(oracle[self.config.n_fronts + self.config.n_bodies:])
                        else:
                            src += '\n'.join(text_asr[self.config.n_fronts + self.config.n_bodies:])
                    tgt = '\n'.join(text)
                    
                    data = {
                        "text": utt["text"],
                        "text_asr": utt["asr"],
                        'src': src,
                        'tgt': tgt,
                        'id': doc["id"],
                    }
                    
                    yield id_, data
                    
                    id_ += 1