def jaccard(graph, lda_topics):
    """
    Builds a function to estimate Jaccard Similarity w.r.t. intermediary topics (from topic graph) in lda_topics.
    """
    Q_it = set(x[0] for x in lda_topics if graph.has_node(x[0]) and graph.node[x[0]]['is_intermediary'])

    def jaccard_similarity(P):
        if not P or not Q_it:
            return 0.0

        P_it = set(x[0] for x in P if graph.has_node(x[0]) and graph.node[x[0]]['is_intermediary'])
        #print P_it, Q_it, float(len(P_it & Q_it)) / len(P_it | Q_it), P_it & Q_it
        return float(len(P_it & Q_it)) / len(P_it | Q_it)

    return jaccard_similarity