def pad_sequences(sequences, pad_tok=None, max_length=None):
    """
    Pad sequence.
    Args:
        sequences: A sequence needs to be padded.
        pad_tok: Token used for padding.
        max_length: Maximal length of the sequence
    Returns: The sequence after padding and the length of the padded sequence.
    """
    if pad_tok is None:
        # 0: "PAD" for words, "O" for tags
        pad_tok = 0
    if max_length is None:
        max_length = max([len(seq) for seq in sequences])
    sequence_padded, sequence_length = [], []
    for seq in sequences:
        seq_ = seq[:max_length] + [pad_tok] * max(max_length - len(seq), 0)
        sequence_padded.append(seq_)
        sequence_length.append(min(len(seq), max_length))
    return sequence_padded, sequence_length