gabrielaltay commited on
Commit
8a85cab
1 Parent(s): 1b7c69c

Upload tmp-scitail.py

Browse files
Files changed (1) hide show
  1. tmp-scitail.py +175 -0
tmp-scitail.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """
17
+ The SciTail dataset is an entailment dataset created from multiple-choice science exams and
18
+ web sentences. Each question and the correct answer choice are converted into an assertive
19
+ statement to form the hypothesis. We use information retrieval to obtain relevant text from
20
+ a large text corpus of web sentences, and use these sentences as a premise P. We crowdsource
21
+ the annotation of such premise-hypothesis pair as supports (entails) or not (neutral), in order
22
+ to create the SciTail dataset. The dataset contains 27,026 examples with 10,101 examples with
23
+ entails label and 16,925 examples with neutral label.
24
+ """
25
+
26
+ import os
27
+
28
+ import datasets
29
+ import pandas as pd
30
+
31
+ from bigbio.utils import schemas
32
+ from bigbio.utils.configs import BigBioConfig
33
+ from bigbio.utils.constants import Lang, Tasks
34
+ from bigbio.utils.license import Licenses
35
+
36
+ _LANGUAGES = [Lang.EN]
37
+ _PUBMED = False
38
+ _LOCAL = False
39
+ _CITATION = """\
40
+ @inproceedings{scitail,
41
+ author = {Tushar Khot and Ashish Sabharwal and Peter Clark},
42
+ booktitle = {AAAI}
43
+ title = {SciTail: A Textual Entailment Dataset from Science Question Answering},
44
+ year = {2018}
45
+ }
46
+ """
47
+
48
+ _DATASETNAME = "scitail"
49
+ _DISPLAYNAME = "SciTail"
50
+
51
+ _DESCRIPTION = """\
52
+ The SciTail dataset is an entailment dataset created from multiple-choice science exams and
53
+ web sentences. Each question and the correct answer choice are converted into an assertive
54
+ statement to form the hypothesis. We use information retrieval to obtain relevant text from
55
+ a large text corpus of web sentences, and use these sentences as a premise P. We crowdsource
56
+ the annotation of such premise-hypothesis pair as supports (entails) or not (neutral), in order
57
+ to create the SciTail dataset. The dataset contains 27,026 examples with 10,101 examples with
58
+ entails label and 16,925 examples with neutral label.
59
+ """
60
+
61
+ _HOMEPAGE = "https://allenai.org/data/scitail"
62
+
63
+ _LICENSE = Licenses.APACHE_2p0
64
+
65
+ _URLS = {
66
+ _DATASETNAME: "https://ai2-public-datasets.s3.amazonaws.com/scitail/SciTailV1.1.zip",
67
+ }
68
+
69
+ _SUPPORTED_TASKS = [Tasks.TEXTUAL_ENTAILMENT]
70
+
71
+ _SOURCE_VERSION = "1.1.0"
72
+
73
+ _BIGBIO_VERSION = "1.0.0"
74
+
75
+
76
+ LABEL_MAP = {"entails": "entailment", "neutral": "neutral"}
77
+
78
+
79
+ class SciTailDataset(datasets.GeneratorBasedBuilder):
80
+ """TODO: Short description of my dataset."""
81
+
82
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
83
+ BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
84
+
85
+ BUILDER_CONFIGS = [
86
+ BigBioConfig(
87
+ name="scitail_source",
88
+ version=SOURCE_VERSION,
89
+ description="SciTail source schema",
90
+ schema="source",
91
+ subset_id="scitail",
92
+ ),
93
+ BigBioConfig(
94
+ name="scitail_bigbio_te",
95
+ version=BIGBIO_VERSION,
96
+ description="SciTail BigBio schema",
97
+ schema="bigbio_te",
98
+ subset_id="scitail",
99
+ ),
100
+ ]
101
+
102
+ DEFAULT_CONFIG_NAME = "scitail_source"
103
+
104
+ def _info(self):
105
+
106
+ if self.config.schema == "source":
107
+ features = datasets.Features(
108
+ {
109
+ "id": datasets.Value("string"),
110
+ "premise": datasets.Value("string"),
111
+ "hypothesis": datasets.Value("string"),
112
+ "label": datasets.Value("string"),
113
+ }
114
+ )
115
+
116
+ elif self.config.schema == "bigbio_te":
117
+ features = schemas.entailment_features
118
+
119
+ return datasets.DatasetInfo(
120
+ description=_DESCRIPTION,
121
+ features=features,
122
+ homepage=_HOMEPAGE,
123
+ license=str(_LICENSE),
124
+ citation=_CITATION,
125
+ )
126
+
127
+ def _split_generators(self, dl_manager):
128
+
129
+ urls = _URLS[_DATASETNAME]
130
+ data_dir = dl_manager.download_and_extract(urls)
131
+
132
+ return [
133
+ datasets.SplitGenerator(
134
+ name=datasets.Split.TRAIN,
135
+ gen_kwargs={
136
+ "filepath": os.path.join(
137
+ data_dir, "SciTailV1.1", "tsv_format", "scitail_1.0_train.tsv"
138
+ ),
139
+ },
140
+ ),
141
+ datasets.SplitGenerator(
142
+ name=datasets.Split.TEST,
143
+ gen_kwargs={
144
+ "filepath": os.path.join(
145
+ data_dir, "SciTailV1.1", "tsv_format", "scitail_1.0_test.tsv"
146
+ ),
147
+ },
148
+ ),
149
+ datasets.SplitGenerator(
150
+ name=datasets.Split.VALIDATION,
151
+ gen_kwargs={
152
+ "filepath": os.path.join(
153
+ data_dir, "SciTailV1.1", "tsv_format", "scitail_1.0_dev.tsv"
154
+ ),
155
+ },
156
+ ),
157
+ ]
158
+
159
+ def _generate_examples(self, filepath):
160
+ # since examples can contain quotes mid text set quoting to QUOTE_NONE (3) when reading tsv
161
+ # e.g.: ... and apply specific "tools" to examples and ...
162
+ data = pd.read_csv(
163
+ filepath, sep="\t", names=["premise", "hypothesis", "label"], quoting=3
164
+ )
165
+ data["id"] = data.index
166
+
167
+ if self.config.schema == "source":
168
+ for _, row in data.iterrows():
169
+ yield row["id"], row.to_dict()
170
+
171
+ elif self.config.schema == "bigbio_te":
172
+ # normalize labels
173
+ data["label"] = data["label"].apply(lambda x: LABEL_MAP[x])
174
+ for _, row in data.iterrows():
175
+ yield row["id"], row.to_dict()