ArneBinder commited on
Commit
8f9f5d5
1 Parent(s): 56fbd0c

adjust for pytorch-ie 0.28.8

Browse files
Files changed (3) hide show
  1. README.md +29 -0
  2. cdcp.py +43 -37
  3. requirements.txt +1 -0
README.md ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PIE Dataset Card for "CDCP"
2
+
3
+ This is a [PyTorch-IE](https://github.com/ChristophAlt/pytorch-ie) wrapper for the
4
+ [CDCP Huggingface dataset loading script](https://huggingface.co/datasets/DFKI-SLT/cdcp).
5
+
6
+ ## Data Schema
7
+
8
+ The document type for this dataset is `CDCPDocument` which defines the following data fields:
9
+
10
+ - `text` (str)
11
+ - `id` (str, optional)
12
+ - `metadata` (dictionary, optional)
13
+
14
+ and the following annotation layers:
15
+
16
+ - `propositions` (annotation type: `LabeledSpan`, target: `text`)
17
+ - `relations` (annotation type: `BinaryRelation`, target: `propositions`)
18
+ - `urls` (annotation type: `Attribute`, target: `propositions`)
19
+
20
+ See [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/annotations.py) for the annotation type definitions.
21
+
22
+ ## Document Converters
23
+
24
+ The dataset provides document converters for the following target document types:
25
+
26
+ - `pytorch_ie.documents.TextDocumentWithLabeledSpansAndBinaryRelations`
27
+
28
+ See [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/documents.py) for the document type
29
+ definitions.
cdcp.py CHANGED
@@ -1,27 +1,27 @@
1
  import dataclasses
 
2
  from typing import Any, Callable, Dict, List, Optional
3
 
4
  import datasets
5
- import pytorch_ie.data.builder
6
  from pytorch_ie.annotations import BinaryRelation, LabeledSpan
7
- from pytorch_ie.core import Annotation, AnnotationList, Document, annotation_field
 
 
 
 
8
 
9
- from src import utils
 
10
 
11
- log = utils.get_pylogger(__name__)
12
 
13
 
14
  def dl2ld(dict_of_lists):
15
  return [dict(zip(dict_of_lists, t)) for t in zip(*dict_of_lists.values())]
16
 
17
 
18
- def ld2dl(list_of_dicts, keys: Optional[List[str]] = None, as_list: bool = False):
19
- if keys is None:
20
- keys = list_of_dicts[0].keys()
21
- if as_list:
22
- return [[d[k] for d in list_of_dicts] for k in keys]
23
- else:
24
- return {k: [d[k] for d in list_of_dicts] for k in keys}
25
 
26
 
27
  @dataclasses.dataclass(frozen=True)
@@ -31,10 +31,7 @@ class Attribute(Annotation):
31
 
32
 
33
  @dataclasses.dataclass
34
- class CDCPDocument(Document):
35
- text: str
36
- id: Optional[str] = None
37
- metadata: Dict[str, Any] = dataclasses.field(default_factory=dict)
38
  propositions: AnnotationList[LabeledSpan] = annotation_field(target="text")
39
  relations: AnnotationList[BinaryRelation] = annotation_field(target="propositions")
40
  urls: AnnotationList[Attribute] = annotation_field(target="propositions")
@@ -42,15 +39,15 @@ class CDCPDocument(Document):
42
 
43
  def example_to_document(
44
  example: Dict[str, Any],
45
- relation_int2str: Callable[[int], str],
46
- proposition_int2str: Callable[[int], str],
47
  ):
48
  document = CDCPDocument(id=example["id"], text=example["text"])
49
  for proposition_dict in dl2ld(example["propositions"]):
50
  proposition = LabeledSpan(
51
  start=proposition_dict["start"],
52
  end=proposition_dict["end"],
53
- label=proposition_int2str(proposition_dict["label"]),
54
  )
55
  document.propositions.append(proposition)
56
  if proposition_dict.get("url", "") != "":
@@ -61,7 +58,7 @@ def example_to_document(
61
  relation = BinaryRelation(
62
  head=document.propositions[relation_dict["head"]],
63
  tail=document.propositions[relation_dict["tail"]],
64
- label=relation_int2str(relation_dict["label"]),
65
  )
66
  document.relations.append(relation)
67
 
@@ -70,8 +67,8 @@ def example_to_document(
70
 
71
  def document_to_example(
72
  document: CDCPDocument,
73
- relation_str2int: Callable[[str], int],
74
- proposition_str2int: Callable[[str], int],
75
  ) -> Dict[str, Any]:
76
  result = {"id": document.id, "text": document.text}
77
  proposition2dict = {}
@@ -80,7 +77,7 @@ def document_to_example(
80
  proposition2dict[proposition] = {
81
  "start": proposition.start,
82
  "end": proposition.end,
83
- "label": proposition_str2int(proposition.label),
84
  "url": "",
85
  }
86
  proposition2idx[proposition] = idx
@@ -95,7 +92,7 @@ def document_to_example(
95
  {
96
  "head": proposition2idx[relation.head],
97
  "tail": proposition2idx[relation.tail],
98
- "label": relation_str2int(relation.label),
99
  }
100
  for relation in document.relations
101
  ]
@@ -104,20 +101,29 @@ def document_to_example(
104
  return result
105
 
106
 
107
- class CDCPConfig(datasets.BuilderConfig):
108
- """BuilderConfig for CDCP."""
109
-
110
- def __init__(self, **kwargs):
111
- """BuilderConfig for CDCP.
112
- Args:
113
- **kwargs: keyword arguments forwarded to super.
114
- """
115
- super().__init__(**kwargs)
 
 
 
 
 
116
 
117
 
118
- class CDCP(pytorch_ie.data.builder.GeneratorBasedBuilder):
119
  DOCUMENT_TYPE = CDCPDocument
120
 
 
 
 
 
121
  BASE_DATASET_PATH = "DFKI-SLT/cdcp"
122
 
123
  BUILDER_CONFIGS = [datasets.BuilderConfig(name="default")]
@@ -126,11 +132,11 @@ class CDCP(pytorch_ie.data.builder.GeneratorBasedBuilder):
126
 
127
  def _generate_document_kwargs(self, dataset):
128
  return {
129
- "relation_int2str": dataset.features["relations"].feature["label"].int2str,
130
- "proposition_int2str": dataset.features["propositions"].feature["label"].int2str,
131
  }
132
 
133
- def _generate_document(self, example, relation_int2str, proposition_int2str):
134
  return example_to_document(
135
- example, relation_int2str=relation_int2str, proposition_int2str=proposition_int2str
136
  )
 
1
  import dataclasses
2
+ import logging
3
  from typing import Any, Callable, Dict, List, Optional
4
 
5
  import datasets
 
6
  from pytorch_ie.annotations import BinaryRelation, LabeledSpan
7
+ from pytorch_ie.core import Annotation, AnnotationList, annotation_field
8
+ from pytorch_ie.documents import (
9
+ TextBasedDocument,
10
+ TextDocumentWithLabeledSpansAndBinaryRelations,
11
+ )
12
 
13
+ from pie_datasets import GeneratorBasedBuilder
14
+ from pie_datasets.document.processing.text_span_trimmer import trim_text_spans
15
 
16
+ log = logging.getLogger(__name__)
17
 
18
 
19
  def dl2ld(dict_of_lists):
20
  return [dict(zip(dict_of_lists, t)) for t in zip(*dict_of_lists.values())]
21
 
22
 
23
+ def ld2dl(list_of_dicts, keys: Optional[List[str]] = None):
24
+ return {k: [d[k] for d in list_of_dicts] for k in keys}
 
 
 
 
 
25
 
26
 
27
  @dataclasses.dataclass(frozen=True)
 
31
 
32
 
33
  @dataclasses.dataclass
34
+ class CDCPDocument(TextBasedDocument):
 
 
 
35
  propositions: AnnotationList[LabeledSpan] = annotation_field(target="text")
36
  relations: AnnotationList[BinaryRelation] = annotation_field(target="propositions")
37
  urls: AnnotationList[Attribute] = annotation_field(target="propositions")
 
39
 
40
  def example_to_document(
41
  example: Dict[str, Any],
42
+ relation_label: datasets.ClassLabel,
43
+ proposition_label: datasets.ClassLabel,
44
  ):
45
  document = CDCPDocument(id=example["id"], text=example["text"])
46
  for proposition_dict in dl2ld(example["propositions"]):
47
  proposition = LabeledSpan(
48
  start=proposition_dict["start"],
49
  end=proposition_dict["end"],
50
+ label=proposition_label.int2str(proposition_dict["label"]),
51
  )
52
  document.propositions.append(proposition)
53
  if proposition_dict.get("url", "") != "":
 
58
  relation = BinaryRelation(
59
  head=document.propositions[relation_dict["head"]],
60
  tail=document.propositions[relation_dict["tail"]],
61
+ label=relation_label.int2str(relation_dict["label"]),
62
  )
63
  document.relations.append(relation)
64
 
 
67
 
68
  def document_to_example(
69
  document: CDCPDocument,
70
+ relation_label: datasets.ClassLabel,
71
+ proposition_label: datasets.ClassLabel,
72
  ) -> Dict[str, Any]:
73
  result = {"id": document.id, "text": document.text}
74
  proposition2dict = {}
 
77
  proposition2dict[proposition] = {
78
  "start": proposition.start,
79
  "end": proposition.end,
80
+ "label": proposition_label.str2int(proposition.label),
81
  "url": "",
82
  }
83
  proposition2idx[proposition] = idx
 
92
  {
93
  "head": proposition2idx[relation.head],
94
  "tail": proposition2idx[relation.tail],
95
+ "label": relation_label.str2int(relation.label),
96
  }
97
  for relation in document.relations
98
  ]
 
101
  return result
102
 
103
 
104
+ def convert_to_text_document_with_labeled_spans_and_binary_relations(
105
+ document: CDCPDocument,
106
+ verbose: bool = True,
107
+ ) -> TextDocumentWithLabeledSpansAndBinaryRelations:
108
+ doc_simplified = document.as_type(
109
+ TextDocumentWithLabeledSpansAndBinaryRelations,
110
+ field_mapping={"propositions": "labeled_spans", "relations": "binary_relations"},
111
+ )
112
+ result = trim_text_spans(
113
+ doc_simplified,
114
+ layer="labeled_spans",
115
+ verbose=verbose,
116
+ )
117
+ return result
118
 
119
 
120
+ class CDCP(GeneratorBasedBuilder):
121
  DOCUMENT_TYPE = CDCPDocument
122
 
123
+ DOCUMENT_CONVERTERS = {
124
+ TextDocumentWithLabeledSpansAndBinaryRelations: convert_to_text_document_with_labeled_spans_and_binary_relations
125
+ }
126
+
127
  BASE_DATASET_PATH = "DFKI-SLT/cdcp"
128
 
129
  BUILDER_CONFIGS = [datasets.BuilderConfig(name="default")]
 
132
 
133
  def _generate_document_kwargs(self, dataset):
134
  return {
135
+ "relation_label": dataset.features["relations"].feature["label"],
136
+ "proposition_label": dataset.features["propositions"].feature["label"],
137
  }
138
 
139
+ def _generate_document(self, example, relation_label, proposition_label):
140
  return example_to_document(
141
+ example, relation_label=relation_label, proposition_label=proposition_label
142
  )
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pie-datasets>=0.3.0