def filter_example(ex, use_src_len=True, use_mt_len=True,
                   use_pe_len=True,
                   min_src_len=1, max_src_len=float('inf'),
                   min_mt_len=1, max_mt_len=float("inf"),
                   min_pe_len=1, max_pe_len=float("inf")):
    """Return whether an example is an acceptable length.

    If used with a dataset as ``filter_pred``, use :func:`partial()`
    for all keyword arguments.

    Args:
        ex (torchtext.data.Example): An object with a ``src``,
            ``mt``, and ``pe`` property.
        use_src_len (bool): Filter based on the length of ``ex.src``.
        use_pe_len (bool): Similar to above.
        min_src_len (int): A non-negative minimally acceptable length
            (examples of exactly this length will be included).
        min_pe_len (int): Similar to above.
        max_src_len (int or float): A non-negative (possibly infinite)
            maximally acceptable length (examples of exactly this length
            will be included).
        max_pe_len (int or float): Similar to above.
    """

    src_len = len(ex.src[0])
    mt_len = len(ex.mt[0])
    pe_len = len(ex.pe[0])
    return (not use_src_len or min_src_len <= src_len <= max_src_len) and \
           (not use_mt_len or min_mt_len <= mt_len <= max_mt_len) and \
           (not use_pe_len or min_pe_len <= pe_len <= max_pe_len)