gabrielaltay
commited on
Commit
•
aaf4b1d
1
Parent(s):
e041342
upload hubscripts/genia_relation_corpus_hub.py to hub from bigbio repo
Browse files- genia_relation_corpus.py +217 -0
genia_relation_corpus.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
The extraction of various relations stated to hold between biomolecular entities is one of the most frequently
|
18 |
+
addressed information extraction tasks in domain studies. Typical relation extraction targets involve protein-protein
|
19 |
+
interactions or gene regulatory relations. However, in the GENIA corpus, such associations involving change in the
|
20 |
+
state or properties of biomolecules are captured in the event annotation.
|
21 |
+
|
22 |
+
The GENIA corpus relation annotation aims to complement the event annotation of the corpus by capturing (primarily)
|
23 |
+
static relations, relations such as part-of that hold between entities without (necessarily) involving change.
|
24 |
+
"""
|
25 |
+
|
26 |
+
import os
|
27 |
+
from pathlib import Path
|
28 |
+
from typing import Dict, List, Tuple
|
29 |
+
|
30 |
+
import datasets
|
31 |
+
|
32 |
+
from .bigbiohub import kb_features
|
33 |
+
from .bigbiohub import BigBioConfig
|
34 |
+
from .bigbiohub import Tasks
|
35 |
+
|
36 |
+
_LANGUAGES = ['English']
|
37 |
+
_PUBMED = True
|
38 |
+
_LOCAL = False
|
39 |
+
_CITATION = """\
|
40 |
+
@inproceedings{pyysalo-etal-2009-static,
|
41 |
+
title = "Static Relations: a Piece in the Biomedical Information Extraction Puzzle",
|
42 |
+
author = "Pyysalo, Sampo and
|
43 |
+
Ohta, Tomoko and
|
44 |
+
Kim, Jin-Dong and
|
45 |
+
Tsujii, Jun{'}ichi",
|
46 |
+
booktitle = "Proceedings of the {B}io{NLP} 2009 Workshop",
|
47 |
+
month = jun,
|
48 |
+
year = "2009",
|
49 |
+
address = "Boulder, Colorado",
|
50 |
+
publisher = "Association for Computational Linguistics",
|
51 |
+
url = "https://aclanthology.org/W09-1301",
|
52 |
+
pages = "1--9",
|
53 |
+
}
|
54 |
+
|
55 |
+
@article{article,
|
56 |
+
author = {Ohta, Tomoko and Pyysalo, Sampo and Kim, Jin-Dong and Tsujii, Jun'ichi},
|
57 |
+
year = {2010},
|
58 |
+
month = {10},
|
59 |
+
pages = {917-28},
|
60 |
+
title = {A reevaluation of biomedical named entity - term relations},
|
61 |
+
volume = {8},
|
62 |
+
journal = {Journal of bioinformatics and computational biology},
|
63 |
+
doi = {10.1142/S0219720010005014}
|
64 |
+
}
|
65 |
+
|
66 |
+
@MISC{Hoehndorf_applyingontology,
|
67 |
+
author = {Robert Hoehndorf and Axel-cyrille Ngonga Ngomo and Sampo Pyysalo and Tomoko Ohta and Anika Oellrich and
|
68 |
+
Dietrich Rebholz-schuhmann},
|
69 |
+
title = {Applying ontology design patterns to the implementation of relations in GENIA},
|
70 |
+
year = {}
|
71 |
+
}
|
72 |
+
"""
|
73 |
+
|
74 |
+
_DATASETNAME = "genia_relation_corpus"
|
75 |
+
_DISPLAYNAME = "GENIA Relation Corpus"
|
76 |
+
|
77 |
+
_DESCRIPTION = """\
|
78 |
+
The extraction of various relations stated to hold between biomolecular entities is one of the most frequently
|
79 |
+
addressed information extraction tasks in domain studies. Typical relation extraction targets involve protein-protein
|
80 |
+
interactions or gene regulatory relations. However, in the GENIA corpus, such associations involving change in the
|
81 |
+
state or properties of biomolecules are captured in the event annotation.
|
82 |
+
|
83 |
+
The GENIA corpus relation annotation aims to complement the event annotation of the corpus by capturing (primarily)
|
84 |
+
static relations, relations such as part-of that hold between entities without (necessarily) involving change.
|
85 |
+
"""
|
86 |
+
|
87 |
+
_HOMEPAGE = "http://www.geniaproject.org/genia-corpus/relation-corpus"
|
88 |
+
|
89 |
+
_LICENSE = 'GENIA Project License for Annotated Corpora'
|
90 |
+
|
91 |
+
_URLS = {
|
92 |
+
_DATASETNAME: {
|
93 |
+
"train": "http://www.nactem.ac.uk/GENIA/current/GENIA-corpus/Relation/GENIA_relation_annotation_training_data.tar.gz",
|
94 |
+
"validation": "http://www.nactem.ac.uk/GENIA/current/GENIA-corpus/Relation/GENIA_relation_annotation_development_data.tar.gz",
|
95 |
+
"test": "http://www.nactem.ac.uk/GENIA/current/GENIA-corpus/Relation/GENIA_relation_annotation_test_data.tar.gz",
|
96 |
+
},
|
97 |
+
}
|
98 |
+
|
99 |
+
_SUPPORTED_TASKS = [Tasks.RELATION_EXTRACTION]
|
100 |
+
|
101 |
+
_SOURCE_VERSION = "1.0.0"
|
102 |
+
|
103 |
+
_BIGBIO_VERSION = "1.0.0"
|
104 |
+
|
105 |
+
|
106 |
+
class GeniaRelationCorpusDataset(datasets.GeneratorBasedBuilder):
|
107 |
+
"""GENIA Relation corpus."""
|
108 |
+
|
109 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
110 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
111 |
+
|
112 |
+
BUILDER_CONFIGS = [
|
113 |
+
BigBioConfig(
|
114 |
+
name="genia_relation_corpus_source",
|
115 |
+
version=SOURCE_VERSION,
|
116 |
+
description="genia_relation_corpus source schema",
|
117 |
+
schema="source",
|
118 |
+
subset_id="genia_relation_corpus",
|
119 |
+
),
|
120 |
+
BigBioConfig(
|
121 |
+
name="genia_relation_corpus_bigbio_kb",
|
122 |
+
version=BIGBIO_VERSION,
|
123 |
+
description="genia_relation_corpus BigBio schema",
|
124 |
+
schema="bigbio_kb",
|
125 |
+
subset_id="genia_relation_corpus",
|
126 |
+
),
|
127 |
+
]
|
128 |
+
|
129 |
+
DEFAULT_CONFIG_NAME = "genia_relation_corpus_source"
|
130 |
+
|
131 |
+
def _info(self) -> datasets.DatasetInfo:
|
132 |
+
if self.config.schema == "source":
|
133 |
+
features = datasets.Features(
|
134 |
+
{
|
135 |
+
"id": datasets.Value("string"),
|
136 |
+
"document_id": datasets.Value("string"),
|
137 |
+
"text": datasets.Value("string"),
|
138 |
+
"text_bound_annotations": [ # T line in brat, e.g. type or event trigger
|
139 |
+
{
|
140 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
141 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
142 |
+
"type": datasets.Value("string"),
|
143 |
+
"id": datasets.Value("string"),
|
144 |
+
}
|
145 |
+
],
|
146 |
+
"relations": [ # R line in brat
|
147 |
+
{
|
148 |
+
"id": datasets.Value("string"),
|
149 |
+
"head": {
|
150 |
+
"ref_id": datasets.Value("string"),
|
151 |
+
"role": datasets.Value("string"),
|
152 |
+
},
|
153 |
+
"tail": {
|
154 |
+
"ref_id": datasets.Value("string"),
|
155 |
+
"role": datasets.Value("string"),
|
156 |
+
},
|
157 |
+
"type": datasets.Value("string"),
|
158 |
+
}
|
159 |
+
],
|
160 |
+
"equivalences": [ # Equiv line in brat
|
161 |
+
{
|
162 |
+
"id": datasets.Value("string"),
|
163 |
+
"ref_ids": datasets.Sequence(datasets.Value("string")),
|
164 |
+
}
|
165 |
+
],
|
166 |
+
},
|
167 |
+
)
|
168 |
+
|
169 |
+
elif self.config.schema == "bigbio_kb":
|
170 |
+
features = kb_features
|
171 |
+
|
172 |
+
return datasets.DatasetInfo(
|
173 |
+
description=_DESCRIPTION,
|
174 |
+
features=features,
|
175 |
+
homepage=_HOMEPAGE,
|
176 |
+
license=str(_LICENSE),
|
177 |
+
citation=_CITATION,
|
178 |
+
)
|
179 |
+
|
180 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
181 |
+
"""Returns SplitGenerators."""
|
182 |
+
urls = _URLS[_DATASETNAME]
|
183 |
+
data_dir = dl_manager.download_and_extract(urls)
|
184 |
+
return [
|
185 |
+
datasets.SplitGenerator(
|
186 |
+
name=split,
|
187 |
+
gen_kwargs={
|
188 |
+
"data_dir": data_dir[split],
|
189 |
+
},
|
190 |
+
)
|
191 |
+
for split in [
|
192 |
+
datasets.Split.TRAIN,
|
193 |
+
datasets.Split.VALIDATION,
|
194 |
+
datasets.Split.TEST,
|
195 |
+
]
|
196 |
+
]
|
197 |
+
|
198 |
+
def _generate_examples(self, data_dir) -> Tuple[int, Dict]:
|
199 |
+
"""Yields examples as (key, example) tuples."""
|
200 |
+
for dirpath, _, filenames in os.walk(data_dir):
|
201 |
+
for guid, filename in enumerate(filenames):
|
202 |
+
if filename.endswith(".txt"):
|
203 |
+
txt_file_path = Path(dirpath, filename)
|
204 |
+
if self.config.schema == "source":
|
205 |
+
example = parsing.parse_brat_file(
|
206 |
+
txt_file_path, annotation_file_suffixes=[".a1", ".rel"]
|
207 |
+
)
|
208 |
+
example["id"] = str(guid)
|
209 |
+
for key in ["events", "attributes", "normalizations"]:
|
210 |
+
del example[key]
|
211 |
+
yield guid, example
|
212 |
+
elif self.config.schema == "bigbio_kb":
|
213 |
+
example = parsing.brat_parse_to_bigbio_kb(
|
214 |
+
parsing.parse_brat_file(txt_file_path)
|
215 |
+
)
|
216 |
+
example["id"] = str(guid)
|
217 |
+
yield guid, example
|