penman.codec#
Serialization of PENMAN graphs.
- class penman.codec.PENMANCodec(model=None)[source]#
An encoder/decoder for PENMAN-serialized graphs.
- decode(s)[source]#
Deserialize PENMAN-notation string s into its Graph object.
- Parameters:
s – a string containing a single PENMAN-serialized graph
- Returns:
The
Graph
object described by s.
Example
>>> from penman.codec import PENMANCodec >>> codec = PENMANCodec() >>> codec.decode('(b / bark-01 :ARG0 (d / dog))') <Graph object (top=b) at ...>
- iterdecode(lines)[source]#
Yield graphs parsed from lines.
- Parameters:
lines – a string or open file with PENMAN-serialized graphs
- Returns:
The
Graph
objects described in lines.
- parse(s)[source]#
Parse PENMAN-notation string s into its tree structure.
- Parameters:
s – a string containing a single PENMAN-serialized graph
- Returns:
The tree structure described by s.
Example
>>> from penman.codec import PENMANCodec >>> codec = PENMANCodec() >>> codec.parse('(b / bark-01 :ARG0 (d / dog))') # noqa Tree(('b', [('/', 'bark-01'), (':ARG0', ('d', [('/', 'dog')]))]))
- iterparse(lines)[source]#
Yield trees parsed from lines.
- Parameters:
lines – a string or open file with PENMAN-serialized graphs
- Returns:
The
Tree
object described in lines.
- encode(g, top=None, indent=-1, compact=False)[source]#
Serialize the graph g into PENMAN notation.
- Parameters:
g – the Graph object
top – if given, the node to use as the top in serialization
indent – how to indent formatted strings
compact – if
True
, put initial attributes on the first line
- Returns:
the PENMAN-serialized string of the Graph g
Example
>>> from penman.graph import Graph >>> from penman.codec import PENMANCodec >>> codec = PENMANCodec() >>> codec.encode(Graph([('h', 'instance', 'hi')])) '(h / hi)'
- format_triples(triples, indent=True)[source]#
Return the formatted triple conjunction of triples.
- Parameters:
triples – an iterable of triples
indent – how to indent formatted strings
- Returns:
the serialized triple conjunction of triples
Example
>>> from penman.codec import PENMANCodec >>> codec = PENMANCodec() >>> codec.format_triples([('a', ':instance', 'alpha'), ... ('a', ':ARG0', 'b'), ... ('b', ':instance', 'beta')]) ... 'instance(a, alpha) ^\nARG0(a, b) ^\ninstance(b, beta)'