def _jaccard_similarity(f1, f2, weight_func):
elements = set(f1)<EOL>elements.update(f2)<EOL>count, w_count, w_total = <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0><EOL>for element in elements:<EOL><INDENT>mi = min(f1.get(element, <NUM_LIT:0>), f2.get(element, <NUM_LIT:0>))<EOL>mx = max(f1.get(element, <NUM_LIT:0>), f2.get(element, <NUM_LIT:0>))<EOL>count += mi<EOL>w = weight_func(element)<EOL>w_count += w * mi<EOL>w_total += w * mx<EOL><DEDENT>if count == <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>return <NUM_LIT:0.0> if w_total == <NUM_LIT:0.0> else w_count / w_total<EOL>
Calculate generalized Jaccard similarity of formulas.

    Returns the weighted similarity value or None if there is no overlap
    at all. If the union of the formulas has a weight of zero (i.e. the
    denominator in the Jaccard similarity is zero), a value of zero is
    returned.