nanakonoda commited on
Commit
7c52952
1 Parent(s): 7e58caa

First version of XNLI Sampled

Browse files
Files changed (1) hide show
  1. xnli_cm_sample.py +186 -0
xnli_cm_sample.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """Sampled XNLI Code-Mixed Corpus in English, German, French for Binary Text Classification"""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+ # TODO: Add BibTeX citation
26
+ # Find for instance the citation on arxiv or on the dataset repo/website
27
+ _CITATION = """\
28
+ # @InProceedings{huggingface:dataset,
29
+ # title = {A great new dataset},
30
+ # author={huggingface, Inc.
31
+ # },
32
+ # year={2020}
33
+ # }
34
+ """
35
+
36
+ # TODO: Add description of the dataset here
37
+ # You can copy an official description
38
+ _DESCRIPTION = """\
39
+ This dataset was generated from XNLI using the CodeMixed Text Generator for a binary text classification task.
40
+ """
41
+
42
+ # # TODO: Add a link to an official homepage for the dataset here
43
+ _HOMEPAGE = ""
44
+
45
+ # # TODO: Add the licence for the dataset here if you can find it
46
+ _LICENSE = ""
47
+
48
+ # TODO: Add link to the official dataset URLs here
49
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
50
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
51
+ _URLS = {
52
+ "fr_ec":
53
+ {"train" : "data/fr_ec/train.tsv",
54
+ "test" : "data/fr_ec/test.tsv"
55
+ },
56
+ "fr_ml":
57
+ {"train" : "data/fr_ml/train.tsv",
58
+ "test" : "data/fr_ml/test.tsv"
59
+ },
60
+ "de_ec":
61
+ {"train" : "data/de_ec/train.tsv",
62
+ "test" : "data/de_ec/test.tsv"
63
+ },
64
+ "de_ml":
65
+ {"train" : "data/de_ml/train.tsv",
66
+ "test" : "data/de_ml/test.tsv"
67
+ },
68
+ }
69
+
70
+
71
+
72
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
73
+ class XnliCodeMixedSampled(datasets.GeneratorBasedBuilder):
74
+ """Sampled XNLI Corpus in English, German, and French"""
75
+
76
+ VERSION = datasets.Version("1.1.0")
77
+
78
+ # This is an example of a dataset with multiple configurations.
79
+ # If you don't want/need to define several sub-sets in your dataset,
80
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
81
+
82
+ # If you need to make complex sub-parts in the datasets with configurable options
83
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
84
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
85
+
86
+ # You will be able to load one or the other configurations in the following list with
87
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
88
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
89
+
90
+ BUILDER_CONFIGS = [
91
+ datasets.BuilderConfig("fr_ec", version=VERSION, description="French-English code mixed dataset using Equivalence Constraint Theory"),
92
+ datasets.BuilderConfig("fr_ml", version=VERSION, description="French-English code mixed dataset using Matrix Language Theory"),
93
+ datasets.BuilderConfig("de_ec", version=VERSION, description="German-English code mixed dataset using Equivalence Constraint Theory"),
94
+ datasets.BuilderConfig("de_ml", version=VERSION, description="German-English code mixed dataset using Matrix Language Theory"),
95
+ ]
96
+
97
+ DEFAULT_CONFIG_NAME = "fr_ec" # It's not mandatory to have a default configuration. Just use one if it make sense.
98
+
99
+ def _info(self):
100
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
101
+ # if self.config.name == "en": # This is the name of the configuration selected in BUILDER_CONFIGS above
102
+ features = datasets.Features(
103
+ {
104
+ "text": datasets.Value("string"),
105
+ "label": datasets.Value("int64"),
106
+ # These are the features of your dataset like images, labels ...
107
+ }
108
+ )
109
+ # else: # This is an example to show how to have different features for "first_domain" and "second_domain"
110
+ # features = datasets.Features(
111
+ # {
112
+ # "sentence": datasets.Value("string"),
113
+ # "option2": datasets.Value("string"),
114
+ # "second_domain_answer": datasets.Value("string")
115
+ # # These are the features of your dataset like images, labels ...
116
+ # }
117
+ # )
118
+
119
+ return datasets.DatasetInfo(
120
+ # This is the description that will appear on the datasets page.
121
+ description=_DESCRIPTION,
122
+ # This defines the different columns of the dataset and their types
123
+ features=features, # Here we define them above because they are different between the two configurations
124
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
125
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
126
+ # supervised_keys=("sentence", "label"),
127
+ # Homepage of the dataset for documentation
128
+ homepage=_HOMEPAGE,
129
+ # License for the dataset if available
130
+ license=_LICENSE,
131
+ # Citation for the dataset
132
+ citation=_CITATION,
133
+ )
134
+
135
+
136
+ def _split_generators(self, dl_manager):
137
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
138
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
139
+
140
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
141
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
142
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
143
+ urls = _URLS[self.config.name]
144
+ data_dir = dl_manager.download_and_extract(urls)
145
+ return [
146
+ datasets.SplitGenerator(
147
+ name=datasets.Split.TRAIN,
148
+ # These kwargs will be passed to _generate_examples
149
+ gen_kwargs={
150
+ "filepath": data_dir["train"],
151
+ "split": "train",
152
+ },
153
+ ),
154
+ # datasets.SplitGenerator(
155
+ # name=datasets.Split.VALIDATION,
156
+ # # These kwargs will be passed to _generate_examples
157
+ # gen_kwargs={
158
+ # "filepath": os.path.join(data_dir, "dev.jsonl"),
159
+ # "split": "dev",
160
+ # },
161
+ # ),
162
+ datasets.SplitGenerator(
163
+ name=datasets.Split.TEST,
164
+ # These kwargs will be passed to _generate_examples
165
+ gen_kwargs={
166
+ "filepath": data_dir["test"],
167
+ "split": "test"
168
+ },
169
+ ),
170
+ ]
171
+
172
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
173
+ def _generate_examples(self, filepath, split):
174
+ print(filepath)
175
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
176
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
177
+ with open(filepath, encoding="utf-8") as f:
178
+ reader = csv.reader(f, delimiter='\t')
179
+ next(reader)
180
+ for key, data in enumerate(reader):
181
+ # if self.config.name == "en":
182
+ # Yields examples as (key, example) tuples
183
+ yield key, {
184
+ "text": data[0],
185
+ "label": data[1],
186
+ }