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