def mk_filter_by_unk_ratio(vocab, max_unk_ratio=0.0, is_tbptt=False):
    """Return a filter function that takes a sentence, and returns True if a sentence
    meet a criterion (unk ratio is below max).

    NOTE: each sentence contains a bos and an eos, which are excluded for calculating unk ratio.
    If is_tbptt is True, offset is changed to 1 (since each sentence only contains eos).
    """
    if max_unk_ratio == 0.0: return lambda sent: True
    offset = 1 if is_tbptt else 2

    def filter(sent):
        cnt = len([w for w in sent if not vocab.contains(w)])
        return cnt / (len(sent) - offset) < max_unk_ratio
    return filter