dbihbka commited on
Commit
00afe0a
1 Parent(s): 6f0180a

Upload tatoeba-challenge.py

Browse files
Files changed (1) hide show
  1. tatoeba-challenge.py +137 -0
tatoeba-challenge.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gzip
2
+ from datasets import (
3
+ BuilderConfig,
4
+ GeneratorBasedBuilder,
5
+ DownloadManager,
6
+ StreamingDownloadManager,
7
+ Version,
8
+ SplitGenerator,
9
+ Split,
10
+ DatasetInfo,
11
+ Features,
12
+ Value,
13
+ )
14
+ from typing import Union
15
+ from pathlib import Path
16
+
17
+
18
+ class TatoebaChallengeConfig(BuilderConfig):
19
+ """Builder config for Tatoeba challenge dataset."""
20
+
21
+ def __init__(self, name: str, version: str, **kwargs):
22
+ assert version == "2023-09-26", "Only v2023-09-26 is supported"
23
+ super().__init__(
24
+ name=name, version=Version(version.replace("-", ".")), **kwargs
25
+ )
26
+ self.version_str = version
27
+ self.data_url = (
28
+ f"https://object.pouta.csc.fi/Tatoeba-Challenge-v{version}/{name}.tar"
29
+ )
30
+
31
+
32
+ class TatoebaChallenge(GeneratorBasedBuilder):
33
+ """Tatoeba challenge dataset."""
34
+
35
+ BUILDER_CONFIG_CLASS = TatoebaChallengeConfig
36
+ BUILDER_CONFIGS = [
37
+ TatoebaChallengeConfig(name="chv-eng", version="2023-09-26"),
38
+ TatoebaChallengeConfig(name="chv-rus", version="2023-09-26"),
39
+ ]
40
+
41
+ def _info(self) -> DatasetInfo:
42
+ src, trg = self.config.name.split("-")
43
+ return DatasetInfo(
44
+ description="""
45
+ The Tatoeba Translation Challenge.
46
+ You can find more about the data here: https://github.com/Helsinki-NLP/Tatoeba-Challenge
47
+
48
+ Here we have only Chuvash-English subset.
49
+ We do not use official dataset https://huggingface.co/datasets/Helsinki-NLP/tatoeba_mt
50
+ here because chv-eng of the dataset contains only test split.
51
+ """,
52
+ features=Features(
53
+ {
54
+ "id": Value("string"),
55
+ src: Value("string"),
56
+ trg: Value("string"),
57
+ }
58
+ ),
59
+ supervised_keys=None,
60
+ homepage="https://github.com/Helsinki-NLP/Tatoeba-Challenge",
61
+ citation="""
62
+ @inproceedings{tiedemann-2020-tatoeba,
63
+ title = "The {T}atoeba {T}ranslation {C}hallenge {--} {R}ealistic Data Sets for Low Resource and Multilingual {MT}",
64
+ author = {Tiedemann, J{\"o}rg},
65
+ booktitle = "Proceedings of the Fifth Conference on Machine Translation",
66
+ month = nov,
67
+ year = "2020",
68
+ address = "Online",
69
+ publisher = "Association for Computational Linguistics",
70
+ url = "https://www.aclweb.org/anthology/2020.wmt-1.139",
71
+ pages = "1174--1182"
72
+ }
73
+ """,
74
+ )
75
+
76
+ def _split_generators(
77
+ self, dl_manager: Union[DownloadManager, StreamingDownloadManager]
78
+ ):
79
+ dl_dir = dl_manager.download_and_extract(self.config.data_url)
80
+ assert isinstance(dl_dir, str)
81
+
82
+ path_to_folder = (
83
+ Path(dl_dir)
84
+ / "data"
85
+ / "release"
86
+ / f"v{self.config.version_str}"
87
+ / self.config.name
88
+ )
89
+ return [
90
+ SplitGenerator(
91
+ name=Split.TRAIN._name,
92
+ gen_kwargs={
93
+ "langs": path_to_folder / "train.id.gz",
94
+ "src": path_to_folder / "train.src.gz",
95
+ "trg": path_to_folder / "train.trg.gz",
96
+ },
97
+ ),
98
+ SplitGenerator(
99
+ name=Split.TEST._name,
100
+ gen_kwargs={
101
+ "langs": path_to_folder / "test.id",
102
+ "src": path_to_folder / "test.src",
103
+ "trg": path_to_folder / "test.trg",
104
+ },
105
+ ),
106
+ ]
107
+
108
+ def _generate_examples(self, langs: Path, src: Path, trg: Path):
109
+ if langs.suffix == ".gz":
110
+ assert src.suffix == trg.suffix
111
+ assert trg.suffix == langs.suffix
112
+
113
+ opener = gzip.open
114
+ else:
115
+ opener = open
116
+
117
+ with opener(langs, "rb") as langs_src:
118
+ with opener(src, "rb") as src_src:
119
+ with opener(trg, "rb") as trg_src:
120
+ for id_, (langs_line, src_line, trg_line) in enumerate(
121
+ zip(langs_src, src_src, trg_src)
122
+ ):
123
+ langs_row = langs_line.decode("utf8").strip().split("\t")
124
+ # train contains 3 symbols
125
+ if len(langs_row) == 3:
126
+ _, src_lang, trg_lang = langs_row
127
+ else:
128
+ src_lang, trg_lang = langs_row
129
+
130
+ yield (
131
+ id_,
132
+ {
133
+ "id": id_,
134
+ src_lang: src_line.decode("utf8").strip(),
135
+ trg_lang: trg_line.decode("utf8").strip(),
136
+ },
137
+ )