baber commited on
Commit
3aac9db
1 Parent(s): dbc6d51

Upload logiqa_test.py

Browse files
Files changed (1) hide show
  1. logiqa_test.py +204 -0
logiqa_test.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """LogiQA dataset."""
15
+
16
+
17
+ import datasets
18
+ import json
19
+
20
+
21
+ _CITATION = """\
22
+ @ARTICLE{10174688,
23
+ author={Liu, Hanmeng and Liu, Jian and Cui, Leyang and Teng, Zhiyang and Duan, Nan and Zhou, Ming and Zhang, Yue},
24
+ journal={IEEE/ACM Transactions on Audio, Speech, and Language Processing},
25
+ title={LogiQA 2.0 — An Improved Dataset for Logical Reasoning in Natural Language Understanding},
26
+ year={2023},
27
+ volume={},
28
+ number={},
29
+ pages={1-16},
30
+ doi={10.1109/TASLP.2023.3293046}}
31
+ """
32
+
33
+ _DESCRIPTION = """\
34
+ 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
35
+ """
36
+
37
+ _HOMEPAGE = "https://github.com/csitfun/LogiQA2.0/tree/main"
38
+
39
+ _LICENSE = (
40
+ "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License"
41
+ )
42
+
43
+ _URLS = {
44
+ "logiqa2": {
45
+ "train": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa/DATA/LOGIQA/train.txt",
46
+ "validation": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa/DATA/LOGIQA/dev.txt",
47
+ "test": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa/DATA/LOGIQA/test.txt",
48
+ },
49
+ "logiqa2_zh": {
50
+ "train": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa/DATA/LOGIQA/train_zh.txt",
51
+ "validation": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa/DATA/LOGIQA/dev_zh.txt",
52
+ "test": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa/DATA/LOGIQA/test_zh.txt",
53
+ },
54
+ "logiqa2_nli": {
55
+ "train": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa2nli/DATA/QA2NLI/train.txt",
56
+ "validation": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa2nli/DATA/QA2NLI/dev.txt",
57
+ "test": "https://raw.githubusercontent.com/csitfun/LogiQA2.0/main/logiqa2nli/DATA/QA2NLI/test.txt",
58
+ },
59
+ }
60
+
61
+
62
+ class LogiQA2(datasets.GeneratorBasedBuilder):
63
+ """TODO: Short description of my dataset."""
64
+
65
+ VERSION = datasets.Version("2.0.0")
66
+
67
+ # This is an example of a dataset with multiple configurations.
68
+ # If you don't want/need to define several sub-sets in your dataset,
69
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
70
+
71
+ # If you need to make complex sub-parts in the datasets with configurable options
72
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
73
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
74
+
75
+ # You will be able to load one or the other configurations in the following list with
76
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
77
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
78
+ BUILDER_CONFIGS = [
79
+ datasets.BuilderConfig(
80
+ name="logiqa2",
81
+ version=VERSION,
82
+ description="The LogiQA multiple answer dataset translated in English from Chinese.",
83
+ ),
84
+ datasets.BuilderConfig(
85
+ name="logiqa2_zh",
86
+ version=VERSION,
87
+ description="The original LogiQA multiple answer dataset in Chinese.",
88
+ ),
89
+ datasets.BuilderConfig(
90
+ name="logiqa2_nli",
91
+ version=VERSION,
92
+ description="The NLI part of LogiQA2.0 dataset",
93
+ ),
94
+ ]
95
+ DEFAULT_CONFIG_NAME = "logiqa2"
96
+
97
+ def _info(self):
98
+
99
+ if self.config.name == "logiqa2_zh":
100
+ features = datasets.Features(
101
+ {
102
+ "answer": datasets.Value("int32"),
103
+ "text": datasets.Value("string"),
104
+ # "type" is a dict with arbitrary keys and values
105
+ "question": datasets.Value("string"),
106
+ "options": datasets.features.Sequence(datasets.Value("string")),
107
+ }
108
+ )
109
+ # # major_premise (maybe minor) is sometimes str, sometimes list
110
+ # # can't get it to work.
111
+ elif self.config.name == "logiqa2_nli":
112
+ features = datasets.Features(
113
+ {
114
+ "label": datasets.Value("string"),
115
+ "major_premise": datasets.features.Sequence(
116
+ datasets.Value("string"),
117
+ ),
118
+ "minor_premise": datasets.features.Sequence(
119
+ datasets.Value("string"),
120
+ ),
121
+ "conclusion": datasets.Value("string"),
122
+ }
123
+ )
124
+ else:
125
+ features = datasets.Features(
126
+ {
127
+ "id": datasets.Value("int32"),
128
+ "answer": datasets.Value("int32"),
129
+ "text": datasets.Value("string"),
130
+ # "type" is a dict with arbitrary keys and values
131
+ "type": datasets.Value("string"),
132
+ "question": datasets.Value("string"),
133
+ "options": datasets.features.Sequence(datasets.Value("string")),
134
+ }
135
+ )
136
+ return datasets.DatasetInfo(
137
+ description=_DESCRIPTION,
138
+ features=features,
139
+ homepage=_HOMEPAGE,
140
+ license=_LICENSE,
141
+ citation=_CITATION,
142
+ )
143
+
144
+ def _split_generators(self, dl_manager):
145
+ _urls = _URLS[self.config.name]
146
+ urls = {
147
+ "train": _urls["train"],
148
+ "test": _urls["test"],
149
+ "validation": _urls["validation"],
150
+ }
151
+ data_dir = dl_manager.download_and_extract(urls)
152
+ return [
153
+ datasets.SplitGenerator(
154
+ name=datasets.Split.TRAIN,
155
+ # These kwargs will be passed to _generate_examples
156
+ gen_kwargs={
157
+ "filepath": data_dir["train"],
158
+ "split": "train",
159
+ },
160
+ ),
161
+ datasets.SplitGenerator(
162
+ name=datasets.Split.TEST,
163
+ # These kwargs will be passed to _generate_examples
164
+ gen_kwargs={"filepath": data_dir["test"], "split": "test"},
165
+ ),
166
+ datasets.SplitGenerator(
167
+ name=datasets.Split.VALIDATION,
168
+ # These kwargs will be passed to _generate_examples
169
+ gen_kwargs={
170
+ "filepath": data_dir["validation"],
171
+ "split": "validation",
172
+ },
173
+ ),
174
+ ]
175
+
176
+ def _generate_examples(self, filepath, split):
177
+ with open(filepath, encoding="utf-8") as f:
178
+ for key, row in enumerate(f):
179
+ data = json.loads(row)
180
+
181
+ if self.config.name == "logiqa2_zh":
182
+ yield key, {
183
+ "answer": data["answer"],
184
+ "text": data["text"],
185
+ "question": data["question"],
186
+ "options": data["options"],
187
+ }
188
+ elif self.config.name == "logiqa2_nli":
189
+ yield key, {
190
+ "label": data["label"],
191
+ "major_premise": data["major_premise"],
192
+ "minor_premise": data["minor_premise"],
193
+ "conclusion": data["conclusion"],
194
+ }
195
+
196
+ else:
197
+ yield key, {
198
+ "id": data["id"],
199
+ "answer": data["answer"],
200
+ "text": data["text"],
201
+ "type": data["type"],
202
+ "question": data["question"],
203
+ "options": data["options"],
204
+ }