Computes Dice Coefficient between two vectors - Java java.lang

Java examples for java.lang:Math Vector

Description

Computes Dice Coefficient between two vectors

Demo Code


//package com.java2s;
import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;
import java.util.Set;

public class Main {
    /**/* w  w w  . j a v a2s. c  o  m*/
     * 
     * Computes Dice Coefficient between two vectors
     * 
     * @param vector1
     * @param vector2
     * @return score
     */
    public static double computeDiceCoeeficient(
            Map<String, Integer> vector1, Map<String, Integer> vector2) {
        double s10 = vector1.size();
        double s01 = vector2.size();
        double m11 = match(vector1, vector2).size();
        return (2.0 * m11) / (s01 + s10);
    }

    /**
     * 
     * Matches vector 1 with vector 2 and returns the vector of matched strings
     * 
     * @param vector1
     * @param vector2
     * @return dotproduct
     */
    public static Map<String, Integer> match(Map<String, Integer> vector1,
            Map<String, Integer> vector2) {
        Map<String, Integer> matchedVector = new HashMap<String, Integer>();
        Set<String> vector1Vocab = vector1.keySet();
        Iterator<String> iter = vector1Vocab.iterator();
        Integer vector1Freq;
        Integer vector2Freq;
        String word = null;
        while (iter.hasNext()) {
            word = iter.next();
            vector1Freq = vector1.get(word);
            if (vector2.containsKey(word)) {
                vector2Freq = vector2.get(word);
                matchedVector.put(
                        word,
                        new Integer(Math.min(vector1Freq.intValue(),
                                vector2Freq.intValue())));
            }
        }
        return matchedVector;
    }
}

Related Tutorials