ccmatrix / ccmatrix.py
yhavinga's picture
Add script and dataset card
03ab1db
raw
history blame
No virus
5.76 kB
# coding=utf-8
# Copyright 2020 HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
import os
import datasets
_DESCRIPTION = """\
CCMatrix: Mining Billions of High-Quality Parallel Sentences on the WEB
We show that margin-based bitext mining in LASER's multilingual sentence space can be applied to
monolingual corpora of billions of sentences to produce high quality aligned translation data.
We use thirty-two snapshots of a curated common crawl corpus [1] totaling 69 billion unique sentences.
Using one unified approach for 80 languages, we were able to mine 10.8 billion parallel sentences,
out of which only 2.9 billion are aligned with English.
IMPORTANT: Please cite reference [2][3] if you use this data.
[1] Guillaume Wenzek, Marie-Anne Lachaux, Alexis Conneau, Vishrav Chaudhary, Francisco Guzmán, Armand Jouli
and Edouard Grave, CCNet: Extracting High Quality Monolingual Datasets from Web Crawl Data
[2] Holger Schwenk, Guillaume Wenzek, Sergey Edunov, Edouard Grave and Armand Joulin,
CCMatrix: Mining Billions of High-Quality Parallel Sentences on the WEB
[3] Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines,
Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky,
Sergey Edunov, Edouard Grave, Michael Auli, and Armand Joulin.
Beyond English-Centric Multilingual Machine Translation
90 languages, 1,197 bitexts
total number of files: 90
total number of tokens: 112.14G
total number of sentence fragments: 7.37G
"""
_HOMEPAGE_URL = "https://opus.nlpl.eu/CCMatrix.php"
_CITATION = """\
Guillaume Wenzek, Marie-Anne Lachaux, Alexis Conneau, Vishrav Chaudhary, Francisco Guzmán, Armand Jouli and Edouard Grave, CCNet: Extracting High Quality Monolingual Datasets from Web Crawl Data
"""
_VERSION = "1.0.0"
_FILE = "CCMatrix.{}.{}" # E.g. CCMatrix.en-nl.nl
_DOWNLOAD_URL = "https://opus.nlpl.eu/download.php?f=CCMatrix/v1/moses/{}.txt.zip"
_LANGUAGES = ["nl", "en", "de", "fr", "es", "lt", "it"]
_LANGUAGE_PAIRS = [(l1, l2) for l1 in _LANGUAGES for l2 in _LANGUAGES if l1 != l2]
_SIZES = ["", "1000_000", "25_000_000"]
_CONFIGS = [(l1, l2, size) for (l1, l2) in _LANGUAGE_PAIRS for size in _SIZES]
class CCMatrixConfig(datasets.BuilderConfig):
def __init__(self, *args, lang1=None, lang2=None, size=None, **kwargs):
super().__init__(
*args,
name=f"{lang1}-{lang2}{'-' + size if size else ''}",
**kwargs,
)
self.lang1 = lang1
self.lang2 = lang2
self.size = size
x, y = (lang1, lang2) if lang1 < lang2 else (lang2, lang1)
self.download_pair = f"{x}-{y}"
class CCMatrix(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
CCMatrixConfig(
lang1=lang1,
lang2=lang2,
size=size,
description=f"Translating {lang1} to {lang2} or vice versa{ ' ' + size + ' rows' if size else ''}",
version=datasets.Version(_VERSION),
)
for lang1, lang2, size in _CONFIGS
]
BUILDER_CONFIG_CLASS = CCMatrixConfig
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"id": datasets.Value("int32"),
"score": datasets.Value("float"),
"translation": datasets.Translation(
languages=(self.config.lang1, self.config.lang2)
),
},
),
supervised_keys=None,
homepage=_HOMEPAGE_URL,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
download_url = _DOWNLOAD_URL.format(self.config.download_pair)
path = dl_manager.download_and_extract(download_url)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"datapath": path},
)
]
def _generate_examples(self, datapath):
l1_path = os.path.join(
datapath, _FILE.format(self.config.download_pair, self.config.lang1)
)
l2_path = os.path.join(
datapath, _FILE.format(self.config.download_pair, self.config.lang2)
)
scores_path = os.path.join(
datapath, _FILE.format(self.config.download_pair, "scores")
)
with open(l1_path, encoding="utf-8") as f1, open(
l2_path, encoding="utf-8"
) as f2, open(scores_path, encoding="utf-8") as f3:
for sentence_counter, (x, y, score) in enumerate(zip(f1, f2, f3)):
if self.config.size and sentence_counter == int(self.config.size):
return
result = (
sentence_counter,
{
"id": sentence_counter,
"score": score,
"translation": {
self.config.lang1: x.strip(),
self.config.lang2: y.strip(),
},
},
)
yield result