File size: 13,208 Bytes
21601b8 |
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# coding=utf-8
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
from typing import Dict, Iterator, List, Tuple
import bioc
import datasets
from bioc import biocxml
from .bigbiohub import kb_features
from .bigbiohub import BigBioConfig
from .bigbiohub import Tasks
_LANGUAGES = ['English']
_PUBMED = True
_LOCAL = False
_CITATION = """\
@Article{islamaj2021nlm,
title={NLM-Chem, a new resource for chemical entity recognition in PubMed full text literature},
author={Islamaj, Rezarta and Leaman, Robert and Kim, Sun and Kwon, Dongseop and Wei, Chih-Hsuan and Comeau, Donald C and Peng, Yifan and Cissel, David and Coss, Cathleen and Fisher, Carol and others},
journal={Scientific Data},
volume={8},
number={1},
pages={1--12},
year={2021},
publisher={Nature Publishing Group}
}
"""
_DATASETNAME = "nlmchem"
_DISPLAYNAME = "NLM-Chem"
_DESCRIPTION = """\
NLM-Chem corpus consists of 150 full-text articles from the PubMed Central Open Access dataset,
comprising 67 different chemical journals, aiming to cover a general distribution of usage of chemical
names in the biomedical literature.
Articles were selected so that human annotation was most valuable (meaning that they were rich in bio-entities,
and current state-of-the-art named entity recognition systems disagreed on bio-entity recognition.
"""
_HOMEPAGE = "https://biocreative.bioinformatics.udel.edu/tasks/biocreative-vii/track-2"
_LICENSE = 'Creative Commons Zero v1.0 Universal'
# files found here `https://ftp.ncbi.nlm.nih.gov/pub/lu/BC7-NLM-Chem-track/` have issues at extraction
# _URLs = {"biocreative": "https://ftp.ncbi.nlm.nih.gov/pub/lu/NLMChem" }
_URLs = {
"source": "https://ftp.ncbi.nlm.nih.gov/pub/lu/BC7-NLM-Chem-track/BC7T2-NLMChem-corpus_v2.BioC.xml.gz",
"bigbio_kb": "https://ftp.ncbi.nlm.nih.gov/pub/lu/BC7-NLM-Chem-track/BC7T2-NLMChem-corpus_v2.BioC.xml.gz",
"bigbio_text": "https://ftp.ncbi.nlm.nih.gov/pub/lu/BC7-NLM-Chem-track/BC7T2-NLMChem-corpus_v2.BioC.xml.gz",
}
_SUPPORTED_TASKS = [
Tasks.NAMED_ENTITY_RECOGNITION,
Tasks.NAMED_ENTITY_DISAMBIGUATION,
Tasks.TEXT_CLASSIFICATION,
]
_SOURCE_VERSION = "1.0.0"
_BIGBIO_VERSION = "1.0.0"
class NLMChemDataset(datasets.GeneratorBasedBuilder):
"""NLMChem"""
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
BUILDER_CONFIGS = [
BigBioConfig(
name="nlmchem_source",
version=SOURCE_VERSION,
description="NLM_Chem source schema",
schema="source",
subset_id="nlmchem",
),
BigBioConfig(
name="nlmchem_bigbio_kb",
version=BIGBIO_VERSION,
description="NLM_Chem BigBio schema (KB)",
schema="bigbio_kb",
subset_id="nlmchem",
),
BigBioConfig(
name="nlmchem_bigbio_text",
version=BIGBIO_VERSION,
description="NLM_Chem BigBio schema (TEXT)",
schema="bigbio_text",
subset_id="nlmchem",
),
]
DEFAULT_CONFIG_NAME = "nlmchem_source" # It's not mandatory to have a default configuration. Just use one if it make sense.
def _info(self):
if self.config.schema == "source":
# this is a variation on the BioC format
features = datasets.Features(
{
"passages": [
{
"document_id": datasets.Value("string"),
"type": datasets.Value("string"),
"text": datasets.Value("string"),
"offset": datasets.Value("int32"),
"entities": [
{
"id": datasets.Value("string"),
"offsets": [[datasets.Value("int32")]],
"text": [datasets.Value("string")],
"type": datasets.Value("string"),
"normalized": [
{
"db_name": datasets.Value("string"),
"db_id": datasets.Value("string"),
}
],
}
],
}
]
}
)
elif self.config.schema == "bigbio_kb":
features = kb_features
elif self.config.schema == "bigbio_text":
features = text_features
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features=features, # Here we define them above because they are different between the two configurations
# 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=_HOMEPAGE,
# License for the dataset if available
license=str(_LICENSE),
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
my_urls = _URLs[self.config.schema]
data_dir = dl_manager.download_and_extract(my_urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": os.path.join(
data_dir, "BC7T2-NLMChem-corpus-train.BioC.xml"
),
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": os.path.join(
data_dir, "BC7T2-NLMChem-corpus-test.BioC.xml"
),
"split": "test",
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": os.path.join(
data_dir, "BC7T2-NLMChem-corpus-dev.BioC.xml"
),
"split": "dev",
},
),
]
def _get_textcls_example(self, d: bioc.BioCDocument) -> Dict:
example = {"document_id": d.id, "text": [], "labels": []}
for p in d.passages:
example["text"].append(p.text)
for a in p.annotations:
if a.infons.get("type") == "MeSH_Indexing_Chemical":
example["labels"].append(a.infons.get("identifier"))
example["text"] = " ".join(example["text"])
return example
def _get_passages_and_entities(
self, d: bioc.BioCDocument
) -> Tuple[List[Dict], List[List[Dict]]]:
passages: List[Dict] = []
entities: List[List[Dict]] = []
text_total_length = 0
po_start = 0
for _, p in enumerate(d.passages):
eo = p.offset - text_total_length
text_total_length += len(p.text) + 1
po_end = po_start + len(p.text)
# annotation used only for document indexing
if p.text is None:
continue
dp = {
"text": p.text,
"type": p.infons.get("type"),
"offsets": [(po_start, po_end)],
"offset": p.offset, # original offset
}
po_start = po_end + 1
passages.append(dp)
pe = []
for a in p.annotations:
a_type = a.infons.get("type")
# no in-text annotation: only for document indexing
if (
self.config.schema == "bigbio_kb"
and a_type == "MeSH_Indexing_Chemical"
):
continue
offsets, text = get_texts_and_offsets_from_bioc_ann(a)
da = {
"type": a_type,
"offsets": [(start - eo, end - eo) for (start, end) in offsets],
"text": text,
"id": a.id,
"normalized": self._get_normalized(a),
}
pe.append(da)
entities.append(pe)
return passages, entities
def _get_normalized(self, a: bioc.BioCAnnotation) -> List[Dict]:
"""
Get normalization DB and ID from annotation identifiers
"""
identifiers = a.infons.get("identifier")
if identifiers is not None:
identifiers = re.split(r",|;", identifiers)
identifiers = [i for i in identifiers if i != "-"]
normalized = [i.split(":") for i in identifiers]
normalized = [
{"db_name": elems[0], "db_id": elems[1]} for elems in normalized
]
else:
normalized = [{"db_name": "-1", "db_id": "-1"}]
return normalized
def _generate_examples(
self,
filepath: str,
split: str, # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
) -> Iterator[Tuple[int, Dict]]:
"""Yields examples as (key, example) tuples."""
reader = biocxml.BioCXMLDocumentReader(str(filepath))
if self.config.schema == "source":
for uid, doc in enumerate(reader):
passages, passages_entities = self._get_passages_and_entities(doc)
for p, pe in zip(passages, passages_entities):
p.pop("offsets") # BioC has only start for passages offsets
p["document_id"] = doc.id
p["entities"] = pe # BioC has per passage entities
yield uid, {"passages": passages}
elif self.config.schema == "bigbio_text":
uid = 0
for idx, doc in enumerate(reader):
example = self._get_textcls_example(doc)
example["id"] = uid
# global id
uid += 1
yield idx, example
elif self.config.schema == "bigbio_kb":
uid = 0
for idx, doc in enumerate(reader):
# global id
uid += 1
passages, passages_entities = self._get_passages_and_entities(doc)
# unpack per-passage entities
entities = [e for pe in passages_entities for e in pe]
for p in passages:
p.pop("offset") # drop original offset
p["text"] = (p["text"],) # text in passage is Sequence
p["id"] = uid # override BioC default id
uid += 1
for e in entities:
e["id"] = uid # override BioC default id
uid += 1
# if split == "validation" and uid == 6705:
# breakpoint()
yield idx, {
"id": uid,
"document_id": doc.id,
"passages": passages,
"entities": entities,
"events": [],
"coreferences": [],
"relations": [],
}
|