gpucce commited on
Commit
c6078e8
1 Parent(s): f74cde8

add script

Browse files
Files changed (1) hide show
  1. invalsi.py +182 -0
invalsi.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """TODO: Add a description here."""
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
+ @misc{esuli2024invalsi,
29
+ title={The Invalsi Benchmark: measuring Language Models Mathematical and Language understanding in Italian},
30
+ author={Andrea Esuli and Giovanni Puccetti},
31
+ year={2024},
32
+ eprint={2403.18697},
33
+ archivePrefix={arXiv},
34
+ primaryClass={cs.CL}
35
+ }
36
+ """
37
+
38
+ # TODO: Add description of the dataset here
39
+ # You can copy an official description
40
+ _DESCRIPTION = """\
41
+ This new dataset is designed to measure Language Models mathematical and language understanding in Italian.
42
+ """
43
+
44
+ # TODO: Add a link to an official homepage for the dataset here
45
+ _HOMEPAGE = ""
46
+
47
+ # TODO: Add the licence for the dataset here if you can find it
48
+ _LICENSE = "CC BY 4.0"
49
+
50
+ # TODO: Add link to the official dataset URLs here
51
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
52
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
53
+ _URLS = {
54
+ "mate": "https://huggingface.co/datasets/ai4text/Invalsi/tree/main/invalsi_mate_data",
55
+ "ita": "https://huggingface.co/datasets/ai4text/Invalsi/tree/main/invalsi_ita_data",
56
+ }
57
+
58
+
59
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
60
+ class Invalsi(datasets.GeneratorBasedBuilder):
61
+ """TODO: Short description of my dataset."""
62
+
63
+ VERSION = datasets.Version("0.1")
64
+
65
+ # This is an example of a dataset with multiple configurations.
66
+ # If you don't want/need to define several sub-sets in your dataset,
67
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
68
+
69
+ # If you need to make complex sub-parts in the datasets with configurable options
70
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
71
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
72
+
73
+ # You will be able to load one or the other configurations in the following list with
74
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
75
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
76
+ BUILDER_CONFIGS = [
77
+ datasets.BuilderConfig(name="mate", version=VERSION, description="Mathematical Understanding"),
78
+ datasets.BuilderConfig(name="ita", version=VERSION, description="Italian Understanding"),
79
+ ]
80
+
81
+ # DEFAULT_CONFIG_NAME = "mate" # It's not mandatory to have a default configuration. Just use one if it make sense.
82
+
83
+ def _info(self):
84
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
85
+ if self.config.name == "mate": # This is the name of the configuration selected in BUILDER_CONFIGS above
86
+ features = datasets.Features(
87
+ {
88
+ "domanda": datasets.Value("string"),
89
+ "risposta": datasets.Value("string"),
90
+ "immagine": datasets.Value("string"),
91
+ "test_id": datasets.Value("string"),
92
+ }
93
+ )
94
+ elif self.config.name == "ita": # This is an example to show how to have different features for "first_domain" and "second_domain"
95
+ features = datasets.Features(
96
+ {
97
+ "testo": datasets.Value("string"),
98
+ "domanda": datasets.Value("string"),
99
+ "risposta": datasets.Value("string"),
100
+ "immagine": datasets.Value("string"),
101
+ "test_id": datasets.Value("string"),
102
+ }
103
+ )
104
+ return datasets.DatasetInfo(
105
+ # This is the description that will appear on the datasets page.
106
+ description=_DESCRIPTION,
107
+ # This defines the different columns of the dataset and their types
108
+ features=features, # Here we define them above because they are different between the two configurations
109
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
110
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
111
+ # supervised_keys=("sentence", "label"),
112
+ # Homepage of the dataset for documentation
113
+ homepage=_HOMEPAGE,
114
+ # License for the dataset if available
115
+ license=_LICENSE,
116
+ # Citation for the dataset
117
+ citation=_CITATION,
118
+ )
119
+
120
+ def _split_generators(self, dl_manager):
121
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
122
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
123
+
124
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
125
+ # 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.
126
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
127
+ urls = _URLS[self.config.name]
128
+ data_dir = dl_manager.download_and_extract(urls)
129
+ if self.config.name == "mate":
130
+ data_file = "invalsi_mate_clean.csv"
131
+ elif self.config.name == "ita":
132
+ data_file = "invalsi_ita_clean.csv"
133
+ return [
134
+ # datasets.SplitGenerator(
135
+ # name=datasets.Split.TRAIN,
136
+ # # These kwargs will be passed to _generate_examples
137
+ # gen_kwargs={
138
+ # "filepath": os.path.join(data_dir, "train.jsonl"),
139
+ # "split": "train",
140
+ # },
141
+ # ),
142
+ datasets.SplitGenerator(
143
+ name=datasets.Split.VALIDATION,
144
+ # These kwargs will be passed to _generate_examples
145
+ gen_kwargs={
146
+ "filepath": os.path.join(data_dir, data_file),
147
+ "split": "val",
148
+ },
149
+ ),
150
+ # datasets.SplitGenerator(
151
+ # name=datasets.Split.TEST,
152
+ # # These kwargs will be passed to _generate_examples
153
+ # gen_kwargs={
154
+ # "filepath": os.path.join(data_dir, "test.jsonl"),
155
+ # "split": "test"
156
+ # },
157
+ # ),
158
+ ]
159
+
160
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
161
+ def _generate_examples(self, filepath, split):
162
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
163
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
164
+ with open(filepath, encoding="utf-8") as f:
165
+ for key, row in enumerate(f):
166
+ data = json.loads(row)
167
+ if self.config.name == "mate":
168
+ # Yields examples as (key, example) tuples
169
+ yield key, {
170
+ "domanda": datasets.Value("string"),
171
+ "risposta": datasets.Value("string"),
172
+ "immagine": datasets.Value("string"),
173
+ "test_id": datasets.Value("string"),
174
+ }
175
+ elif self.config.name == "ita":
176
+ yield key, {
177
+ "testo": datasets.Value("string"),
178
+ "domanda": datasets.Value("string"),
179
+ "risposta": datasets.Value("string"),
180
+ "immagine": datasets.Value("string"),
181
+ "test_id": datasets.Value("string"),
182
+ }