ArneBinder commited on
Commit
3ddae0f
·
1 Parent(s): db9c9bb

add DOCUMENT_CONVERTERS

Browse files
Files changed (1) hide show
  1. tacred.py +24 -0
tacred.py CHANGED
@@ -3,8 +3,10 @@ from typing import Any, Callable, Dict, List, Optional, Tuple
3
 
4
  import datasets
5
  import pytorch_ie.data.builder
 
6
  from pytorch_ie.annotations import BinaryRelation, LabeledSpan, _post_init_single_label
7
  from pytorch_ie.core import Annotation, AnnotationList, Document, annotation_field
 
8
 
9
 
10
  @dataclass(eq=True, frozen=True)
@@ -36,6 +38,12 @@ class TacredDocument(Document):
36
  dependency_relations: AnnotationList[TokenRelation] = annotation_field(target="tokens")
37
 
38
 
 
 
 
 
 
 
39
  def example_to_document(
40
  example: Dict[str, Any],
41
  relation_int2str: Callable[[int], str],
@@ -135,6 +143,18 @@ def document_to_example(
135
  }
136
 
137
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  class TacredConfig(datasets.BuilderConfig):
139
  """BuilderConfig for Tacred."""
140
 
@@ -149,6 +169,10 @@ class TacredConfig(datasets.BuilderConfig):
149
  class Tacred(pytorch_ie.data.builder.GeneratorBasedBuilder):
150
  DOCUMENT_TYPE = TacredDocument
151
 
 
 
 
 
152
  BASE_DATASET_PATH = "DFKI-SLT/tacred"
153
 
154
  BUILDER_CONFIGS = [
 
3
 
4
  import datasets
5
  import pytorch_ie.data.builder
6
+ from pytorch_ie import token_based_document_to_text_based
7
  from pytorch_ie.annotations import BinaryRelation, LabeledSpan, _post_init_single_label
8
  from pytorch_ie.core import Annotation, AnnotationList, Document, annotation_field
9
+ from pytorch_ie.documents import TextDocumentWithLabeledEntitiesAndRelations, TokenBasedDocument
10
 
11
 
12
  @dataclass(eq=True, frozen=True)
 
38
  dependency_relations: AnnotationList[TokenRelation] = annotation_field(target="tokens")
39
 
40
 
41
+ @dataclass
42
+ class SimpleTacredDocument(TokenBasedDocument):
43
+ entities: AnnotationList[LabeledSpan] = annotation_field(target="tokens")
44
+ relations: AnnotationList[BinaryRelation] = annotation_field(target="entities")
45
+
46
+
47
  def example_to_document(
48
  example: Dict[str, Any],
49
  relation_int2str: Callable[[int], str],
 
143
  }
144
 
145
 
146
+ def convert_to_text_document_with_labeled_entities_and_relations(
147
+ document: TacredDocument,
148
+ ) -> TextDocumentWithLabeledEntitiesAndRelations:
149
+ doc_simplified = document.as_type(SimpleTacredDocument)
150
+ result = token_based_document_to_text_based(
151
+ doc_simplified,
152
+ result_document_type=TextDocumentWithLabeledEntitiesAndRelations,
153
+ join_tokens_with=" ",
154
+ )
155
+ return result
156
+
157
+
158
  class TacredConfig(datasets.BuilderConfig):
159
  """BuilderConfig for Tacred."""
160
 
 
169
  class Tacred(pytorch_ie.data.builder.GeneratorBasedBuilder):
170
  DOCUMENT_TYPE = TacredDocument
171
 
172
+ DOCUMENT_CONVERTERS = {
173
+ TextDocumentWithLabeledEntitiesAndRelations: convert_to_text_document_with_labeled_entities_and_relations,
174
+ }
175
+
176
  BASE_DATASET_PATH = "DFKI-SLT/tacred"
177
 
178
  BUILDER_CONFIGS = [