Commit
·
cea3572
1
Parent(s):
3ddae0f
adjust for pytorch-ie 0.28.0
Browse files- requirements.txt +1 -0
- tacred.py +38 -46
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pie-datasets>=0.3.0
|
tacred.py
CHANGED
|
@@ -1,12 +1,16 @@
|
|
| 1 |
from dataclasses import dataclass, field
|
| 2 |
-
from typing import Any,
|
| 3 |
|
| 4 |
import datasets
|
| 5 |
-
|
| 6 |
-
from pytorch_ie import
|
| 7 |
-
from pytorch_ie.
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
@dataclass(eq=True, frozen=True)
|
|
@@ -14,10 +18,7 @@ class TokenRelation(Annotation):
|
|
| 14 |
head_idx: int
|
| 15 |
tail_idx: int
|
| 16 |
label: str
|
| 17 |
-
score: float = 1.0
|
| 18 |
-
|
| 19 |
-
def __post_init__(self) -> None:
|
| 20 |
-
_post_init_single_label(self)
|
| 21 |
|
| 22 |
|
| 23 |
@dataclass(eq=True, frozen=True)
|
|
@@ -27,10 +28,7 @@ class TokenAttribute(Annotation):
|
|
| 27 |
|
| 28 |
|
| 29 |
@dataclass
|
| 30 |
-
class TacredDocument(
|
| 31 |
-
tokens: Tuple[str, ...]
|
| 32 |
-
id: Optional[str] = None
|
| 33 |
-
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 34 |
stanford_ner: AnnotationList[TokenAttribute] = annotation_field(target="tokens")
|
| 35 |
stanford_pos: AnnotationList[TokenAttribute] = annotation_field(target="tokens")
|
| 36 |
entities: AnnotationList[LabeledSpan] = annotation_field(target="tokens")
|
|
@@ -40,14 +38,14 @@ class TacredDocument(Document):
|
|
| 40 |
|
| 41 |
@dataclass
|
| 42 |
class SimpleTacredDocument(TokenBasedDocument):
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
|
| 47 |
def example_to_document(
|
| 48 |
example: Dict[str, Any],
|
| 49 |
-
|
| 50 |
-
|
| 51 |
) -> TacredDocument:
|
| 52 |
document = TacredDocument(
|
| 53 |
tokens=tuple(example["token"]), id=example["id"], metadata=dict(doc_id=example["docid"])
|
|
@@ -72,17 +70,17 @@ def example_to_document(
|
|
| 72 |
head = LabeledSpan(
|
| 73 |
start=example["subj_start"],
|
| 74 |
end=example["subj_end"],
|
| 75 |
-
label=
|
| 76 |
)
|
| 77 |
tail = LabeledSpan(
|
| 78 |
start=example["obj_start"],
|
| 79 |
end=example["obj_end"],
|
| 80 |
-
label=
|
| 81 |
)
|
| 82 |
document.entities.append(head)
|
| 83 |
document.entities.append(tail)
|
| 84 |
|
| 85 |
-
relation_str =
|
| 86 |
relation = BinaryRelation(head=head, tail=tail, label=relation_str)
|
| 87 |
document.relations.append(relation)
|
| 88 |
|
|
@@ -90,30 +88,20 @@ def example_to_document(
|
|
| 90 |
|
| 91 |
|
| 92 |
def _entity_to_dict(
|
| 93 |
-
entity: LabeledSpan, key_prefix: str = "",
|
| 94 |
) -> Dict[str, Any]:
|
| 95 |
return {
|
| 96 |
f"{key_prefix}start": entity.start,
|
| 97 |
f"{key_prefix}end": entity.end,
|
| 98 |
-
f"{key_prefix}type":
|
| 99 |
-
if label_mapping is not None
|
| 100 |
-
else entity.label,
|
| 101 |
}
|
| 102 |
|
| 103 |
|
| 104 |
def document_to_example(
|
| 105 |
document: TacredDocument,
|
| 106 |
-
|
| 107 |
-
|
| 108 |
) -> Dict[str, Any]:
|
| 109 |
-
|
| 110 |
-
ner2idx = {name: idx for idx, name in enumerate(ner_names)} if ner_names is not None else None
|
| 111 |
-
rel2idx = (
|
| 112 |
-
{name: idx for idx, name in enumerate(relation_names)}
|
| 113 |
-
if relation_names is not None
|
| 114 |
-
else None
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
token = list(document.tokens)
|
| 118 |
stanford_ner_dict = {ner.idx: ner.label for ner in document.stanford_ner}
|
| 119 |
stanford_pos_dict = {pos.idx: pos.label for pos in document.stanford_pos}
|
|
@@ -132,24 +120,27 @@ def document_to_example(
|
|
| 132 |
return {
|
| 133 |
"id": document.id,
|
| 134 |
"docid": document.metadata["doc_id"],
|
| 135 |
-
"relation": rel.label if
|
| 136 |
"token": token,
|
| 137 |
"stanford_ner": stanford_ner,
|
| 138 |
"stanford_pos": stanford_pos,
|
| 139 |
"stanford_deprel": stanford_deprel,
|
| 140 |
"stanford_head": stanford_head,
|
| 141 |
-
**_entity_to_dict(obj, key_prefix="obj_",
|
| 142 |
-
**_entity_to_dict(subj, key_prefix="subj_",
|
| 143 |
}
|
| 144 |
|
| 145 |
|
| 146 |
-
def
|
| 147 |
document: TacredDocument,
|
| 148 |
-
) ->
|
| 149 |
-
doc_simplified = document.as_type(
|
|
|
|
|
|
|
|
|
|
| 150 |
result = token_based_document_to_text_based(
|
| 151 |
doc_simplified,
|
| 152 |
-
result_document_type=
|
| 153 |
join_tokens_with=" ",
|
| 154 |
)
|
| 155 |
return result
|
|
@@ -160,17 +151,18 @@ class TacredConfig(datasets.BuilderConfig):
|
|
| 160 |
|
| 161 |
def __init__(self, **kwargs):
|
| 162 |
"""BuilderConfig for Tacred.
|
|
|
|
| 163 |
Args:
|
| 164 |
**kwargs: keyword arguments forwarded to super.
|
| 165 |
"""
|
| 166 |
super().__init__(**kwargs)
|
| 167 |
|
| 168 |
|
| 169 |
-
class Tacred(
|
| 170 |
DOCUMENT_TYPE = TacredDocument
|
| 171 |
|
| 172 |
DOCUMENT_CONVERTERS = {
|
| 173 |
-
|
| 174 |
}
|
| 175 |
|
| 176 |
BASE_DATASET_PATH = "DFKI-SLT/tacred"
|
|
@@ -193,8 +185,8 @@ class Tacred(pytorch_ie.data.builder.GeneratorBasedBuilder):
|
|
| 193 |
|
| 194 |
def _generate_document_kwargs(self, dataset):
|
| 195 |
return {
|
| 196 |
-
"
|
| 197 |
-
"
|
| 198 |
}
|
| 199 |
|
| 200 |
def _generate_document(self, example, **kwargs):
|
|
|
|
| 1 |
from dataclasses import dataclass, field
|
| 2 |
+
from typing import Any, Dict, Optional
|
| 3 |
|
| 4 |
import datasets
|
| 5 |
+
from pytorch_ie.annotations import BinaryRelation, LabeledSpan
|
| 6 |
+
from pytorch_ie.core import Annotation, AnnotationList, annotation_field
|
| 7 |
+
from pytorch_ie.documents import (
|
| 8 |
+
TextDocumentWithLabeledSpansAndBinaryRelations,
|
| 9 |
+
TokenBasedDocument,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
from pie_datasets import GeneratorBasedBuilder
|
| 13 |
+
from pie_datasets.document.conversion import token_based_document_to_text_based
|
| 14 |
|
| 15 |
|
| 16 |
@dataclass(eq=True, frozen=True)
|
|
|
|
| 18 |
head_idx: int
|
| 19 |
tail_idx: int
|
| 20 |
label: str
|
| 21 |
+
score: float = field(default=1.0, compare=False)
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
@dataclass(eq=True, frozen=True)
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
@dataclass
|
| 31 |
+
class TacredDocument(TokenBasedDocument):
|
|
|
|
|
|
|
|
|
|
| 32 |
stanford_ner: AnnotationList[TokenAttribute] = annotation_field(target="tokens")
|
| 33 |
stanford_pos: AnnotationList[TokenAttribute] = annotation_field(target="tokens")
|
| 34 |
entities: AnnotationList[LabeledSpan] = annotation_field(target="tokens")
|
|
|
|
| 38 |
|
| 39 |
@dataclass
|
| 40 |
class SimpleTacredDocument(TokenBasedDocument):
|
| 41 |
+
labeled_spans: AnnotationList[LabeledSpan] = annotation_field(target="tokens")
|
| 42 |
+
binary_relations: AnnotationList[BinaryRelation] = annotation_field(target="labeled_spans")
|
| 43 |
|
| 44 |
|
| 45 |
def example_to_document(
|
| 46 |
example: Dict[str, Any],
|
| 47 |
+
relation_labels: datasets.ClassLabel,
|
| 48 |
+
ner_labels: datasets.ClassLabel,
|
| 49 |
) -> TacredDocument:
|
| 50 |
document = TacredDocument(
|
| 51 |
tokens=tuple(example["token"]), id=example["id"], metadata=dict(doc_id=example["docid"])
|
|
|
|
| 70 |
head = LabeledSpan(
|
| 71 |
start=example["subj_start"],
|
| 72 |
end=example["subj_end"],
|
| 73 |
+
label=ner_labels.int2str(example["subj_type"]),
|
| 74 |
)
|
| 75 |
tail = LabeledSpan(
|
| 76 |
start=example["obj_start"],
|
| 77 |
end=example["obj_end"],
|
| 78 |
+
label=ner_labels.int2str(example["obj_type"]),
|
| 79 |
)
|
| 80 |
document.entities.append(head)
|
| 81 |
document.entities.append(tail)
|
| 82 |
|
| 83 |
+
relation_str = relation_labels.int2str(example["relation"])
|
| 84 |
relation = BinaryRelation(head=head, tail=tail, label=relation_str)
|
| 85 |
document.relations.append(relation)
|
| 86 |
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
def _entity_to_dict(
|
| 91 |
+
entity: LabeledSpan, key_prefix: str = "", labels: Optional[datasets.ClassLabel] = None
|
| 92 |
) -> Dict[str, Any]:
|
| 93 |
return {
|
| 94 |
f"{key_prefix}start": entity.start,
|
| 95 |
f"{key_prefix}end": entity.end,
|
| 96 |
+
f"{key_prefix}type": labels.str2int(entity.label) if labels is not None else entity.label,
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
|
| 99 |
|
| 100 |
def document_to_example(
|
| 101 |
document: TacredDocument,
|
| 102 |
+
ner_labels: Optional[datasets.ClassLabel] = None,
|
| 103 |
+
relation_labels: Optional[datasets.ClassLabel] = None,
|
| 104 |
) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
token = list(document.tokens)
|
| 106 |
stanford_ner_dict = {ner.idx: ner.label for ner in document.stanford_ner}
|
| 107 |
stanford_pos_dict = {pos.idx: pos.label for pos in document.stanford_pos}
|
|
|
|
| 120 |
return {
|
| 121 |
"id": document.id,
|
| 122 |
"docid": document.metadata["doc_id"],
|
| 123 |
+
"relation": rel.label if relation_labels is None else relation_labels.str2int(rel.label),
|
| 124 |
"token": token,
|
| 125 |
"stanford_ner": stanford_ner,
|
| 126 |
"stanford_pos": stanford_pos,
|
| 127 |
"stanford_deprel": stanford_deprel,
|
| 128 |
"stanford_head": stanford_head,
|
| 129 |
+
**_entity_to_dict(obj, key_prefix="obj_", labels=ner_labels),
|
| 130 |
+
**_entity_to_dict(subj, key_prefix="subj_", labels=ner_labels),
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
+
def convert_to_text_document_with_labeled_spans_and_binary_relations(
|
| 135 |
document: TacredDocument,
|
| 136 |
+
) -> TextDocumentWithLabeledSpansAndBinaryRelations:
|
| 137 |
+
doc_simplified = document.as_type(
|
| 138 |
+
SimpleTacredDocument,
|
| 139 |
+
field_mapping={"entities": "labeled_spans", "relations": "binary_relations"},
|
| 140 |
+
)
|
| 141 |
result = token_based_document_to_text_based(
|
| 142 |
doc_simplified,
|
| 143 |
+
result_document_type=TextDocumentWithLabeledSpansAndBinaryRelations,
|
| 144 |
join_tokens_with=" ",
|
| 145 |
)
|
| 146 |
return result
|
|
|
|
| 151 |
|
| 152 |
def __init__(self, **kwargs):
|
| 153 |
"""BuilderConfig for Tacred.
|
| 154 |
+
|
| 155 |
Args:
|
| 156 |
**kwargs: keyword arguments forwarded to super.
|
| 157 |
"""
|
| 158 |
super().__init__(**kwargs)
|
| 159 |
|
| 160 |
|
| 161 |
+
class Tacred(GeneratorBasedBuilder):
|
| 162 |
DOCUMENT_TYPE = TacredDocument
|
| 163 |
|
| 164 |
DOCUMENT_CONVERTERS = {
|
| 165 |
+
TextDocumentWithLabeledSpansAndBinaryRelations: convert_to_text_document_with_labeled_spans_and_binary_relations,
|
| 166 |
}
|
| 167 |
|
| 168 |
BASE_DATASET_PATH = "DFKI-SLT/tacred"
|
|
|
|
| 185 |
|
| 186 |
def _generate_document_kwargs(self, dataset):
|
| 187 |
return {
|
| 188 |
+
"ner_labels": dataset.features["subj_type"],
|
| 189 |
+
"relation_labels": dataset.features["relation"],
|
| 190 |
}
|
| 191 |
|
| 192 |
def _generate_document(self, example, **kwargs):
|