File size: 3,916 Bytes
89739fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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