penman.model#

Semantic models for interpreting graphs.

class penman.model.Model(top_variable='top', top_role=':TOP', concept_role=':instance', roles=None, normalizations=None, reifications=None)[source]#

A semantic model for Penman graphs.

The model defines things like valid roles and transformations.

Parameters:
  • top_variable – the variable of the graph’s top

  • top_role – the role linking the graph’s top to the top node

  • concept_role – the role associated with node concepts

  • roles – a mapping of roles to associated data

  • normalizations – a mapping of roles to normalized roles

  • reifications – a list of 4-tuples used to define reifications

classmethod from_dict(d)[source]#

Instantiate a model from a dictionary.

has_role(role)[source]#

Return True if role is defined by the model.

If role is not in the model but a single deinversion of role is in the model, then True is returned. Otherwise False is returned, even if something like canonicalize_role() could return a valid role.

errors(graph)[source]#

Return a description of model errors detected in graph.

The description is a dictionary mapping a context to a list of errors. A context is a triple if the error is relevant for the triple, or None for general graph errors.

Example

>>> from penman.models.amr import model
>>> from penman.graph import Graph
>>> g = Graph([('a', ':instance', 'alpha'),
...            ('a', ':foo', 'bar'),
...            ('b', ':instance', 'beta')])
>>> for context, errors in model.errors(g).items():
...     print(context, errors)
...
('a', ':foo', 'bar') ['invalid role']
('b', ':instance', 'beta') ['unreachable']
is_role_inverted(role)[source]#

Return True if role is inverted.

invert_role(role)[source]#

Invert role.

invert(triple)[source]#

Invert triple.

This will invert or deinvert a triple regardless of its current state. deinvert() will deinvert a triple only if it is already inverted. Unlike canonicalize(), this will not perform multiple inversions or replace the role with a normalized form.

deinvert(triple)[source]#

De-invert triple if it is inverted.

Unlike invert(), this only inverts a triple if the model considers it to be already inverted, otherwise it is left alone. Unlike canonicalize(), this will not normalize multiple inversions or replace the role with a normalized form.

canonicalize_role(role)[source]#

Canonicalize role.

Role canonicalization will do the following:

  • Ensure the role starts with ‘:’

  • Normalize multiple inversions (e.g., ARG0-of-of becomes ARG0), but it does not change the direction of the role

  • Replace the resulting role with a normalized form if one is defined in the model

canonicalize(triple)[source]#

Canonicalize triple.

See canonicalize_role() for a description of how the role is canonicalized. Unlike invert(), this does not swap the source and target of triple.

is_role_reifiable(role)[source]#

Return True if role can be reified.

reify(triple, variables=None)[source]#

Return the three triples that reify triple.

Note that, unless variables is given, the node variable for the reified node is not necessarily valid for the target graph. When incorporating the reified triples, this variable should then be replaced.

If the role of triple does not have a defined reification, a ModelError is raised.

Parameters:
  • triple – the triple to reify

  • variables – a set of variables that should not be used for the reified node’s variable

Returns:

The 3-tuple of triples that reify triple.

is_concept_dereifiable(concept)[source]#

Return True if concept can be dereified.

dereify(instance_triple, source_triple, target_triple)[source]#

Return the triple that dereifies the three argument triples.

If the target of instance_triple does not have a defined dereification, or if the roles of source_triple and target_triple do not match those for the dereification of the concept, a ModelError is raised. A ValueError is raised if instance_triple is not an instance triple or any triple does not have the same source variable as the others.

Parameters:
  • instance_triple – the triple containing the node’s concept

  • source_triple – the source triple from the node

  • target_triple – the target triple from the node

Returns:

The triple that dereifies the three argument triples.

original_order(role)[source]#

Role sorting key that does not change the order.

alphanumeric_order(role)[source]#

Role sorting key for alphanumeric order.

canonical_order(role)[source]#

Role sorting key that finds a canonical order.

random_order(role)[source]#

Role sorting key that randomizes the order.