ArneBinder
commited on
Commit
•
f9c96da
1
Parent(s):
3612ae0
from https://github.com/ArneBinder/pie-datasets/pull/134
Browse filesalso contains the fix https://github.com/ArneBinder/pie-datasets/pull/139
- README.md +34 -0
- biorel.py +122 -0
- requirements.txt +1 -0
README.md
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# PIE Dataset Card for "BioRel"
|
2 |
+
|
3 |
+
This is a [PyTorch-IE](https://github.com/ChristophAlt/pytorch-ie) wrapper for the
|
4 |
+
[BioRel Huggingface dataset loading script](https://huggingface.co/datasets/DFKI-SLT/BioRel).
|
5 |
+
|
6 |
+
## Data Schema
|
7 |
+
|
8 |
+
The document type for this dataset is `BioRelDocument` which defines the following data fields:
|
9 |
+
|
10 |
+
- `text` (str)
|
11 |
+
|
12 |
+
and the following annotation layers:
|
13 |
+
|
14 |
+
- `entities` (annotation type: `SpanWithIdAndName`, target: `text`)
|
15 |
+
- `relations` (annotation type: `BinaryRelation`, target: `entities`)
|
16 |
+
|
17 |
+
`SpanWithIdAndName` is a custom annotation type that extends typical `Span` with the following data fields:
|
18 |
+
|
19 |
+
- `id` (str, for entity identification)
|
20 |
+
- `name` (str, entity string between span start and end)
|
21 |
+
|
22 |
+
See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/annotations.py) and
|
23 |
+
[here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/annotations.py) for the annotation
|
24 |
+
type definitions.
|
25 |
+
|
26 |
+
## Document Converters
|
27 |
+
|
28 |
+
The dataset provides predefined document converters for the following target document types:
|
29 |
+
|
30 |
+
- `pie_modules.documents.TextDocumentWithLabeledSpansAndBinaryRelations`
|
31 |
+
|
32 |
+
See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/documents.py) and
|
33 |
+
[here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/documents.py) for the document type
|
34 |
+
definitions.
|
biorel.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import dataclasses
|
2 |
+
import logging
|
3 |
+
from typing import Any
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
from pytorch_ie import AnnotationLayer, annotation_field
|
7 |
+
from pytorch_ie.annotations import BinaryRelation, LabeledSpan, Span
|
8 |
+
from pytorch_ie.documents import (
|
9 |
+
TextBasedDocument,
|
10 |
+
TextDocumentWithLabeledSpansAndBinaryRelations,
|
11 |
+
)
|
12 |
+
|
13 |
+
from pie_datasets import ArrowBasedBuilder, GeneratorBasedBuilder
|
14 |
+
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
warning_counter = 0
|
17 |
+
|
18 |
+
|
19 |
+
@dataclasses.dataclass(frozen=True)
|
20 |
+
class SpanWithIdAndName(Span):
|
21 |
+
id: str
|
22 |
+
name: str
|
23 |
+
|
24 |
+
def resolve(self) -> Any:
|
25 |
+
return self.id, self.name, super().resolve()
|
26 |
+
|
27 |
+
|
28 |
+
@dataclasses.dataclass
|
29 |
+
class BioRelDocument(TextBasedDocument):
|
30 |
+
entities: AnnotationLayer[SpanWithIdAndName] = annotation_field(target="text")
|
31 |
+
relations: AnnotationLayer[BinaryRelation] = annotation_field(target="entities")
|
32 |
+
|
33 |
+
|
34 |
+
def example_to_document(example) -> BioRelDocument:
|
35 |
+
document = BioRelDocument(text=example["text"])
|
36 |
+
head = SpanWithIdAndName(
|
37 |
+
id=example["h"]["id"],
|
38 |
+
name=example["h"]["name"],
|
39 |
+
start=example["h"]["pos"][0],
|
40 |
+
end=example["h"]["pos"][1],
|
41 |
+
)
|
42 |
+
tail = SpanWithIdAndName(
|
43 |
+
id=example["t"]["id"],
|
44 |
+
name=example["t"]["name"],
|
45 |
+
start=example["t"]["pos"][0],
|
46 |
+
end=example["t"]["pos"][1],
|
47 |
+
)
|
48 |
+
document.entities.extend([head, tail])
|
49 |
+
|
50 |
+
relation = BinaryRelation(head=head, tail=tail, label=example["relation"])
|
51 |
+
document.relations.append(relation)
|
52 |
+
return document
|
53 |
+
|
54 |
+
|
55 |
+
def document_to_example(document):
|
56 |
+
head = document.entities[0]
|
57 |
+
tail = document.entities[1]
|
58 |
+
return {
|
59 |
+
"text": document.text,
|
60 |
+
"relation": document.relations[0].label,
|
61 |
+
"h": {"id": head.id, "name": head.name, "pos": [head.start, head.end]},
|
62 |
+
"t": {"id": tail.id, "name": tail.name, "pos": [tail.start, tail.end]},
|
63 |
+
}
|
64 |
+
|
65 |
+
|
66 |
+
def convert_to_text_document_with_labeled_spans_and_binary_relations(
|
67 |
+
document: BioRelDocument,
|
68 |
+
) -> TextDocumentWithLabeledSpansAndBinaryRelations:
|
69 |
+
text_document = TextDocumentWithLabeledSpansAndBinaryRelations(text=document.text)
|
70 |
+
old2new_spans = {}
|
71 |
+
ids = []
|
72 |
+
names = []
|
73 |
+
|
74 |
+
for entity in document.entities: # in our case two entities (head and tail)
|
75 |
+
# create LabeledSpan and append
|
76 |
+
labeled_span = LabeledSpan(start=entity.start, end=entity.end, label="ENTITY")
|
77 |
+
text_document.labeled_spans.append(labeled_span)
|
78 |
+
|
79 |
+
# Map the original entity to the new labeled span
|
80 |
+
old2new_spans[entity] = labeled_span
|
81 |
+
|
82 |
+
ids.append(entity.id)
|
83 |
+
names.append(entity.name)
|
84 |
+
|
85 |
+
if len(document.relations) != 1: # one relation between two entities
|
86 |
+
raise ValueError(f"Expected exactly one relation, got {len(document.relations)}")
|
87 |
+
old_rel = document.relations[0]
|
88 |
+
|
89 |
+
# create BinaryRelation and append
|
90 |
+
rel = BinaryRelation(
|
91 |
+
head=old2new_spans[old_rel.head],
|
92 |
+
tail=old2new_spans[old_rel.tail],
|
93 |
+
label=old_rel.label,
|
94 |
+
)
|
95 |
+
text_document.binary_relations.append(rel)
|
96 |
+
text_document.metadata["entity_ids"] = ids
|
97 |
+
text_document.metadata["entity_names"] = names
|
98 |
+
|
99 |
+
return text_document
|
100 |
+
|
101 |
+
|
102 |
+
class BioRel(ArrowBasedBuilder):
|
103 |
+
DOCUMENT_TYPE = BioRelDocument
|
104 |
+
BASE_DATASET_PATH = "DFKI-SLT/BioRel"
|
105 |
+
BASE_DATASET_REVISION = "e4869c484c582cfbc7ead10d4d421bd4b275fa4e"
|
106 |
+
|
107 |
+
BUILDER_CONFIGS = [
|
108 |
+
datasets.BuilderConfig(
|
109 |
+
version=datasets.Version("1.0.0"),
|
110 |
+
description="BioRel dataset",
|
111 |
+
)
|
112 |
+
]
|
113 |
+
|
114 |
+
DOCUMENT_CONVERTERS = {
|
115 |
+
TextDocumentWithLabeledSpansAndBinaryRelations: convert_to_text_document_with_labeled_spans_and_binary_relations
|
116 |
+
}
|
117 |
+
|
118 |
+
def _generate_document(self, example, **kwargs):
|
119 |
+
return example_to_document(example)
|
120 |
+
|
121 |
+
def _generate_example(self, document: BioRelDocument, **kwargs):
|
122 |
+
return document_to_example(document)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
pie-datasets>=0.6.0,<0.11.0
|