Commit
•
828add6
1
Parent(s):
2d757a8
Use iter_files
Browse files- chebi_nactem.py +68 -78
chebi_nactem.py
CHANGED
@@ -14,8 +14,9 @@
|
|
14 |
# limitations under the License.
|
15 |
|
16 |
import os.path
|
|
|
17 |
from pathlib import Path
|
18 |
-
from typing import Dict, List, Tuple
|
19 |
|
20 |
import datasets
|
21 |
|
@@ -25,7 +26,7 @@ from .bigbiohub import Tasks
|
|
25 |
from .bigbiohub import parse_brat_file
|
26 |
|
27 |
|
28 |
-
_LANGUAGES = [
|
29 |
_PUBMED = True
|
30 |
_LOCAL = False
|
31 |
_CITATION = """\
|
@@ -66,7 +67,7 @@ and over 6,000 relations between entities.
|
|
66 |
|
67 |
_HOMEPAGE = "http://www.nactem.ac.uk/chebi"
|
68 |
|
69 |
-
_LICENSE =
|
70 |
|
71 |
_URLS = {
|
72 |
_DATASETNAME: "http://www.nactem.ac.uk/chebi/ChEBI.zip",
|
@@ -154,86 +155,75 @@ class ChebiNactemDatasset(datasets.GeneratorBasedBuilder):
|
|
154 |
"chebi_nactem_abstr_ann2": os.path.join("ChEBI", "abstracts", "Annotator2"),
|
155 |
"chebi_nactem_fullpaper": os.path.join("ChEBI", "fullpapers"),
|
156 |
}
|
157 |
-
|
158 |
-
subset_dir = Path(data_dir) / subset_paths[self.config.subset_id]
|
159 |
-
|
160 |
return [
|
161 |
datasets.SplitGenerator(
|
162 |
name=datasets.Split.TRAIN,
|
163 |
-
|
164 |
-
gen_kwargs={
|
165 |
-
"data_dir": subset_dir,
|
166 |
-
},
|
167 |
)
|
168 |
]
|
169 |
|
170 |
-
def _generate_examples(self,
|
171 |
"""Yields examples as (key, example) tuples."""
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
}
|
235 |
-
for relation in contents["relations"]
|
236 |
-
],
|
237 |
-
}
|
238 |
-
else:
|
239 |
-
raise NotImplementedError(self.config.schema)
|
|
|
14 |
# limitations under the License.
|
15 |
|
16 |
import os.path
|
17 |
+
from itertools import count
|
18 |
from pathlib import Path
|
19 |
+
from typing import Dict, Iterable, List, Tuple
|
20 |
|
21 |
import datasets
|
22 |
|
|
|
26 |
from .bigbiohub import parse_brat_file
|
27 |
|
28 |
|
29 |
+
_LANGUAGES = ["English"]
|
30 |
_PUBMED = True
|
31 |
_LOCAL = False
|
32 |
_CITATION = """\
|
|
|
67 |
|
68 |
_HOMEPAGE = "http://www.nactem.ac.uk/chebi"
|
69 |
|
70 |
+
_LICENSE = "Creative Commons Attribution 4.0 International"
|
71 |
|
72 |
_URLS = {
|
73 |
_DATASETNAME: "http://www.nactem.ac.uk/chebi/ChEBI.zip",
|
|
|
155 |
"chebi_nactem_abstr_ann2": os.path.join("ChEBI", "abstracts", "Annotator2"),
|
156 |
"chebi_nactem_fullpaper": os.path.join("ChEBI", "fullpapers"),
|
157 |
}
|
158 |
+
subset_dir = os.path.join(data_dir, subset_paths[self.config.subset_id])
|
|
|
|
|
159 |
return [
|
160 |
datasets.SplitGenerator(
|
161 |
name=datasets.Split.TRAIN,
|
162 |
+
gen_kwargs={"file_paths": dl_manager.iter_files(subset_dir)},
|
|
|
|
|
|
|
163 |
)
|
164 |
]
|
165 |
|
166 |
+
def _generate_examples(self, file_paths: Iterable[str]) -> Tuple[int, Dict]:
|
167 |
"""Yields examples as (key, example) tuples."""
|
168 |
|
169 |
+
uid = count(0)
|
170 |
+
|
171 |
+
for idx, file_path in enumerate(file_paths):
|
172 |
+
if os.path.basename(file_path).endswith(".txt"):
|
173 |
+
contents = parse_brat_file(
|
174 |
+
Path(file_path), annotation_file_suffixes=[".ann"]
|
175 |
+
)
|
176 |
+
|
177 |
+
if self.config.schema == "source":
|
178 |
+
yield idx, {
|
179 |
+
"document_id": contents["document_id"],
|
180 |
+
"text": contents["text"],
|
181 |
+
"entities": contents["text_bound_annotations"],
|
182 |
+
"relations": [
|
183 |
+
{
|
184 |
+
"id": relation["id"],
|
185 |
+
"type": relation["type"],
|
186 |
+
"arg1": relation["head"]["ref_id"],
|
187 |
+
"arg2": relation["tail"]["ref_id"],
|
188 |
+
}
|
189 |
+
for relation in contents["relations"]
|
190 |
+
],
|
191 |
+
}
|
192 |
+
|
193 |
+
elif self.config.schema == "bigbio_kb":
|
194 |
+
yield idx, {
|
195 |
+
"id": next(uid),
|
196 |
+
"document_id": contents["document_id"],
|
197 |
+
"passages": [
|
198 |
+
{
|
199 |
+
"id": next(uid),
|
200 |
+
"type": "",
|
201 |
+
"text": [contents["text"]],
|
202 |
+
"offsets": [(0, len(contents["text"]))],
|
203 |
+
}
|
204 |
+
],
|
205 |
+
"entities": [
|
206 |
+
{
|
207 |
+
"id": f"{idx}_{entity['id']}",
|
208 |
+
"type": entity["type"],
|
209 |
+
"offsets": entity["offsets"],
|
210 |
+
"text": entity["text"],
|
211 |
+
"normalized": [],
|
212 |
+
}
|
213 |
+
for entity in contents["text_bound_annotations"]
|
214 |
+
],
|
215 |
+
"events": [],
|
216 |
+
"coreferences": [],
|
217 |
+
"relations": [
|
218 |
+
{
|
219 |
+
"id": f"{idx}_{relation['id']}",
|
220 |
+
"type": relation["type"],
|
221 |
+
"arg1_id": f"{idx}_{relation['head']['ref_id']}",
|
222 |
+
"arg2_id": f"{idx}_{relation['tail']['ref_id']}",
|
223 |
+
"normalized": [],
|
224 |
+
}
|
225 |
+
for relation in contents["relations"]
|
226 |
+
],
|
227 |
+
}
|
228 |
+
else:
|
229 |
+
raise NotImplementedError(self.config.schema)
|
|
|
|
|
|
|
|
|
|
|
|