def collapse_unary(tree, collapsePOS=False, collapseRoot=False, joinChar='+'):
 if ((collapseRoot == False) and isinstance(tree, Tree) and (len(tree) == 1)):
  nodeList = [tree[0]]
 else:
  nodeList = [tree]
 while (nodeList != []):
  node = nodeList.pop()
  if isinstance(node, Tree):
   if ((len(node) == 1) and isinstance(node[0], Tree) and ((collapsePOS == True) or isinstance(node[(0, 0)], Tree))):
    node.set_label(((node.label() + joinChar) + node[0].label()))
    node[0:] = [child for child in node[0]]
    nodeList.append(node)
   else:
    for child in node:
     nodeList.append(child)

def collapse_unary ( tree , collapsePOS = False , collapseRoot = False , joinChar = '+' ) : if ( ( collapseRoot == False ) and isinstance ( tree , Tree ) and ( len ( tree ) == 1 ) ) : nodeList = [ tree [ 0 ] ] else : nodeList = [ tree ] while ( nodeList != [ ] ) : node = nodeList . pop ( ) if isinstance ( node , Tree ) : if ( ( len ( node ) == 1 ) and isinstance ( node [ 0 ] , Tree ) and ( ( collapsePOS == True ) or isinstance ( node [ ( 0 , 0 ) ] , Tree ) ) ) : node . set_label ( ( ( node . label ( ) + joinChar ) + node [ 0 ] . label ( ) ) ) node [ 0 : ] = [ child for child in node [ 0 ] ] nodeList . append ( node ) else : for child in node : nodeList . append ( child )
collapse subtrees with a single child into a new non-terminal joined by joinchar .