File size: 14,813 Bytes
cb6fe6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258a114
 
 
 
 
cb6fe6b
d8ff8a1
 
 
 
 
 
 
 
 
 
 
cb6fe6b
8e3e8ad
 
 
a581af9
 
 
 
 
 
258a114
4201212
4b9c392
c44b81d
cc0f26b
 
411c244
 
 
cc0f26b
 
 
 
 
 
 
 
 
 
 
 
f160e10
cc0f26b
 
1d51f23
 
 
c44b81d
cc0f26b
 
1aa9557
cc0f26b
 
a54a116
 
 
 
 
 
329ee2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07e7913
 
 
 
 
c44b81d
258a114
 
 
cb6fe6b
05a86d3
cb6fe6b
329ee2f
 
 
05a86d3
411c244
99e6afb
258a114
c44b81d
258a114
 
cb6fe6b
d49a95a
258a114
 
c5dd22a
258a114
 
 
 
 
 
c3a5a4f
1d51f23
c5dd22a
 
 
 
 
 
1d51f23
 
1aa9557
258a114
639c15e
 
 
 
 
 
 
258a114
 
a54a116
 
 
639c15e
 
 
 
 
a54a116
 
258a114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c44b81d
258a114
eb42f09
1fa01aa
89c4572
 
 
 
258a114
 
 
 
 
 
 
 
cb6fe6b
258a114
 
 
 
c3a5a4f
 
 
 
1d51f23
 
 
 
 
 
 
1aa9557
cb6fe6b
 
 
 
 
c5dd22a
cb6fe6b
 
 
 
 
 
a54a116
736e070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f697e2
736e070
 
 
 
 
 
 
 
258a114
 
 
 
 
 
c3a5a4f
 
 
 
258a114
c5dd22a
cb6fe6b
 
 
00eb333
258a114
1aa9557
258a114
 
 
 
 
c5dd22a
258a114
 
 
 
 
 
329ee2f
736e070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f697e2
736e070
 
 
 
 
 
 
 
c44b81d
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# 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.
"""LogiQA dataset."""

import datasets
import json
import ast
import pandas as pd
import csv

_CITATION = """\
@ARTICLE{10174688,
  author={Liu, Hanmeng and Liu, Jian and Cui, Leyang and Teng, Zhiyang and Duan, Nan and Zhou, Ming and Zhang, Yue},
  journal={IEEE/ACM Transactions on Audio, Speech, and Language Processing},
  title={LogiQA 2.0 — An Improved Dataset for Logical Reasoning in Natural Language Understanding},
  year={2023},
  volume={},
  number={},
  pages={1-16},
  doi={10.1109/TASLP.2023.3293046}}
"""

_DESCRIPTION = """\
The dataset is an amendment and re-annotation of LogiQA in 2020, a large-scale logical reasoning reading comprehension dataset adapted from the Chinese Civil Service Examination. We increase the data size, refine the texts with manual translation by professionals, and improve the quality by removing items with distinctive cultural features like Chinese idioms. Furthermore, we conduct a fine-grained annotation on the dataset and turn it into a two-way natural language inference (NLI) task, resulting in 35k premise-hypothesis pairs with gold labels, making it the first large-scale NLI dataset for complex logical reasoning
"""

_HOMEPAGE = "https://github.com/csitfun/LogiQA2.0/tree/main"

_LICENSE = (
    "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License"
)
HEAD= 'https://raw.githubusercontent.com/microsoft/AGIEval/main/data/v1/'

_URLS = {
    "sat_en": {
        "test": HEAD+'sat-en.jsonl',
    },
    "sat_en_without_passage": {
        "test": HEAD+'sat-en-without-passage.jsonl',
    },
    "sat_math": {
        "test": HEAD+'sat-math.jsonl'
    },
    "lsat_ar": {
        "test": HEAD+'lsat-ar.jsonl'
    },
    "lsat_lr": {
        "test": HEAD+'lsat-lr.jsonl'
    },
    "lsat_rc": {
        "test": HEAD+'lsat-rc.jsonl'
    },
    "logiqa_en": {
        "test": HEAD+'logiqa-en.jsonl'
    },
    "logiqa_zh": {
        "test": HEAD+'logiqa-zh.jsonl'
    },
    "aqua_rat": {
        "test": HEAD+'aqua-rat.jsonl'
    },
    'math': {
        "test": HEAD+'math.jsonl'
    },
    'jec_qa_ca': {
        "test": HEAD+'jec-qa-ca.jsonl'
    },
    'jec_qa_kd': {
        "test": HEAD+'jec-qa-kd.jsonl'
    },
    'gaokao_biology': {
        "test": HEAD+'gaokao-biology.jsonl'
    },
    'gaokao_chemistry': {
        "test": HEAD+'gaokao-chemistry.jsonl'
    },
    'gaokao_chinese': {
        "test": HEAD+'gaokao-chinese.jsonl'
    },
    'gaokao_chemistry': {
        "test": HEAD+'gaokao-chemistry.jsonl'
    },
    'gaokao_english': {
        "test": HEAD+'gaokao-english.jsonl'
    },
    'gaokao_geography': {
        "test": HEAD+'gaokao-geography.jsonl'
    },
    'gaokao_history': {
        "test": HEAD+'gaokao-history.jsonl'
    },
    'gaokao_physics': {
        "test": HEAD+'gaokao-physics.jsonl'
    },
    'gaokao_mathqa': {
        "test": HEAD+'gaokao-mathqa.jsonl'
    },
    'gaokao_mathcloze': {
        "test": HEAD+'gaokao-mathcloze.jsonl'
    },
    'few_shot': {
        'few_shot':'https://raw.githubusercontent.com/microsoft/AGIEval/main/data/few_shot_prompts.csv'
    }
    
}


class AgiEval(datasets.GeneratorBasedBuilder):
    """TODO: Short description of my dataset."""

    # VERSION = datasets.Version("2.0.1")
    # 25/08/2023: Removed row 56 of `sat_en`(label > num_of_choices).
    BUILDER_CONFIGS = []
    BUILDER_CONFIGS = [datasets.BuilderConfig(
            name=e,
            version=datasets.Version("2.0.1"),
            description="",
        ) for e in list(_URLS.keys()) if e!="few_shot"
    ]
    DEFAULT_CONFIG_NAME = "aqua_rat"

    def _info(self):

        if self.config.name in ["aqua_rat", 'lsat_lr', 'lsat_rc', 'lsat_ar']:
            features = datasets.Features(
                {
                    "passage": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "options": datasets.features.Sequence(datasets.Value("string")),
                    "label": datasets.ClassLabel(num_classes=5, names=["A", "B", "C", "D", "E"]),
                    "solution": datasets.Value("string"),
                }
            )
        elif self.config.name in ["sat_math", "sat_en_without_passage", "sat_en", "logiqa_en", "logiqa_zh", 'gaokao_mathqa', 'gaokao_chinese', 'gaokao_history', 'gaokao_geography', 'gaokao_biology', 'gaokao_chemistry', 'gaokao_english']:
            features = datasets.Features(
                {
                     "passage": datasets.Value("string"),
                     "question": datasets.Value("string"),
                     "options": datasets.features.Sequence(datasets.Value("string")),
                     "label": datasets.ClassLabel(num_classes=4, names=["A", "B", "C", "D"]),
                     "solution": datasets.Value("string"),
                 }
            )
        elif self.config.name == "math":
            features = datasets.Features(
                {
                     "passage": datasets.Value("string"),
                     "question": datasets.Value("string"),
                     "answer": datasets.Value("string"),
                     "solution": datasets.Value("string"),
                     "level": datasets.Value("int32"),
                     "type": datasets.Value("string"),
                 }
            )
        elif self.config.name in ['gaokao_physics', 'jec_qa_ca', 'jec_qa_kd']:
            features = datasets.Features(
                {
                     "passage": datasets.Value("string"),   
                     "question": datasets.Value("string"),
                     "options": datasets.features.Sequence(datasets.Value("string")),
                     "label": datasets.features.Sequence(datasets.Value("string")),
                     "solution": datasets.Value("string"),
                 }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        _urls = _URLS[self.config.name]
        urls = {
            "test": _urls["test"],
            "few_shot": _URLS["few_shot"]["few_shot"],
        }
        data_dir = dl_manager.download_and_extract(urls)
        splits = [
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": data_dir["test"], "split": "test"},
            ),
        ]
        splits.append(datasets.SplitGenerator(
            name="few_shot",
            gen_kwargs={"filepath": data_dir["few_shot"], "split": "few_shot"},
        ))

        return splits

    def _generate_examples(self, filepath, split):
        # Mapping for column names in CSV to dataset names
        names = {'aqua_rat': 'aqua-rat', 'sat_en': 'sat-en', 'sat_math': 'sat-math',
                 'lsat_ar': 'lsat-ar', 'lsat_lr': 'lsat-lr', 'lsat_rc': 'lsat-rc',
                 'logiqa_en': 'logiqa-en', 'logiqa_zh': 'logiqa-zh', 'math': 'math',
                'sat_en_without_passage': 'sat-en', 'jec_qa_ca':'jec-qa-ca', 
                'jec_qa_kd':'jec-qa-kd', 'gaokao_mathqa':'gaokao-mathqa', 'gaokao_chinese':'gaokao-chinese', 
                 'gaokao_history':'gaokao-history', 'gaokao_geography':'gaokao-geography', 
                 'gaokao_biology':'gaokao-biology', 'gaokao_chemistry':'gaokao-chemistry', 
                 'gaokao_english':"gaokao-english", "gaokao_physics":"gaokao-physics"}

        if split == "few_shot":
            # Load the data from the CSV
            df = pd.read_csv(filepath, keep_default_na=False)

            # Extract samples and explanations
            samples = df[df.index % 2 == 0].reset_index(drop=True)
            explanations = df[df.index % 2 != 0].reset_index(drop=True)

            for key in range(samples.shape[0]):
                try:
                    data = ast.literal_eval(samples[names[self.config.name]][key])
                    explanation_row = explanations[names[self.config.name]][key]
                    if self.config.name in ["aqua_rat", 'lsat_lr', 'lsat_rc', 'lsat_ar', "sat_math", "sat_en_without_passage", "logiqa_en","logiqa_zh", "sat_en"]:
                        label_index = "ABCDE".index(data["label"])
                        if label_index > len(data["options"]) - 1:
                            continue
                        yield key, {
                            "passage": data["passage"],
                            "question": data["question"],
                            "options": data["options"],
                            "label": data["label"],
                            "solution": str(explanation_row),
                        }
                    elif self.config.name == "math":
                        if not data.get("level"):
                            data["level"] = data['other']['level']
                        if not data.get("type"):
                            data["type"] = data['other']['type']
                        yield key, {
                            "passage": data["passage"],
                            "question": data["question"],
                            "answer": data["answer"],
                            "level": data["level"],
                            "type": data["type"],
                            "solution": str(explanation_row),
                        }
                    elif self.config.name in ['gaokao_physics', 'jec_qa_ca', 'jec_qa_kd']:
                        if isinstance(data["label"], str):
                            yield key, {
                                 "passage": data["passage"],
                                 "question": data["question"],
                                 "options": data["options"], 
                                 "label": [data["label"]],
                                 "solution": str(explanation_row),
                            }
                        else:
                            yield key, {
                                 "passage": data["passage"],
                                 "question": data["question"],
                                 "options": data["options"], 
                                 "label": data["label"],
                                 "solution": str(explanation_row),
                            }
                    elif self.config.name in ['gaokao_mathqa', 'gaokao_chinese', 'gaokao_history', 'gaokao_geography', 'gaokao_biology', 'gaokao_chemistry', 'gaokao_english']:
                        if data["label"] in ["A", "B", "C", "D", "E"]:
                            yield key, {
                                 "passage": data["passage"],   
                                 "question": data["question"],
                                 "options": data["options"],
                                 "label": data["label"],
                                 "solution": str(explanation_row),
                                 }
                except:
                    pass
        else:
            with open(filepath, encoding="utf-8") as f:
                for key, row in enumerate(f):
                    data = json.loads(row)
                    if self.config.name in ["aqua_rat", 'lsat_lr', 'lsat_rc', 'lsat_ar', "sat_math", "sat_en_without_passage", "logiqa_en","logiqa_zh", "sat_en"]:
                        label_index = "ABCDE".index(data["label"])
                        if label_index > len(data["options"]) - 1:
                            continue
                        yield key, {
                            "passage": data["passage"],
                            "question": data["question"],
                            "options": data["options"],
                            "label": data["label"],
                            "solution": data["other"]["solution"] if data["other"] is not None else data["label"],
                        }
                    elif self.config.name == "math":
                        if not data.get("level"):
                            data["level"] = data['other']['level']
                        if not data.get("type"):
                            data["type"] = data['other']['type']
                        yield key, {
                            "passage": data["passage"],
                            "question": data["question"],
                            "answer": data["answer"],
                            "solution": data["other"]["solution"],
                            "level": data["level"],
                            "type": data["type"],
                        }
                    elif self.config.name in ['gaokao_physics', 'jec_qa_ca', 'jec_qa_kd']:
                        if isinstance(data["label"], str):
                            yield key, {
                                 "passage": data["passage"],
                                 "question": data["question"],
                                 "options": data["options"], 
                                 "label": [data["label"]],
                                 "solution": data["label"],
                            }
                        else:
                            yield key, {
                                 "passage": data["passage"],
                                 "question": data["question"],
                                 "options": data["options"], 
                                 "label": data["label"],
                                 "solution": data["label"],
                            }
                    elif self.config.name in ['gaokao_mathqa', 'gaokao_chinese', 'gaokao_history', 'gaokao_geography', 'gaokao_biology', 'gaokao_chemistry', 'gaokao_english']:
                        if data["label"] in ["A", "B", "C", "D", "E"]:
                            yield key, {
                                 "passage": data["passage"],   
                                 "question": data["question"],
                                 "options": data["options"],
                                 "label": data["label"],
                                 "solution": data["label"],
                                 }