segram.grammar.graph module

class segram.grammar.graph.Graph(data: Mapping[Any, tuple[Any, ...]])[source]

Bases: MutableMapping, SegramABC

Graph object.

This class provides concrete generic implementations of all methods except for __getitem__, __setitem__, __delitem__, __iter__, and __len__.

This is a mutable mapping with several additional methods

for reversing the key-value pairing or testing whether a graph is a directed acyclic graph (DAG).

property data: dict[Any, tuple[Any, ...]]

Dictionary mapping names and values for main slots.

get_rev() Self[source]

Get reversed graph (mapping children to parent sets).

update_rev() None[source]

Update reversed graph.

is_comparable_with(other: Mapping) bool[source]

Are self and other comparable.

toposort(*, reversed: bool = False) TopologicalSorter[source]

Get topological sorter.

Parameters:

reversed – Should reversed topological sorter be returned.

iter_hierarchy() Iterable[tuple[int, Any, Any]][source]

Iterate over parents and children according to the implicit directed acyclic graph hierarchy.

Yields:
  • depth – Depth index.

  • parent, child – Parent and child nodes.

Construct from links.

Parameters:

links – Iterable of links (parent-child pairs).

class segram.grammar.graph.PhraseGraph(data: Mapping[Any, tuple[Any, ...]])[source]

Bases: Graph

PhraseGraph.

This class provides concrete generic implementations of all methods except for __getitem__, __setitem__, __delitem__, __iter__, and __len__.

This is a mutable mapping with several additional methods

for reversing the key-value pairing or testing whether a graph is a directed acyclic graph (DAG).

is_comparable_with(other: Any) bool[source]

Are self and other comparable.

to_str(*, color: bool = False, indent: int = 4, **kwds: Any) str[source]

Represent as a string.

print(**kwds: Any) None[source]

Print graph hierarchy.

classmethod from_data(sent: Span, data: dict[int, tuple[int, int]]) Self[source]

Construct from data dictionary.

Parameters:
  • sent – Sentence object.

  • data – Data dictionary.

  • cdict – Mapping from ordinal numbers to nodes.

to_data() dict[int, tuple[int, int]][source]

Dump to data dictionary.

Parameters:

odict – Mapping from nodes to their ordinal numbers within the node sequence.

Returns:

Mapping from node ordinal numbers of nodes to lists of t riples with two integers giving the index of the target node, index of subordinating conjunction token and the name of a dependency symbol.

Return type:

data