From 7cf4046bbee567dba6a296e6418ac8f10a0d2285 Mon Sep 17 00:00:00 2001 From: Mirko Lenz Date: Thu, 13 Jul 2023 15:21:37 +0100 Subject: [PATCH] feat: add xaif export feature --- arguebuf/dump/__init__.py | 4 ++- arguebuf/dump/_dump_xaif.py | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 arguebuf/dump/_dump_xaif.py diff --git a/arguebuf/dump/__init__.py b/arguebuf/dump/__init__.py index 6147cec..b7ef8e3 100644 --- a/arguebuf/dump/__init__.py +++ b/arguebuf/dump/__init__.py @@ -1,5 +1,6 @@ from ._config import Config, Format from ._dump_aif import dump_aif as aif +from ._dump_d2 import dump_d2 as d2 from ._dump_dict import dump_dict as dict from ._dump_graphviz import dump_graphviz as graphviz from ._dump_io import dump_io as io @@ -8,7 +9,7 @@ from ._dump_path import dump_file as file from ._dump_path import dump_file as folder from ._dump_protobuf import dump_protobuf as protobuf -from ._dump_d2 import dump_d2 as d2 +from ._dump_xaif import dump_xaif as xaif __all__ = ( "file", @@ -17,6 +18,7 @@ "graphviz", "d2", "aif", + "xaif", "protobuf", "dict", "json", diff --git a/arguebuf/dump/_dump_xaif.py b/arguebuf/dump/_dump_xaif.py new file mode 100644 index 0000000..1d4102d --- /dev/null +++ b/arguebuf/dump/_dump_xaif.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +from arguebuf.model import AbstractNode, AtomNode, Edge, Graph, SchemeNode +from arguebuf.model.node import NO_SCHEME_LABEL +from arguebuf.model.scheme import scheme2aif +from arguebuf.schemas import xaif as xaif_schema + +__all__ = ("dump_xaif",) + + +def dump_xaif(obj: Graph) -> xaif_schema.Graph: + """Export structure of Graph instance to AIF argument graph format.""" + return { + "AIF": { + "nodes": [_dump_node(node) for node in obj.nodes.values()], + "edges": [_dump_edge(edge) for edge in obj.edges.values()], + "locutions": [], + "schemefulfillments": [], + "participants": [], + }, + "text": "
".join(resource.text for resource in obj.resources.values()), + "OVA": { + "firstname": "", + "surname": "", + "url": "", + "nodes": [], + "edges": [], + }, + } + + +def _dump_node(obj: AbstractNode) -> xaif_schema.AifNode: + if isinstance(obj, AtomNode): + return _dump_atom(obj) + elif isinstance(obj, SchemeNode): + return _dump_scheme(obj) + + raise ValueError("Node type not supported.") + + +def _dump_scheme(obj: SchemeNode) -> xaif_schema.AifNode: + """Export SchemeNode object into AIF Node object.""" + + return { + "nodeID": obj.id, + "text": obj.scheme.value if obj.scheme else NO_SCHEME_LABEL, + "type": scheme2aif[type(obj.scheme)] if obj.scheme else "", + } + + +def _dump_atom(obj: AtomNode) -> xaif_schema.AifNode: + """Export AtomNode object into AIF Node object.""" + return { + "nodeID": obj.id, + "text": obj.plain_text, + "type": "I", + } + + +def _dump_edge(obj: Edge) -> xaif_schema.AifEdge: + """Export Edge object into AIF Edge format.""" + return { + "edgeID": str(obj.id), + "fromID": str(obj.source.id), + "toID": str(obj.target.id), + }