def convert_to_tokens(sequence, reverse_vocabulary, terminal_id=0):
    """Convert a token id sequence back to tokens
    :param sequence a 1-d array of token ids.
    :param reverse_vocabulary a map from token ids to tokens.
    :param terminal_id a token id indicating end of sequence.
    :returns a sequence of tokens that does not include any tokens after terminal_id."""
    tokens = []
    for token_id in sequence:
        if token_id == terminal_id:
            break
        tokens.append(reverse_vocabulary[token_id])
    return tokens