penman.tree#

Definitions of tree structures.

class penman.tree.Tree(node, metadata=None)[source]#

A tree structure.

A tree is essentially a node that contains other nodes, but this Tree class is useful to contain any metadata and to provide tree-based methods.

nodes()[source]#

Return the nodes in the tree as a flat list.

reset_variables(fmt='{prefix}{j}')[source]#

Recreate node variables formatted using fmt.

The fmt string can be formatted with the following values:

  • prefix: first alphabetic character in the node’s concept

  • i: 0-based index of the current occurrence of the prefix

  • j: 1-based index starting from the second occurrence

walk()[source]#

Iterate over branches in the tree.

This function yields pairs of (path, branch) where each path is a tuple of 0-based indices of branches to get to branch. For example, the path (2, 0) is the concept branch (‘/’, ‘bark-01’) in the tree for the following PENMAN string, traversing first to the third (index 2) branch of the top node, then to the first (index 0) branch of that node:

(t / try-01
   :ARG0 (d / dog)
   :ARG1 (b / bark-01
            :ARG0 d))

The (path, branch) pairs are yielded in depth-first order of the tree traversal.

penman.tree.is_atomic(x)[source]#

Return True if x is a valid atomic value.

Examples

>>> from penman.tree import is_atomic
>>> is_atomic('a')
True
>>> is_atomic(None)
True
>>> is_atomic(3.14)
True
>>> is_atomic(('a', [('/', 'alpha')]))
False