File size: 13,983 Bytes
aab6dc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""TODO(sciTail): Add a description here."""

from __future__ import absolute_import, division, print_function

import csv
import json
import os
import textwrap

import datasets


# TODO(sciTail): BibTeX citation
_CITATION = """\
inproceedings{scitail,
     Author = {Tushar Khot and Ashish Sabharwal and Peter Clark},
     Booktitle = {AAAI},
     Title = {{SciTail}: A Textual Entailment Dataset from Science Question Answering},
     Year = {2018}
}
"""

# TODO(sciTail):
_DESCRIPTION = """\
The SciTail dataset is an entailment dataset created from multiple-choice science exams and web sentences. Each question
and the correct answer choice are converted into an assertive statement to form the hypothesis. We use information
retrieval to obtain relevant text from a large text corpus of web sentences, and use these sentences as a premise P. We
crowdsource the annotation of such premise-hypothesis pair as supports (entails) or not (neutral), in order to create
the SciTail dataset. The dataset contains 27,026 examples with 10,101 examples with entails label and 16,925 examples
with neutral label
"""

_URL = "http://data.allenai.org.s3.amazonaws.com/downloads/SciTailV1.1.zip"


class ScitailConfig(datasets.BuilderConfig):

    """ BuilderConfig for Xquad"""

    def __init__(self, **kwargs):
        """

        Args:
            **kwargs: keyword arguments forwarded to super.
        """
        super(ScitailConfig, self).__init__(version=datasets.Version("1.1.0", ""), **kwargs)


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

    # TODO(sciTail): Set up version.
    VERSION = datasets.Version("1.1.0")
    BUILDER_CONFIGS = [
        ScitailConfig(
            name="snli_format",
            description="JSONL format used by SNLI with a JSON object corresponding to each entailment example in each line.",
        ),
        ScitailConfig(
            name="tsv_format", description="Tab-separated format with three columns: premise hypothesis label"
        ),
        ScitailConfig(
            name="dgem_format",
            description="Tab-separated format used by the DGEM model: premise hypothesis label hypothesis graph structure",
        ),
        ScitailConfig(
            name="predictor_format",
            description=textwrap.dedent(
                """\
          AllenNLP predictors work only with JSONL format. This folder contains the SciTail train/dev/test in JSONL format
        so that it can be loaded into the predictors. Each line is a JSON object with the following keys:
        gold_label : the example label from {entails, neutral}
        sentence1: the premise
        sentence2: the hypothesis
        sentence2_structure: structure from the hypothesis """
            ),
        ),
    ]

    def _info(self):
        # TODO(sciTail): Specifies the datasets.DatasetInfo object
        if self.config.name == "snli_format":
            return datasets.DatasetInfo(
                # This is the description that will appear on the datasets page.
                description=_DESCRIPTION,
                # datasets.features.FeatureConnectors
                features=datasets.Features(
                    {
                        "sentence1_binary_parse": datasets.Value("string"),
                        "sentence1_parse": datasets.Value("string"),
                        "sentence1": datasets.Value("string"),
                        "sentence2_parse": datasets.Value("string"),
                        "sentence2": datasets.Value("string"),
                        "annotator_labels": datasets.features.Sequence(datasets.Value("string")),
                        "gold_label": datasets.Value("string")
                        # These are the features of your dataset like images, labels ...
                    }
                ),
                # If there's a common (input, target) tuple from the features,
                # specify them here. They'll be used if as_supervised=True in
                # builder.as_dataset.
                supervised_keys=None,
                # Homepage of the dataset for documentation
                homepage="https://allenai.org/data/scitail",
                citation=_CITATION,
            )
        elif self.config.name == "tsv_format":
            return datasets.DatasetInfo(
                # This is the description that will appear on the datasets page.
                description=_DESCRIPTION,
                # datasets.features.FeatureConnectors
                features=datasets.Features(
                    {
                        "premise": datasets.Value("string"),
                        "hypothesis": datasets.Value("string"),
                        "label": datasets.Value("string")
                        # These are the features of your dataset like images, labels ...
                    }
                ),
                # If there's a common (input, target) tuple from the features,
                # specify them here. They'll be used if as_supervised=True in
                # builder.as_dataset.
                supervised_keys=None,
                # Homepage of the dataset for documentation
                homepage="https://allenai.org/data/scitail",
                citation=_CITATION,
            )
        elif self.config.name == "predictor_format":
            return datasets.DatasetInfo(
                # This is the description that will appear on the datasets page.
                description=_DESCRIPTION,
                # datasets.features.FeatureConnectors
                features=datasets.Features(
                    {
                        "answer": datasets.Value("string"),
                        "sentence2_structure": datasets.Value("string"),
                        "sentence1": datasets.Value("string"),
                        "sentence2": datasets.Value("string"),
                        "gold_label": datasets.Value("string"),
                        "question": datasets.Value("string")
                        # These are the features of your dataset like images, labels ...
                    }
                ),
                # If there's a common (input, target) tuple from the features,
                # specify them here. They'll be used if as_supervised=True in
                # builder.as_dataset.
                supervised_keys=None,
                # Homepage of the dataset for documentation
                homepage="https://allenai.org/data/scitail",
                citation=_CITATION,
            )
        elif self.config.name == "dgem_format":
            return datasets.DatasetInfo(
                # This is the description that will appear on the datasets page.
                description=_DESCRIPTION,
                # datasets.features.FeatureConnectors
                features=datasets.Features(
                    {
                        "premise": datasets.Value("string"),
                        "hypothesis": datasets.Value("string"),
                        "label": datasets.Value("string"),
                        "hypothesis_graph_structure": datasets.Value("string")
                        # These are the features of your dataset like images, labels ...
                    }
                ),
                # If there's a common (input, target) tuple from the features,
                # specify them here. They'll be used if as_supervised=True in
                # builder.as_dataset.
                supervised_keys=None,
                # Homepage of the dataset for documentation
                homepage="https://allenai.org/data/scitail",
                citation=_CITATION,
            )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # TODO(sciTail): Downloads the data and defines the splits
        # dl_manager is a datasets.download.DownloadManager that can be used to
        # download and extract URLs
        dl_dir = dl_manager.download_and_extract(_URL)
        data_dir = os.path.join(dl_dir, "SciTailV1.1")
        snli = os.path.join(data_dir, "snli_format")
        dgem = os.path.join(data_dir, "dgem_format")
        tsv = os.path.join(data_dir, "tsv_format")
        predictor = os.path.join(data_dir, "predictor_format")
        if self.config.name == "snli_format":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(snli, "scitail_1.0_train.txt")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(snli, "scitail_1.0_test.txt")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(snli, "scitail_1.0_dev.txt")},
                ),
            ]
        elif self.config.name == "tsv_format":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(tsv, "scitail_1.0_train.tsv")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(tsv, "scitail_1.0_test.tsv")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(tsv, "scitail_1.0_dev.tsv")},
                ),
            ]
        elif self.config.name == "predictor_format":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(predictor, "scitail_1.0_structure_train.jsonl")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(predictor, "scitail_1.0_structure_test.jsonl")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(predictor, "scitail_1.0_structure_dev.jsonl")},
                ),
            ]
        elif self.config.name == "dgem_format":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(dgem, "scitail_1.0_structure_train.tsv")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(dgem, "scitail_1.0_structure_test.tsv")},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={"filepath": os.path.join(dgem, "scitail_1.0_structure_dev.tsv")},
                ),
            ]

    def _generate_examples(self, filepath):
        """Yields examples."""
        # TODO(sciTail): Yields (key, example) tuples from the dataset
        with open(filepath, encoding="utf-8") as f:
            if self.config.name == "snli_format":
                for id_, row in enumerate(f):
                    data = json.loads(row)

                    yield id_, {
                        "sentence1_binary_parse": data["sentence1_binary_parse"],
                        "sentence1_parse": data["sentence1_parse"],
                        "sentence1": data["sentence1"],
                        "sentence2_parse": data["sentence2_parse"],
                        "sentence2": data["sentence2"],
                        "annotator_labels": data["annotator_labels"],
                        "gold_label": data["gold_label"],
                    }
            elif self.config.name == "tsv_format":
                data = csv.reader(f, delimiter="\t")
                for id_, row in enumerate(data):
                    yield id_, {"premise": row[0], "hypothesis": row[1], "label": row[2]}
            elif self.config.name == "dgem_format":
                data = csv.reader(f, delimiter="\t")
                for id_, row in enumerate(data):
                    yield id_, {
                        "premise": row[0],
                        "hypothesis": row[1],
                        "label": row[2],
                        "hypothesis_graph_structure": row[3],
                    }
            elif self.config.name == "predictor_format":
                for id_, row in enumerate(f):
                    data = json.loads(row)
                    yield id_, {
                        "answer": data["answer"],
                        "sentence2_structure": data["sentence2_structure"],
                        "sentence1": data["sentence1"],
                        "sentence2": data["sentence2"],
                        "gold_label": data["gold_label"],
                        "question": data["question"],
                    }