penman.interface

Functions for basic reading and writing of PENMAN graphs.

Graph-reading Functions

penman.interface.decode(s, model=None, triples=False)[source]

Deserialize PENMAN-serialized s into its Graph object

Parameters
  • s – a string containing a single PENMAN-serialized graph

  • model – the model used for interpreting the graph

  • triples – if True, read as a conjunction of triples

Returns

the Graph object described by s

Example

>>> decode('(b / bark :ARG1 (d / dog))')
<Graph object (top=b) at ...>
penman.interface.loads(string, model=None, triples=False)[source]

Deserialize a list of PENMAN-encoded graphs from string.

Parameters
  • string – a string containing graph data

  • model – the model used for interpreting the graph

  • triples – if True, read as a conjunction of triples

Returns

a list of Graph objects

penman.interface.load(source, model=None, triples=False)[source]

Deserialize a list of PENMAN-encoded graphs from source.

Parameters
  • source – a filename or file-like object to read from

  • model – the model used for interpreting the graph

  • triples – if True, read as a conjunction of triples

Returns

a list of Graph objects

Graph-writing Functions

penman.interface.encode(g, top=None, model=None, triples=False, indent=-1, compact=False)[source]

Serialize the graph g from top to PENMAN notation.

Parameters
  • g – the Graph object

  • top – if given, the node to use as the top in serialization

  • model – the model used for interpreting the graph

  • triples – if True, serialize as a conjunction of triples

  • 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

>>> encode(Graph([('h', 'instance', 'hi')]))
(h / hi)
penman.interface.dumps(graphs, model=None, triples=False, indent=-1, compact=False)[source]

Serialize each graph in graphs to the PENMAN format.

Parameters
  • graphs – an iterable of Graph objects

  • model – the model used for interpreting the graph

  • triples – if True, serialize as a conjunction of triples

  • indent – how to indent formatted strings

  • compact – if True, put initial attributes on the first line

Returns

the string of serialized graphs

penman.interface.dump(graphs, file, model=None, triples=False, indent=-1, compact=False)[source]

Serialize each graph in graphs to PENMAN and write to file.

Parameters
  • graphs – an iterable of Graph objects

  • file – a filename or file-like object to write to

  • model – the model used for interpreting the graph

  • triples – if True, serialize as a conjunction of triples

  • indent – how to indent formatted strings

  • compact – if True, put initial attributes on the first line