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:
Technical specifications: https://github.com/delph-in/docs/wiki/SemiRfc
Overview and usage: https://github.com/delph-in/docs/wiki/RmrsSemi
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
MultiHierarchyof variables; node data contains the property lists
- properties¶
a
MultiHierarchyof properties
- roles¶
mapping of role names to allowed variable types
- predicates¶
a
MultiHierarchyof predicates; node data contains lists of synopses
The data in the SEM-I can be directly inspected via the
variables,properties,roles, andpredicatesattributes.>>> 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
TypeHierarchyobjects.- 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)]
- 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
SynopsisRoleobjects. 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
Trueif 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 ordera mapping (e.g., a dict) of roles to variable types which must match roles in the synopsis; the variable type may be
Nonewhich 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.
Exceptions and Warnings¶
- exception delphin.semi.SemIError(*args, **kwargs)[source]¶
Bases:
PyDelphinExceptionRaised when loading an invalid SEM-I.
- exception delphin.semi.SemISyntaxError(message=None, filename=None, lineno=None, offset=None, text=None)[source]¶
Bases:
PyDelphinSyntaxErrorRaised when loading an invalid SEM-I.
- exception delphin.semi.SemIWarning(*args, **kwargs)[source]¶
Bases:
PyDelphinWarningWarning class for questionable SEM-Is.