Commit
·
ca22192
1
Parent(s):
a91125c
upload hubscripts/pdr_hub.py to hub from bigbio repo
Browse files
pdr.py
ADDED
@@ -0,0 +1,462 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
The corpus of plant-disease relation consists of plants and diseases and their relation to PubMed abstract.
|
17 |
+
The corpus consists of about 2400 plant and disease entities and 300 annotated relations from 179 abstracts.
|
18 |
+
|
19 |
+
The big-bio and source version of this script are made by merging the 2 provided annotations on locations they intersected.
|
20 |
+
Both annotations (1, 2) are provided as separate source schemas.
|
21 |
+
"""
|
22 |
+
from collections import defaultdict
|
23 |
+
from pathlib import Path
|
24 |
+
from typing import Dict, Iterator, Optional, Tuple
|
25 |
+
|
26 |
+
import datasets
|
27 |
+
|
28 |
+
from .bigbiohub import
|
29 |
+
from .bigbiohub import BigBioConfig
|
30 |
+
from .bigbiohub import Tasks
|
31 |
+
|
32 |
+
_LANGUAGES = ['English']
|
33 |
+
_PUBMED = True
|
34 |
+
_LOCAL = False
|
35 |
+
_CITATION = """\
|
36 |
+
@article{kim2019corpus,
|
37 |
+
title={A corpus of plant--disease relations in the biomedical domain},
|
38 |
+
author={Kim, Baeksoo and Choi, Wonjun and Lee, Hyunju},
|
39 |
+
journal={PLoS One},
|
40 |
+
volume={14},
|
41 |
+
number={8},
|
42 |
+
pages={e0221582},
|
43 |
+
year={2019},
|
44 |
+
publisher={Public Library of Science San Francisco, CA USA}
|
45 |
+
}
|
46 |
+
"""
|
47 |
+
|
48 |
+
_DATASETNAME = "pdr"
|
49 |
+
_DISPLAYNAME = "PDR"
|
50 |
+
|
51 |
+
_DESCRIPTION = """
|
52 |
+
The corpus of plant-disease relation consists of plants and diseases and their relation to PubMed abstract.
|
53 |
+
The corpus consists of about 2400 plant and disease entities and 300 annotated relations from 179 abstracts.
|
54 |
+
"""
|
55 |
+
|
56 |
+
_HOMEPAGE = "http://gcancer.org/pdr/"
|
57 |
+
_LICENSE = 'License information unavailable'
|
58 |
+
_URLS = {_DATASETNAME: "http://gcancer.org/pdr/Plant-Disease_Corpus.tar.gz"}
|
59 |
+
|
60 |
+
_SUPPORTED_TASKS = [
|
61 |
+
Tasks.NAMED_ENTITY_RECOGNITION,
|
62 |
+
# Tasks.RELATION_EXTRACTION,
|
63 |
+
Tasks.EVENT_EXTRACTION,
|
64 |
+
Tasks.COREFERENCE_RESOLUTION,
|
65 |
+
]
|
66 |
+
|
67 |
+
_SOURCE_VERSION = "1.0.0"
|
68 |
+
_BIGBIO_VERSION = "1.0.0"
|
69 |
+
|
70 |
+
|
71 |
+
class PDRDataset(datasets.GeneratorBasedBuilder):
|
72 |
+
"""The corpus of plant-disease relation consists of plants and diseases and their relation to PubMed abstract"""
|
73 |
+
|
74 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
75 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
76 |
+
|
77 |
+
BUILDER_CONFIGS = [
|
78 |
+
BigBioConfig(
|
79 |
+
name="pdr_annotator1_source",
|
80 |
+
version=SOURCE_VERSION,
|
81 |
+
description="PDR annotator 1 source schema",
|
82 |
+
schema="source",
|
83 |
+
subset_id="pdr_annotator1",
|
84 |
+
),
|
85 |
+
BigBioConfig(
|
86 |
+
name="pdr_annotator2_source",
|
87 |
+
version=SOURCE_VERSION,
|
88 |
+
description="PDR annotator 2 source schema",
|
89 |
+
schema="source",
|
90 |
+
subset_id="pdr_annotator2",
|
91 |
+
),
|
92 |
+
BigBioConfig(
|
93 |
+
name="pdr_source",
|
94 |
+
version=SOURCE_VERSION,
|
95 |
+
description="PDR source schema",
|
96 |
+
schema="source",
|
97 |
+
subset_id="pdr",
|
98 |
+
),
|
99 |
+
BigBioConfig(
|
100 |
+
name="pdr_bigbio_kb",
|
101 |
+
version=BIGBIO_VERSION,
|
102 |
+
description="PDR BigBio schema",
|
103 |
+
schema="bigbio_kb",
|
104 |
+
subset_id="pdr",
|
105 |
+
),
|
106 |
+
]
|
107 |
+
|
108 |
+
DEFAULT_CONFIG_NAME = "pdr_source"
|
109 |
+
|
110 |
+
def _info(self):
|
111 |
+
if self.config.schema == "source":
|
112 |
+
features = datasets.Features(
|
113 |
+
{
|
114 |
+
"document_id": datasets.Value("string"),
|
115 |
+
"text": datasets.Value("string"),
|
116 |
+
"entities": [
|
117 |
+
{
|
118 |
+
"id": datasets.Value("string"),
|
119 |
+
"type": datasets.Value("string"),
|
120 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
121 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
122 |
+
"normalized": [
|
123 |
+
{
|
124 |
+
"db_name": datasets.Value("string"),
|
125 |
+
"db_id": datasets.Value("string"),
|
126 |
+
}
|
127 |
+
],
|
128 |
+
}
|
129 |
+
],
|
130 |
+
"relations": [
|
131 |
+
{
|
132 |
+
"id": datasets.Value("string"),
|
133 |
+
"type": datasets.Value("string"),
|
134 |
+
"arg1_id": datasets.Value("string"),
|
135 |
+
"arg2_id": datasets.Value("string"),
|
136 |
+
"normalized": [
|
137 |
+
{
|
138 |
+
"db_name": datasets.Value("string"),
|
139 |
+
"db_id": datasets.Value("string"),
|
140 |
+
}
|
141 |
+
],
|
142 |
+
}
|
143 |
+
],
|
144 |
+
"events": [
|
145 |
+
{
|
146 |
+
"id": datasets.Value("string"),
|
147 |
+
"type": datasets.Value("string"),
|
148 |
+
# refers to the text_bound_annotation of the trigger
|
149 |
+
"trigger": {
|
150 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
151 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
152 |
+
},
|
153 |
+
"arguments": [
|
154 |
+
{
|
155 |
+
"role": datasets.Value("string"),
|
156 |
+
"ref_id": datasets.Value("string"),
|
157 |
+
}
|
158 |
+
],
|
159 |
+
}
|
160 |
+
],
|
161 |
+
"coreferences": [
|
162 |
+
{
|
163 |
+
"id": datasets.Value("string"),
|
164 |
+
"entity_ids": datasets.Sequence(datasets.Value("string")),
|
165 |
+
}
|
166 |
+
],
|
167 |
+
},
|
168 |
+
)
|
169 |
+
|
170 |
+
elif self.config.schema == "bigbio_kb":
|
171 |
+
features = kb_features
|
172 |
+
|
173 |
+
return datasets.DatasetInfo(
|
174 |
+
description=_DESCRIPTION,
|
175 |
+
features=features,
|
176 |
+
homepage=_HOMEPAGE,
|
177 |
+
license=str(_LICENSE),
|
178 |
+
citation=_CITATION,
|
179 |
+
)
|
180 |
+
|
181 |
+
def _split_generators(self, dl_manager):
|
182 |
+
urls = _URLS[_DATASETNAME]
|
183 |
+
data_dir = Path(dl_manager.download_and_extract(urls))
|
184 |
+
data_dir = data_dir / "Plant-Disease_Corpus"
|
185 |
+
|
186 |
+
return [
|
187 |
+
datasets.SplitGenerator(
|
188 |
+
name=datasets.Split.TRAIN,
|
189 |
+
gen_kwargs={"data_dir": data_dir},
|
190 |
+
)
|
191 |
+
]
|
192 |
+
|
193 |
+
def _generate_examples(self, data_dir: Path) -> Iterator[Tuple[str, Dict]]:
|
194 |
+
if self.config.schema == "source":
|
195 |
+
for file in data_dir.iterdir():
|
196 |
+
if not str(file).endswith(".txt"):
|
197 |
+
continue
|
198 |
+
|
199 |
+
if self.config.subset_id == "pdr_annotator1":
|
200 |
+
# Provide annotations of annotator 1
|
201 |
+
example = parsing.parse_brat_file(file, [".ann"])
|
202 |
+
example = parsing.brat_parse_to_bigbio_kb(example)
|
203 |
+
|
204 |
+
elif self.config.subset_id == "pdr_annotator2":
|
205 |
+
# Provide annotations of annotator 2
|
206 |
+
example = parsing.parse_brat_file(file, [".ann2"])
|
207 |
+
example = parsing.brat_parse_to_bigbio_kb(example)
|
208 |
+
|
209 |
+
elif self.config.subset_id == "pdr":
|
210 |
+
# Provide merged version of annotator 1 and 2
|
211 |
+
annotator1 = parsing.parse_brat_file(file, [".ann"])
|
212 |
+
annotator1 = parsing.brat_parse_to_bigbio_kb(annotator1)
|
213 |
+
|
214 |
+
annotator2 = parsing.parse_brat_file(file, [".ann2"])
|
215 |
+
annotator2 = parsing.brat_parse_to_bigbio_kb(annotator2)
|
216 |
+
|
217 |
+
example = self._merge_annotations_by_intersection(
|
218 |
+
file, annotator1, annotator2
|
219 |
+
)
|
220 |
+
|
221 |
+
example["text"] = example["passages"][0]["text"][0]
|
222 |
+
example.pop("id", None)
|
223 |
+
example.pop("passages", None)
|
224 |
+
|
225 |
+
yield example["document_id"], example
|
226 |
+
|
227 |
+
elif self.config.schema == "bigbio_kb":
|
228 |
+
for file in data_dir.iterdir():
|
229 |
+
if not str(file).endswith(".txt"):
|
230 |
+
continue
|
231 |
+
|
232 |
+
annotator1 = parsing.parse_brat_file(file, [".ann"])
|
233 |
+
annotator1 = parsing.brat_parse_to_bigbio_kb(annotator1)
|
234 |
+
|
235 |
+
annotator2 = parsing.parse_brat_file(file, [".ann2"])
|
236 |
+
annotator2 = parsing.brat_parse_to_bigbio_kb(annotator2)
|
237 |
+
|
238 |
+
merged_annotation = self._merge_annotations_by_intersection(
|
239 |
+
file, annotator1, annotator2
|
240 |
+
)
|
241 |
+
merged_annotation["id"] = merged_annotation["document_id"]
|
242 |
+
|
243 |
+
yield merged_annotation["id"], merged_annotation
|
244 |
+
|
245 |
+
def _merge_annotations_by_intersection(
|
246 |
+
self, file: Path, example_ann1: Dict, example_ann2: Dict
|
247 |
+
) -> Dict:
|
248 |
+
"""
|
249 |
+
Merges the two given examples by only keeping annotations on which both annotators agree.
|
250 |
+
"""
|
251 |
+
id_prefix = str(file.stem) + "_"
|
252 |
+
|
253 |
+
# Mapping entity identifiers from annotator 1 / 2 to merged entity ids
|
254 |
+
a1_entity_to_merged_entity = {}
|
255 |
+
a2_entity_to_merged_entity = {}
|
256 |
+
merged_entities = []
|
257 |
+
|
258 |
+
# 1. Find all common entities, i.e. both annotators agree on same type and their offsets overlap
|
259 |
+
entity_id = 1
|
260 |
+
for entity1 in example_ann1["entities"]:
|
261 |
+
for entity2 in example_ann2["entities"]:
|
262 |
+
if (
|
263 |
+
self._overlaps(entity1, entity2)
|
264 |
+
and entity1["type"] == entity2["type"]
|
265 |
+
):
|
266 |
+
text_entity1 = "".join(entity1["text"])
|
267 |
+
text_entity2 = "".join(entity2["text"])
|
268 |
+
|
269 |
+
longer_entity = (
|
270 |
+
entity1 if len(text_entity1) > len(text_entity2) else entity2
|
271 |
+
)
|
272 |
+
merged_entity_id = id_prefix + f"E{entity_id}"
|
273 |
+
entity_id += 1
|
274 |
+
|
275 |
+
merged_entity = longer_entity.copy()
|
276 |
+
merged_entity["id"] = merged_entity_id
|
277 |
+
merged_entity["normalized"] = []
|
278 |
+
merged_entities.append(merged_entity)
|
279 |
+
|
280 |
+
a1_entity_to_merged_entity[entity1["id"]] = merged_entity_id
|
281 |
+
a2_entity_to_merged_entity[entity2["id"]] = merged_entity_id
|
282 |
+
break
|
283 |
+
|
284 |
+
# Find all relations the two annotators agree on
|
285 |
+
relations_ann1 = self._map_relations(example_ann1, a1_entity_to_merged_entity)
|
286 |
+
relations_ann2 = self._map_relations(example_ann2, a2_entity_to_merged_entity)
|
287 |
+
relations = []
|
288 |
+
relation_id = 1
|
289 |
+
|
290 |
+
for rel_type, relations_1 in relations_ann1.items():
|
291 |
+
relations_2 = relations_ann2[rel_type]
|
292 |
+
|
293 |
+
for relation_pair_1 in relations_1:
|
294 |
+
for relation_pair_2 in relations_2:
|
295 |
+
if relation_pair_1 == relation_pair_2:
|
296 |
+
relations.append(
|
297 |
+
{
|
298 |
+
"id": id_prefix + f"R{relation_id}",
|
299 |
+
"type": rel_type,
|
300 |
+
"arg1_id": relation_pair_1[0],
|
301 |
+
"arg2_id": relation_pair_1[1],
|
302 |
+
"normalized": [],
|
303 |
+
}
|
304 |
+
)
|
305 |
+
relation_id += 1
|
306 |
+
break
|
307 |
+
|
308 |
+
# Find all events the two annotators agree on
|
309 |
+
events_ann1 = self._map_events(example_ann1, a1_entity_to_merged_entity)
|
310 |
+
events_ann2 = self._map_events(example_ann2, a2_entity_to_merged_entity)
|
311 |
+
events = []
|
312 |
+
event_id = 1
|
313 |
+
|
314 |
+
for event_type, events_1 in events_ann1.items():
|
315 |
+
events_2 = events_ann2[event_type]
|
316 |
+
|
317 |
+
for (trigger1, theme1, cause1) in events_1:
|
318 |
+
for (trigger2, theme2, cause2) in events_2:
|
319 |
+
if (
|
320 |
+
theme1 == theme2
|
321 |
+
and cause1 == cause2
|
322 |
+
and self._overlaps(trigger1, trigger2)
|
323 |
+
):
|
324 |
+
trigger1_text = "".join(trigger1["text"])
|
325 |
+
trigger2_text = "".join(trigger2["text"])
|
326 |
+
|
327 |
+
longer_trigger = (
|
328 |
+
trigger1
|
329 |
+
if len(trigger1_text) >= len(trigger2_text)
|
330 |
+
else trigger2
|
331 |
+
)
|
332 |
+
events.append(
|
333 |
+
{
|
334 |
+
"id": id_prefix + f"T{event_id}",
|
335 |
+
"type": event_type,
|
336 |
+
"trigger": longer_trigger,
|
337 |
+
"arguments": [
|
338 |
+
{"role": "Theme", "ref_id": theme1},
|
339 |
+
{"role": "Cause", "ref_id": cause1},
|
340 |
+
],
|
341 |
+
}
|
342 |
+
)
|
343 |
+
event_id += 1
|
344 |
+
break
|
345 |
+
|
346 |
+
# Find all coreferences the annotators agree on
|
347 |
+
coferences_ann1 = self._map_coreferences(
|
348 |
+
example_ann1, a1_entity_to_merged_entity
|
349 |
+
)
|
350 |
+
coferences_ann2 = self._map_coreferences(
|
351 |
+
example_ann2, a2_entity_to_merged_entity
|
352 |
+
)
|
353 |
+
coreferences = []
|
354 |
+
coreference_id = 1
|
355 |
+
|
356 |
+
for _, entity_ids1 in coferences_ann1.items():
|
357 |
+
for _, entity_ids2 in coferences_ann2.items():
|
358 |
+
if entity_ids1.intersection(entity_ids2) == entity_ids1.union(
|
359 |
+
entity_ids2
|
360 |
+
):
|
361 |
+
coreferences.append(
|
362 |
+
{
|
363 |
+
"id": id_prefix + f"CO{coreference_id}",
|
364 |
+
"entity_ids": list(entity_ids1),
|
365 |
+
}
|
366 |
+
)
|
367 |
+
coreference_id += 1
|
368 |
+
|
369 |
+
merged_example = example_ann1.copy()
|
370 |
+
merged_example["entities"] = merged_entities
|
371 |
+
merged_example["relations"] = relations
|
372 |
+
merged_example["events"] = events
|
373 |
+
merged_example["coreferences"] = coreferences
|
374 |
+
|
375 |
+
return merged_example
|
376 |
+
|
377 |
+
def _map_relations(self, example: Dict, entity_id_mapping: Dict) -> Dict:
|
378 |
+
"""
|
379 |
+
Maps the all relations of the given example to their merged entity identifiers
|
380 |
+
(if existent)
|
381 |
+
"""
|
382 |
+
relation_map = defaultdict(list)
|
383 |
+
|
384 |
+
for relation in example["relations"]:
|
385 |
+
arg1_id = relation["arg1_id"]
|
386 |
+
arg2_id = relation["arg2_id"]
|
387 |
+
|
388 |
+
# Are both entities also in the merged version?
|
389 |
+
if arg1_id not in entity_id_mapping or arg2_id not in entity_id_mapping:
|
390 |
+
continue
|
391 |
+
|
392 |
+
com_arg1_id = entity_id_mapping[arg1_id]
|
393 |
+
com_arg2_id = entity_id_mapping[arg2_id]
|
394 |
+
|
395 |
+
relation_map[relation["type"]].append((com_arg1_id, com_arg2_id))
|
396 |
+
|
397 |
+
return relation_map
|
398 |
+
|
399 |
+
def _map_events(self, example: Dict, entity_id_mapping: Dict) -> Dict:
|
400 |
+
"""
|
401 |
+
Maps the all events of the given example to their merged entity identifiers
|
402 |
+
(if existent)
|
403 |
+
"""
|
404 |
+
event_map = defaultdict(list)
|
405 |
+
|
406 |
+
for event in example["events"]:
|
407 |
+
theme_id = self._get_event_argument(event, "Theme")
|
408 |
+
cause_id = self._get_event_argument(event, "Cause")
|
409 |
+
|
410 |
+
if theme_id not in entity_id_mapping or cause_id not in entity_id_mapping:
|
411 |
+
continue
|
412 |
+
|
413 |
+
common_theme_id = entity_id_mapping[theme_id]
|
414 |
+
common_cause_id = entity_id_mapping[cause_id]
|
415 |
+
|
416 |
+
event_map[event["type"]].append(
|
417 |
+
(event["trigger"], common_theme_id, common_cause_id)
|
418 |
+
)
|
419 |
+
|
420 |
+
return event_map
|
421 |
+
|
422 |
+
def _map_coreferences(self, annotation: Dict, entity_mapping: Dict) -> Dict:
|
423 |
+
"""
|
424 |
+
Maps the all coreferences of the given example to their merged entity identifiers
|
425 |
+
(if existent)
|
426 |
+
"""
|
427 |
+
id_to_corefs = defaultdict(set)
|
428 |
+
for coreference in annotation["coreferences"]:
|
429 |
+
entity_ids = set(
|
430 |
+
[
|
431 |
+
entity_mapping[id]
|
432 |
+
for id in coreference["entity_ids"]
|
433 |
+
if id in entity_mapping
|
434 |
+
]
|
435 |
+
)
|
436 |
+
|
437 |
+
# Are both id's also in the merged version?
|
438 |
+
if len(entity_ids) > 1:
|
439 |
+
id_to_corefs[coreference["id"]] = entity_ids
|
440 |
+
|
441 |
+
return id_to_corefs
|
442 |
+
|
443 |
+
def _overlaps(self, annotation1: Dict, annotation2: Dict) -> bool:
|
444 |
+
"""
|
445 |
+
Checks whether the offsets of the two given annotations overlap.
|
446 |
+
"""
|
447 |
+
for (start1, end1) in annotation1["offsets"]:
|
448 |
+
for (start2, end2) in annotation2["offsets"]:
|
449 |
+
if (start2 <= start1 <= end2) or (start2 <= end1 <= end2):
|
450 |
+
return True
|
451 |
+
|
452 |
+
return False
|
453 |
+
|
454 |
+
def _get_event_argument(self, event: Dict, role: str) -> Optional[str]:
|
455 |
+
"""
|
456 |
+
Returns the argument with the given role from the given event annotation.
|
457 |
+
"""
|
458 |
+
for argument in event["arguments"]:
|
459 |
+
if argument["role"] == role:
|
460 |
+
return argument["ref_id"]
|
461 |
+
|
462 |
+
return None
|