samanantar / samanantar.py
albertvillanova's picture
Add dataset loading script
89739fa verified
raw
history blame
3.92 kB
# 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."""
import re
import pandas as pd
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://storage.googleapis.com/samanantar-public/V0.3/source_wise_splits.zip",
}
_LANGUAGES = ["as", "bn", "gu", "hi", "kn", "ml", "mr", "or", "pa", "ta", "te"]
PATH_PATTERN = re.compile(r"/(?:existing|created)/(?P<data_source>[^/]+)/")
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"),
"data_source": datasets.Value("string"),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls = _URLS[str(self.config.version)]
archive = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"paths": dl_manager.iter_files([archive]),
},
),
]
def _generate_examples(self, paths):
id_ = 0
for path in paths:
if "/created/" in path and f"/en-{self.config.language}/{self.config.language}_sents.tsv" in path:
match = PATH_PATTERN.search(path)
df = pd.read_csv(path, sep="\t")
for row in df.to_dict(orient="records"):
row.update(match.groupdict())
yield id_, row
id_ += 1