ArneBinder commited on
Commit
67a4e57
·
1 Parent(s): 8a1283f

use BRAT document types from pie-datasets

Browse files
Files changed (2) hide show
  1. brat.py +7 -28
  2. requirements.txt +1 -1
brat.py CHANGED
@@ -1,14 +1,17 @@
1
- import dataclasses
2
  import logging
3
  from collections import defaultdict
4
  from typing import Any, Dict, List, Optional, Tuple, Union
5
 
6
  import datasets
7
  from pytorch_ie.annotations import BinaryRelation, LabeledMultiSpan, LabeledSpan
8
- from pytorch_ie.core import Annotation, AnnotationList, annotation_field
9
- from pytorch_ie.documents import TextBasedDocument
10
 
11
  from pie_datasets import GeneratorBasedBuilder
 
 
 
 
 
12
 
13
  logger = logging.getLogger(__name__)
14
 
@@ -24,33 +27,9 @@ def ld2dl(
24
  return {k: [dic[k] for dic in list_fo_dicts] for k in keys}
25
 
26
 
27
- @dataclasses.dataclass(eq=True, frozen=True)
28
- class Attribute(Annotation):
29
- annotation: Annotation
30
- label: str
31
- value: Optional[str] = None
32
- score: Optional[float] = dataclasses.field(default=None, compare=False)
33
-
34
-
35
- @dataclasses.dataclass
36
- class BratDocument(TextBasedDocument):
37
- spans: AnnotationList[LabeledMultiSpan] = annotation_field(target="text")
38
- relations: AnnotationList[BinaryRelation] = annotation_field(target="spans")
39
- span_attributes: AnnotationList[Attribute] = annotation_field(target="spans")
40
- relation_attributes: AnnotationList[Attribute] = annotation_field(target="relations")
41
-
42
-
43
- @dataclasses.dataclass
44
- class BratDocumentWithMergedSpans(TextBasedDocument):
45
- spans: AnnotationList[LabeledSpan] = annotation_field(target="text")
46
- relations: AnnotationList[BinaryRelation] = annotation_field(target="spans")
47
- span_attributes: AnnotationList[Attribute] = annotation_field(target="spans")
48
- relation_attributes: AnnotationList[Attribute] = annotation_field(target="relations")
49
-
50
-
51
  def example_to_document(
52
  example: Dict[str, Any], merge_fragmented_spans: bool = False
53
- ) -> BratDocument:
54
  if merge_fragmented_spans:
55
  doc = BratDocumentWithMergedSpans(text=example["context"], id=example["file_name"])
56
  else:
 
 
1
  import logging
2
  from collections import defaultdict
3
  from typing import Any, Dict, List, Optional, Tuple, Union
4
 
5
  import datasets
6
  from pytorch_ie.annotations import BinaryRelation, LabeledMultiSpan, LabeledSpan
7
+ from pytorch_ie.core import Annotation
 
8
 
9
  from pie_datasets import GeneratorBasedBuilder
10
+ from pie_datasets.document.types import (
11
+ Attribute,
12
+ BratDocument,
13
+ BratDocumentWithMergedSpans,
14
+ )
15
 
16
  logger = logging.getLogger(__name__)
17
 
 
27
  return {k: [dic[k] for dic in list_fo_dicts] for k in keys}
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def example_to_document(
31
  example: Dict[str, Any], merge_fragmented_spans: bool = False
32
+ ) -> Union[BratDocument, BratDocumentWithMergedSpans]:
33
  if merge_fragmented_spans:
34
  doc = BratDocumentWithMergedSpans(text=example["context"], id=example["file_name"])
35
  else:
requirements.txt CHANGED
@@ -1 +1 @@
1
- pie-datasets>=0.3.0
 
1
+ pie-datasets>=0.3.1