albertvillanova HF staff commited on
Commit
89739fa
1 Parent(s): 3ec186d

Add dataset loading script

Browse files
Files changed (1) hide show
  1. samanantar.py +103 -0
samanantar.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Samanantar dataset."""
16
+
17
+ import re
18
+
19
+ import pandas as pd
20
+
21
+ import datasets
22
+
23
+
24
+ _CITATION = """\
25
+ @misc{ramesh2021samanantar,
26
+ title={Samanantar: The Largest Publicly Available Parallel Corpora Collection for 11 Indic Languages},
27
+ 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},
28
+ year={2021},
29
+ eprint={2104.05596},
30
+ archivePrefix={arXiv},
31
+ primaryClass={cs.CL}
32
+ }
33
+ """
34
+
35
+ _DESCRIPTION = """\
36
+ 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.
37
+ """
38
+
39
+ _HOMEPAGE = "https://indicnlp.ai4bharat.org/samanantar/"
40
+
41
+ _LICENSE = "Creative Commons Attribution-NonCommercial 4.0 International"
42
+
43
+ _URLS = {
44
+ "0.3.0": "https://storage.googleapis.com/samanantar-public/V0.3/source_wise_splits.zip",
45
+ }
46
+ _LANGUAGES = ["as", "bn", "gu", "hi", "kn", "ml", "mr", "or", "pa", "ta", "te"]
47
+
48
+ PATH_PATTERN = re.compile(r"/(?:existing|created)/(?P<data_source>[^/]+)/")
49
+
50
+
51
+ class SamanantarConfig(datasets.BuilderConfig):
52
+ VERSION = datasets.Version("0.3.0")
53
+
54
+ def __init__(self, language=None, version=VERSION, **kwargs):
55
+ super().__init__(name=language, version=version, **kwargs)
56
+ self.language = language
57
+
58
+
59
+ class Samanantar(datasets.GeneratorBasedBuilder):
60
+ """Samanantar dataset."""
61
+
62
+ BUILDER_CONFIG_CLASS = SamanantarConfig
63
+ BUILDER_CONFIGS = [SamanantarConfig(language=language) for language in _LANGUAGES]
64
+
65
+ def _info(self):
66
+ return datasets.DatasetInfo(
67
+ description=_DESCRIPTION,
68
+ features=datasets.Features(
69
+ {
70
+ "idx": datasets.Value("int64"),
71
+ "src": datasets.Value("string"),
72
+ "tgt": datasets.Value("string"),
73
+ "data_source": datasets.Value("string"),
74
+ }
75
+ ),
76
+ supervised_keys=None,
77
+ homepage=_HOMEPAGE,
78
+ license=_LICENSE,
79
+ citation=_CITATION,
80
+ )
81
+
82
+ def _split_generators(self, dl_manager):
83
+ urls = _URLS[str(self.config.version)]
84
+ archive = dl_manager.download_and_extract(urls)
85
+ return [
86
+ datasets.SplitGenerator(
87
+ name=datasets.Split.TRAIN,
88
+ gen_kwargs={
89
+ "paths": dl_manager.iter_files([archive]),
90
+ },
91
+ ),
92
+ ]
93
+
94
+ def _generate_examples(self, paths):
95
+ id_ = 0
96
+ for path in paths:
97
+ if "/created/" in path and f"/en-{self.config.language}/{self.config.language}_sents.tsv" in path:
98
+ match = PATH_PATTERN.search(path)
99
+ df = pd.read_csv(path, sep="\t")
100
+ for row in df.to_dict(orient="records"):
101
+ row.update(match.groupdict())
102
+ yield id_, row
103
+ id_ += 1