def filter_vocab(vocab, df, min_df):
    """
    Filter out rare tokens and construct a new vocabulary.
    @param vocab: input vocabulary
    @param df: document frequency dictionary (term: frequency)
    @param min_df: minimum document frequency
    """
    new_vocab = {'PAD': 0}
    for term in vocab:
        if term != 'PAD' and df[term] >= min_df:
            new_vocab[term] = len(new_vocab)
    return new_vocab