File size: 4,889 Bytes
00afe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gzip
from datasets import (
    BuilderConfig,
    GeneratorBasedBuilder,
    DownloadManager,
    StreamingDownloadManager,
    Version,
    SplitGenerator,
    Split,
    DatasetInfo,
    Features,
    Value,
)
from typing import Union
from pathlib import Path


class TatoebaChallengeConfig(BuilderConfig):
    """Builder config for Tatoeba challenge dataset."""

    def __init__(self, name: str, version: str, **kwargs):
        assert version == "2023-09-26", "Only v2023-09-26 is supported"
        super().__init__(
            name=name, version=Version(version.replace("-", ".")), **kwargs
        )
        self.version_str = version
        self.data_url = (
            f"https://object.pouta.csc.fi/Tatoeba-Challenge-v{version}/{name}.tar"
        )


class TatoebaChallenge(GeneratorBasedBuilder):
    """Tatoeba challenge dataset."""

    BUILDER_CONFIG_CLASS = TatoebaChallengeConfig
    BUILDER_CONFIGS = [
        TatoebaChallengeConfig(name="chv-eng", version="2023-09-26"),
        TatoebaChallengeConfig(name="chv-rus", version="2023-09-26"),
    ]

    def _info(self) -> DatasetInfo:
        src, trg = self.config.name.split("-")
        return DatasetInfo(
            description="""
               The Tatoeba Translation Challenge.
               You can find more about the data here: https://github.com/Helsinki-NLP/Tatoeba-Challenge

                Here we have only Chuvash-English subset.
               We do not use official dataset https://huggingface.co/datasets/Helsinki-NLP/tatoeba_mt
               here because chv-eng of the dataset contains only test split.
               """,
            features=Features(
                {
                    "id": Value("string"),
                    src: Value("string"),
                    trg: Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="https://github.com/Helsinki-NLP/Tatoeba-Challenge",
            citation="""
           @inproceedings{tiedemann-2020-tatoeba,
               title = "The {T}atoeba {T}ranslation {C}hallenge {--} {R}ealistic Data Sets for Low Resource and Multilingual {MT}",
               author = {Tiedemann, J{\"o}rg},
               booktitle = "Proceedings of the Fifth Conference on Machine Translation",
               month = nov,
               year = "2020",
               address = "Online",
               publisher = "Association for Computational Linguistics",
               url = "https://www.aclweb.org/anthology/2020.wmt-1.139",
               pages = "1174--1182"
           }
           """,
        )

    def _split_generators(
        self, dl_manager: Union[DownloadManager, StreamingDownloadManager]
    ):
        dl_dir = dl_manager.download_and_extract(self.config.data_url)
        assert isinstance(dl_dir, str)

        path_to_folder = (
            Path(dl_dir)
            / "data"
            / "release"
            / f"v{self.config.version_str}"
            / self.config.name
        )
        return [
            SplitGenerator(
                name=Split.TRAIN._name,
                gen_kwargs={
                    "langs": path_to_folder / "train.id.gz",
                    "src": path_to_folder / "train.src.gz",
                    "trg": path_to_folder / "train.trg.gz",
                },
            ),
            SplitGenerator(
                name=Split.TEST._name,
                gen_kwargs={
                    "langs": path_to_folder / "test.id",
                    "src": path_to_folder / "test.src",
                    "trg": path_to_folder / "test.trg",
                },
            ),
        ]

    def _generate_examples(self, langs: Path, src: Path, trg: Path):
        if langs.suffix == ".gz":
            assert src.suffix == trg.suffix
            assert trg.suffix == langs.suffix

            opener = gzip.open
        else:
            opener = open

        with opener(langs, "rb") as langs_src:
            with opener(src, "rb") as src_src:
                with opener(trg, "rb") as trg_src:
                    for id_, (langs_line, src_line, trg_line) in enumerate(
                        zip(langs_src, src_src, trg_src)
                    ):
                        langs_row = langs_line.decode("utf8").strip().split("\t")
                        # train contains 3 symbols
                        if len(langs_row) == 3:
                            _, src_lang, trg_lang = langs_row
                        else:
                            src_lang, trg_lang = langs_row

                        yield (
                            id_,
                            {
                                "id": id_,
                                src_lang: src_line.decode("utf8").strip(),
                                trg_lang: trg_line.decode("utf8").strip(),
                            },
                        )