(nodes)
return nodes
Collapse any nodes that only have a single child.

  Collapsing of a node is done by removing that node and attaching its child to
  its parent(s).

  Args:
    nodes: A set of Synsets.

  Returns:
    A set containing the Synsets in nodes that were not collapsed, with
    potentially modified children and parents lists due to collapsing other
    synsets.
Collapse any nodes that only have a single child.
Collapse any nodes that only have a single child .
def collapse(nodes):
  """Collapse any nodes that only have a single child.

  Collapsing of a node is done by removing that node and attaching its child to
  its parent(s).

  Args:
    nodes: A set of Synsets.

  Returns:
    A set containing the Synsets in nodes that were not collapsed, with
    potentially modified children and parents lists due to collapsing other
    synsets.
  """

  def collapse_once(nodes):
    """Perform a pass of the collapsing as described above."""
    num_collapsed = 0
    non_collapsed_nodes = set()
    for n in nodes:
      if len(n.children) == 1:
        # attach the only child to all of n's parents
        n.children[0].parents += n.parents
        for p in n.parents:
          p.children.append(n.children[0])

        # Remove all connections to and from n
        isolate_node(n)

        num_collapsed += 1
      else:
        non_collapsed_nodes.add(n)

    assert len(nodes) - len(non_collapsed_nodes) == num_collapsed
    return non_collapsed_nodes, num_collapsed

  nodes, num_collapsed = collapse_once(nodes)
  while num_collapsed:
    nodes, num_collapsed = collapse_once(nodes)
  return nodes
def collapse ( nodes ) : def collapse_once ( nodes ) : """Perform a pass of the collapsing as described above.""" num_collapsed = 0 non_collapsed_nodes = set ( ) for n in nodes : if len ( n . children ) == 1 : # attach the only child to all of n's parents n . children [ 0 ] . parents += n . parents for p in n . parents : p . children . append ( n . children [ 0 ] ) # Remove all connections to and from n isolate_node ( n ) num_collapsed += 1 else : non_collapsed_nodes . add ( n ) assert len ( nodes ) - len ( non_collapsed_nodes ) == num_collapsed return non_collapsed_nodes , num_collapsed nodes , num_collapsed = collapse_once ( nodes ) while num_collapsed : nodes , num_collapsed = collapse_once ( nodes ) return nodes