#!/usr/bin/env python

def root_is_labeled_SQ(tree):
    """Return true if the root of tree is an SQ node of some kind, else False."""
    if tree.node.startswith('SQ'):
        return True
    else: 
        return False

def has_leftmost_aux_daughter(tree):
    """Return True if tree has an aux-tree daughter that is at least before any NP or SBAR nodes."""
    daughters = [daughter for daughter in tree if re.search(r'(NP$|SBAR$|^V|MD)', daughter.node)]
    if daughters and is_aux_tree(daughters[0]):
        return True
    else:
        return False

def is_aux_tree(tree):
    """Return True if argument tree is of the form (V*|MD aux), else False."""
    verbal_re = re.compile(r'(^V|MD)', re.I)
    aux_re = re.compile(r'(Is|Are|Was|Were|Have|Has|Had|Can|Could|Shall|Should|Will|Would|May|Might|Must|Do|Does|Did|Wo)', re.I)
    if is_preterminal(tree) and verbal_re.search(tree.node) and aux_re.search(tree[0]):
        return True
    else: 
        return False
 
def is_preterminal(tree):
    """Return True if tree is of the form (PARENT CHILD), else False."""
    if len(list(tree.subtrees())) == 1:
        return True
    else:
        return False    
