holylovenia
commited on
Commit
•
e9785dd
1
Parent(s):
6e43292
Upload tatoeba.py with huggingface_hub
Browse files- tatoeba.py +192 -0
tatoeba.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
from datasets.download.download_manager import DownloadManager
|
6 |
+
|
7 |
+
from seacrowd.utils import schemas
|
8 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
9 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
10 |
+
|
11 |
+
_CITATION = """\
|
12 |
+
@article{tatoeba,
|
13 |
+
title = {Massively Multilingual Sentence Embeddings for Zero-Shot Cross-Lingual Transfer and Beyond},
|
14 |
+
author = {Mikel, Artetxe and Holger, Schwenk,},
|
15 |
+
journal = {arXiv:1812.10464v2},
|
16 |
+
year = {2018}
|
17 |
+
}
|
18 |
+
"""
|
19 |
+
|
20 |
+
_LOCAL = False
|
21 |
+
_LANGUAGES = ["ind", "vie", "tgl", "jav", "tha", "eng"]
|
22 |
+
_DATASETNAME = "tatoeba"
|
23 |
+
_DESCRIPTION = """\
|
24 |
+
This dataset is a subset of the Tatoeba corpus containing language pairs for Indonesian, Vietnamese, Tagalog, Javanese, and Thai.
|
25 |
+
The original dataset description can be found below:
|
26 |
+
|
27 |
+
This data is extracted from the Tatoeba corpus, dated Saturday 2018/11/17.
|
28 |
+
For each languages, we have selected 1000 English sentences and their translations, if available. Please check
|
29 |
+
this paper for a description of the languages, their families and scripts as well as baseline results.
|
30 |
+
Please note that the English sentences are not identical for all language pairs. This means that the results are
|
31 |
+
not directly comparable across languages. In particular, the sentences tend to have less variety for several
|
32 |
+
low-resource languages, e.g. "Tom needed water", "Tom needs water", "Tom is getting water", ...
|
33 |
+
"""
|
34 |
+
|
35 |
+
_HOMEPAGE = "https://github.com/facebookresearch/LASER/blob/main/data/tatoeba/v1/README.md"
|
36 |
+
_LICENSE = Licenses.APACHE_2_0.value
|
37 |
+
_URL = "https://github.com/facebookresearch/LASER/raw/main/data/tatoeba/v1/"
|
38 |
+
|
39 |
+
_SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION]
|
40 |
+
_SOURCE_VERSION = "1.0.0"
|
41 |
+
_SEACROWD_VERSION = "2024.06.20"
|
42 |
+
|
43 |
+
|
44 |
+
class TatoebaDataset(datasets.GeneratorBasedBuilder):
|
45 |
+
"""Tatoeba subset for Indonesian, Vietnamese, Tagalog, Javanese, and Thai."""
|
46 |
+
|
47 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
48 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
49 |
+
|
50 |
+
SEACROWD_SCHEMA_NAME = "t2t"
|
51 |
+
|
52 |
+
# Add configurations for loading a dataset per language.
|
53 |
+
dataset_names = sorted([f"tatoeba_{lang}_eng" for lang in _LANGUAGES[:-1]]) + sorted([f"tatoeba_eng_{lang}" for lang in _LANGUAGES[:-1]])
|
54 |
+
BUILDER_CONFIGS = []
|
55 |
+
for name in dataset_names:
|
56 |
+
source_config = SEACrowdConfig(
|
57 |
+
name=f"{name}_source",
|
58 |
+
version=SOURCE_VERSION,
|
59 |
+
description=f"{_DATASETNAME} source schema",
|
60 |
+
schema="source",
|
61 |
+
subset_id=name,
|
62 |
+
)
|
63 |
+
BUILDER_CONFIGS.append(source_config)
|
64 |
+
seacrowd_config = SEACrowdConfig(
|
65 |
+
name=f"{name}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
66 |
+
version=SEACROWD_VERSION,
|
67 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
68 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
69 |
+
subset_id=name,
|
70 |
+
)
|
71 |
+
BUILDER_CONFIGS.append(seacrowd_config)
|
72 |
+
|
73 |
+
# Add configuration that allows loading all datasets at once.
|
74 |
+
BUILDER_CONFIGS.extend(
|
75 |
+
[
|
76 |
+
# tatoeba_source
|
77 |
+
SEACrowdConfig(
|
78 |
+
name=f"{_DATASETNAME}_source",
|
79 |
+
version=SOURCE_VERSION,
|
80 |
+
description=f"{_DATASETNAME} source schema (all)",
|
81 |
+
schema="source",
|
82 |
+
subset_id=_DATASETNAME,
|
83 |
+
),
|
84 |
+
# tatoeba_seacrowd_t2t
|
85 |
+
SEACrowdConfig(
|
86 |
+
name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
87 |
+
version=SEACROWD_VERSION,
|
88 |
+
description=f"{_DATASETNAME} SEACrowd schema (all)",
|
89 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
90 |
+
subset_id=_DATASETNAME,
|
91 |
+
),
|
92 |
+
]
|
93 |
+
)
|
94 |
+
|
95 |
+
# Choose first language as default
|
96 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
97 |
+
|
98 |
+
def _info(self) -> datasets.DatasetInfo:
|
99 |
+
if self.config.schema == "source":
|
100 |
+
features = datasets.Features(
|
101 |
+
{
|
102 |
+
"source_sentence": datasets.Value("string"),
|
103 |
+
"target_sentence": datasets.Value("string"),
|
104 |
+
"source_lang": datasets.Value("string"),
|
105 |
+
"target_lang": datasets.Value("string"),
|
106 |
+
}
|
107 |
+
)
|
108 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
109 |
+
features = schemas.text2text_features
|
110 |
+
return datasets.DatasetInfo(
|
111 |
+
description=_DESCRIPTION,
|
112 |
+
features=features,
|
113 |
+
homepage=_HOMEPAGE,
|
114 |
+
license=_LICENSE,
|
115 |
+
citation=_CITATION,
|
116 |
+
)
|
117 |
+
|
118 |
+
def _split_generators(self, dl_manager: DownloadManager) -> List[datasets.SplitGenerator]:
|
119 |
+
"""Return SplitGenerators."""
|
120 |
+
language_pairs = []
|
121 |
+
tatoeba_source_data = []
|
122 |
+
tatoeba_eng_data = []
|
123 |
+
|
124 |
+
lang_1 = self.config.name.split("_")[1]
|
125 |
+
lang_2 = self.config.name.split("_")[2]
|
126 |
+
if lang_1 == "eng":
|
127 |
+
lang = lang_2
|
128 |
+
else:
|
129 |
+
lang = lang_1
|
130 |
+
|
131 |
+
if lang in _LANGUAGES:
|
132 |
+
# Load data per language
|
133 |
+
tatoeba_source_data.append(dl_manager.download_and_extract(_URL + f"tatoeba.{lang}-eng.{lang_1}"))
|
134 |
+
tatoeba_eng_data.append(dl_manager.download_and_extract(_URL + f"tatoeba.{lang}-eng.{lang_2}"))
|
135 |
+
language_pairs.append((lang_1, lang_2))
|
136 |
+
else:
|
137 |
+
# Load examples from all languages at once
|
138 |
+
# We just want to run this part when tatoeba_source / tatoeba_seacrowd_t2t was chosen.
|
139 |
+
for lang in _LANGUAGES[:-1]:
|
140 |
+
tatoeba_source_data.append(dl_manager.download_and_extract(_URL + f"tatoeba.{lang}-eng.{lang}"))
|
141 |
+
tatoeba_eng_data.append(dl_manager.download_and_extract(_URL + f"tatoeba.{lang}-eng.eng"))
|
142 |
+
language_pairs.append((lang, "eng"))
|
143 |
+
return [
|
144 |
+
datasets.SplitGenerator(
|
145 |
+
name=datasets.Split.VALIDATION,
|
146 |
+
gen_kwargs={
|
147 |
+
"filepaths": (tatoeba_source_data, tatoeba_eng_data),
|
148 |
+
"split": "dev",
|
149 |
+
"language_pairs": language_pairs,
|
150 |
+
},
|
151 |
+
)
|
152 |
+
]
|
153 |
+
|
154 |
+
def _generate_examples(self, filepaths: Tuple[List[Path], List[Path]], split: str, language_pairs: List[str]) -> Tuple[int, Dict]:
|
155 |
+
"""Yield examples as (key, example) tuples"""
|
156 |
+
source_files, target_files = filepaths
|
157 |
+
source_sents = []
|
158 |
+
target_sents = []
|
159 |
+
source_langs = []
|
160 |
+
target_langs = []
|
161 |
+
|
162 |
+
for source_file, target_file, (lang_1, lang_2) in zip(source_files, target_files, language_pairs):
|
163 |
+
with open(source_file, encoding="utf-8") as f1:
|
164 |
+
for row in f1:
|
165 |
+
source_sents.append(row.strip())
|
166 |
+
source_langs.append(lang_1)
|
167 |
+
with open(target_file, encoding="utf-8") as f2:
|
168 |
+
for row in f2:
|
169 |
+
target_sents.append(row.strip())
|
170 |
+
target_langs.append(lang_2)
|
171 |
+
|
172 |
+
for idx, (source, target, lang_src, lang_tgt) in enumerate(zip(source_sents, target_sents, source_langs, target_langs)):
|
173 |
+
if self.config.schema == "source":
|
174 |
+
example = {
|
175 |
+
"source_sentence": source,
|
176 |
+
"target_sentence": target,
|
177 |
+
# The source_lang in the HuggingFace source seems incorrect
|
178 |
+
# I am overriding it with the actual language code.
|
179 |
+
"source_lang": lang_src,
|
180 |
+
"target_lang": lang_tgt,
|
181 |
+
}
|
182 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
183 |
+
example = {
|
184 |
+
"id": str(idx),
|
185 |
+
"text_1": source,
|
186 |
+
"text_2": target,
|
187 |
+
# The source_lang in the HuggingFace source seems incorrect
|
188 |
+
# I am overriding it with the actual language code.
|
189 |
+
"text_1_name": lang_src,
|
190 |
+
"text_2_name": lang_tgt,
|
191 |
+
}
|
192 |
+
yield idx, example
|