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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"""
|
| 17 |
+
MLEE is an event extraction corpus consisting of manually annotated abstracts of papers
|
| 18 |
+
on angiogenesis. It contains annotations for entities, relations, events and coreferences
|
| 19 |
+
The annotations span molecular, cellular, tissue, and organ-level processes.
|
| 20 |
+
"""
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
from typing import Dict, List
|
| 23 |
+
|
| 24 |
+
import datasets
|
| 25 |
+
|
| 26 |
+
from .bigbiohub import kb_features
|
| 27 |
+
from .bigbiohub import BigBioConfig
|
| 28 |
+
from .bigbiohub import Tasks
|
| 29 |
+
|
| 30 |
+
_SOURCE_VIEW_NAME = "source"
|
| 31 |
+
_UNIFIED_VIEW_NAME = "bigbio"
|
| 32 |
+
|
| 33 |
+
_LANGUAGES = ['English']
|
| 34 |
+
_PUBMED = True
|
| 35 |
+
_LOCAL = False
|
| 36 |
+
_CITATION = """\
|
| 37 |
+
@article{pyysalo2012event,
|
| 38 |
+
title={Event extraction across multiple levels of biological organization},
|
| 39 |
+
author={Pyysalo, Sampo and Ohta, Tomoko and Miwa, Makoto and Cho, Han-Cheol and Tsujii, Jun'ichi and Ananiadou, Sophia},
|
| 40 |
+
journal={Bioinformatics},
|
| 41 |
+
volume={28},
|
| 42 |
+
number={18},
|
| 43 |
+
pages={i575--i581},
|
| 44 |
+
year={2012},
|
| 45 |
+
publisher={Oxford University Press}
|
| 46 |
+
}
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
_DESCRIPTION = """\
|
| 50 |
+
MLEE is an event extraction corpus consisting of manually annotated abstracts of papers
|
| 51 |
+
on angiogenesis. It contains annotations for entities, relations, events and coreferences
|
| 52 |
+
The annotations span molecular, cellular, tissue, and organ-level processes.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
_DATASETNAME = "mlee"
|
| 56 |
+
_DISPLAYNAME = "MLEE"
|
| 57 |
+
|
| 58 |
+
_HOMEPAGE = "http://www.nactem.ac.uk/MLEE/"
|
| 59 |
+
|
| 60 |
+
_LICENSE = 'Creative Commons Attribution Non Commercial Share Alike 3.0 Unported'
|
| 61 |
+
_URLs = {
|
| 62 |
+
"source": "http://www.nactem.ac.uk/MLEE/MLEE-1.0.2-rev1.tar.gz",
|
| 63 |
+
"bigbio_kb": "http://www.nactem.ac.uk/MLEE/MLEE-1.0.2-rev1.tar.gz",
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
_SUPPORTED_TASKS = [
|
| 67 |
+
Tasks.EVENT_EXTRACTION,
|
| 68 |
+
Tasks.NAMED_ENTITY_RECOGNITION,
|
| 69 |
+
Tasks.RELATION_EXTRACTION,
|
| 70 |
+
Tasks.COREFERENCE_RESOLUTION,
|
| 71 |
+
]
|
| 72 |
+
_SOURCE_VERSION = "1.0.0"
|
| 73 |
+
_BIGBIO_VERSION = "1.0.0"
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
class MLEE(datasets.GeneratorBasedBuilder):
|
| 77 |
+
"""Write a short docstring documenting what this dataset is"""
|
| 78 |
+
|
| 79 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 80 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
| 81 |
+
|
| 82 |
+
BUILDER_CONFIGS = [
|
| 83 |
+
BigBioConfig(
|
| 84 |
+
name="mlee_source",
|
| 85 |
+
version=SOURCE_VERSION,
|
| 86 |
+
description="MLEE source schema",
|
| 87 |
+
schema="source",
|
| 88 |
+
subset_id="mlee",
|
| 89 |
+
),
|
| 90 |
+
BigBioConfig(
|
| 91 |
+
name="mlee_bigbio_kb",
|
| 92 |
+
version=SOURCE_VERSION,
|
| 93 |
+
description="MLEE BigBio schema",
|
| 94 |
+
schema="bigbio_kb",
|
| 95 |
+
subset_id="mlee",
|
| 96 |
+
),
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
DEFAULT_CONFIG_NAME = "mlee_source"
|
| 100 |
+
|
| 101 |
+
_ROLE_MAPPING = {
|
| 102 |
+
"Theme2": "Theme",
|
| 103 |
+
"Instrument2": "Instrument",
|
| 104 |
+
"Participant2": "Participant",
|
| 105 |
+
"Participant3": "Participant",
|
| 106 |
+
"Participant4": "Participant",
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
def _info(self):
|
| 110 |
+
"""
|
| 111 |
+
Provide information about MLEE:
|
| 112 |
+
- `features` defines the schema of the parsed data set. The schema depends on the
|
| 113 |
+
chosen `config`: If it is `_SOURCE_VIEW_NAME` the schema is the schema of the
|
| 114 |
+
original data. If `config` is `_UNIFIED_VIEW_NAME`, then the schema is the
|
| 115 |
+
canonical KB-task schema defined in `biomedical/schemas/kb.py`.
|
| 116 |
+
|
| 117 |
+
"""
|
| 118 |
+
if self.config.schema == "source":
|
| 119 |
+
features = datasets.Features(
|
| 120 |
+
{
|
| 121 |
+
"id": datasets.Value("string"),
|
| 122 |
+
"document_id": datasets.Value("string"),
|
| 123 |
+
"text": datasets.Value("string"),
|
| 124 |
+
"text_bound_annotations": [ # T line in brat, e.g. type or event trigger
|
| 125 |
+
{
|
| 126 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
| 127 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
| 128 |
+
"type": datasets.Value("string"),
|
| 129 |
+
"id": datasets.Value("string"),
|
| 130 |
+
}
|
| 131 |
+
],
|
| 132 |
+
"events": [ # E line in brat
|
| 133 |
+
{
|
| 134 |
+
"trigger": datasets.Value(
|
| 135 |
+
"string"
|
| 136 |
+
), # refers to the text_bound_annotation of the trigger,
|
| 137 |
+
"id": datasets.Value("string"),
|
| 138 |
+
"type": datasets.Value("string"),
|
| 139 |
+
"arguments": datasets.Sequence(
|
| 140 |
+
{
|
| 141 |
+
"role": datasets.Value("string"),
|
| 142 |
+
"ref_id": datasets.Value("string"),
|
| 143 |
+
}
|
| 144 |
+
),
|
| 145 |
+
}
|
| 146 |
+
],
|
| 147 |
+
"relations": [ # R line in brat
|
| 148 |
+
{
|
| 149 |
+
"id": datasets.Value("string"),
|
| 150 |
+
"head": {
|
| 151 |
+
"ref_id": datasets.Value("string"),
|
| 152 |
+
"role": datasets.Value("string"),
|
| 153 |
+
},
|
| 154 |
+
"tail": {
|
| 155 |
+
"ref_id": datasets.Value("string"),
|
| 156 |
+
"role": datasets.Value("string"),
|
| 157 |
+
},
|
| 158 |
+
"type": datasets.Value("string"),
|
| 159 |
+
}
|
| 160 |
+
],
|
| 161 |
+
"equivalences": [ # Equiv line in brat
|
| 162 |
+
{
|
| 163 |
+
"id": datasets.Value("string"),
|
| 164 |
+
"ref_ids": datasets.Sequence(datasets.Value("string")),
|
| 165 |
+
}
|
| 166 |
+
],
|
| 167 |
+
"attributes": [ # M or A lines in brat
|
| 168 |
+
{
|
| 169 |
+
"id": datasets.Value("string"),
|
| 170 |
+
"type": datasets.Value("string"),
|
| 171 |
+
"ref_id": datasets.Value("string"),
|
| 172 |
+
"value": datasets.Value("string"),
|
| 173 |
+
}
|
| 174 |
+
],
|
| 175 |
+
"normalizations": [ # N lines in brat
|
| 176 |
+
{
|
| 177 |
+
"id": datasets.Value("string"),
|
| 178 |
+
"type": datasets.Value("string"),
|
| 179 |
+
"ref_id": datasets.Value("string"),
|
| 180 |
+
"resource_name": datasets.Value(
|
| 181 |
+
"string"
|
| 182 |
+
), # Name of the resource, e.g. "Wikipedia"
|
| 183 |
+
"cuid": datasets.Value(
|
| 184 |
+
"string"
|
| 185 |
+
), # ID in the resource, e.g. 534366
|
| 186 |
+
"text": datasets.Value(
|
| 187 |
+
"string"
|
| 188 |
+
), # Human readable description/name of the entity, e.g. "Barack Obama"
|
| 189 |
+
}
|
| 190 |
+
],
|
| 191 |
+
},
|
| 192 |
+
)
|
| 193 |
+
elif self.config.schema == "bigbio_kb":
|
| 194 |
+
features = kb_features
|
| 195 |
+
|
| 196 |
+
return datasets.DatasetInfo(
|
| 197 |
+
# This is the description that will appear on the datasets page.
|
| 198 |
+
description=_DESCRIPTION,
|
| 199 |
+
features=features,
|
| 200 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
| 201 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
| 202 |
+
# This is not applicable for MLEE.
|
| 203 |
+
# supervised_keys=("sentence", "label"),
|
| 204 |
+
# Homepage of the dataset for documentation
|
| 205 |
+
homepage=_HOMEPAGE,
|
| 206 |
+
# License for the dataset if available
|
| 207 |
+
license=str(_LICENSE),
|
| 208 |
+
# Citation for the dataset
|
| 209 |
+
citation=_CITATION,
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
def _split_generators(
|
| 213 |
+
self, dl_manager: datasets.DownloadManager
|
| 214 |
+
) -> List[datasets.SplitGenerator]:
|
| 215 |
+
"""
|
| 216 |
+
Create the three splits provided by MLEE: train, validation and test.
|
| 217 |
+
|
| 218 |
+
Each split is created by instantiating a `datasets.SplitGenerator`, which will
|
| 219 |
+
call `this._generate_examples` with the keyword arguments in `gen_kwargs`.
|
| 220 |
+
"""
|
| 221 |
+
|
| 222 |
+
my_urls = _URLs[self.config.schema]
|
| 223 |
+
data_dir = Path(dl_manager.download_and_extract(my_urls))
|
| 224 |
+
data_files = {
|
| 225 |
+
"train": data_dir
|
| 226 |
+
/ "MLEE-1.0.2-rev1"
|
| 227 |
+
/ "standoff"
|
| 228 |
+
/ "development"
|
| 229 |
+
/ "train",
|
| 230 |
+
"dev": data_dir / "MLEE-1.0.2-rev1" / "standoff" / "development" / "test",
|
| 231 |
+
"test": data_dir / "MLEE-1.0.2-rev1" / "standoff" / "test" / "test",
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
return [
|
| 235 |
+
datasets.SplitGenerator(
|
| 236 |
+
name=datasets.Split.TRAIN,
|
| 237 |
+
gen_kwargs={"data_files": data_files["train"]},
|
| 238 |
+
),
|
| 239 |
+
datasets.SplitGenerator(
|
| 240 |
+
name=datasets.Split.VALIDATION,
|
| 241 |
+
gen_kwargs={"data_files": data_files["dev"]},
|
| 242 |
+
),
|
| 243 |
+
datasets.SplitGenerator(
|
| 244 |
+
name=datasets.Split.TEST,
|
| 245 |
+
gen_kwargs={"data_files": data_files["test"]},
|
| 246 |
+
),
|
| 247 |
+
]
|
| 248 |
+
|
| 249 |
+
def _standardize_arguments_roles(self, kb_example: Dict) -> Dict:
|
| 250 |
+
|
| 251 |
+
for event in kb_example["events"]:
|
| 252 |
+
for argument in event["arguments"]:
|
| 253 |
+
role = argument["role"]
|
| 254 |
+
argument["role"] = self._ROLE_MAPPING.get(role, role)
|
| 255 |
+
|
| 256 |
+
return kb_example
|
| 257 |
+
|
| 258 |
+
def _generate_examples(self, data_files: Path):
|
| 259 |
+
"""
|
| 260 |
+
Yield one `(guid, example)` pair per abstract in MLEE.
|
| 261 |
+
The contents of `example` will depend on the chosen configuration.
|
| 262 |
+
"""
|
| 263 |
+
if self.config.schema == "source":
|
| 264 |
+
txt_files = list(data_files.glob("*txt"))
|
| 265 |
+
for guid, txt_file in enumerate(txt_files):
|
| 266 |
+
example = parsing.parse_brat_file(txt_file)
|
| 267 |
+
example["id"] = str(guid)
|
| 268 |
+
yield guid, example
|
| 269 |
+
elif self.config.schema == "bigbio_kb":
|
| 270 |
+
txt_files = list(data_files.glob("*txt"))
|
| 271 |
+
for guid, txt_file in enumerate(txt_files):
|
| 272 |
+
example = parsing.brat_parse_to_bigbio_kb(
|
| 273 |
+
parsing.parse_brat_file(txt_file)
|
| 274 |
+
)
|
| 275 |
+
example = self._standardize_arguments_roles(example)
|
| 276 |
+
example["id"] = str(guid)
|
| 277 |
+
yield guid, example
|
| 278 |
+
else:
|
| 279 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|