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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import re
|
| 17 |
+
from typing import Dict, Iterator, List, Tuple
|
| 18 |
+
|
| 19 |
+
import bioc
|
| 20 |
+
import datasets
|
| 21 |
+
from bioc import biocxml
|
| 22 |
+
|
| 23 |
+
from .bigbiohub import kb_features
|
| 24 |
+
from .bigbiohub import BigBioConfig
|
| 25 |
+
from .bigbiohub import Tasks
|
| 26 |
+
|
| 27 |
+
_LANGUAGES = ['English']
|
| 28 |
+
_PUBMED = True
|
| 29 |
+
_LOCAL = False
|
| 30 |
+
_CITATION = """\
|
| 31 |
+
@article{Krallinger2015,
|
| 32 |
+
title = {The CHEMDNER corpus of chemicals and drugs and its annotation principles},
|
| 33 |
+
author = {
|
| 34 |
+
Krallinger, Martin and Rabal, Obdulia and Leitner, Florian and Vazquez,
|
| 35 |
+
Miguel and Salgado, David and Lu, Zhiyong and Leaman, Robert and Lu, Yanan
|
| 36 |
+
and Ji, Donghong and Lowe, Daniel M. and Sayle, Roger A. and
|
| 37 |
+
Batista-Navarro, Riza Theresa and Rak, Rafal and Huber, Torsten and
|
| 38 |
+
Rockt{\"a}schel, Tim and Matos, S{\'e}rgio and Campos, David and Tang,
|
| 39 |
+
Buzhou and Xu, Hua and Munkhdalai, Tsendsuren and Ryu, Keun Ho and Ramanan,
|
| 40 |
+
S. V. and Nathan, Senthil and {\v{Z}}itnik, Slavko and Bajec, Marko and
|
| 41 |
+
Weber, Lutz and Irmer, Matthias and Akhondi, Saber A. and Kors, Jan A. and
|
| 42 |
+
Xu, Shuo and An, Xin and Sikdar, Utpal Kumar and Ekbal, Asif and Yoshioka,
|
| 43 |
+
Masaharu and Dieb, Thaer M. and Choi, Miji and Verspoor, Karin and Khabsa,
|
| 44 |
+
Madian and Giles, C. Lee and Liu, Hongfang and Ravikumar, Komandur
|
| 45 |
+
Elayavilli and Lamurias, Andre and Couto, Francisco M. and Dai, Hong-Jie
|
| 46 |
+
and Tsai, Richard Tzong-Han and Ata, Caglar and Can, Tolga and Usi{\'e},
|
| 47 |
+
Anabel and Alves, Rui and Segura-Bedmar, Isabel and Mart{\'i}nez, Paloma
|
| 48 |
+
and Oyarzabal, Julen and Valencia, Alfonso
|
| 49 |
+
},
|
| 50 |
+
year = 2015,
|
| 51 |
+
month = {Jan},
|
| 52 |
+
day = 19,
|
| 53 |
+
journal = {Journal of Cheminformatics},
|
| 54 |
+
volume = 7,
|
| 55 |
+
number = 1,
|
| 56 |
+
pages = {S2},
|
| 57 |
+
doi = {10.1186/1758-2946-7-S1-S2},
|
| 58 |
+
issn = {1758-2946},
|
| 59 |
+
url = {https://doi.org/10.1186/1758-2946-7-S1-S2},
|
| 60 |
+
abstract = {
|
| 61 |
+
The automatic extraction of chemical information from text requires the
|
| 62 |
+
recognition of chemical entity mentions as one of its key steps. When
|
| 63 |
+
developing supervised named entity recognition (NER) systems, the
|
| 64 |
+
availability of a large, manually annotated text corpus is desirable.
|
| 65 |
+
Furthermore, large corpora permit the robust evaluation and comparison of
|
| 66 |
+
different approaches that detect chemicals in documents. We present the
|
| 67 |
+
CHEMDNER corpus, a collection of 10,000 PubMed abstracts that contain a
|
| 68 |
+
total of 84,355 chemical entity mentions labeled manually by expert
|
| 69 |
+
chemistry literature curators, following annotation guidelines specifically
|
| 70 |
+
defined for this task. The abstracts of the CHEMDNER corpus were selected
|
| 71 |
+
to be representative for all major chemical disciplines. Each of the
|
| 72 |
+
chemical entity mentions was manually labeled according to its
|
| 73 |
+
structure-associated chemical entity mention (SACEM) class: abbreviation,
|
| 74 |
+
family, formula, identifier, multiple, systematic and trivial. The
|
| 75 |
+
difficulty and consistency of tagging chemicals in text was measured using
|
| 76 |
+
an agreement study between annotators, obtaining a percentage agreement of
|
| 77 |
+
91. For a subset of the CHEMDNER corpus (the test set of 3,000 abstracts)
|
| 78 |
+
we provide not only the Gold Standard manual annotations, but also mentions
|
| 79 |
+
automatically detected by the 26 teams that participated in the BioCreative
|
| 80 |
+
IV CHEMDNER chemical mention recognition task. In addition, we release the
|
| 81 |
+
CHEMDNER silver standard corpus of automatically extracted mentions from
|
| 82 |
+
17,000 randomly selected PubMed abstracts. A version of the CHEMDNER corpus
|
| 83 |
+
in the BioC format has been generated as well. We propose a standard for
|
| 84 |
+
required minimum information about entity annotations for the construction
|
| 85 |
+
of domain specific corpora on chemical and drug entities. The CHEMDNER
|
| 86 |
+
corpus and annotation guidelines are available at:
|
| 87 |
+
ttp://www.biocreative.org/resources/biocreative-iv/chemdner-corpus/
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
_DESCRIPTION = """\
|
| 93 |
+
We present the CHEMDNER corpus, a collection of 10,000 PubMed abstracts that
|
| 94 |
+
contain a total of 84,355 chemical entity mentions labeled manually by expert
|
| 95 |
+
chemistry literature curators, following annotation guidelines specifically
|
| 96 |
+
defined for this task. The abstracts of the CHEMDNER corpus were selected to be
|
| 97 |
+
representative for all major chemical disciplines. Each of the chemical entity
|
| 98 |
+
mentions was manually labeled according to its structure-associated chemical
|
| 99 |
+
entity mention (SACEM) class: abbreviation, family, formula, identifier,
|
| 100 |
+
multiple, systematic and trivial.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
_DATASETNAME = "chemdner"
|
| 104 |
+
_DISPLAYNAME = "CHEMDNER"
|
| 105 |
+
|
| 106 |
+
_HOMEPAGE = "https://biocreative.bioinformatics.udel.edu/resources/biocreative-iv/chemdner-corpus/"
|
| 107 |
+
|
| 108 |
+
_LICENSE = 'License information unavailable'
|
| 109 |
+
|
| 110 |
+
_URLs = {
|
| 111 |
+
"source": "https://ftp.ncbi.nlm.nih.gov/pub/lu/BC7-NLM-Chem-track/BC7T2-CHEMDNER-corpus_v2.BioC.xml.gz",
|
| 112 |
+
"bigbio_kb": "https://ftp.ncbi.nlm.nih.gov/pub/lu/BC7-NLM-Chem-track/BC7T2-CHEMDNER-corpus_v2.BioC.xml.gz",
|
| 113 |
+
"bigbio_text": "https://ftp.ncbi.nlm.nih.gov/pub/lu/BC7-NLM-Chem-track/BC7T2-CHEMDNER-corpus_v2.BioC.xml.gz",
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
_SUPPORTED_TASKS = [
|
| 117 |
+
Tasks.NAMED_ENTITY_RECOGNITION,
|
| 118 |
+
Tasks.TEXT_CLASSIFICATION,
|
| 119 |
+
]
|
| 120 |
+
_SOURCE_VERSION = "1.0.0"
|
| 121 |
+
_BIGBIO_VERSION = "1.0.0"
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class CHEMDNERDataset(datasets.GeneratorBasedBuilder):
|
| 125 |
+
"""CHEMDNER"""
|
| 126 |
+
|
| 127 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 128 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
| 129 |
+
|
| 130 |
+
BUILDER_CONFIGS = [
|
| 131 |
+
BigBioConfig(
|
| 132 |
+
name="chemdner_source",
|
| 133 |
+
version=SOURCE_VERSION,
|
| 134 |
+
description="CHEMDNER source schema",
|
| 135 |
+
schema="source",
|
| 136 |
+
subset_id="chemdner",
|
| 137 |
+
),
|
| 138 |
+
BigBioConfig(
|
| 139 |
+
name="chemdner_bigbio_kb",
|
| 140 |
+
version=BIGBIO_VERSION,
|
| 141 |
+
description="CHEMDNER BigBio schema (KB)",
|
| 142 |
+
schema="bigbio_kb",
|
| 143 |
+
subset_id="chemdner",
|
| 144 |
+
),
|
| 145 |
+
BigBioConfig(
|
| 146 |
+
name="chemdner_bigbio_text",
|
| 147 |
+
version=BIGBIO_VERSION,
|
| 148 |
+
description="CHEMDNER BigBio schema (TEXT)",
|
| 149 |
+
schema="bigbio_text",
|
| 150 |
+
subset_id="chemdner",
|
| 151 |
+
),
|
| 152 |
+
]
|
| 153 |
+
|
| 154 |
+
DEFAULT_CONFIG_NAME = "chemdner_source"
|
| 155 |
+
|
| 156 |
+
def _info(self):
|
| 157 |
+
|
| 158 |
+
if self.config.schema == "source":
|
| 159 |
+
# this is a variation on the BioC format
|
| 160 |
+
features = datasets.Features(
|
| 161 |
+
{
|
| 162 |
+
"passages": [
|
| 163 |
+
{
|
| 164 |
+
"document_id": datasets.Value("string"),
|
| 165 |
+
"type": datasets.Value("string"),
|
| 166 |
+
"text": datasets.Value("string"),
|
| 167 |
+
"offset": datasets.Value("int32"),
|
| 168 |
+
"entities": [
|
| 169 |
+
{
|
| 170 |
+
"id": datasets.Value("string"),
|
| 171 |
+
"offsets": [[datasets.Value("int32")]],
|
| 172 |
+
"text": [datasets.Value("string")],
|
| 173 |
+
"type": datasets.Value("string"),
|
| 174 |
+
"normalized": [
|
| 175 |
+
{
|
| 176 |
+
"db_name": datasets.Value("string"),
|
| 177 |
+
"db_id": datasets.Value("string"),
|
| 178 |
+
}
|
| 179 |
+
],
|
| 180 |
+
}
|
| 181 |
+
],
|
| 182 |
+
}
|
| 183 |
+
]
|
| 184 |
+
}
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
elif self.config.schema == "bigbio_kb":
|
| 188 |
+
features = kb_features
|
| 189 |
+
|
| 190 |
+
elif self.config.schema == "bigbio_text":
|
| 191 |
+
features = text_features
|
| 192 |
+
|
| 193 |
+
return datasets.DatasetInfo(
|
| 194 |
+
description=_DESCRIPTION,
|
| 195 |
+
features=features,
|
| 196 |
+
supervised_keys=None,
|
| 197 |
+
homepage=_HOMEPAGE,
|
| 198 |
+
license=str(_LICENSE),
|
| 199 |
+
citation=_CITATION,
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
def _split_generators(self, dl_manager):
|
| 203 |
+
"""Returns SplitGenerators."""
|
| 204 |
+
|
| 205 |
+
my_urls = _URLs[self.config.schema]
|
| 206 |
+
data_dir = dl_manager.download_and_extract(my_urls) + "/"
|
| 207 |
+
return [
|
| 208 |
+
datasets.SplitGenerator(
|
| 209 |
+
name=datasets.Split.TRAIN,
|
| 210 |
+
# These kwargs will be passed to _generate_examples
|
| 211 |
+
gen_kwargs={
|
| 212 |
+
"filepath": os.path.join(
|
| 213 |
+
data_dir, "BC7T2-CHEMDNER-corpus-training.BioC.xml"
|
| 214 |
+
),
|
| 215 |
+
"split": "train",
|
| 216 |
+
},
|
| 217 |
+
),
|
| 218 |
+
datasets.SplitGenerator(
|
| 219 |
+
name=datasets.Split.TEST,
|
| 220 |
+
# These kwargs will be passed to _generate_examples
|
| 221 |
+
gen_kwargs={
|
| 222 |
+
"filepath": os.path.join(
|
| 223 |
+
data_dir, "BC7T2-CHEMDNER-corpus-evaluation.BioC.xml"
|
| 224 |
+
),
|
| 225 |
+
"split": "test",
|
| 226 |
+
},
|
| 227 |
+
),
|
| 228 |
+
datasets.SplitGenerator(
|
| 229 |
+
name=datasets.Split.VALIDATION,
|
| 230 |
+
# These kwargs will be passed to _generate_examples
|
| 231 |
+
gen_kwargs={
|
| 232 |
+
"filepath": os.path.join(
|
| 233 |
+
data_dir, "BC7T2-CHEMDNER-corpus-development.BioC.xml"
|
| 234 |
+
),
|
| 235 |
+
"split": "dev",
|
| 236 |
+
},
|
| 237 |
+
),
|
| 238 |
+
]
|
| 239 |
+
|
| 240 |
+
def _get_passages_and_entities(
|
| 241 |
+
self, d: bioc.BioCDocument
|
| 242 |
+
) -> Tuple[List[Dict], List[List[Dict]]]:
|
| 243 |
+
|
| 244 |
+
passages: List[Dict] = []
|
| 245 |
+
entities: List[List[Dict]] = []
|
| 246 |
+
|
| 247 |
+
text_total_length = 0
|
| 248 |
+
|
| 249 |
+
po_start = 0
|
| 250 |
+
|
| 251 |
+
for i, p in enumerate(d.passages):
|
| 252 |
+
|
| 253 |
+
eo = p.offset - text_total_length
|
| 254 |
+
|
| 255 |
+
text_total_length += len(p.text) + 1
|
| 256 |
+
|
| 257 |
+
po_end = po_start + len(p.text)
|
| 258 |
+
|
| 259 |
+
dp = {
|
| 260 |
+
"text": p.text,
|
| 261 |
+
"type": p.infons.get("type"),
|
| 262 |
+
"offsets": [(po_start, po_end)],
|
| 263 |
+
"offset": p.offset, # original offset
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
po_start = po_end + 1
|
| 267 |
+
|
| 268 |
+
passages.append(dp)
|
| 269 |
+
|
| 270 |
+
pe = []
|
| 271 |
+
|
| 272 |
+
for a in p.annotations:
|
| 273 |
+
|
| 274 |
+
a_type = a.infons.get("type")
|
| 275 |
+
|
| 276 |
+
if (
|
| 277 |
+
self.config.schema == "bigbio_kb"
|
| 278 |
+
and a_type == "MeSH_Indexing_Chemical"
|
| 279 |
+
):
|
| 280 |
+
continue
|
| 281 |
+
|
| 282 |
+
if (
|
| 283 |
+
a.text == None or a.text == ""
|
| 284 |
+
) and self.config.schema == "bigbio_kb":
|
| 285 |
+
continue
|
| 286 |
+
|
| 287 |
+
offsets, text = get_texts_and_offsets_from_bioc_ann(a)
|
| 288 |
+
|
| 289 |
+
da = {
|
| 290 |
+
"type": a_type,
|
| 291 |
+
"offsets": [(start - eo, end - eo) for (start, end) in offsets],
|
| 292 |
+
"text": text,
|
| 293 |
+
"id": a.id,
|
| 294 |
+
"normalized": self._get_normalized(a),
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
pe.append(da)
|
| 298 |
+
|
| 299 |
+
entities.append(pe)
|
| 300 |
+
|
| 301 |
+
return passages, entities
|
| 302 |
+
|
| 303 |
+
def _get_normalized(self, a: bioc.BioCAnnotation) -> List[Dict]:
|
| 304 |
+
"""
|
| 305 |
+
Get normalization DB and ID from annotation identifiers
|
| 306 |
+
"""
|
| 307 |
+
|
| 308 |
+
identifiers = a.infons.get("identifier")
|
| 309 |
+
|
| 310 |
+
if identifiers is not None:
|
| 311 |
+
|
| 312 |
+
identifiers = re.split(r",|;", identifiers)
|
| 313 |
+
|
| 314 |
+
identifiers = [i for i in identifiers if i != "-"]
|
| 315 |
+
|
| 316 |
+
normalized = [i.split(":") for i in identifiers]
|
| 317 |
+
|
| 318 |
+
normalized = [
|
| 319 |
+
{"db_name": elems[0], "db_id": elems[1]} for elems in normalized
|
| 320 |
+
]
|
| 321 |
+
|
| 322 |
+
else:
|
| 323 |
+
|
| 324 |
+
# No normalization
|
| 325 |
+
normalized = []
|
| 326 |
+
|
| 327 |
+
return normalized
|
| 328 |
+
|
| 329 |
+
def _get_textcls_example(self, d: bioc.BioCDocument) -> Dict:
|
| 330 |
+
|
| 331 |
+
example = {"document_id": d.id, "text": [], "labels": []}
|
| 332 |
+
|
| 333 |
+
for p in d.passages:
|
| 334 |
+
|
| 335 |
+
example["text"].append(p.text)
|
| 336 |
+
|
| 337 |
+
for a in p.annotations:
|
| 338 |
+
|
| 339 |
+
if a.infons.get("type") == "MeSH_Indexing_Chemical":
|
| 340 |
+
|
| 341 |
+
example["labels"].append(a.infons.get("identifier"))
|
| 342 |
+
|
| 343 |
+
example["text"] = " ".join(example["text"])
|
| 344 |
+
|
| 345 |
+
return example
|
| 346 |
+
|
| 347 |
+
def _generate_examples(
|
| 348 |
+
self,
|
| 349 |
+
filepath: str,
|
| 350 |
+
split: str,
|
| 351 |
+
) -> Iterator[Tuple[int, Dict]]:
|
| 352 |
+
"""Yields examples as (key, example) tuples."""
|
| 353 |
+
|
| 354 |
+
reader = biocxml.BioCXMLDocumentReader(str(filepath))
|
| 355 |
+
|
| 356 |
+
if self.config.schema == "source":
|
| 357 |
+
|
| 358 |
+
for uid, doc in enumerate(reader):
|
| 359 |
+
|
| 360 |
+
passages, passages_entities = self._get_passages_and_entities(doc)
|
| 361 |
+
|
| 362 |
+
for p, pe in zip(passages, passages_entities):
|
| 363 |
+
|
| 364 |
+
p.pop("offsets") # BioC has only start for passages offsets
|
| 365 |
+
|
| 366 |
+
p["document_id"] = doc.id
|
| 367 |
+
p["entities"] = pe # BioC has per passage entities
|
| 368 |
+
|
| 369 |
+
yield uid, {"passages": passages}
|
| 370 |
+
|
| 371 |
+
elif self.config.schema == "bigbio_kb":
|
| 372 |
+
|
| 373 |
+
uid = 0
|
| 374 |
+
|
| 375 |
+
for idx, doc in enumerate(reader):
|
| 376 |
+
|
| 377 |
+
passages, passages_entities = self._get_passages_and_entities(doc)
|
| 378 |
+
|
| 379 |
+
# global id
|
| 380 |
+
uid += 1
|
| 381 |
+
|
| 382 |
+
# unpack per-passage entities
|
| 383 |
+
entities = [e for pe in passages_entities for e in pe]
|
| 384 |
+
|
| 385 |
+
for p in passages:
|
| 386 |
+
p.pop("offset") # drop original offset
|
| 387 |
+
p["text"] = (p["text"],) # text in passage is Sequence
|
| 388 |
+
p["id"] = uid # override BioC default id
|
| 389 |
+
uid += 1
|
| 390 |
+
|
| 391 |
+
for e in entities:
|
| 392 |
+
e["id"] = uid # override BioC default id
|
| 393 |
+
uid += 1
|
| 394 |
+
|
| 395 |
+
yield idx, {
|
| 396 |
+
"id": uid,
|
| 397 |
+
"document_id": doc.id,
|
| 398 |
+
"passages": passages,
|
| 399 |
+
"entities": entities,
|
| 400 |
+
"events": [],
|
| 401 |
+
"coreferences": [],
|
| 402 |
+
"relations": [],
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
elif self.config.schema == "bigbio_text":
|
| 406 |
+
|
| 407 |
+
uid = 0
|
| 408 |
+
|
| 409 |
+
for idx, doc in enumerate(reader):
|
| 410 |
+
|
| 411 |
+
example = self._get_textcls_example(doc)
|
| 412 |
+
example["id"] = uid
|
| 413 |
+
|
| 414 |
+
# global id
|
| 415 |
+
uid += 1
|
| 416 |
+
|
| 417 |
+
yield idx, example
|