get Similarity between two string list - Java java.lang

Java examples for java.lang:String Algorithm

Description

get Similarity between two string list

Demo Code


//package com.java2s;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

public class Main {
    public static double getSimilarity(Vector<String> doc1,
            Vector<String> doc2) {
        int size = 0, size2 = 0;
        if (doc1 != null && (size = doc1.size()) > 0 && doc2 != null
                && (size2 = doc2.size()) > 0) {

            Map<String, int[]> intersectMap = new HashMap<String, int[]>();

            for (int i = 0; i < size; i++) {
                String d1 = doc1.get(i);
                String charIndex = d1;
                if (charIndex != null) {
                    int[] fq = intersectMap.get(charIndex);
                    if (fq != null && fq.length == 2) {
                        fq[0]++;//from w  w  w  .j  a va  2  s. c o  m
                    } else {
                        fq = new int[2];
                        fq[0] = 1;
                        fq[1] = 0;
                        intersectMap.put(charIndex, fq);
                    }
                }
            }

            for (int i = 0; i < size2; i++) {
                String d2 = doc2.get(i);
                String charIndex = d2;
                if (charIndex != null) {
                    int[] fq = intersectMap.get(charIndex);
                    if (fq != null && fq.length == 2) {
                        fq[1]++;
                    } else {
                        fq = new int[2];
                        fq[0] = 0;
                        fq[1] = 1;
                        intersectMap.put(charIndex, fq);
                    }
                }
            }
            Iterator<String> iterator = intersectMap.keySet().iterator();
            double sqdoc1 = 0;
            double sqdoc2 = 0;
            double denominator = 0;
            while (iterator.hasNext()) {
                int[] c = intersectMap.get(iterator.next());
                denominator += c[0] * c[1];
                sqdoc1 += c[0] * c[0];
                sqdoc2 += c[1] * c[1];
            }

            return denominator / Math.sqrt(sqdoc1 * sqdoc2);
        } else {
            throw new NullPointerException(
                    " the Document is null or have not cahrs!!");
        }
    }
}

Related Tutorials