def parse_constrained(rrp, sentence, constraints):
    """Example of how to find the highest scoring parse in the n-best list
    match certain span constraints. constraints is a mapping from spans
    (start, end) to a list of allowed labels for that span.  Returns None
    if no parses match."""
    def consistent(tree, constraints):
        mapping = {}
        for subtree in tree.all_subtrees():
            mapping[subtree.span()] = subtree.label
        for span, allowed_labels in constraints.items():
            if mapping.get(span) not in allowed_labels:
                return False
        return True

    nbest_list = rrp.parse(sentence)
    for item in nbest_list:
        if consistent(item.ptb_parse, constraints):
            return item.ptb_parse
    else:
        return None