digraph_writer module

class py_amr2fred.digraph_writer.DigraphWriter

Bases: object

A utility class for converting nodes and RDF graphs into Graphviz DOT format and generating graphical representations.

This class provides methods to translate hierarchical structures of Node objects into the DOT language, allowing for visualization as PNG or SVG images. Additionally, it supports RDF graphs by generating DOT representations and linking nodes accordingly.

static check_visibility(root: Node) Node

Update the visibility status of nodes in the graph.

This method iterates through the node hierarchy and removes nodes marked as invisible.

Parameters:

root (Node) – The root node of the structure.

Returns:

The updated root node with visibility-filtered sub-nodes.

Return type:

Node

static graph_to_digraph(graph: Graph, not_visible_graph: Graph | None = None) str

Convert an RDF graph into DOT graph format.

This method processes the RDF triples and translates them into DOT representation, applying styles based on specific prefixes.

Parameters:
  • graph (Graph) – The RDF graph to be converted.

  • not_visible_graph (Graph, optional) – An optional graph containing hidden triples to be excluded.

Returns:

A string in DOT format representing the RDF graph.

Return type:

str

static node_to_digraph(root: Node)

Convert a root Node into DOT graph format.

This method translates a given hierarchical node structure into the Graphviz DOT language.

Parameters:

root (Node) – The root node to be converted.

Returns:

A string representing the graph in DOT format.

Return type:

str

static to_digraph(root: Node) str

Recursively generate a DOT representation of a Node and its connected sub-nodes.

Nodes are styled based on their properties, such as malformed status and specific prefixes.

Parameters:

root (Node) – The root node of the graph.

Returns:

A string in DOT format representing the hierarchical structure.

Return type:

str

static to_png(root: Node | Graph, not_visible_graph: Graph | None = None) IO | str

Generate a PNG image of the graph representation.

If Graphviz is installed, this method returns an image file of the translated root node or RDF graph. If Graphviz is not installed, it returns the DOT representation as a string.

Parameters:
  • root (Node | Graph) – The root node or RDF graph to be visualized.

  • not_visible_graph (Graph, optional) – An optional graph containing hidden triples.

Returns:

A PNG image file if Graphviz is available, otherwise a DOT-format string.

Return type:

IO | str

static to_svg_string(root: Node | Graph, not_visible_graph: Graph | None = None) str

Generate an SVG representation of the graph.

If Graphviz is installed, it returns an SVG image as a string. Otherwise, it returns the DOT format representation.

Parameters:
  • root (Node | Graph) – The root node or RDF graph to be visualized.

  • not_visible_graph (Graph, optional) – An optional graph containing hidden triples.

Returns:

A string containing the SVG representation of the graph.

Return type:

str