def process_single_text(raw_text, max_seq_length, encoder,
                        BOS_token, EOS_token, PAD_token):
    """Processes a single piece of text. Performs BPE encoding,
    converting to indexes, truncation, and padding, etc.
    """
    # BPE
    tokens = encoder.encode(raw_text)

    # Truncate
    max_len = max_seq_length
    if BOS_token is not None and len(BOS_token) > 0:
        max_len -= 1
    if EOS_token is not None and len(EOS_token) > 0:
        max_len -= 1
    tokens = tokens[:max_len]

    # Append special tokens
    if BOS_token is not None and len(BOS_token) > 0:
        tokens = [encoder.encoder[BOS_token]] + tokens
    if EOS_token is not None and len(EOS_token) > 0:
        tokens = tokens + [encoder.encoder[EOS_token]]

    token_length = len(tokens)

    # Pad
    PAD_token_id = encoder.encoder[PAD_token]
    while len(tokens) < max_seq_length:
        tokens.append(PAD_token_id)

    assert len(tokens) == max_seq_length

    return tokens, token_length