samanantar / samanantar.py
albertvillanova's picture
Refactor loading script (#6)
21ffe29
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
"""Samanantar dataset."""
from pathlib import Path
import datasets
_CITATION = """\
@misc{ramesh2021samanantar,
title={Samanantar: The Largest Publicly Available Parallel Corpora Collection for 11 Indic Languages},
author={Gowtham Ramesh and Sumanth Doddapaneni and Aravinth Bheemaraj and Mayank Jobanputra and Raghavan AK and Ajitesh Sharma and Sujit Sahoo and Harshita Diddee and Mahalakshmi J and Divyanshu Kakwani and Navneet Kumar and Aswin Pradeep and Srihari Nagaraj and Kumar Deepak and Vivek Raghavan and Anoop Kunchukuttan and Pratyush Kumar and Mitesh Shantadevi Khapra},
year={2021},
eprint={2104.05596},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
"""
_DESCRIPTION = """\
Samanantar is the largest publicly available parallel corpora collection for Indic languages: Assamese, Bengali, Gujarati, Hindi, Kannada, Malayalam, Marathi, Oriya, Punjabi, Tamil, Telugu. The corpus has 49.6M sentence pairs between English to Indian Languages.
"""
_HOMEPAGE = "https://indicnlp.ai4bharat.org/samanantar/"
_LICENSE = "Creative Commons Attribution-NonCommercial 4.0 International"
_URLS = {
"0.3.0": "https://ai4b-my.sharepoint.com/:u:/g/personal/sumanthdoddapaneni_ai4bharat_org/EXhX84sbTQhLrsURCU9DlUwBVyJ10cYK9bQQe1SMljf_yA?e=q7GJpb&download=1",
}
_LANGUAGES = ["as", "bn", "gu", "hi", "kn", "ml", "mr", "or", "pa", "ta", "te"]
class SamanantarConfig(datasets.BuilderConfig):
VERSION = datasets.Version("0.3.0")
def __init__(self, language=None, version=VERSION, **kwargs):
super().__init__(name=language, version=version, **kwargs)
self.language = language
class Samanantar(datasets.GeneratorBasedBuilder):
"""Samanantar dataset."""
BUILDER_CONFIG_CLASS = SamanantarConfig
BUILDER_CONFIGS = [SamanantarConfig(language=language) for language in _LANGUAGES]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"idx": datasets.Value("int64"),
"src": datasets.Value("string"),
"tgt": datasets.Value("string"),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls = _URLS[str(self.config.version)]
data_dir = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"data_dir": (Path(data_dir) / "v2" / f"en-{self.config.language}"),
},
),
]
def _generate_examples(self, data_dir):
src_path = data_dir / "train.en"
tgt_path = data_dir / f"train.{self.config.language}"
with src_path.open(encoding="utf-8") as src_file, tgt_path.open(encoding="utf-8") as tgt_file:
for idx, (src_line, tgt_line) in enumerate(zip(src_file, tgt_file)):
yield idx, {"idx": idx, "src": src_line.strip(), "tgt": tgt_line.strip()}