Update sourcedata_nlp based on git version 0af7241
Browse files- README.md +42 -0
- bigbiohub.py +590 -0
- sourcedata_nlp.py +349 -0
README.md
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
bigbio_language:
|
5 |
+
- English
|
6 |
+
license: "cc-by-4.0"
|
7 |
+
bigbio_license_shortname: cc-by-4.0
|
8 |
+
multilinguality: monolingual
|
9 |
+
pretty_name: SourceData NLP
|
10 |
+
homepage: https://sourcedata.embo.org/
|
11 |
+
bigbio_pubmed: false
|
12 |
+
bigbio_public: true
|
13 |
+
bigbio_tasks:
|
14 |
+
- NAMED_ENTITY_RECOGNITION
|
15 |
+
- NAMED_ENTITY_DISAMBIGUATION
|
16 |
+
paperswithcode_id: sourcedata-nlp
|
17 |
+
---
|
18 |
+
|
19 |
+
|
20 |
+
# Dataset Card for SourceData NLP
|
21 |
+
|
22 |
+
## Dataset Description
|
23 |
+
|
24 |
+
- **Homepage:** https://sourcedata.embo.org/
|
25 |
+
- **Pubmed:** False
|
26 |
+
- **Public:** True
|
27 |
+
- **Tasks:** NER,NED
|
28 |
+
|
29 |
+
|
30 |
+
SourceData-NLP is a named entity recognition and entity linking/disambiguation dataset produced through the routine curation of papers during the publication process. All annotations are in figure legends from published papers in molecular and cell biologyThe dataset consists of eight classes of biomedical entities (small molecules, gene products, subcellular components, cell lines, cell types, tissues, organisms, and diseases), their role in the experimental design, and the nature of the experimental method as an additional class. SourceData-NLP contains more than 620,000 annotated biomedical entities, curated from 18,689 figures in 3,223 papers in molecular and cell biology.
|
31 |
+
|
32 |
+
|
33 |
+
## Citation Information
|
34 |
+
|
35 |
+
```
|
36 |
+
@article{abreu2023sourcedata,
|
37 |
+
title={The SourceData-NLP dataset: integrating curation into scientific publishing for training large language models},
|
38 |
+
author={Abreu-Vicente, Jorge and Sonntag, Hannah and Eidens, Thomas and Lemberger, Thomas},
|
39 |
+
journal={arXiv preprint arXiv:2310.20440},
|
40 |
+
year={2023}
|
41 |
+
}
|
42 |
+
```
|
bigbiohub.py
ADDED
@@ -0,0 +1,590 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import defaultdict
|
2 |
+
from dataclasses import dataclass
|
3 |
+
from enum import Enum
|
4 |
+
import logging
|
5 |
+
from pathlib import Path
|
6 |
+
from types import SimpleNamespace
|
7 |
+
from typing import TYPE_CHECKING, Dict, Iterable, List, Tuple
|
8 |
+
|
9 |
+
import datasets
|
10 |
+
|
11 |
+
if TYPE_CHECKING:
|
12 |
+
import bioc
|
13 |
+
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
|
17 |
+
BigBioValues = SimpleNamespace(NULL="<BB_NULL_STR>")
|
18 |
+
|
19 |
+
|
20 |
+
@dataclass
|
21 |
+
class BigBioConfig(datasets.BuilderConfig):
|
22 |
+
"""BuilderConfig for BigBio."""
|
23 |
+
|
24 |
+
name: str = None
|
25 |
+
version: datasets.Version = None
|
26 |
+
description: str = None
|
27 |
+
schema: str = None
|
28 |
+
subset_id: str = None
|
29 |
+
|
30 |
+
|
31 |
+
class Tasks(Enum):
|
32 |
+
NAMED_ENTITY_RECOGNITION = "NER"
|
33 |
+
NAMED_ENTITY_DISAMBIGUATION = "NED"
|
34 |
+
EVENT_EXTRACTION = "EE"
|
35 |
+
RELATION_EXTRACTION = "RE"
|
36 |
+
COREFERENCE_RESOLUTION = "COREF"
|
37 |
+
QUESTION_ANSWERING = "QA"
|
38 |
+
TEXTUAL_ENTAILMENT = "TE"
|
39 |
+
SEMANTIC_SIMILARITY = "STS"
|
40 |
+
TEXT_PAIRS_CLASSIFICATION = "TXT2CLASS"
|
41 |
+
PARAPHRASING = "PARA"
|
42 |
+
TRANSLATION = "TRANSL"
|
43 |
+
SUMMARIZATION = "SUM"
|
44 |
+
TEXT_CLASSIFICATION = "TXTCLASS"
|
45 |
+
|
46 |
+
|
47 |
+
entailment_features = datasets.Features(
|
48 |
+
{
|
49 |
+
"id": datasets.Value("string"),
|
50 |
+
"premise": datasets.Value("string"),
|
51 |
+
"hypothesis": datasets.Value("string"),
|
52 |
+
"label": datasets.Value("string"),
|
53 |
+
}
|
54 |
+
)
|
55 |
+
|
56 |
+
pairs_features = datasets.Features(
|
57 |
+
{
|
58 |
+
"id": datasets.Value("string"),
|
59 |
+
"document_id": datasets.Value("string"),
|
60 |
+
"text_1": datasets.Value("string"),
|
61 |
+
"text_2": datasets.Value("string"),
|
62 |
+
"label": datasets.Value("string"),
|
63 |
+
}
|
64 |
+
)
|
65 |
+
|
66 |
+
qa_features = datasets.Features(
|
67 |
+
{
|
68 |
+
"id": datasets.Value("string"),
|
69 |
+
"question_id": datasets.Value("string"),
|
70 |
+
"document_id": datasets.Value("string"),
|
71 |
+
"question": datasets.Value("string"),
|
72 |
+
"type": datasets.Value("string"),
|
73 |
+
"choices": [datasets.Value("string")],
|
74 |
+
"context": datasets.Value("string"),
|
75 |
+
"answer": datasets.Sequence(datasets.Value("string")),
|
76 |
+
}
|
77 |
+
)
|
78 |
+
|
79 |
+
text_features = datasets.Features(
|
80 |
+
{
|
81 |
+
"id": datasets.Value("string"),
|
82 |
+
"document_id": datasets.Value("string"),
|
83 |
+
"text": datasets.Value("string"),
|
84 |
+
"labels": [datasets.Value("string")],
|
85 |
+
}
|
86 |
+
)
|
87 |
+
|
88 |
+
text2text_features = datasets.Features(
|
89 |
+
{
|
90 |
+
"id": datasets.Value("string"),
|
91 |
+
"document_id": datasets.Value("string"),
|
92 |
+
"text_1": datasets.Value("string"),
|
93 |
+
"text_2": datasets.Value("string"),
|
94 |
+
"text_1_name": datasets.Value("string"),
|
95 |
+
"text_2_name": datasets.Value("string"),
|
96 |
+
}
|
97 |
+
)
|
98 |
+
|
99 |
+
kb_features = datasets.Features(
|
100 |
+
{
|
101 |
+
"id": datasets.Value("string"),
|
102 |
+
"document_id": datasets.Value("string"),
|
103 |
+
"passages": [
|
104 |
+
{
|
105 |
+
"id": datasets.Value("string"),
|
106 |
+
"type": datasets.Value("string"),
|
107 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
108 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
109 |
+
}
|
110 |
+
],
|
111 |
+
"entities": [
|
112 |
+
{
|
113 |
+
"id": datasets.Value("string"),
|
114 |
+
"type": datasets.Value("string"),
|
115 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
116 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
117 |
+
"normalized": [
|
118 |
+
{
|
119 |
+
"db_name": datasets.Value("string"),
|
120 |
+
"db_id": datasets.Value("string"),
|
121 |
+
}
|
122 |
+
],
|
123 |
+
}
|
124 |
+
],
|
125 |
+
"events": [
|
126 |
+
{
|
127 |
+
"id": datasets.Value("string"),
|
128 |
+
"type": datasets.Value("string"),
|
129 |
+
# refers to the text_bound_annotation of the trigger
|
130 |
+
"trigger": {
|
131 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
132 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
133 |
+
},
|
134 |
+
"arguments": [
|
135 |
+
{
|
136 |
+
"role": datasets.Value("string"),
|
137 |
+
"ref_id": datasets.Value("string"),
|
138 |
+
}
|
139 |
+
],
|
140 |
+
}
|
141 |
+
],
|
142 |
+
"coreferences": [
|
143 |
+
{
|
144 |
+
"id": datasets.Value("string"),
|
145 |
+
"entity_ids": datasets.Sequence(datasets.Value("string")),
|
146 |
+
}
|
147 |
+
],
|
148 |
+
"relations": [
|
149 |
+
{
|
150 |
+
"id": datasets.Value("string"),
|
151 |
+
"type": datasets.Value("string"),
|
152 |
+
"arg1_id": datasets.Value("string"),
|
153 |
+
"arg2_id": datasets.Value("string"),
|
154 |
+
"normalized": [
|
155 |
+
{
|
156 |
+
"db_name": datasets.Value("string"),
|
157 |
+
"db_id": datasets.Value("string"),
|
158 |
+
}
|
159 |
+
],
|
160 |
+
}
|
161 |
+
],
|
162 |
+
}
|
163 |
+
)
|
164 |
+
|
165 |
+
|
166 |
+
TASK_TO_SCHEMA = {
|
167 |
+
Tasks.NAMED_ENTITY_RECOGNITION.name: "KB",
|
168 |
+
Tasks.NAMED_ENTITY_DISAMBIGUATION.name: "KB",
|
169 |
+
Tasks.EVENT_EXTRACTION.name: "KB",
|
170 |
+
Tasks.RELATION_EXTRACTION.name: "KB",
|
171 |
+
Tasks.COREFERENCE_RESOLUTION.name: "KB",
|
172 |
+
Tasks.QUESTION_ANSWERING.name: "QA",
|
173 |
+
Tasks.TEXTUAL_ENTAILMENT.name: "TE",
|
174 |
+
Tasks.SEMANTIC_SIMILARITY.name: "PAIRS",
|
175 |
+
Tasks.TEXT_PAIRS_CLASSIFICATION.name: "PAIRS",
|
176 |
+
Tasks.PARAPHRASING.name: "T2T",
|
177 |
+
Tasks.TRANSLATION.name: "T2T",
|
178 |
+
Tasks.SUMMARIZATION.name: "T2T",
|
179 |
+
Tasks.TEXT_CLASSIFICATION.name: "TEXT",
|
180 |
+
}
|
181 |
+
|
182 |
+
SCHEMA_TO_TASKS = defaultdict(set)
|
183 |
+
for task, schema in TASK_TO_SCHEMA.items():
|
184 |
+
SCHEMA_TO_TASKS[schema].add(task)
|
185 |
+
SCHEMA_TO_TASKS = dict(SCHEMA_TO_TASKS)
|
186 |
+
|
187 |
+
VALID_TASKS = set(TASK_TO_SCHEMA.keys())
|
188 |
+
VALID_SCHEMAS = set(TASK_TO_SCHEMA.values())
|
189 |
+
|
190 |
+
SCHEMA_TO_FEATURES = {
|
191 |
+
"KB": kb_features,
|
192 |
+
"QA": qa_features,
|
193 |
+
"TE": entailment_features,
|
194 |
+
"T2T": text2text_features,
|
195 |
+
"TEXT": text_features,
|
196 |
+
"PAIRS": pairs_features,
|
197 |
+
}
|
198 |
+
|
199 |
+
|
200 |
+
def get_texts_and_offsets_from_bioc_ann(ann: "bioc.BioCAnnotation") -> Tuple:
|
201 |
+
|
202 |
+
offsets = [(loc.offset, loc.offset + loc.length) for loc in ann.locations]
|
203 |
+
|
204 |
+
text = ann.text
|
205 |
+
|
206 |
+
if len(offsets) > 1:
|
207 |
+
i = 0
|
208 |
+
texts = []
|
209 |
+
for start, end in offsets:
|
210 |
+
chunk_len = end - start
|
211 |
+
texts.append(text[i : chunk_len + i])
|
212 |
+
i += chunk_len
|
213 |
+
while i < len(text) and text[i] == " ":
|
214 |
+
i += 1
|
215 |
+
else:
|
216 |
+
texts = [text]
|
217 |
+
|
218 |
+
return offsets, texts
|
219 |
+
|
220 |
+
|
221 |
+
def remove_prefix(a: str, prefix: str) -> str:
|
222 |
+
if a.startswith(prefix):
|
223 |
+
a = a[len(prefix) :]
|
224 |
+
return a
|
225 |
+
|
226 |
+
|
227 |
+
def parse_brat_file(
|
228 |
+
txt_file: Path,
|
229 |
+
annotation_file_suffixes: List[str] = None,
|
230 |
+
parse_notes: bool = False,
|
231 |
+
) -> Dict:
|
232 |
+
"""
|
233 |
+
Parse a brat file into the schema defined below.
|
234 |
+
`txt_file` should be the path to the brat '.txt' file you want to parse, e.g. 'data/1234.txt'
|
235 |
+
Assumes that the annotations are contained in one or more of the corresponding '.a1', '.a2' or '.ann' files,
|
236 |
+
e.g. 'data/1234.ann' or 'data/1234.a1' and 'data/1234.a2'.
|
237 |
+
Will include annotator notes, when `parse_notes == True`.
|
238 |
+
brat_features = datasets.Features(
|
239 |
+
{
|
240 |
+
"id": datasets.Value("string"),
|
241 |
+
"document_id": datasets.Value("string"),
|
242 |
+
"text": datasets.Value("string"),
|
243 |
+
"text_bound_annotations": [ # T line in brat, e.g. type or event trigger
|
244 |
+
{
|
245 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
246 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
247 |
+
"type": datasets.Value("string"),
|
248 |
+
"id": datasets.Value("string"),
|
249 |
+
}
|
250 |
+
],
|
251 |
+
"events": [ # E line in brat
|
252 |
+
{
|
253 |
+
"trigger": datasets.Value(
|
254 |
+
"string"
|
255 |
+
), # refers to the text_bound_annotation of the trigger,
|
256 |
+
"id": datasets.Value("string"),
|
257 |
+
"type": datasets.Value("string"),
|
258 |
+
"arguments": datasets.Sequence(
|
259 |
+
{
|
260 |
+
"role": datasets.Value("string"),
|
261 |
+
"ref_id": datasets.Value("string"),
|
262 |
+
}
|
263 |
+
),
|
264 |
+
}
|
265 |
+
],
|
266 |
+
"relations": [ # R line in brat
|
267 |
+
{
|
268 |
+
"id": datasets.Value("string"),
|
269 |
+
"head": {
|
270 |
+
"ref_id": datasets.Value("string"),
|
271 |
+
"role": datasets.Value("string"),
|
272 |
+
},
|
273 |
+
"tail": {
|
274 |
+
"ref_id": datasets.Value("string"),
|
275 |
+
"role": datasets.Value("string"),
|
276 |
+
},
|
277 |
+
"type": datasets.Value("string"),
|
278 |
+
}
|
279 |
+
],
|
280 |
+
"equivalences": [ # Equiv line in brat
|
281 |
+
{
|
282 |
+
"id": datasets.Value("string"),
|
283 |
+
"ref_ids": datasets.Sequence(datasets.Value("string")),
|
284 |
+
}
|
285 |
+
],
|
286 |
+
"attributes": [ # M or A lines in brat
|
287 |
+
{
|
288 |
+
"id": datasets.Value("string"),
|
289 |
+
"type": datasets.Value("string"),
|
290 |
+
"ref_id": datasets.Value("string"),
|
291 |
+
"value": datasets.Value("string"),
|
292 |
+
}
|
293 |
+
],
|
294 |
+
"normalizations": [ # N lines in brat
|
295 |
+
{
|
296 |
+
"id": datasets.Value("string"),
|
297 |
+
"type": datasets.Value("string"),
|
298 |
+
"ref_id": datasets.Value("string"),
|
299 |
+
"resource_name": datasets.Value(
|
300 |
+
"string"
|
301 |
+
), # Name of the resource, e.g. "Wikipedia"
|
302 |
+
"cuid": datasets.Value(
|
303 |
+
"string"
|
304 |
+
), # ID in the resource, e.g. 534366
|
305 |
+
"text": datasets.Value(
|
306 |
+
"string"
|
307 |
+
), # Human readable description/name of the entity, e.g. "Barack Obama"
|
308 |
+
}
|
309 |
+
],
|
310 |
+
### OPTIONAL: Only included when `parse_notes == True`
|
311 |
+
"notes": [ # # lines in brat
|
312 |
+
{
|
313 |
+
"id": datasets.Value("string"),
|
314 |
+
"type": datasets.Value("string"),
|
315 |
+
"ref_id": datasets.Value("string"),
|
316 |
+
"text": datasets.Value("string"),
|
317 |
+
}
|
318 |
+
],
|
319 |
+
},
|
320 |
+
)
|
321 |
+
"""
|
322 |
+
|
323 |
+
example = {}
|
324 |
+
example["document_id"] = txt_file.with_suffix("").name
|
325 |
+
with txt_file.open() as f:
|
326 |
+
example["text"] = f.read()
|
327 |
+
|
328 |
+
# If no specific suffixes of the to-be-read annotation files are given - take standard suffixes
|
329 |
+
# for event extraction
|
330 |
+
if annotation_file_suffixes is None:
|
331 |
+
annotation_file_suffixes = [".a1", ".a2", ".ann"]
|
332 |
+
|
333 |
+
if len(annotation_file_suffixes) == 0:
|
334 |
+
raise AssertionError(
|
335 |
+
"At least one suffix for the to-be-read annotation files should be given!"
|
336 |
+
)
|
337 |
+
|
338 |
+
ann_lines = []
|
339 |
+
for suffix in annotation_file_suffixes:
|
340 |
+
annotation_file = txt_file.with_suffix(suffix)
|
341 |
+
if annotation_file.exists():
|
342 |
+
with annotation_file.open() as f:
|
343 |
+
ann_lines.extend(f.readlines())
|
344 |
+
|
345 |
+
example["text_bound_annotations"] = []
|
346 |
+
example["events"] = []
|
347 |
+
example["relations"] = []
|
348 |
+
example["equivalences"] = []
|
349 |
+
example["attributes"] = []
|
350 |
+
example["normalizations"] = []
|
351 |
+
|
352 |
+
if parse_notes:
|
353 |
+
example["notes"] = []
|
354 |
+
|
355 |
+
for line in ann_lines:
|
356 |
+
line = line.strip()
|
357 |
+
if not line:
|
358 |
+
continue
|
359 |
+
|
360 |
+
if line.startswith("T"): # Text bound
|
361 |
+
ann = {}
|
362 |
+
fields = line.split("\t")
|
363 |
+
|
364 |
+
ann["id"] = fields[0]
|
365 |
+
ann["type"] = fields[1].split()[0]
|
366 |
+
ann["offsets"] = []
|
367 |
+
span_str = remove_prefix(fields[1], (ann["type"] + " "))
|
368 |
+
text = fields[2]
|
369 |
+
for span in span_str.split(";"):
|
370 |
+
start, end = span.split()
|
371 |
+
ann["offsets"].append([int(start), int(end)])
|
372 |
+
|
373 |
+
# Heuristically split text of discontiguous entities into chunks
|
374 |
+
ann["text"] = []
|
375 |
+
if len(ann["offsets"]) > 1:
|
376 |
+
i = 0
|
377 |
+
for start, end in ann["offsets"]:
|
378 |
+
chunk_len = end - start
|
379 |
+
ann["text"].append(text[i : chunk_len + i])
|
380 |
+
i += chunk_len
|
381 |
+
while i < len(text) and text[i] == " ":
|
382 |
+
i += 1
|
383 |
+
else:
|
384 |
+
ann["text"] = [text]
|
385 |
+
|
386 |
+
example["text_bound_annotations"].append(ann)
|
387 |
+
|
388 |
+
elif line.startswith("E"):
|
389 |
+
ann = {}
|
390 |
+
fields = line.split("\t")
|
391 |
+
|
392 |
+
ann["id"] = fields[0]
|
393 |
+
|
394 |
+
ann["type"], ann["trigger"] = fields[1].split()[0].split(":")
|
395 |
+
|
396 |
+
ann["arguments"] = []
|
397 |
+
for role_ref_id in fields[1].split()[1:]:
|
398 |
+
argument = {
|
399 |
+
"role": (role_ref_id.split(":"))[0],
|
400 |
+
"ref_id": (role_ref_id.split(":"))[1],
|
401 |
+
}
|
402 |
+
ann["arguments"].append(argument)
|
403 |
+
|
404 |
+
example["events"].append(ann)
|
405 |
+
|
406 |
+
elif line.startswith("R"):
|
407 |
+
ann = {}
|
408 |
+
fields = line.split("\t")
|
409 |
+
|
410 |
+
ann["id"] = fields[0]
|
411 |
+
ann["type"] = fields[1].split()[0]
|
412 |
+
|
413 |
+
ann["head"] = {
|
414 |
+
"role": fields[1].split()[1].split(":")[0],
|
415 |
+
"ref_id": fields[1].split()[1].split(":")[1],
|
416 |
+
}
|
417 |
+
ann["tail"] = {
|
418 |
+
"role": fields[1].split()[2].split(":")[0],
|
419 |
+
"ref_id": fields[1].split()[2].split(":")[1],
|
420 |
+
}
|
421 |
+
|
422 |
+
example["relations"].append(ann)
|
423 |
+
|
424 |
+
# '*' seems to be the legacy way to mark equivalences,
|
425 |
+
# but I couldn't find any info on the current way
|
426 |
+
# this might have to be adapted dependent on the brat version
|
427 |
+
# of the annotation
|
428 |
+
elif line.startswith("*"):
|
429 |
+
ann = {}
|
430 |
+
fields = line.split("\t")
|
431 |
+
|
432 |
+
ann["id"] = fields[0]
|
433 |
+
ann["ref_ids"] = fields[1].split()[1:]
|
434 |
+
|
435 |
+
example["equivalences"].append(ann)
|
436 |
+
|
437 |
+
elif line.startswith("A") or line.startswith("M"):
|
438 |
+
ann = {}
|
439 |
+
fields = line.split("\t")
|
440 |
+
|
441 |
+
ann["id"] = fields[0]
|
442 |
+
|
443 |
+
info = fields[1].split()
|
444 |
+
ann["type"] = info[0]
|
445 |
+
ann["ref_id"] = info[1]
|
446 |
+
|
447 |
+
if len(info) > 2:
|
448 |
+
ann["value"] = info[2]
|
449 |
+
else:
|
450 |
+
ann["value"] = ""
|
451 |
+
|
452 |
+
example["attributes"].append(ann)
|
453 |
+
|
454 |
+
elif line.startswith("N"):
|
455 |
+
ann = {}
|
456 |
+
fields = line.split("\t")
|
457 |
+
|
458 |
+
ann["id"] = fields[0]
|
459 |
+
ann["text"] = fields[2]
|
460 |
+
|
461 |
+
info = fields[1].split()
|
462 |
+
|
463 |
+
ann["type"] = info[0]
|
464 |
+
ann["ref_id"] = info[1]
|
465 |
+
ann["resource_name"] = info[2].split(":")[0]
|
466 |
+
ann["cuid"] = info[2].split(":")[1]
|
467 |
+
example["normalizations"].append(ann)
|
468 |
+
|
469 |
+
elif parse_notes and line.startswith("#"):
|
470 |
+
ann = {}
|
471 |
+
fields = line.split("\t")
|
472 |
+
|
473 |
+
ann["id"] = fields[0]
|
474 |
+
ann["text"] = fields[2] if len(fields) == 3 else BigBioValues.NULL
|
475 |
+
|
476 |
+
info = fields[1].split()
|
477 |
+
|
478 |
+
ann["type"] = info[0]
|
479 |
+
ann["ref_id"] = info[1]
|
480 |
+
example["notes"].append(ann)
|
481 |
+
|
482 |
+
return example
|
483 |
+
|
484 |
+
|
485 |
+
def brat_parse_to_bigbio_kb(brat_parse: Dict) -> Dict:
|
486 |
+
"""
|
487 |
+
Transform a brat parse (conforming to the standard brat schema) obtained with
|
488 |
+
`parse_brat_file` into a dictionary conforming to the `bigbio-kb` schema (as defined in ../schemas/kb.py)
|
489 |
+
:param brat_parse:
|
490 |
+
"""
|
491 |
+
|
492 |
+
unified_example = {}
|
493 |
+
|
494 |
+
# Prefix all ids with document id to ensure global uniqueness,
|
495 |
+
# because brat ids are only unique within their document
|
496 |
+
id_prefix = brat_parse["document_id"] + "_"
|
497 |
+
|
498 |
+
# identical
|
499 |
+
unified_example["document_id"] = brat_parse["document_id"]
|
500 |
+
unified_example["passages"] = [
|
501 |
+
{
|
502 |
+
"id": id_prefix + "_text",
|
503 |
+
"type": "abstract",
|
504 |
+
"text": [brat_parse["text"]],
|
505 |
+
"offsets": [[0, len(brat_parse["text"])]],
|
506 |
+
}
|
507 |
+
]
|
508 |
+
|
509 |
+
# get normalizations
|
510 |
+
ref_id_to_normalizations = defaultdict(list)
|
511 |
+
for normalization in brat_parse["normalizations"]:
|
512 |
+
ref_id_to_normalizations[normalization["ref_id"]].append(
|
513 |
+
{
|
514 |
+
"db_name": normalization["resource_name"],
|
515 |
+
"db_id": normalization["cuid"],
|
516 |
+
}
|
517 |
+
)
|
518 |
+
|
519 |
+
# separate entities and event triggers
|
520 |
+
unified_example["events"] = []
|
521 |
+
non_event_ann = brat_parse["text_bound_annotations"].copy()
|
522 |
+
for event in brat_parse["events"]:
|
523 |
+
event = event.copy()
|
524 |
+
event["id"] = id_prefix + event["id"]
|
525 |
+
trigger = next(
|
526 |
+
tr
|
527 |
+
for tr in brat_parse["text_bound_annotations"]
|
528 |
+
if tr["id"] == event["trigger"]
|
529 |
+
)
|
530 |
+
if trigger in non_event_ann:
|
531 |
+
non_event_ann.remove(trigger)
|
532 |
+
event["trigger"] = {
|
533 |
+
"text": trigger["text"].copy(),
|
534 |
+
"offsets": trigger["offsets"].copy(),
|
535 |
+
}
|
536 |
+
for argument in event["arguments"]:
|
537 |
+
argument["ref_id"] = id_prefix + argument["ref_id"]
|
538 |
+
|
539 |
+
unified_example["events"].append(event)
|
540 |
+
|
541 |
+
unified_example["entities"] = []
|
542 |
+
anno_ids = [ref_id["id"] for ref_id in non_event_ann]
|
543 |
+
for ann in non_event_ann:
|
544 |
+
entity_ann = ann.copy()
|
545 |
+
entity_ann["id"] = id_prefix + entity_ann["id"]
|
546 |
+
entity_ann["normalized"] = ref_id_to_normalizations[ann["id"]]
|
547 |
+
unified_example["entities"].append(entity_ann)
|
548 |
+
|
549 |
+
# massage relations
|
550 |
+
unified_example["relations"] = []
|
551 |
+
skipped_relations = set()
|
552 |
+
for ann in brat_parse["relations"]:
|
553 |
+
if (
|
554 |
+
ann["head"]["ref_id"] not in anno_ids
|
555 |
+
or ann["tail"]["ref_id"] not in anno_ids
|
556 |
+
):
|
557 |
+
skipped_relations.add(ann["id"])
|
558 |
+
continue
|
559 |
+
unified_example["relations"].append(
|
560 |
+
{
|
561 |
+
"arg1_id": id_prefix + ann["head"]["ref_id"],
|
562 |
+
"arg2_id": id_prefix + ann["tail"]["ref_id"],
|
563 |
+
"id": id_prefix + ann["id"],
|
564 |
+
"type": ann["type"],
|
565 |
+
"normalized": [],
|
566 |
+
}
|
567 |
+
)
|
568 |
+
if len(skipped_relations) > 0:
|
569 |
+
example_id = brat_parse["document_id"]
|
570 |
+
logger.info(
|
571 |
+
f"Example:{example_id}: The `bigbio_kb` schema allows `relations` only between entities."
|
572 |
+
f" Skip (for now): "
|
573 |
+
f"{list(skipped_relations)}"
|
574 |
+
)
|
575 |
+
|
576 |
+
# get coreferences
|
577 |
+
unified_example["coreferences"] = []
|
578 |
+
for i, ann in enumerate(brat_parse["equivalences"], start=1):
|
579 |
+
is_entity_cluster = True
|
580 |
+
for ref_id in ann["ref_ids"]:
|
581 |
+
if not ref_id.startswith("T"): # not textbound -> no entity
|
582 |
+
is_entity_cluster = False
|
583 |
+
elif ref_id not in anno_ids: # event trigger -> no entity
|
584 |
+
is_entity_cluster = False
|
585 |
+
if is_entity_cluster:
|
586 |
+
entity_ids = [id_prefix + i for i in ann["ref_ids"]]
|
587 |
+
unified_example["coreferences"].append(
|
588 |
+
{"id": id_prefix + str(i), "entity_ids": entity_ids}
|
589 |
+
)
|
590 |
+
return unified_example
|
sourcedata_nlp.py
ADDED
@@ -0,0 +1,349 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
We present the SourceData-NLP dataset produced through the routine curation of papers during the publication process.
|
18 |
+
A unique feature of this dataset is its emphasis on the annotation of bioentities in figure legends.
|
19 |
+
We annotate eight classes of biomedical entities (small molecules, gene products, subcellular components,
|
20 |
+
cell lines, cell types, tissues, organisms, and diseases), their role in the experimental design,
|
21 |
+
and the nature of the experimental method as an additional class.
|
22 |
+
SourceData-NLP contains more than 620,000 annotated biomedical entities, curated from 18,689 figures in
|
23 |
+
3,223 papers in molecular and cell biology.
|
24 |
+
|
25 |
+
[bigbio_schema_name] = kb
|
26 |
+
"""
|
27 |
+
|
28 |
+
import itertools
|
29 |
+
import json
|
30 |
+
import os
|
31 |
+
from typing import Dict, List, Tuple
|
32 |
+
|
33 |
+
import datasets
|
34 |
+
|
35 |
+
from .bigbiohub import BigBioConfig, Tasks, kb_features
|
36 |
+
|
37 |
+
_LANGUAGES = ["English"]
|
38 |
+
_PUBMED = True
|
39 |
+
_LOCAL = False
|
40 |
+
_DISPLAYNAME = "SourceData-NLP"
|
41 |
+
|
42 |
+
_CITATION = """\
|
43 |
+
@article{abreu2023sourcedata,
|
44 |
+
title={The SourceData-NLP dataset: integrating curation into scientific publishing
|
45 |
+
for training large language models},
|
46 |
+
author={Abreu-Vicente, Jorge and Sonntag, Hannah and Eidens, Thomas and Lemberger, Thomas},
|
47 |
+
journal={arXiv preprint arXiv:2310.20440},
|
48 |
+
year={2023}
|
49 |
+
}
|
50 |
+
"""
|
51 |
+
|
52 |
+
_DATASETNAME = "sourcedata_nlp"
|
53 |
+
|
54 |
+
_DESCRIPTION = """\
|
55 |
+
SourceData is an NER/NED dataset of expert annotations of nine
|
56 |
+
entity types in figure captions from biomedical research papers.
|
57 |
+
"""
|
58 |
+
|
59 |
+
_HOMEPAGE = "https://sourcedata.embo.org/"
|
60 |
+
|
61 |
+
|
62 |
+
_LICENSE = "CC_BY_4p0"
|
63 |
+
|
64 |
+
|
65 |
+
_URLS = {
|
66 |
+
_DATASETNAME: (
|
67 |
+
"https://huggingface.co/datasets/EMBO/SourceData/resolve/main/bigbio/source_data_json_splits_2.0.2.zip"
|
68 |
+
)
|
69 |
+
}
|
70 |
+
|
71 |
+
|
72 |
+
_SUPPORTED_TASKS = [Tasks.NAMED_ENTITY_DISAMBIGUATION, Tasks.NAMED_ENTITY_RECOGNITION]
|
73 |
+
|
74 |
+
_SOURCE_VERSION = "2.0.2"
|
75 |
+
|
76 |
+
_BIGBIO_VERSION = "1.0.0"
|
77 |
+
|
78 |
+
|
79 |
+
class SourceDataNlpDataset(datasets.GeneratorBasedBuilder):
|
80 |
+
"""NER + NED dataset of multiple entity types from figure captions of scientific publications"""
|
81 |
+
|
82 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
83 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
84 |
+
|
85 |
+
BUILDER_CONFIGS = [
|
86 |
+
BigBioConfig(
|
87 |
+
name="sourcedata_nlp_source",
|
88 |
+
version=SOURCE_VERSION,
|
89 |
+
description="sourcedata_nlp source schema",
|
90 |
+
schema="source",
|
91 |
+
subset_id="sourcedata_nlp",
|
92 |
+
),
|
93 |
+
BigBioConfig(
|
94 |
+
name="sourcedata_nlp_bigbio_kb",
|
95 |
+
version=BIGBIO_VERSION,
|
96 |
+
description="sourcedata_nlp BigBio schema",
|
97 |
+
schema="bigbio_kb",
|
98 |
+
subset_id="sourcedata_nlp",
|
99 |
+
),
|
100 |
+
]
|
101 |
+
|
102 |
+
DEFAULT_CONFIG_NAME = "sourcedata_nlp_source"
|
103 |
+
|
104 |
+
def _info(self) -> datasets.DatasetInfo:
|
105 |
+
if self.config.schema == "source":
|
106 |
+
features = datasets.Features(
|
107 |
+
{
|
108 |
+
"doi": datasets.Value("string"),
|
109 |
+
"abstract": datasets.Value("string"),
|
110 |
+
"figures": [
|
111 |
+
{
|
112 |
+
"fig_id": datasets.Value("string"),
|
113 |
+
"label": datasets.Value("string"),
|
114 |
+
"fig_graphic_url": datasets.Value("string"),
|
115 |
+
"panels": [
|
116 |
+
{
|
117 |
+
"panel_id": datasets.Value("string"),
|
118 |
+
"text": datasets.Value("string"),
|
119 |
+
"panel_graphic_url": datasets.Value("string"),
|
120 |
+
"entities": [
|
121 |
+
{
|
122 |
+
"annotation_id": datasets.Value("string"),
|
123 |
+
"source": datasets.Value("string"),
|
124 |
+
"category": datasets.Value("string"),
|
125 |
+
"entity_type": datasets.Value("string"),
|
126 |
+
"role": datasets.Value("string"),
|
127 |
+
"text": datasets.Value("string"),
|
128 |
+
"ext_ids": datasets.Value("string"),
|
129 |
+
"norm_text": datasets.Value("string"),
|
130 |
+
"ext_dbs": datasets.Value("string"),
|
131 |
+
"in_caption": datasets.Value("bool"),
|
132 |
+
"ext_names": datasets.Value("string"),
|
133 |
+
"ext_tax_ids": datasets.Value("string"),
|
134 |
+
"ext_tax_names": datasets.Value("string"),
|
135 |
+
"ext_urls": datasets.Value("string"),
|
136 |
+
"offsets": [datasets.Value("int64")],
|
137 |
+
}
|
138 |
+
],
|
139 |
+
}
|
140 |
+
],
|
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=_LICENSE,
|
154 |
+
citation=_CITATION,
|
155 |
+
)
|
156 |
+
|
157 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
158 |
+
"""Returns SplitGenerators."""
|
159 |
+
|
160 |
+
urls = _URLS[_DATASETNAME]
|
161 |
+
data_dir = dl_manager.download_and_extract(urls)
|
162 |
+
|
163 |
+
return [
|
164 |
+
datasets.SplitGenerator(
|
165 |
+
name=datasets.Split.TRAIN,
|
166 |
+
gen_kwargs={
|
167 |
+
"filepath": os.path.join(data_dir, "train.jsonl"),
|
168 |
+
},
|
169 |
+
),
|
170 |
+
datasets.SplitGenerator(
|
171 |
+
name=datasets.Split.TEST,
|
172 |
+
gen_kwargs={
|
173 |
+
"filepath": os.path.join(data_dir, "test.jsonl"),
|
174 |
+
},
|
175 |
+
),
|
176 |
+
datasets.SplitGenerator(
|
177 |
+
name=datasets.Split.VALIDATION,
|
178 |
+
gen_kwargs={
|
179 |
+
"filepath": os.path.join(data_dir, "validation.jsonl"),
|
180 |
+
},
|
181 |
+
),
|
182 |
+
]
|
183 |
+
|
184 |
+
def _generate_examples(self, filepath) -> Tuple[int, Dict]:
|
185 |
+
"""Yields examples as (key, example) tuples."""
|
186 |
+
|
187 |
+
if self.config.schema == "source":
|
188 |
+
with open(filepath) as fstream:
|
189 |
+
for line in fstream:
|
190 |
+
document = self._parse_document(line)
|
191 |
+
doc_figs = document["figures"]
|
192 |
+
all_figures = []
|
193 |
+
for fig in doc_figs:
|
194 |
+
all_panels = []
|
195 |
+
figure = {
|
196 |
+
"fig_id": fig["fig_id"],
|
197 |
+
"label": fig["label"],
|
198 |
+
"fig_graphic_url": fig["fig_graphic_url"],
|
199 |
+
}
|
200 |
+
for p in fig["panels"]:
|
201 |
+
panel = {
|
202 |
+
"panel_id": p["panel_id"],
|
203 |
+
"text": p["text"].strip(),
|
204 |
+
"panel_graphic_url": p["panel_graphic_url"],
|
205 |
+
"entities": [
|
206 |
+
{
|
207 |
+
"annotation_id": t["tag_id"],
|
208 |
+
"source": t["source"],
|
209 |
+
"category": t["category"],
|
210 |
+
"entity_type": t["entity_type"],
|
211 |
+
"role": t["role"],
|
212 |
+
"text": t["text"],
|
213 |
+
"ext_ids": t["ext_ids"],
|
214 |
+
"norm_text": t["norm_text"],
|
215 |
+
"ext_dbs": t["ext_dbs"],
|
216 |
+
"in_caption": bool(t["in_caption"]),
|
217 |
+
"ext_names": t["ext_names"],
|
218 |
+
"ext_tax_ids": t["ext_tax_ids"],
|
219 |
+
"ext_tax_names": t["ext_tax_names"],
|
220 |
+
"ext_urls": t["ext_urls"],
|
221 |
+
"offsets": t["local_offsets"],
|
222 |
+
}
|
223 |
+
for t in p["tags"]
|
224 |
+
],
|
225 |
+
}
|
226 |
+
for e in panel["entities"]:
|
227 |
+
assert type(e["offsets"]) == list
|
228 |
+
if len(panel["entities"]) == 0:
|
229 |
+
continue
|
230 |
+
all_panels.append(panel)
|
231 |
+
|
232 |
+
figure["panels"] = all_panels
|
233 |
+
|
234 |
+
# Pass on all figures that aren't split into panels
|
235 |
+
if len(all_panels) == 0:
|
236 |
+
continue
|
237 |
+
all_figures.append(figure)
|
238 |
+
|
239 |
+
output = {
|
240 |
+
"doi": document["doi"],
|
241 |
+
"abstract": document["abstract"],
|
242 |
+
"figures": all_figures,
|
243 |
+
}
|
244 |
+
yield document["doi"], output
|
245 |
+
|
246 |
+
elif self.config.schema == "bigbio_kb":
|
247 |
+
uid = itertools.count(0)
|
248 |
+
|
249 |
+
with open(filepath) as fstream:
|
250 |
+
for line in fstream:
|
251 |
+
output = {}
|
252 |
+
document = self._parse_document(line)
|
253 |
+
|
254 |
+
# Get ids for each document + list of passages
|
255 |
+
output["id"] = next(uid)
|
256 |
+
output["document_id"] = document["doi"]
|
257 |
+
output["passages"] = document["passages"]
|
258 |
+
for i, passage in enumerate(output["passages"]):
|
259 |
+
passage["id"] = next(uid)
|
260 |
+
passage_text = passage["text"].strip()
|
261 |
+
passage["text"] = [passage_text]
|
262 |
+
passage_offsets = passage["offsets"]
|
263 |
+
if i == 0:
|
264 |
+
passage_offsets[1] = len(passage_text.strip())
|
265 |
+
passage["offsets"] = [
|
266 |
+
[
|
267 |
+
passage_offsets[0],
|
268 |
+
passage_offsets[0] + passage_offsets[1],
|
269 |
+
]
|
270 |
+
]
|
271 |
+
entities = []
|
272 |
+
for fig in document["figures"]:
|
273 |
+
for panel in fig["panels"]:
|
274 |
+
for tag in panel["tags"]:
|
275 |
+
# Create two separate ents if both role and tag are labeled.
|
276 |
+
ent_type = self._get_entity_type(tag)
|
277 |
+
if ent_type is not None:
|
278 |
+
ent = {
|
279 |
+
"id": next(uid),
|
280 |
+
"type": ent_type,
|
281 |
+
"text": [tag["text"]],
|
282 |
+
"offsets": [tag["document_offsets"]],
|
283 |
+
"normalized": [
|
284 |
+
{"db_name": db_name, "db_id": db_id}
|
285 |
+
for db_name, db_id in zip(tag["ext_dbs"], tag["ext_ids"])
|
286 |
+
],
|
287 |
+
}
|
288 |
+
entities.append(ent)
|
289 |
+
|
290 |
+
# When entity has a role as well, add an additional entity for this
|
291 |
+
# Necessary to create duplicate entity due to constraints of BigBio schema
|
292 |
+
# These can be consolidated by matching up document ID + offsets
|
293 |
+
role = self._get_entity_role(tag)
|
294 |
+
if role is not None:
|
295 |
+
role_ent = {
|
296 |
+
"id": next(uid),
|
297 |
+
"type": role,
|
298 |
+
"text": [tag["text"]],
|
299 |
+
"offsets": [tag["document_offsets"]],
|
300 |
+
"normalized": [
|
301 |
+
{"db_name": db_name, "db_id": db_id}
|
302 |
+
for db_name, db_id in zip(tag["ext_dbs"], tag["ext_ids"])
|
303 |
+
],
|
304 |
+
}
|
305 |
+
entities.append(role_ent)
|
306 |
+
|
307 |
+
output["entities"] = entities
|
308 |
+
|
309 |
+
output["relations"] = []
|
310 |
+
output["events"] = []
|
311 |
+
output["coreferences"] = []
|
312 |
+
|
313 |
+
yield output["document_id"], output
|
314 |
+
|
315 |
+
def _parse_document(self, raw_document):
|
316 |
+
doc = json.loads(raw_document.strip())
|
317 |
+
return doc
|
318 |
+
|
319 |
+
def _get_entity_type(self, tag):
|
320 |
+
if tag["entity_type"] == "molecule":
|
321 |
+
return "SMALL_MOLECULE"
|
322 |
+
elif tag["entity_type"] in ["geneprod", "gene", "protein"]:
|
323 |
+
return "GENEPROD"
|
324 |
+
elif tag["entity_type"] == "subcellular":
|
325 |
+
return "SUBCELLULAR"
|
326 |
+
elif tag["entity_type"] == "cell_type":
|
327 |
+
return "CELL_TYPE"
|
328 |
+
elif tag["entity_type"] == "tissue":
|
329 |
+
return "TISSUE"
|
330 |
+
elif tag["entity_type"] == "organism":
|
331 |
+
return "ORGANISM"
|
332 |
+
elif tag["category"] == "assay":
|
333 |
+
return "EXP_ASSAY"
|
334 |
+
elif tag["category"] == "disease":
|
335 |
+
return "DISEASE"
|
336 |
+
elif tag["entity_type"] == "cell_line":
|
337 |
+
return "CELL_LINE"
|
338 |
+
|
339 |
+
def _get_entity_role(self, tag):
|
340 |
+
if tag["entity_type"] == "molecule":
|
341 |
+
if tag["role"] == "intervention":
|
342 |
+
return "CONTROLLED_VAR"
|
343 |
+
elif tag["role"] == "assayed":
|
344 |
+
return "MEASURED_VAR"
|
345 |
+
elif tag["entity_type"] in ["geneprod", "gene", "protein"]:
|
346 |
+
if tag["role"] == "intervention":
|
347 |
+
return "CONTROLLED_VAR"
|
348 |
+
elif tag["role"] == "assayed":
|
349 |
+
return "MEASURED_VAR"
|