ArneBinder
commited on
Commit
•
56fbd0c
1
Parent(s):
161ba49
Create cdcp.py
Browse files
cdcp.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
28 |
+
class Attribute(Annotation):
|
29 |
+
value: str
|
30 |
+
annotation: 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")
|
41 |
+
|
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", "") != "":
|
57 |
+
url = Attribute(annotation=proposition, value=proposition_dict["url"])
|
58 |
+
document.urls.append(url)
|
59 |
+
|
60 |
+
for relation_dict in dl2ld(example["relations"]):
|
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 |
+
|
68 |
+
return document
|
69 |
+
|
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 = {}
|
78 |
+
proposition2idx = {}
|
79 |
+
for idx, proposition in enumerate(document.propositions):
|
80 |
+
proposition2dict[proposition] = {
|
81 |
+
"start": proposition.start,
|
82 |
+
"end": proposition.end,
|
83 |
+
"label": proposition_str2int(proposition.label),
|
84 |
+
"url": "",
|
85 |
+
}
|
86 |
+
proposition2idx[proposition] = idx
|
87 |
+
for url in document.urls:
|
88 |
+
proposition2dict[url.annotation]["url"] = url.value
|
89 |
+
|
90 |
+
result["propositions"] = ld2dl(
|
91 |
+
proposition2dict.values(), keys=["start", "end", "label", "url"]
|
92 |
+
)
|
93 |
+
|
94 |
+
relations = [
|
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 |
+
]
|
102 |
+
result["relations"] = ld2dl(relations, keys=["head", "tail", "label"])
|
103 |
+
|
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")]
|
124 |
+
|
125 |
+
DEFAULT_CONFIG_NAME = "default" # type: ignore
|
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 |
+
)
|