|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""No Language Left Behind (NLLB) |
|
|
|
The "No Language Left Behind" paper is a dataset with translation examples across 200 languages. |
|
This paper reuses prior work, and for some language pairs is just reusing CC-Matrix published on statmt.org. |
|
Depending on the language pair chosen, this script will fetch either the original |
|
version from statmt.org, or the new one from AllenAI. |
|
""" |
|
|
|
import datasets |
|
import typing as tp |
|
|
|
_CITATION = ( |
|
"@article{team2022NoLL," |
|
"title={No Language Left Behind: Scaling Human-Centered Machine Translation}," |
|
"author={Nllb team and Marta Ruiz Costa-juss{\`a} and James Cross and Onur Celebi and Maha Elbayad and Kenneth Heafield and Kevin Heffernan and Elahe Kalbassi and Janice Lam and Daniel Licht and Jean Maillard and Anna Sun and Skyler Wang and Guillaume Wenzek and Alison Youngblood and Bapi Akula and Lo{\"i}c Barrault and Gabriel Mejia Gonzalez and Prangthip Hansanti and John Hoffman and Semarley Jarrett and Kaushik Ram Sadagopan and Dirk Rowe and Shannon L. Spruit and C. Tran and Pierre Andrews and Necip Fazil Ayan and Shruti Bhosale and Sergey Edunov and Angela Fan and Cynthia Gao and Vedanuj Goswami and Francisco Guzm'an and Philipp Koehn and Alexandre Mourachko and Christophe Ropers and Safiyyah Saleem and Holger Schwenk and Jeff Wang}," |
|
"journal={ArXiv}," |
|
"year={2022}," |
|
"volume={abs/2207.04672}" |
|
"}" |
|
) |
|
|
|
|
|
_DESCRIPTION = "" |
|
|
|
_HOMEPAGE = "" |
|
|
|
_LICENSE = "https://opendatacommons.org/licenses/by/1-0/" |
|
|
|
from .nllb_lang_pairs import LANG_PAIRS as _LANGUAGE_PAIRS |
|
from .ccmatrix_lang_pairs import LANG_PAIRS as _CCMATRIX_LANGUAGE_PAIRS |
|
|
|
_URL_BASE = "https://storage.googleapis.com/allennlp-data-bucket/nllb/" |
|
|
|
_URLs = { |
|
f"{src_lg}-{trg_lg}": f"{_URL_BASE}{src_lg}-{trg_lg}.gz" |
|
for src_lg, trg_lg in _LANGUAGE_PAIRS |
|
} |
|
|
|
_STATMT_URL = "http://data.statmt.org/cc-matrix/" |
|
_URLs.update( |
|
{ |
|
f"{src_lg}-{trg_lg}": f"{_STATMT_URL}{src_lg}-{trg_lg}.bitextf.tsv.gz" |
|
for src_lg, trg_lg in _CCMATRIX_LANGUAGE_PAIRS |
|
} |
|
) |
|
|
|
|
|
class NLLBTaskConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for No Language Left Behind Dataset.""" |
|
|
|
def __init__(self, src_lg, tgt_lg, source, **kwargs): |
|
super(NLLBTaskConfig, self).__init__(**kwargs) |
|
self.src_lg = src_lg |
|
self.tgt_lg = tgt_lg |
|
url = _URLs.get(f"{src_lg}-{tgt_lg}", "") |
|
self.source = source |
|
|
|
|
|
def _builder_configs() -> tp.List[NLLBTaskConfig]: |
|
configs = [] |
|
for (src_lg, tgt_lg) in _LANGUAGE_PAIRS: |
|
configs.append( |
|
NLLBTaskConfig( |
|
name=f"{src_lg}-{tgt_lg}", |
|
version=datasets.Version("1.0.0"), |
|
description=f"No Language Left Behind (NLLB): {src_lg} - {tgt_lg}", |
|
src_lg=src_lg, |
|
tgt_lg=tgt_lg, |
|
source="allenai", |
|
) |
|
) |
|
|
|
for (src_lg, tgt_lg) in _CCMATRIX_LANGUAGE_PAIRS: |
|
configs.append( |
|
NLLBTaskConfig( |
|
name=f"{src_lg}-{tgt_lg}", |
|
version=datasets.Version("1.0.0"), |
|
description=f"No Language Left Behind (NLLB): {src_lg} - {tgt_lg}", |
|
src_lg=src_lg, |
|
tgt_lg=tgt_lg, |
|
source="mtstats", |
|
) |
|
) |
|
return configs |
|
|
|
|
|
class NLLB(datasets.GeneratorBasedBuilder): |
|
"""No Language Left Behind Dataset.""" |
|
|
|
BUILDER_CONFIGS = _builder_configs() |
|
BUILDER_CONFIG_CLASS = NLLBTaskConfig |
|
|
|
def _info(self): |
|
|
|
features = datasets.Features( |
|
{ |
|
"translation": datasets.Translation( |
|
languages=(self.config.src_lg, self.config.tgt_lg) |
|
), |
|
"laser_score": datasets.Value("float32"), |
|
"source_sentence_lid": datasets.Value("float32"), |
|
"target_sentence_lid": datasets.Value("float32"), |
|
"source_sentence_source": datasets.Value("string"), |
|
"source_sentence_url": datasets.Value("string"), |
|
"target_sentence_source": datasets.Value("string"), |
|
"target_sentence_url": datasets.Value("string"), |
|
} |
|
) |
|
if self.config.source == "mtstats": |
|
|
|
features = datasets.Features( |
|
{ |
|
"translation": datasets.Translation( |
|
languages=(self.config.src_lg, self.config.tgt_lg) |
|
), |
|
"laser_score": datasets.Value("float32"), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
supervised_keys=None, |
|
homepage=_HOMEPAGE, |
|
license=_LICENSE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
pair = f"{self.config.src_lg}-{self.config.tgt_lg}" |
|
url = _URLs[pair] |
|
data_file = dl_manager.download_and_extract( |
|
url |
|
) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"filepath": data_file, |
|
"source_lg": self.config.src_lg, |
|
"target_lg": self.config.tgt_lg, |
|
}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, filepath, source_lg, target_lg): |
|
if self.config.source == "mtstats": |
|
|
|
return self._generate_minimal_examples(filepath, source_lg, target_lg) |
|
|
|
return self._generate_full_examples(filepath, source_lg, target_lg) |
|
|
|
def _generate_full_examples(self, filepath, source_lg, target_lg): |
|
with open(filepath, encoding="utf-8") as f: |
|
|
|
for id_, example in enumerate(f): |
|
try: |
|
datarow = example.split("\t") |
|
row = {} |
|
row["translation"] = { |
|
source_lg: datarow[0], |
|
target_lg: datarow[1], |
|
} |
|
row["laser_score"] = float(datarow[2]) |
|
row["source_sentence_lid"] = float(datarow[3]) |
|
row["target_sentence_lid"] = float(datarow[4]) |
|
row["source_sentence_source"] = datarow[5] |
|
row["source_sentence_url"] = datarow[6] |
|
row["target_sentence_source"] = datarow[7] |
|
row["target_sentence_url"] = datarow[8] |
|
row = { |
|
k: None if not v else v for k, v in row.items() |
|
} |
|
except: |
|
print(datarow) |
|
raise |
|
yield id_, row |
|
|
|
def _generate_minimal_examples(self, filepath, source_lg, target_lg): |
|
with open(filepath, encoding="utf-8") as f: |
|
for i, example in enumerate(f): |
|
try: |
|
(score, src, tgt) = example.rstrip("\n").split("\t") |
|
row = { |
|
"translation": { |
|
source_lg: src, |
|
target_lg: tgt, |
|
}, |
|
"laser_score": score, |
|
} |
|
except: |
|
print(example) |
|
raise |
|
yield i, row |
|
|
|
|
|
|
|
|
|
|