First version of the re-medical-annotations dataset.
Browse files- README.md +33 -0
- dataset.py +126 -0
README.md
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Dataset Card for re-medical-annotations
|
2 |
+
|
3 |
+
## Dataset Description
|
4 |
+
|
5 |
+
### Dataset Summary
|
6 |
+
|
7 |
+
HuggingFace Dataset from the Inception Medical Annotations project.
|
8 |
+
|
9 |
+
This dataset can be used locally with any archive downloaded from Inception that contains relation annotations.
|
10 |
+
|
11 |
+
**Example**: load the dataset from the "RE Temporality POC"
|
12 |
+
|
13 |
+
```
|
14 |
+
import datasets
|
15 |
+
|
16 |
+
ds = datasets.load_dataset(
|
17 |
+
data_dir=<Inception Archive path>,
|
18 |
+
labels = ["bound"],
|
19 |
+
)
|
20 |
+
```
|
21 |
+
|
22 |
+
## Dataset Structure
|
23 |
+
|
24 |
+
### Data Fields
|
25 |
+
|
26 |
+
- `text (str)`: text of the sentence
|
27 |
+
- `subj_start (int)`: start char of the relation subject mention
|
28 |
+
- `subj_end (int)`: end char of the relation subject mention, exclusive
|
29 |
+
- `subj_type (str)`: NER label of the relation subject
|
30 |
+
- `obj_start (int)`: start char of the relation object mention
|
31 |
+
- `obj_end (int)`: end char of the relation object mention, exclusive
|
32 |
+
- `obj_type (str)`: NER label of the relation object
|
33 |
+
- `relation (str)`: the relation label of this instance
|
dataset.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""RE Dataset, Arkhn style."""
|
2 |
+
import itertools
|
3 |
+
import os
|
4 |
+
import zipfile
|
5 |
+
from dataclasses import dataclass
|
6 |
+
from glob import glob
|
7 |
+
from pathlib import Path
|
8 |
+
from typing import Optional
|
9 |
+
|
10 |
+
import datasets
|
11 |
+
from cassis import Cas, load_cas_from_xmi, load_typesystem
|
12 |
+
|
13 |
+
# You can copy an official description
|
14 |
+
_DESCRIPTION = (
|
15 |
+
"This dataset is designed to solve the great task of Relation Extraction and "
|
16 |
+
"is crafted with a lot of care."
|
17 |
+
)
|
18 |
+
|
19 |
+
SENTENCE_CAS = "de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence"
|
20 |
+
CUSTOM_RELATION_CAS = "custom.Relation"
|
21 |
+
DOCUMENT_METADATA = "de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData"
|
22 |
+
CUSTOM_SPAN = "custom.Span"
|
23 |
+
|
24 |
+
|
25 |
+
@dataclass
|
26 |
+
class ReMedicalAnnotationsConfig(datasets.BuilderConfig):
|
27 |
+
"""BuilderConfig for ReMedicalAnnotations dataset."""
|
28 |
+
|
29 |
+
labels: Optional[list[str]] = None
|
30 |
+
|
31 |
+
|
32 |
+
class ReMedicalAnnotations(datasets.GeneratorBasedBuilder):
|
33 |
+
"""This dataset is designed to solve the great task of Relation Extraction and is crafted
|
34 |
+
with a lot of care."""
|
35 |
+
|
36 |
+
BUILDER_CONFIG_CLASS = ReMedicalAnnotationsConfig
|
37 |
+
VERSION = datasets.Version("1.1.0")
|
38 |
+
|
39 |
+
def _info(self):
|
40 |
+
|
41 |
+
return datasets.DatasetInfo(
|
42 |
+
# This is the description that will appear on the datasets page.
|
43 |
+
description=_DESCRIPTION,
|
44 |
+
# This defines the different columns of the dataset and their types
|
45 |
+
features=datasets.Features(
|
46 |
+
{
|
47 |
+
"text": datasets.Value("string"),
|
48 |
+
"subj_start": datasets.Value("int32"),
|
49 |
+
"subj_end": datasets.Value("int32"),
|
50 |
+
"subj_type": datasets.Value("string"),
|
51 |
+
"obj_start": datasets.Value("int32"),
|
52 |
+
"obj_end": datasets.Value("int32"),
|
53 |
+
"obj_type": datasets.Value("string"),
|
54 |
+
"relation": datasets.ClassLabel(names=["no_relation"] + self.config.labels),
|
55 |
+
}
|
56 |
+
),
|
57 |
+
)
|
58 |
+
|
59 |
+
def _split_generators(self, dl_manager):
|
60 |
+
data_dir = dl_manager.extract(self.config.data_dir)
|
61 |
+
return [
|
62 |
+
datasets.SplitGenerator(
|
63 |
+
name=datasets.Split.TRAIN,
|
64 |
+
# These kwargs will be passed to _generate_examples
|
65 |
+
gen_kwargs={
|
66 |
+
"filepath": data_dir,
|
67 |
+
"split": "all",
|
68 |
+
},
|
69 |
+
)
|
70 |
+
]
|
71 |
+
|
72 |
+
@staticmethod
|
73 |
+
def get_cas_objects(filepath: str):
|
74 |
+
cas_objects: list[Cas] = []
|
75 |
+
|
76 |
+
curation_path = os.path.join(filepath, "curation")
|
77 |
+
for zip_subset_path in sorted(glob(curation_path + "/**/*.zip")):
|
78 |
+
with zipfile.ZipFile(zip_subset_path) as zip_subset:
|
79 |
+
subset_folder = str(Path(zip_subset_path).parent)
|
80 |
+
zip_subset.extractall(subset_folder)
|
81 |
+
with open(glob(subset_folder + "/*.xml")[0], "rb") as f:
|
82 |
+
typesystem = load_typesystem(f)
|
83 |
+
with open(glob(subset_folder + "/*.xmi")[0], "rb") as f:
|
84 |
+
cas = load_cas_from_xmi(f, typesystem=typesystem)
|
85 |
+
cas_objects.append(cas)
|
86 |
+
|
87 |
+
return cas_objects
|
88 |
+
|
89 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
90 |
+
def _generate_examples(self, filepath: str, split: str):
|
91 |
+
"""Generate RE examples from an unzipped Inception dataset."""
|
92 |
+
key = 0
|
93 |
+
for cas in self.get_cas_objects(filepath=filepath):
|
94 |
+
examples = cas.select(SENTENCE_CAS)
|
95 |
+
for example in examples:
|
96 |
+
offset = (
|
97 |
+
cas.select(DOCUMENT_METADATA)[0]
|
98 |
+
.get_covered_text()
|
99 |
+
.find(example.get_covered_text())
|
100 |
+
)
|
101 |
+
|
102 |
+
# retrieve all the relations as tuples (dependant, governor) with the span ids
|
103 |
+
# and the corresponding relation label
|
104 |
+
relations = {}
|
105 |
+
for relation in cas.select_covered(CUSTOM_RELATION_CAS, example):
|
106 |
+
relations[(relation.Dependent.xmiID, relation.Governor.xmiID)] = relation.label
|
107 |
+
|
108 |
+
# Create all possible combinations of 2 entities (we keep in undirected for now)
|
109 |
+
entities = cas.select_covered(CUSTOM_SPAN, example)
|
110 |
+
combinations = itertools.combinations(entities, 2)
|
111 |
+
for ent1, ent2 in combinations:
|
112 |
+
if (ent1.xmiID, ent2.xmiID) in relations.keys():
|
113 |
+
relation = relations[(ent1.xmiID, ent2.xmiID)]
|
114 |
+
else:
|
115 |
+
relation = "no_relation"
|
116 |
+
yield key, {
|
117 |
+
"text": example.get_covered_text(),
|
118 |
+
"subj_start": ent1.begin - offset,
|
119 |
+
"subj_end": ent1.end - offset,
|
120 |
+
"subj_type": ent1.label,
|
121 |
+
"obj_start": ent2.begin - offset,
|
122 |
+
"obj_end": ent2.end - offset,
|
123 |
+
"obj_type": ent2.label,
|
124 |
+
"relation": relation,
|
125 |
+
}
|
126 |
+
key += 1
|