delphin.semi

Semantic Interface (SEM-I)

Semantic interfaces (SEM-Is) describe the inventory of semantic components in a grammar, including variables, properties, roles, and predicates. This information can be used for validating semantic structures or for filling out missing information in incomplete representations.

See also

The following DELPH-IN wikis contain more information:

Loading a SEM-I from a File

The load() module function is used to read the regular file-based SEM-I definitions, but there is also a dictionary representation which may be useful for JSON serialization, e.g., for an HTTP API that makes use of SEM-Is. See SemI.to_dict() for the later.

delphin.semi.load(source, encoding='utf-8')[source]

Interpret and return the SEM-I defined at path source.

Parameters
  • source – the path of the top file for the SEM-I. Note: this must be a path and not an open file.

  • encoding (str) – the character encoding of the file

Returns

The SemI defined by source

The SemI Class

The main class modeling a semantic interface is SemI. The predicate synopses have enough complexity that two more subclasses are used to make inspection easier: Synopsis contains the role information for an individual predicate synopsis, and each role is modeled with a SynopsisRole class.

class delphin.semi.SemI(variables=None, properties=None, roles=None, predicates=None)[source]

A semantic interface.

SEM-Is describe the semantic inventory for a grammar. These include the variable types, valid properties for variables, valid roles for predications, and a lexicon of predicates with associated roles.

Parameters
  • variables – a mapping of (var, {‘parents’: […], ‘properties’: […]})

  • properties – a mapping of (prop, {‘parents’: […]})

  • roles – a mapping of (role, {‘value’: …})

  • predicates – a mapping of (pred, {‘parents’: […], ‘synopses’: […]})

variables

a MultiHierarchy of variables; node data contains the property lists

properties

a MultiHierarchy of properties

roles

mapping of role names to allowed variable types

predicates

a MultiHierarchy of predicates; node data contains lists of synopses

The data in the SEM-I can be directly inspected via the variables, properties, roles, and predicates attributes.

>>> smi = semi.load('../grammars/erg/etc/erg.smi')
>>> smi.variables['e']
<delphin.tfs.TypeHierarchyNode object at 0x7fa02f877388>
>>> smi.variables['e'].parents
['i']
>>> smi.variables['e'].data
[('SF', 'sf'), ('TENSE', 'tense'), ('MOOD', 'mood'), ('PROG', 'bool'), ('PERF', 'bool')]
>>> 'sf' in smi.properties
True
>>> smi.roles['ARG0']
'i'
>>> for synopsis in smi.predicates['can_able'].data:
...     print(', '.join('{0.name} {0.value}'.format(roledata)
...                     for roledata in synopsis))
...
ARG0 e, ARG1 i, ARG2 p
>>> smi.predicates.descendants('some_q')
['_another_q', '_many+a_q', '_an+additional_q', '_what+a_q', '_such+a_q', '_some_q_indiv', '_some_q', '_a_q']

Note that the variables, properties, and predicates are TypeHierarchy objects.

find_synopsis(predicate, args=None)[source]

Return the first matching synopsis for predicate.

predicate will be normalized before lookup.

Synopses can be matched by a description of arguments which is tested with Synopsis.subsumes(). If no condition is given, the first synopsis is returned.

Parameters
  • predicate – predicate symbol whose synopsis will be returned

  • args – description of arguments that must be subsumable by the synopsis

Returns

matching synopsis as a list of (role, value, properties, optional) role tuples

Raises

SemIError – if predicate is undefined or if no matching synopsis can be found

Example

>>> smi.find_synopsis('_write_v_to')
[('ARG0', 'e', [], False), ('ARG1', 'i', [], False),
 ('ARG2', 'p', [], True), ('ARG3', 'h', [], True)]
>>> smi.find_synopsis('_write_v_to', args='eii')
[('ARG0', 'e', [], False), ('ARG1', 'i', [], False),
 ('ARG2', 'i', [], False)]
classmethod from_dict(d)[source]

Instantiate a SemI from a dictionary representation.

to_dict()[source]

Return a dictionary representation of the SemI.

class delphin.semi.Synopsis(roles)[source]

A SEM-I predicate synopsis.

A synopsis describes the roles of a predicate in a semantic structure, so it is no more than a tuple of roles as SynopsisRole objects. The length of the synopsis is thus the arity of a predicate while the individual role items detail the role names, argument types, associated properties, and optionality.

classmethod from_dict(d)[source]

Create a Synopsis from its dictionary representation.

Example:

>>> synopsis = Synopsis.from_dict({
...     'roles': [
...         {'name': 'ARG0', 'value': 'e'},
...         {'name': 'ARG1', 'value': 'x',
...          'properties': {'NUM': 'sg'}}
...     ]
... })
...
>>> len(synopsis)
2
subsumes(args, variables=None)[source]

Return True if the Synopsis subsumes args.

The args argument is a description of MRS arguments. It may take two different forms:

  • a sequence (e.g., string or list) of variable types, e.g., “exh”, which must be subsumed by the role values of the synopsis in order

  • a mapping (e.g., a dict) of roles to variable types which must match roles in the synopsis; the variable type may be None which matches any role value

In both cases, the sequence or mapping must be a subset of the roles of the synopsis, and any missing must be optional roles, otherwise the synopsis does not subsume args.

The variables argument is a variable hierarchy. If it is None, variables will be checked for strict equality.

to_dict()[source]

Return a dictionary representation of the Synopsis.

Example:

>>> Synopsis([
...     SynopsisRole('ARG0', 'e'),
...     SynopsisRole('ARG1', 'x', {'NUM': 'sg'})
... ]).to_dict()
{'roles': [{'name': 'ARG0', 'value': 'e'},
           {'name': 'ARG1', 'value': 'x',
            'properties': {'NUM': 'sg'}}]}
class delphin.semi.SynopsisRole(name, value, properties=None, optional=False)[source]

Role data associated with a SEM-I predicate synopsis.

Parameters
  • name (str) – the role name

  • value (str) – the role value (variable type or “string”)

  • properties (dict) – properties associated with the role’s value

  • optional (bool) – a flag indicating if the role is optional

Example:

>>> role = SynopsisRole('ARG0', 'x', {'PERS': '3'}, False)

Exceptions and Warnings

exception delphin.semi.SemIError(*args, **kwargs)[source]

Bases: delphin.exceptions.PyDelphinException

Raised when loading an invalid SEM-I.

exception delphin.semi.SemISyntaxError(message=None, filename=None, lineno=None, offset=None, text=None)[source]

Bases: delphin.exceptions.PyDelphinSyntaxError

Raised when loading an invalid SEM-I.

exception delphin.semi.SemIWarning(*args, **kwargs)[source]

Bases: delphin.exceptions.PyDelphinWarning

Warning class for questionable SEM-Is.