|
import dataclasses |
|
from collections import defaultdict |
|
from typing import Any, Callable, Dict, List, Optional, Tuple |
|
|
|
import datasets |
|
import pytorch_ie.data.builder |
|
from pytorch_ie.annotations import Span |
|
from pytorch_ie.core import Annotation, AnnotationList, annotation_field |
|
from pytorch_ie.documents import TextBasedDocument |
|
|
|
from src import utils |
|
|
|
log = utils.get_pylogger(__name__) |
|
|
|
|
|
def dl2ld(dict_of_lists): |
|
return [dict(zip(dict_of_lists, t)) for t in zip(*dict_of_lists.values())] |
|
|
|
|
|
def ld2dl(list_of_dicts, keys: Optional[List[str]] = None, as_list: bool = False): |
|
if keys is None: |
|
keys = list_of_dicts[0].keys() |
|
if as_list: |
|
return [[d[k] for d in list_of_dicts] for k in keys] |
|
else: |
|
return {k: [d[k] for d in list_of_dicts] for k in keys} |
|
|
|
|
|
@dataclasses.dataclass(frozen=True) |
|
class LabeledAnnotationCollection(Annotation): |
|
annotations: Tuple[Annotation, ...] |
|
label: str |
|
|
|
|
|
@dataclasses.dataclass(frozen=True) |
|
class MultiRelation(Annotation): |
|
heads: Tuple[Annotation, ...] |
|
tails: Tuple[Annotation, ...] |
|
label: str |
|
|
|
|
|
@dataclasses.dataclass |
|
class ArgMicroDocument(TextBasedDocument): |
|
topic_id: Optional[str] = None |
|
stance: Optional[str] = None |
|
edus: AnnotationList[Span] = annotation_field(target="text") |
|
adus: AnnotationList[LabeledAnnotationCollection] = annotation_field(target="edus") |
|
relations: AnnotationList[MultiRelation] = annotation_field(target="adus") |
|
|
|
|
|
def example_to_document( |
|
example: Dict[str, Any], |
|
adu_type_int2str: Callable[[int], str], |
|
edge_type_int2str: Callable[[int], str], |
|
stance_int2str: Callable[[int], str], |
|
) -> ArgMicroDocument: |
|
stance = stance_int2str(example["stance"]) |
|
document = ArgMicroDocument( |
|
id=example["id"], |
|
text=example["text"], |
|
topic_id=example["topic_id"] if example["topic_id"] != "UNDEFINED" else None, |
|
stance=stance if stance != "UNDEFINED" else None, |
|
) |
|
|
|
edus_dict = { |
|
edu["id"]: Span(start=edu["start"], end=edu["end"]) for edu in dl2ld(example["edus"]) |
|
} |
|
|
|
adu_id2edus = defaultdict(list) |
|
edges_multi_source = defaultdict(dict) |
|
for edge in dl2ld(example["edges"]): |
|
edge_type = edge_type_int2str(edge["type"]) |
|
if edge_type == "seg": |
|
adu_id2edus[edge["trg"]].append(edus_dict[edge["src"]]) |
|
elif edge_type == "add": |
|
if "src" not in edges_multi_source[edge["trg"]]: |
|
edges_multi_source[edge["trg"]]["src"] = [] |
|
edges_multi_source[edge["trg"]]["src"].append(edge["src"]) |
|
else: |
|
edges_multi_source[edge["id"]]["type"] = edge_type |
|
edges_multi_source[edge["id"]]["trg"] = edge["trg"] |
|
if "src" not in edges_multi_source[edge["id"]]: |
|
edges_multi_source[edge["id"]]["src"] = [] |
|
edges_multi_source[edge["id"]]["src"].append(edge["src"]) |
|
adus_dict = {} |
|
for adu in dl2ld(example["adus"]): |
|
adu_type = adu_type_int2str(adu["type"]) |
|
adu_edus = adu_id2edus[adu["id"]] |
|
adus_dict[adu["id"]] = LabeledAnnotationCollection( |
|
annotations=tuple(adu_edus), label=adu_type |
|
) |
|
|
|
rels_dict = {} |
|
for edge_id, edge in edges_multi_source.items(): |
|
edge_target = edge["trg"] |
|
if edge_target in edges_multi_source: |
|
targets = edges_multi_source[edge_target]["src"] |
|
else: |
|
targets = [edge_target] |
|
if any(target in edges_multi_source for target in targets): |
|
raise Exception("Multi-hop relations are not supported") |
|
rel = MultiRelation( |
|
heads=tuple(adus_dict[source] for source in edge["src"]), |
|
tails=tuple(adus_dict[target] for target in targets), |
|
label=edge["type"], |
|
) |
|
rels_dict[edge_id] = rel |
|
|
|
document.edus.extend(edus_dict.values()) |
|
document.adus.extend(adus_dict.values()) |
|
document.relations.extend(rels_dict.values()) |
|
document.metadata["edu_ids"] = list(edus_dict.keys()) |
|
document.metadata["adu_ids"] = list(adus_dict.keys()) |
|
document.metadata["rel_ids"] = list(rels_dict.keys()) |
|
|
|
document.metadata["rel_seg_ids"] = { |
|
edge["src"]: edge["id"] |
|
for edge in dl2ld(example["edges"]) |
|
if edge_type_int2str(edge["type"]) == "seg" |
|
} |
|
document.metadata["rel_add_ids"] = { |
|
edge["src"]: edge["id"] |
|
for edge in dl2ld(example["edges"]) |
|
if edge_type_int2str(edge["type"]) == "add" |
|
} |
|
return document |
|
|
|
|
|
def document_to_example( |
|
document: ArgMicroDocument, |
|
adu_type_str2int: Callable[[str], int], |
|
edge_type_str2int: Callable[[str], int], |
|
stance_str2int: Callable[[str], int], |
|
) -> Dict[str, Any]: |
|
result = { |
|
"id": document.id, |
|
"text": document.text, |
|
"topic_id": document.topic_id or "UNDEFINED", |
|
"stance": stance_str2int(document.stance or "UNDEFINED"), |
|
} |
|
|
|
|
|
edus = { |
|
edu: {"id": edu_id, "start": edu.start, "end": edu.end} |
|
for edu_id, edu in zip(document.metadata["edu_ids"], document.edus) |
|
} |
|
result["edus"] = ld2dl( |
|
sorted(edus.values(), key=lambda x: x["id"]), keys=["id", "start", "end"] |
|
) |
|
|
|
|
|
adus = { |
|
adu: {"id": adu_id, "type": adu_type_str2int(adu.label)} |
|
for adu_id, adu in zip(document.metadata["adu_ids"], document.adus) |
|
} |
|
result["adus"] = ld2dl(sorted(adus.values(), key=lambda x: x["id"]), keys=["id", "type"]) |
|
|
|
|
|
rels_dict: Dict[str, MultiRelation] = { |
|
rel_id: rel for rel_id, rel in zip(document.metadata["rel_ids"], document.relations) |
|
} |
|
heads2rel_id = { |
|
rel.heads: red_id for red_id, rel in zip(document.metadata["rel_ids"], document.relations) |
|
} |
|
edges = [] |
|
for rel_id, rel in rels_dict.items(): |
|
|
|
if rel.label == "und": |
|
target_id = heads2rel_id[rel.tails] |
|
else: |
|
if len(rel.tails) > 1: |
|
raise Exception("Multi-target relations are not supported") |
|
target_id = adus[rel.tails[0]]["id"] |
|
source_id = adus[rel.heads[0]]["id"] |
|
edge = { |
|
"id": rel_id, |
|
"src": source_id, |
|
"trg": target_id, |
|
"type": edge_type_str2int(rel.label), |
|
} |
|
edges.append(edge) |
|
|
|
for head in rel.heads[1:]: |
|
source_id = adus[head]["id"] |
|
edge_id = document.metadata["rel_add_ids"][source_id] |
|
edge = { |
|
"id": edge_id, |
|
"src": source_id, |
|
"trg": rel_id, |
|
"type": edge_type_str2int("add"), |
|
} |
|
edges.append(edge) |
|
|
|
for adu_id, adu in zip(document.metadata["adu_ids"], document.adus): |
|
for edu in adu.annotations: |
|
source_id = edus[edu]["id"] |
|
target_id = adus[adu]["id"] |
|
edge_id = document.metadata["rel_seg_ids"][source_id] |
|
edge = { |
|
"id": edge_id, |
|
"src": source_id, |
|
"trg": target_id, |
|
"type": edge_type_str2int("seg"), |
|
} |
|
edges.append(edge) |
|
|
|
result["edges"] = ld2dl( |
|
sorted(edges, key=lambda x: x["id"]), keys=["id", "src", "trg", "type"] |
|
) |
|
return result |
|
|
|
|
|
class ArgMicro(pytorch_ie.data.builder.GeneratorBasedBuilder): |
|
DOCUMENT_TYPE = ArgMicroDocument |
|
|
|
BASE_DATASET_PATH = "DFKI-SLT/argmicro" |
|
|
|
BUILDER_CONFIGS = [datasets.BuilderConfig(name="en"), datasets.BuilderConfig(name="de")] |
|
|
|
def _generate_document_kwargs(self, dataset): |
|
return { |
|
"adu_type_int2str": dataset.features["adus"].feature["type"].int2str, |
|
"edge_type_int2str": dataset.features["edges"].feature["type"].int2str, |
|
"stance_int2str": dataset.features["stance"].int2str, |
|
} |
|
|
|
def _generate_document(self, example, adu_type_int2str, edge_type_int2str, stance_int2str): |
|
return example_to_document( |
|
example, |
|
adu_type_int2str=adu_type_int2str, |
|
edge_type_int2str=edge_type_int2str, |
|
stance_int2str=stance_int2str, |
|
) |
|
|