delphin.mrs.xmrs

Classes and functions for general *MRS processing.

class delphin.mrs.xmrs.Dmrs(nodes=None, links=None, top=None, index=None, xarg=None, lnk=None, surface=None, identifier=None)[source]

Construct an Xmrs using DMRS components.

Dependency Minimal Recursion Semantics (DMRS) have a list of Node objects and a list of Link objects. There are no variables or handles, so these will need to be created in order to make an Xmrs object. The top node may be set directly via a parameter or may be implicitly set via a Link from the special nodeid 0. If both are given, the link is ignored. The index and xarg nodes may only be set via parameters.

Parameters:
  • nodes – an iterable of Node objects
  • links – an iterable of Link objects
  • top – the scopal top node
  • index – the non-scopal top node
  • xarg – the external argument node
  • lnk – the Lnk object associating the MRS to the surface form
  • surface – the surface string
  • identifier – a discourse-utterance id

Example:

>>> rain = Node(10000, Pred.surface('_rain_v_1_rel'),
>>>             sortinfo={'cvarsort': 'e'})
>>> ltop_link = Link(0, 10000, post='H')
>>> d = Dmrs([rain], [ltop_link])
classmethod from_dict(d)[source]

Decode a dictionary, as from to_dict(), into a Dmrs object.

classmethod from_triples(triples, remap_nodeids=True)[source]

Decode triples, as from to_triples(), into a Dmrs object.

to_dict(short_pred=True, properties=True)[source]

Encode the Dmrs as a dictionary suitable for JSON serialization.

to_triples(short_pred=True, properties=True)[source]

Encode the Dmrs as triples suitable for PENMAN serialization.

class delphin.mrs.xmrs.Mrs(top=None, index=None, xarg=None, rels=None, hcons=None, icons=None, lnk=None, surface=None, identifier=None, vars=None)[source]

Construct an Xmrs using MRS components.

Formally, Minimal Recursion Semantics (MRS) have a top handle, a bag of Elementary Predications, and a bag of Handle Constraints. All arguments, including intrinsic arguments and constant arguments, are expected to be contained by the EPs.

Parameters:
  • top – the TOP (or LTOP) variable
  • index – the INDEX variable
  • xarg – the XARG variable
  • rels – an iterable of ElementaryPredications
  • hcons – an iterable of HandleConstraints
  • icons – an iterable of IndividualConstraints
  • lnk – the Lnk object associating the MRS to the surface form
  • surface – the surface string
  • identifier – a discourse-utterance id
  • vars – a mapping of variables to a list of (property, value) pairs

Example:

>>> m = Mrs(
>>>     top='h0',
>>>     index='e2',
>>>     rels=[ElementaryPredication(
>>>         Pred.surface('_rain_v_1_rel'),
>>>         label='h1',
>>>         args={'ARG0': 'e2'},
>>>         vars={'e2': {'SF': 'prop-or-ques', 'TENSE': 'present'}}
>>>     )],
>>>     hcons=[HandleConstraint('h0', 'qeq', 'h1')]
>>> )
classmethod from_dict(d)[source]

Decode a dictionary, as from to_dict(), into an Mrs object.

to_dict(short_pred=True, properties=True)[source]

Encode the Mrs as a dictionary suitable for JSON serialization.

delphin.mrs.xmrs.Rmrs(top=None, index=None, xarg=None, eps=None, args=None, hcons=None, icons=None, lnk=None, surface=None, identifier=None, vars=None)[source]

Construct an Xmrs from RMRS components.

Robust Minimal Recursion Semantics (RMRS) are like MRS, but all predications have a nodeid (“anchor”), and arguments are not contained by the source predications, but instead reference the nodeid of their predication.

Parameters:
  • top – the TOP (or maybe LTOP) variable
  • index – the INDEX variable
  • xarg – the XARG variable
  • eps – an iterable of EPs
  • args – a nested mapping of {nodeid: {rargname: value}}
  • hcons – an iterable of HandleConstraint objects
  • icons – an iterable of IndividualConstraint objects
  • lnk – the Lnk object associating the MRS to the surface form
  • surface – the surface string
  • identifier – a discourse-utterance id
  • vars – a mapping of variables to a list of (property, value) pairs

Example:

>>> m = Rmrs(
>>>     top='h0',
>>>     index='e2',
>>>     eps=[ElementaryPredication(
>>>         10000,
>>>         Pred.surface('_rain_v_1_rel'),
>>>         'h1'
>>>     )],
>>>     args={10000: {'ARG0': 'e2'}},
>>>     hcons=[HandleConstraint('h0', 'qeq', 'h1'),
>>>     vars={'e2': {'SF': 'prop-or-ques', 'TENSE': 'present'}}
>>> )
class delphin.mrs.xmrs.Xmrs(top=None, index=None, xarg=None, eps=None, hcons=None, icons=None, vars=None, lnk=None, surface=None, identifier=None)[source]

Xmrs is a common class for Mrs, Rmrs, and Dmrs objects.

Parameters:
  • top – the TOP (or maybe LTOP) variable
  • index – the INDEX variable
  • xarg – the XARG variable
  • eps – an iterable of EPs (see above)
  • hcons – an iterable of HCONS (see above)
  • icons – an iterable of ICONS (see above)
  • vars – a mapping of variable to a list of property-value pairs
  • lnk – the Lnk object associating the Xmrs to the surface form
  • surface – the surface string
  • identifier – a discourse-utterance id

Xmrs can be instantiated directly, but it may be more convenient to use the Mrs(), Rmrs(), or Dmrs() constructor functions.

Variables are simply strings, but must be of the proper form in order to be recognized as variables and not constants. The form is basically a sequence of non-integers followed by a sequence of integers, but see delphin.mrs.components.var_re for the regular expression used to determine a match.

The eps argument is an iterable of tuples representing ElementaryPredications. These can be objects of the ElementaryPredication class itself, or an equivalent tuple. The same goes for hcons and icons with the HandleConstraint and IndividualConstraint classes, respectively.

top

the top (i.e. LTOP) handle

index

the semantic index

xarg

the external argument

lnk

surface alignment

Type:Lnk
surface

the surface string

identifier

a discourse-utterance ID (often unset)

add_eps(eps)[source]

Incorporate the list of EPs given by eps.

add_hcons(hcons)[source]

Incorporate the list of HandleConstraints given by hcons.

add_icons(icons)[source]

Incorporate the individual constraints given by icons.

args(nodeid)[source]

Return the arguments for the predication given by nodeid.

All arguments (including intrinsic and constant arguments) are included. MOD/EQ links are not considered arguments. If only arguments that target other predications are desired, see outgoing_args().

Parameters:nodeid – the nodeid of the EP that is the arguments’ source
Returns:dict{role: tgt}
ep(nodeid)[source]

Return the ElementaryPredication with the given nodeid.

eps(nodeids=None)[source]

Return the EPs with the given nodeid, or all EPs.

Parameters:nodeids – an iterable of nodeids of EPs to return; if None, return all EPs
classmethod from_xmrs(xmrs, **kwargs)[source]

Facilitate conversion among subclasses.

Parameters:
  • xmrs (Xmrs) – instance to convert from; possibly an instance of a subclass, such as Mrs or Dmrs
  • **kwargs – additional keyword arguments that may be used by a subclass’s redefinition of from_xmrs().
hcon(hi)[source]

Return the HandleConstraint with high variable hi.

hcons()[source]

Return the list of HCONS.

icons(left=None)[source]

Return the ICONS with left variable left, or all ICONS.

Parameters:left – the left variable of the ICONS to return; if None, return all ICONS
identifier = None

A discourse-utterance id

incoming_args(nodeid)[source]

Return the arguments that target nodeid.

Valid arguments include regular variable arguments and scopal (label-selecting or HCONS) arguments. MOD/EQ links and intrinsic arguments are not included.

Parameters:nodeid – the nodeid of the EP that is the arguments’ target
Returns:dict{source_nodeid: {rargname: value}}
is_connected()[source]

Return True if the Xmrs represents a connected graph.

Subgraphs can be connected through things like arguments, QEQs, and label equalities.

is_well_formed()[source]

Return True if the Xmrs is well-formed, False otherwise.

See validate()

label(nodeid)[source]

Return the label of the predication given by nodeid

labels(nodeids=None)[source]

Return the list of labels for nodeids, or all labels.

Parameters:nodeids – an iterable of nodeids for predications to get labels from; if None, return labels for all predications

Note

This returns the label of each predication, even if it’s shared by another predication. Thus, zip(nodeids, xmrs.labels(nodeids)) will pair nodeids with their labels.

Returns:A list of labels
labelset(label)[source]

Return the set of nodeids for predications that share label.

Parameters:label – the label that returned nodeids share.
Returns:A set of nodeids, which may be an empty set.
labelset_heads(label)[source]

Return the heads of the labelset selected by label.

Parameters:label – the label from which to find head nodes/EPs.
Returns:An iterable of nodeids.
lnk = None

A Lnk object to associate the Xmrs to the surface form

ltop

The top handle if specified; None otherwise.

Note

Equivalent to top

nodeid(iv, quantifier=False)[source]

Return the nodeid of the predication selected by iv.

Parameters:
  • iv – the intrinsic variable of the predication to select
  • quantifier – if True, treat iv as a bound variable and find its quantifier; otherwise the non-quantifier will be returned
nodeids(ivs=None, quantifier=None)[source]

Return the list of nodeids given by ivs, or all nodeids.

Parameters:
  • ivs – the intrinsic variables of the predications to select; if None, return all nodeids (but see quantifier)
  • quantifier – if True, only return nodeids of quantifiers; if False, only return non-quantifiers; if None (the default), return both
outgoing_args(nodeid)[source]

Return the arguments going from nodeid to other predications.

Valid arguments include regular variable arguments and scopal (label-selecting or HCONS) arguments. MOD/EQ links, intrinsic arguments, and constant arguments are not included.

Parameters:nodeid – the nodeid of the EP that is the arguments’ source
Returns:dict{role: tgt}
pred(nodeid)[source]

Return the Pred object for the predications given by nodeid.

preds(nodeids=None)[source]

Return the Pred objects for nodeids, or all Preds.

Parameters:nodeids – an iterable of nodeids of predications to return Preds from; if None, return all Preds
properties(var_or_nodeid, as_list=False)[source]

Return a dictionary of variable properties for var_or_nodeid.

Parameters:var_or_nodeid – if a variable, return the properties associated with the variable; if a nodeid, return the properties associated with the intrinsic variable of the predication given by the nodeid
subgraph(nodeids)[source]

Return an Xmrs object with only the specified nodeids.

Necessary variables and arguments are also included in order to connect any nodes that are connected in the original Xmrs.

Parameters:nodeids – the nodeids of the nodes/EPs to include in the subgraph.
Returns:An Xmrs object.
surface = None

The surface string

validate()[source]

Check that the Xmrs is well-formed.

The Xmrs is analyzed and a list of problems is compiled. If any problems exist, an XmrsError is raised with the list joined as the error message. A well-formed Xmrs has the following properties:

  • All predications have an intrinsic variable
  • Every intrinsic variable belongs one predication and maybe one quantifier
  • Every predication has no more than one quantifier
  • All predications have a label
  • The graph of predications form a net (i.e. are connected). Connectivity can be established with variable arguments, QEQs, or label-equality.
  • The lo-handle for each QEQ must exist as the label of a predication
variables()[source]

Return the list of all variables.