Skip to content

Commit

Permalink
feat: add xaif export feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkolenz committed Jul 13, 2023
1 parent dc45599 commit 7cf4046
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
4 changes: 3 additions & 1 deletion arguebuf/dump/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand All @@ -17,6 +18,7 @@
"graphviz",
"d2",
"aif",
"xaif",
"protobuf",
"dict",
"json",
Expand Down
66 changes: 66 additions & 0 deletions arguebuf/dump/_dump_xaif.py
Original file line number Diff line number Diff line change
@@ -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": "<br>".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),
}

0 comments on commit 7cf4046

Please sign in to comment.