gabrielaltay commited on
Commit
106cf39
1 Parent(s): 7f31ee9

upload hubscripts/scicite_hub.py to hub from bigbio repo

Browse files
Files changed (1) hide show
  1. scicite.py +235 -0
scicite.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ A dataset loader for the SciCite dataset.
18
+
19
+ SciCite is a dataset of 11K manually annotated citation intents based on
20
+ citation context in the computer science and biomedical domains.
21
+
22
+ Some of the code in this module is based on the corresponding module in the
23
+ datasets library.
24
+ https://github.com/huggingface/datasets/blob/master/datasets/scicite/scicite.py
25
+
26
+ In the source schema, we follow the datasets implementation and replace
27
+ missing values.
28
+ TODO: Use standard BigBio missing values.
29
+ """
30
+
31
+ import json
32
+ import os
33
+ from typing import Dict, List, Tuple
34
+
35
+ import datasets
36
+ import numpy as np
37
+
38
+ from .bigbiohub import text.features
39
+ from .bigbiohub import BigBioConfig
40
+ from .bigbiohub import Tasks
41
+
42
+ _LANGUAGES = ['English']
43
+ _PUBMED = False
44
+ _LOCAL = False
45
+ _CITATION = """\
46
+ @inproceedings{cohan:naacl19,
47
+ author = {Arman Cohan and Waleed Ammar and Madeleine van Zuylen and Field Cady},
48
+ title = {Structural Scaffolds for Citation Intent Classification in Scientific Publications},
49
+ booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
50
+ year = {2019},
51
+ url = {https://aclanthology.org/N19-1361/},
52
+ doi = {10.18653/v1/N19-1361},
53
+ }
54
+ """
55
+
56
+ _DATASETNAME = "scicite"
57
+ _DISPLAYNAME = "SciCite"
58
+
59
+ _DESCRIPTION = """\
60
+ SciCite is a dataset of 11K manually annotated citation intents based on
61
+ citation context in the computer science and biomedical domains.
62
+ """
63
+
64
+ _HOMEPAGE = "https://allenai.org/data/scicite"
65
+
66
+ _LICENSE = 'License information unavailable'
67
+
68
+ _URLS = {
69
+ _DATASETNAME: "https://s3-us-west-2.amazonaws.com/ai2-s2-research/scicite/scicite.tar.gz",
70
+ }
71
+
72
+ _SUPPORTED_TASKS = [Tasks.TEXT_CLASSIFICATION]
73
+
74
+ _SOURCE_VERSION = "1.0.0"
75
+
76
+ _BIGBIO_VERSION = "1.0.0"
77
+
78
+
79
+ class SciciteDataset(datasets.GeneratorBasedBuilder):
80
+ """SciCite is a dataset of 11K manually annotated citation intents based on
81
+ citation context in the computer science and biomedical domains."""
82
+
83
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
84
+ BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
85
+
86
+ # You will be able to load the "source" or "bigbio" configurations with
87
+ # ds_source = datasets.load_dataset('scicite', name='source')
88
+ # ds_bigbio = datasets.load_dataset('scicite', name='bigbio')
89
+
90
+ BUILDER_CONFIGS = [
91
+ BigBioConfig(
92
+ name="scicite_source",
93
+ version=SOURCE_VERSION,
94
+ description="SciCite source schema",
95
+ schema="source",
96
+ subset_id="scicite",
97
+ ),
98
+ BigBioConfig(
99
+ name="scicite_bigbio_text",
100
+ version=BIGBIO_VERSION,
101
+ description="SciCite BigBio schema",
102
+ schema="bigbio_text",
103
+ subset_id="scicite",
104
+ ),
105
+ ]
106
+
107
+ DEFAULT_CONFIG_NAME = "scicite_source"
108
+
109
+ def _info(self) -> datasets.DatasetInfo:
110
+ if self.config.schema == "source":
111
+ features = datasets.Features(
112
+ {
113
+ "source": datasets.Value("string"),
114
+ "citeStart": datasets.Value("int64"),
115
+ "sectionName": datasets.Value("string"),
116
+ "string": datasets.Value("string"),
117
+ "citeEnd": datasets.Value("int64"),
118
+ "label": datasets.features.ClassLabel(
119
+ names=["method", "background", "result"]
120
+ ),
121
+ "label_confidence": datasets.Value("float"),
122
+ "label2": datasets.features.ClassLabel(
123
+ names=["supportive", "not_supportive", "cant_determine", "none"]
124
+ ),
125
+ "label2_confidence": datasets.Value("float"),
126
+ "citingPaperId": datasets.Value("string"),
127
+ "citedPaperId": datasets.Value("string"),
128
+ "isKeyCitation": datasets.Value("bool"),
129
+ "id": datasets.Value("string"),
130
+ "unique_id": datasets.Value("string"),
131
+ "excerpt_index": datasets.Value("int64"),
132
+ }
133
+ )
134
+ elif self.config.schema == "bigbio_text":
135
+ features = text.features
136
+ else:
137
+ raise ValueError("Unrecognized schema: %s" % self.config.schema)
138
+
139
+ return datasets.DatasetInfo(
140
+ description=_DESCRIPTION,
141
+ features=features,
142
+ homepage=_HOMEPAGE,
143
+ license=str(_LICENSE),
144
+ citation=_CITATION,
145
+ )
146
+
147
+ def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
148
+ """Returns SplitGenerators."""
149
+ urls = _URLS[_DATASETNAME]
150
+ data_dir = dl_manager.download_and_extract(urls)
151
+
152
+ return [
153
+ datasets.SplitGenerator(
154
+ name=datasets.Split.TRAIN,
155
+ gen_kwargs={
156
+ "filepath": os.path.join(data_dir, "scicite", "train.jsonl"),
157
+ "split": "train",
158
+ },
159
+ ),
160
+ datasets.SplitGenerator(
161
+ name=datasets.Split.TEST,
162
+ gen_kwargs={
163
+ "filepath": os.path.join(data_dir, "scicite", "test.jsonl"),
164
+ "split": "test",
165
+ },
166
+ ),
167
+ datasets.SplitGenerator(
168
+ name=datasets.Split.VALIDATION,
169
+ gen_kwargs={
170
+ "filepath": os.path.join(data_dir, "scicite", "dev.jsonl"),
171
+ "split": "dev",
172
+ },
173
+ ),
174
+ ]
175
+
176
+ def _generate_examples(self, filepath, split: str) -> Tuple[int, Dict]:
177
+ """Yields examples as (key, example) tuples."""
178
+
179
+ with open(filepath, "r") as data_file:
180
+ examples = [json.loads(line) for line in data_file]
181
+
182
+ # Preprocesses examples
183
+ keys = set()
184
+ for example in examples:
185
+ # Fixes duplicate keys
186
+ if example["unique_id"] in keys:
187
+ example["unique_id"] = example["unique_id"] + "_duplicate"
188
+ else:
189
+ keys.add(example["unique_id"])
190
+
191
+ if self.config.schema == "source":
192
+ for example in examples:
193
+ yield str(example["unique_id"]), {
194
+ "string": example["string"],
195
+ "label": str(example["label"]),
196
+ "sectionName": str(example["sectionName"]),
197
+ "citingPaperId": str(example["citingPaperId"]),
198
+ "citedPaperId": str(example["citedPaperId"]),
199
+ "excerpt_index": int(example["excerpt_index"]),
200
+ "isKeyCitation": bool(example["isKeyCitation"]),
201
+ "label2": str(example.get("label2", "none")),
202
+ "citeEnd": _safe_int(example["citeEnd"]),
203
+ "citeStart": _safe_int(example["citeStart"]),
204
+ "source": str(example["source"]),
205
+ "label_confidence": float(
206
+ example.get("label_confidence", np.nan)
207
+ ),
208
+ "label2_confidence": float(
209
+ example.get("label2_confidence", np.nan)
210
+ ),
211
+ "id": str(example["id"]),
212
+ "unique_id": str(example["unique_id"]),
213
+ }
214
+
215
+ elif self.config.schema == "bigbio_text":
216
+ for example in examples:
217
+ if "label2" in example:
218
+ labels = [example["label"], example["label2"]]
219
+ else:
220
+ labels = [example["label"]]
221
+
222
+ yield str(example["unique_id"]), {
223
+ "id": example["unique_id"],
224
+ "document_id": example["citingPaperId"],
225
+ "text": example["string"],
226
+ "labels": labels,
227
+ }
228
+
229
+
230
+ def _safe_int(a):
231
+ try:
232
+ # skip NaNs
233
+ return int(a)
234
+ except ValueError:
235
+ return -1