'Convert a list of tagged tokens to a chunk-parse tree.'

def _tagged_to_parse(self, tagged_tokens):

    sent = Tree('S', [])
    for (tok, tag) in tagged_tokens:
        if (tag == 'O'):
            sent.append(tok)
        elif tag.startswith('B-'):
            sent.append(Tree(tag[2:], [tok]))
        elif tag.startswith('I-'):
            if (sent and isinstance(sent[(-1)], Tree) and (sent[(-1)].label() == tag[2:])):
                sent[(-1)].append(tok)
            else:
                sent.append(Tree(tag[2:], [tok]))
    return sent
