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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
AnEM corpus is a domain- and species-independent resource manually annotated for anatomical
|
17 |
+
entity mentions using a fine-grained classification system. The corpus consists of 500 documents
|
18 |
+
(over 90,000 words) selected randomly from citation abstracts and full-text papers with
|
19 |
+
the aim of making the corpus representative of the entire available biomedical scientific
|
20 |
+
literature. The corpus annotation covers mentions of both healthy and pathological anatomical
|
21 |
+
entities and contains over 3,000 annotated mentions.
|
22 |
+
"""
|
23 |
+
|
24 |
+
from pathlib import Path
|
25 |
+
from typing import Dict, List, Tuple
|
26 |
+
|
27 |
+
import datasets
|
28 |
+
|
29 |
+
from .bigbiohub import kb_features
|
30 |
+
from .bigbiohub import BigBioConfig
|
31 |
+
from .bigbiohub import Tasks
|
32 |
+
|
33 |
+
_LANGUAGES = ['English']
|
34 |
+
_PUBMED = True
|
35 |
+
_LOCAL = False
|
36 |
+
_CITATION = """\
|
37 |
+
@inproceedings{ohta-etal-2012-open,
|
38 |
+
author = {Ohta, Tomoko and Pyysalo, Sampo and Tsujii, Jun{'}ichi and Ananiadou, Sophia},
|
39 |
+
title = {Open-domain Anatomical Entity Mention Detection},
|
40 |
+
journal = {},
|
41 |
+
volume = {W12-43},
|
42 |
+
year = {2012},
|
43 |
+
url = {https://aclanthology.org/W12-4304},
|
44 |
+
doi = {},
|
45 |
+
biburl = {},
|
46 |
+
bibsource = {},
|
47 |
+
publisher = {Association for Computational Linguistics}
|
48 |
+
}
|
49 |
+
"""
|
50 |
+
|
51 |
+
_DATASETNAME = "an_em"
|
52 |
+
_DISPLAYNAME = "AnEM"
|
53 |
+
|
54 |
+
_DESCRIPTION = """\
|
55 |
+
AnEM corpus is a domain- and species-independent resource manually annotated for anatomical
|
56 |
+
entity mentions using a fine-grained classification system. The corpus consists of 500 documents
|
57 |
+
(over 90,000 words) selected randomly from citation abstracts and full-text papers with
|
58 |
+
the aim of making the corpus representative of the entire available biomedical scientific
|
59 |
+
literature. The corpus annotation covers mentions of both healthy and pathological anatomical
|
60 |
+
entities and contains over 3,000 annotated mentions.
|
61 |
+
"""
|
62 |
+
|
63 |
+
|
64 |
+
_HOMEPAGE = "http://www.nactem.ac.uk/anatomy/"
|
65 |
+
|
66 |
+
_LICENSE = 'Creative Commons Attribution Share Alike 3.0 Unported'
|
67 |
+
|
68 |
+
_URLS = {
|
69 |
+
_DATASETNAME: "http://www.nactem.ac.uk/anatomy/data/AnEM-1.0.4.tar.gz",
|
70 |
+
}
|
71 |
+
|
72 |
+
_SUPPORTED_TASKS = [
|
73 |
+
Tasks.NAMED_ENTITY_RECOGNITION,
|
74 |
+
Tasks.COREFERENCE_RESOLUTION,
|
75 |
+
Tasks.RELATION_EXTRACTION,
|
76 |
+
]
|
77 |
+
|
78 |
+
_SOURCE_VERSION = "1.0.4"
|
79 |
+
_BIGBIO_VERSION = "1.0.0"
|
80 |
+
|
81 |
+
|
82 |
+
class AnEMDataset(datasets.GeneratorBasedBuilder):
|
83 |
+
"""Anatomical Entity Mention (AnEM) corpus"""
|
84 |
+
|
85 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
86 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
87 |
+
|
88 |
+
BUILDER_CONFIGS = [
|
89 |
+
BigBioConfig(
|
90 |
+
name="an_em_source",
|
91 |
+
version=SOURCE_VERSION,
|
92 |
+
description="AnEM source schema",
|
93 |
+
schema="source",
|
94 |
+
subset_id="an_em",
|
95 |
+
),
|
96 |
+
BigBioConfig(
|
97 |
+
name="an_em_bigbio_kb",
|
98 |
+
version=BIGBIO_VERSION,
|
99 |
+
description="AnEM BigBio schema",
|
100 |
+
schema="bigbio_kb",
|
101 |
+
subset_id="an_em",
|
102 |
+
),
|
103 |
+
]
|
104 |
+
|
105 |
+
DEFAULT_CONFIG_NAME = "an_em_source"
|
106 |
+
|
107 |
+
def _info(self) -> datasets.DatasetInfo:
|
108 |
+
if self.config.schema == "source":
|
109 |
+
features = datasets.Features(
|
110 |
+
{
|
111 |
+
"document_id": datasets.Value("string"),
|
112 |
+
"text": datasets.Value("string"),
|
113 |
+
"document_type": datasets.Value("string"),
|
114 |
+
"text_type": datasets.Value("string"),
|
115 |
+
"entities": [
|
116 |
+
{
|
117 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
118 |
+
"text": datasets.Value("string"),
|
119 |
+
"type": datasets.Value("string"),
|
120 |
+
"entity_id": datasets.Value("string"),
|
121 |
+
}
|
122 |
+
],
|
123 |
+
"equivalences": [
|
124 |
+
{
|
125 |
+
"entity_id": datasets.Value("string"),
|
126 |
+
"ref_ids": datasets.Sequence(datasets.Value("string")),
|
127 |
+
}
|
128 |
+
],
|
129 |
+
"relations": [
|
130 |
+
{
|
131 |
+
"id": datasets.Value("string"),
|
132 |
+
"head": {
|
133 |
+
"ref_id": datasets.Value("string"),
|
134 |
+
"role": datasets.Value("string"),
|
135 |
+
},
|
136 |
+
"tail": {
|
137 |
+
"ref_id": datasets.Value("string"),
|
138 |
+
"role": datasets.Value("string"),
|
139 |
+
},
|
140 |
+
"type": datasets.Value("string"),
|
141 |
+
}
|
142 |
+
],
|
143 |
+
}
|
144 |
+
)
|
145 |
+
|
146 |
+
elif self.config.schema == "bigbio_kb":
|
147 |
+
features = kb_features
|
148 |
+
|
149 |
+
return datasets.DatasetInfo(
|
150 |
+
description=_DESCRIPTION,
|
151 |
+
features=features,
|
152 |
+
homepage=_HOMEPAGE,
|
153 |
+
license=str(_LICENSE),
|
154 |
+
citation=_CITATION,
|
155 |
+
)
|
156 |
+
|
157 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
158 |
+
"""Returns SplitGenerators."""
|
159 |
+
urls = _URLS[_DATASETNAME]
|
160 |
+
data_dir = Path(dl_manager.download_and_extract(urls))
|
161 |
+
all_data = data_dir / "AnEM-1.0.4" / "standoff"
|
162 |
+
|
163 |
+
return [
|
164 |
+
datasets.SplitGenerator(
|
165 |
+
name=datasets.Split.TRAIN,
|
166 |
+
gen_kwargs={
|
167 |
+
"filepath": all_data,
|
168 |
+
"split_path": data_dir
|
169 |
+
/ "AnEM-1.0.4"
|
170 |
+
/ "development"
|
171 |
+
/ "train-files.list",
|
172 |
+
"split": "train",
|
173 |
+
},
|
174 |
+
),
|
175 |
+
datasets.SplitGenerator(
|
176 |
+
name=datasets.Split.TEST,
|
177 |
+
gen_kwargs={
|
178 |
+
"filepath": all_data,
|
179 |
+
"split_path": data_dir / "AnEM-1.0.4" / "test" / "test-files.list",
|
180 |
+
"split": "test",
|
181 |
+
},
|
182 |
+
),
|
183 |
+
datasets.SplitGenerator(
|
184 |
+
name=datasets.Split.VALIDATION,
|
185 |
+
gen_kwargs={
|
186 |
+
"filepath": all_data,
|
187 |
+
"split_path": data_dir
|
188 |
+
/ "AnEM-1.0.4"
|
189 |
+
/ "development"
|
190 |
+
/ "test-files.list",
|
191 |
+
"split": "dev",
|
192 |
+
},
|
193 |
+
),
|
194 |
+
]
|
195 |
+
|
196 |
+
def _generate_examples(self, filepath, split_path, split: str) -> Tuple[int, Dict]:
|
197 |
+
"""Yields examples as (key, example) tuples."""
|
198 |
+
|
199 |
+
with open(split_path, "r") as sp:
|
200 |
+
split_list = [line.rstrip() for line in sp]
|
201 |
+
|
202 |
+
if self.config.schema == "source":
|
203 |
+
for file in filepath.iterdir():
|
204 |
+
|
205 |
+
# Use brat text files and consider files in the provided split list
|
206 |
+
if (file.suffix != ".txt") or (file.stem not in split_list):
|
207 |
+
continue
|
208 |
+
brat_parsed = parse.parse_brat_file(file)
|
209 |
+
source_example = self._brat_to_source(file, brat_parsed)
|
210 |
+
|
211 |
+
yield source_example["document_id"], source_example
|
212 |
+
|
213 |
+
elif self.config.schema == "bigbio_kb":
|
214 |
+
for file in filepath.iterdir():
|
215 |
+
|
216 |
+
# Use brat text files and consider files in the provided split list
|
217 |
+
if (file.suffix != ".txt") or (file.stem not in split_list):
|
218 |
+
continue
|
219 |
+
brat_parsed = parse.parse_brat_file(file)
|
220 |
+
bigbio_kb_example = parse.brat_parse_to_bigbio_kb(brat_parsed)
|
221 |
+
|
222 |
+
bigbio_kb_example["id"] = bigbio_kb_example["document_id"]
|
223 |
+
|
224 |
+
doc_type, text_type = self.get_document_type_and_text_type(file)
|
225 |
+
bigbio_kb_example["passages"][0]["type"] = text_type
|
226 |
+
|
227 |
+
yield bigbio_kb_example["id"], bigbio_kb_example
|
228 |
+
|
229 |
+
def _brat_to_source(self, filepath, brat_example):
|
230 |
+
"""
|
231 |
+
Converts parsed brat example to source schema example
|
232 |
+
"""
|
233 |
+
document_type, text_type = self.get_document_type_and_text_type(filepath)
|
234 |
+
|
235 |
+
source_example = {
|
236 |
+
"document_id": brat_example["document_id"],
|
237 |
+
"text": brat_example["text"],
|
238 |
+
"document_type": document_type,
|
239 |
+
"text_type": text_type,
|
240 |
+
"entities": [
|
241 |
+
{
|
242 |
+
"offsets": brat_entity["offsets"],
|
243 |
+
"text": brat_entity["text"],
|
244 |
+
"type": brat_entity["type"],
|
245 |
+
"entity_id": f"{brat_example['document_id']}_{brat_entity['id']}",
|
246 |
+
}
|
247 |
+
for brat_entity in brat_example["text_bound_annotations"]
|
248 |
+
],
|
249 |
+
"equivalences": [
|
250 |
+
{
|
251 |
+
"entity_id": brat_entity["id"],
|
252 |
+
"ref_ids": [
|
253 |
+
f"{brat_example['document_id']}_{ids}"
|
254 |
+
for ids in brat_entity["ref_ids"]
|
255 |
+
],
|
256 |
+
}
|
257 |
+
for brat_entity in brat_example["equivalences"]
|
258 |
+
],
|
259 |
+
"relations": [
|
260 |
+
{
|
261 |
+
"id": f"{brat_example['document_id']}_{brat_entity['id']}",
|
262 |
+
"head": {
|
263 |
+
"ref_id": f"{brat_example['document_id']}_{brat_entity['head']['ref_id']}",
|
264 |
+
"role": brat_entity["head"]["role"],
|
265 |
+
},
|
266 |
+
"tail": {
|
267 |
+
"ref_id": f"{brat_example['document_id']}_{brat_entity['tail']['ref_id']}",
|
268 |
+
"role": brat_entity["tail"]["role"],
|
269 |
+
},
|
270 |
+
"type": brat_entity["type"],
|
271 |
+
}
|
272 |
+
for brat_entity in brat_example["relations"]
|
273 |
+
],
|
274 |
+
}
|
275 |
+
|
276 |
+
return source_example
|
277 |
+
|
278 |
+
def get_document_type_and_text_type(self, input_file: Path) -> Tuple[str, str]:
|
279 |
+
"""
|
280 |
+
Implementation used from
|
281 |
+
https://github.com/bigscience-workshop/biomedical/blob/master/biodatasets/anat_em/anat_em.py
|
282 |
+
|
283 |
+
Extracts the document type (PubMed(PM) or PubMedCentral (PMC)) and the respective
|
284 |
+
text type (abstract for PM and sec or caption for (PMC) from the name of the given
|
285 |
+
file, e.g.:
|
286 |
+
|
287 |
+
PMID-9778569.txt -> ("PM", "abstract")
|
288 |
+
|
289 |
+
PMC-1274342-sec-02.txt -> ("PMC", "sec")
|
290 |
+
|
291 |
+
PMC-1592597-caption-02.ann -> ("PMC", "caption")
|
292 |
+
|
293 |
+
"""
|
294 |
+
name_parts = str(input_file.stem).split("-")
|
295 |
+
|
296 |
+
if name_parts[0] == "PMID":
|
297 |
+
return "PM", "abstract"
|
298 |
+
|
299 |
+
elif name_parts[0] == "PMC":
|
300 |
+
return "PMC", name_parts[2]
|
301 |
+
else:
|
302 |
+
raise AssertionError(f"Unexpected file prefix {name_parts[0]}")
|