gabrielaltay commited on
Commit
e5e4e82
1 Parent(s): 1b67cf1

upload hubscripts/pubhealth_hub.py to hub from bigbio repo

Browse files
Files changed (1) hide show
  1. pubhealth.py +209 -0
pubhealth.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 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
+ A dataset of 11,832 claims for fact- checking, which are related a range of health topics
17
+ including biomedical subjects (e.g., infectious diseases, stem cell research), government healthcare policy
18
+ (e.g., abortion, mental health, women’s health), and other public health-related stories
19
+ """
20
+
21
+ import csv
22
+ import os
23
+ from pathlib import Path
24
+
25
+ import datasets
26
+
27
+ from .bigbiohub import pairs_features
28
+ from .bigbiohub import BigBioConfig
29
+ from .bigbiohub import Tasks
30
+
31
+ logger = datasets.utils.logging.get_logger(__name__)
32
+
33
+ _LANGUAGES = ['English']
34
+ _PUBMED = False
35
+ _LOCAL = False
36
+ _CITATION = """\
37
+ @article{kotonya2020explainable,
38
+ title={Explainable automated fact-checking for public health claims},
39
+ author={Kotonya, Neema and Toni, Francesca},
40
+ journal={arXiv preprint arXiv:2010.09926},
41
+ year={2020}
42
+ }
43
+ """
44
+
45
+ _DATASETNAME = "pubhealth"
46
+ _DISPLAYNAME = "PUBHEALTH"
47
+
48
+ _DESCRIPTION = """\
49
+ A dataset of 11,832 claims for fact- checking, which are related a range of health topics
50
+ including biomedical subjects (e.g., infectious diseases, stem cell research), government healthcare policy
51
+ (e.g., abortion, mental health, women’s health), and other public health-related stories
52
+ """
53
+
54
+ _HOMEPAGE = "https://github.com/neemakot/Health-Fact-Checking/tree/master/data"
55
+
56
+ _LICENSE = 'MIT License'
57
+
58
+ _URLs = {
59
+ _DATASETNAME: "https://drive.google.com/uc?export=download&id=1eTtRs5cUlBP5dXsx-FTAlmXuB6JQi2qj"
60
+ }
61
+
62
+ _SUPPORTED_TASKS = [Tasks.TEXT_CLASSIFICATION]
63
+ _SOURCE_VERSION = "1.0.0"
64
+ _BIGBIO_VERSION = "1.0.0"
65
+
66
+ _CLASSES = ["true", "false", "unproven", "mixture"]
67
+
68
+
69
+ class PUBHEALTHDataset(datasets.GeneratorBasedBuilder):
70
+ """Pubhealth text classification dataset"""
71
+
72
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
73
+ BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
74
+
75
+ BUILDER_CONFIGS = [
76
+ BigBioConfig(
77
+ name="pubhealth_source",
78
+ version=SOURCE_VERSION,
79
+ description="PUBHEALTH source schema",
80
+ schema="source",
81
+ subset_id="pubhealth",
82
+ ),
83
+ BigBioConfig(
84
+ name="pubhealth_bigbio_pairs",
85
+ version=BIGBIO_VERSION,
86
+ description="PUBHEALTH BigBio schema",
87
+ schema="bigbio_pairs",
88
+ subset_id="pubhealth",
89
+ ),
90
+ ]
91
+
92
+ DEFAULT_CONFIG_NAME = "pubhealth_source"
93
+
94
+ def _info(self):
95
+
96
+ if self.config.schema == "source":
97
+ features = datasets.Features(
98
+ {
99
+ "claim_id": datasets.Value("string"),
100
+ "claim": datasets.Value("string"),
101
+ "date_published": datasets.Value("string"),
102
+ "explanation": datasets.Value("string"),
103
+ "fact_checkers": datasets.Value("string"),
104
+ "main_text": datasets.Value("string"),
105
+ "sources": datasets.Value("string"),
106
+ "label": datasets.ClassLabel(names=_CLASSES),
107
+ "subjects": datasets.Value("string"),
108
+ }
109
+ )
110
+
111
+ # Using in entailment schema
112
+ elif self.config.schema == "bigbio_pairs":
113
+ features = pairs_features
114
+
115
+ return datasets.DatasetInfo(
116
+ description=_DESCRIPTION,
117
+ features=features,
118
+ homepage=_HOMEPAGE,
119
+ license=str(_LICENSE),
120
+ citation=_CITATION,
121
+ )
122
+
123
+ def _split_generators(self, dl_manager):
124
+ """Returns SplitGenerators."""
125
+ urls = _URLs[_DATASETNAME]
126
+ data_dir = Path(dl_manager.download_and_extract(urls))
127
+
128
+ return [
129
+ datasets.SplitGenerator(
130
+ name=datasets.Split.TRAIN,
131
+ gen_kwargs={
132
+ "filepath": os.path.join(data_dir, "PUBHEALTH/train.tsv"),
133
+ "split": "train",
134
+ },
135
+ ),
136
+ datasets.SplitGenerator(
137
+ name=datasets.Split.TEST,
138
+ gen_kwargs={
139
+ "filepath": os.path.join(data_dir, "PUBHEALTH/test.tsv"),
140
+ "split": "test",
141
+ },
142
+ ),
143
+ datasets.SplitGenerator(
144
+ name=datasets.Split.VALIDATION,
145
+ gen_kwargs={
146
+ "filepath": os.path.join(data_dir, "PUBHEALTH/dev.tsv"),
147
+ "split": "validation",
148
+ },
149
+ ),
150
+ ]
151
+
152
+ def _generate_examples(self, filepath, split):
153
+ """Yields examples as (key, example) tuples."""
154
+
155
+ with open(filepath, encoding="utf-8") as csv_file:
156
+ csv_reader = csv.reader(
157
+ csv_file,
158
+ quotechar='"',
159
+ delimiter="\t",
160
+ quoting=csv.QUOTE_NONE,
161
+ skipinitialspace=True,
162
+ )
163
+ next(csv_reader, None) # remove column headers
164
+ for id_, row in enumerate(csv_reader):
165
+ # train.tsv/dev.tsv only has 9 columns
166
+ # test.tsv has an additional column at the beginning
167
+ # Some entries are malformed, will log skipped lines
168
+ if len(row) < 9:
169
+ logger.info("Line %s is malformed", id_)
170
+ continue
171
+ (
172
+ claim_id,
173
+ claim,
174
+ date_published,
175
+ explanation,
176
+ fact_checkers,
177
+ main_text,
178
+ sources,
179
+ label,
180
+ subjects,
181
+ ) = row[
182
+ -9:
183
+ ] # only take last 9 columns to fix test.tsv disparity
184
+
185
+ if label not in _CLASSES:
186
+ logger.info("Line %s is missing label", id_)
187
+ continue
188
+
189
+ if self.config.schema == "source":
190
+ yield id_, {
191
+ "claim_id": claim_id,
192
+ "claim": claim,
193
+ "date_published": date_published,
194
+ "explanation": explanation,
195
+ "fact_checkers": fact_checkers,
196
+ "main_text": main_text,
197
+ "sources": sources,
198
+ "label": label,
199
+ "subjects": subjects,
200
+ }
201
+
202
+ elif self.config.schema == "bigbio_pairs":
203
+ yield id_, {
204
+ "id": id_, # uid is an unique identifier for every record that starts from 0
205
+ "document_id": claim_id,
206
+ "text_1": claim,
207
+ "text_2": explanation,
208
+ "label": label,
209
+ }