File size: 10,803 Bytes
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
990acb6
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
990acb6
5dc576d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from typing import Optional, Callable, List, Dict, Any, Tuple, Generator
from dataclasses import dataclass
import itertools
import os
import datasets
from .utils import Sample, list_keyby, parse_tmx, parse_sgm, parse_tsv, cleanup, normalize, dict_map, dict_filter_keys, dict_flatten

logger = datasets.logging.get_logger(
    name=__name__
)

@dataclass(frozen=True)
class Candidate:
    name: str
    url: str
    paths: Tuple[str, ...]
    num_examples: int
    parser: Callable[
        [Tuple[str, ...]], Generator[Sample, None, None]
    ]

    def download_paths(
        self,
        base_path: str
    ):
        return tuple(
            os.path.join(base_path, path)
            for path in self.paths
        )


@dataclass(frozen=True)
class Constraint:
    start: Optional[int] = None
    stop: Optional[int] = None
    step: Optional[int] = None


_CANDIDATES = [
    Candidate(
        name='europarl_v10',
        url='https://statmt.org/europarl/v10/training/europarl-v10.de-en.tsv.gz',
        paths=('.',),
        num_examples=1828521,
        parser=lambda filepaths: parse_tsv(
            filepaths=filepaths,
            columns={
                'de': 0, 'en': 1
            }
        )
    ),
    Candidate(
        name='newscommentary_v17',
        url='https://www.statmt.org/news-commentary/v17/training/news-commentary-v17.de-en.tsv.gz',
        paths=('.',),
        num_examples=418621,
        parser=lambda filepaths: parse_tsv(
            filepaths=filepaths,
            columns={
                'de': 0, 'en': 1
            }
        )
    ),
    Candidate(
        name='wikititles_v3',
        url='https://object.pouta.csc.fi/OPUS-WikiTitles/v3/tmx/de-en.tmx.gz',
        paths=('.',),
        num_examples=1386770,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='ecb_2017',
        url='https://s3-eu-west-1.amazonaws.com/tilde-model/ecb2017.de-en.tmx.zip',
        paths=('ecb2017.UNIQUE.de-en.tmx',),
        num_examples=4147,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='rapid_2019',
        url='https://s3-eu-west-1.amazonaws.com/tilde-model/rapid2019.de-en.tmx.zip',
        paths=('RAPID_2019.UNIQUE.de-en.tmx',),
        num_examples=939808,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='eesc_2017',
        url='https://s3-eu-west-1.amazonaws.com/tilde-model/EESC2017.de-en.tmx.zip',
        paths=('EESC.de-en.tmx',),
        num_examples=2857850,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='ema_2016',
        url='https://s3-eu-west-1.amazonaws.com/tilde-model/EMA2016.de-en.tmx.zip',
        paths=('EMEA2016.de-en.tmx',),
        num_examples=347631,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='europat_v3',
        url='https://web-language-models.s3.amazonaws.com/europat/release3/de-en.txt.gz',
        paths=('.',),
        num_examples=19734742,
        parser=lambda filepaths: parse_tsv(
            filepaths=filepaths,
            columns={
                'de': 0, 'en': 1
            }
        )
    ),
    Candidate(
        name='books_v1',
        url='https://object.pouta.csc.fi/OPUS-Books/v1/tmx/de-en.tmx.gz',
        paths=('.',),
        num_examples=51106,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='ted2020_v1',
        url='https://object.pouta.csc.fi/OPUS-TED2020/v1/tmx/de-en.tmx.gz',
        paths=('.',),
        num_examples=289374,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='qed_v2',
        url='https://object.pouta.csc.fi/OPUS-QED/v2.0a/tmx/de-en.tmx.gz',
        paths=('.',),
        num_examples=492811,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='eubookshop_v2',
        url='https://object.pouta.csc.fi/OPUS-EUbookshop/v2/tmx/de-en.tmx.gz',
        paths=('.',),
        num_examples=8312724,
        parser=lambda filepaths: parse_tmx(
            filepaths=filepaths,
            attributes={
                'de': 'xml:lang="de"', 'en': 'xml:lang="en"',
            }
        )
    ),
    Candidate(
        name='newstest2018',
        url='https://data.statmt.org/wmt22/translation-task/dev.tgz',
        paths=('dev/sgm/newstest2018-deen-src.de.sgm',
               'dev/sgm/newstest2018-deen-ref.en.sgm'),
        num_examples=2998,
        parser=lambda filepaths: parse_sgm(
            filepaths=filepaths,
            files={
                'de': 0, 'en': 1
            }
        )
    ),
    Candidate(
        name='newstest2019',
        url='https://data.statmt.org/wmt22/translation-task/dev.tgz',
        paths=('dev/sgm/newstest2019-deen-src.de.sgm',
               'dev/sgm/newstest2019-deen-ref.en.sgm'),
        num_examples=2000,
        parser=lambda filepaths: parse_sgm(
            filepaths=filepaths,
            files={
                'de': 0, 'en': 1
            }
        )
    )
]

_CANDIDATES_BY_NAME = list_keyby(
    input=_CANDIDATES,
    key_fn=lambda candidate: candidate.name
)


class NordmannConfig(
    datasets.BuilderConfig
):
    def __init__(
        self,
        splits: Dict[datasets.NamedSplit, List[str]],
        constraints: Dict[str, Constraint],
        normalizer: Callable[[Sample], Sample],
        filter: Callable[[Sample], bool],
        **kwargs: Any
    ):
        assert splits

        datasets.BuilderConfig.__init__(
            self, **kwargs
        )

        self.splits = dict_map(
            input=splits, map_fn=lambda key, value: (
                key, dict_filter_keys(
                    input=_CANDIDATES_BY_NAME, keys=value
                )
            )
        )
        self.constraints = constraints
        self.normalizer = normalizer
        self.filter = filter


class Nordmann(
    datasets.GeneratorBasedBuilder
):
    BUILDER_CONFIG_CLASS = NordmannConfig
    BUILDER_CONFIGS = [
        NordmannConfig(
            name='balanced',
            description='NORDMANN 2023 (balanced) translation task dataset.',
            version=datasets.Version(
                version_str='0.0.1'
            ),
            splits={
                datasets.Split.TRAIN: [
                    'europarl_v10',
                    'newscommentary_v17',
                    'wikititles_v3',
                    'europat_v3',
                    'books_v1',
                    'ted2020_v1',
                    'qed_v2',
                    'eubookshop_v2'
                ],
                datasets.Split.VALIDATION: [
                    'newstest2018'
                ],
                datasets.Split.TEST: [
                    'newstest2019'
                ]
            },
            constraints={
                'europat_v3': Constraint(stop=1000000),
                'eubookshop_v2': Constraint(stop=2000000)
            },
            normalizer=normalize(
                strip_whitespaces=True,
                clean_control_characters=True,
                enforce_unicode_form='NFC'
            ),
            filter=cleanup(
                length_min=4,
                length_max=4096,
                length_ratio_max=1.33,
                alpha_ratio_min=.5
            )
        )
    ]

    def _info(
        self
    ):
        features = {
            'translation': datasets.features.Translation(
                languages=['de', 'en']
            )
        }

        return datasets.DatasetInfo(
            description='Translation dataset based on statmt.org',
            features=datasets.Features(features)
        )

    def _split_generators(
        self,
        dl_manager: datasets.DownloadManager
    ):
        self.config: NordmannConfig

        urls = dict_map(
            input=dict_flatten(
                input=self.config.splits
            ),
            map_fn=lambda key, value: (
                key, value.url
            )
        )

        base_paths: Dict[str, str]
        base_paths = dl_manager.download_and_extract( # pyright: ignore
            url_or_urls=urls
        )

        generators: List[datasets.SplitGenerator]
        generators = list()
        for split, split_candidates in self.config.splits.items():
            generators.append(
                datasets.SplitGenerator(
                    name=str(split),
                    gen_kwargs={
                        'candidates': split_candidates,
                        'base_paths': base_paths
                    }
                )
            )

        return generators

    def _generate_examples( # pyright: ignore
        self,
        candidates: Dict[str, Candidate],
        base_paths: Dict[str, str]
    ):
        self.config: NordmannConfig

        for name, candidate in candidates.items():
            constraint = (
                self.config.constraints[name]
                if name in self.config.constraints else Constraint()
            )

            samples = candidate.parser(
                candidate.download_paths(
                    base_path=base_paths[name]
                )
            )

            for sample_num, sample in enumerate(
                itertools.islice(
                    samples,
                    constraint.start,
                    constraint.stop,
                    constraint.step
                )
            ):
                normalized_sample = self.config.normalizer(sample)

                if not self.config.filter(normalized_sample):
                    continue

                yield candidate.name + '_' + str(sample_num), normalized_sample

            samples.close()