Compute Jaccard's Coefficient of Similarity - Java java.lang

Java examples for java.lang:String Algorithm

Description

Compute Jaccard's Coefficient of Similarity

Demo Code


import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class Main{
    /**/*w ww  .ja va  2  s.  c om*/
     * Compute Jaccard's Coefficient of Similarity
     * @param p1
     * @param p2
     * @return
     */
    public static double computeJaccardsCoefficient(Point p1, Point p2) {
        Collection<String> tweet1 = new TreeSet<String>(Arrays.asList(p1
                .getTweet().split(" ")));

        Collection<String> tweet2 = new TreeSet<String>(Arrays.asList(p2
                .getTweet().split(" ")));

        Collection<String> intersectionOfTweets = new TreeSet<String>(
                tweet1);
        intersectionOfTweets.retainAll(tweet2);

        Collection<String> unionOfTweets = new TreeSet<String>(tweet1);
        unionOfTweets.addAll(tweet2);

        double jaccardsCoefficient = (double) intersectionOfTweets.size()
                / (double) unionOfTweets.size();
        return jaccardsCoefficient;
    }
}

Related Tutorials