src/main/java/eu/digitisation/ngram/Distance.java
public static double cosine(NgramModel first, NgramModel second) {
        int sum = 0;
        int norm1 = 0;
        int norm2 = 0;

        if (first.order != second.order) {
            throw new IllegalArgumentException("Illegal comparison "
                    + "of n-gram models with different n");
        }
        for (String s : first.occur.keySet()) {
            if (s.length() > 0) {
                int val1 = first.occur.get(s).getValue();
                int val2 = second.occur.containsKey(s)
                        ? second.occur.get(s).getValue() : 0;
                sum += val1 * val2;
                norm1 += val1 * val1;
                norm2 += val2 * val2;
            }
        }
        for (String s : second.occur.keySet()) {
            if (s.length() > 0 && !first.occur.containsKey(s)) {
                int val2 = second.occur.get(s).getValue();
                norm2 += val2 * val2;
            }
        }
        if (norm1 > 0 && norm2 > 0) {
            return sum / (Math.sqrt(norm1) * Math.sqrt(norm2));
        } else {
            return 0;
        }
    }
/**
     *
     * @param first an NgramModel
     * @param second a second NgramModel (its order must be identical to
     * first's)
     * @return
     */
@param first an NgramModel
@param second a second NgramModel (its order must be identical to
first's)
@return
@param first an NgramModel @param second a second NgramModel ( its order must be identical to first ' s ) @return
public static double cosine(NgramModel first, NgramModel second) {
        int sum = 0;
        int norm1 = 0;
        int norm2 = 0;
        if (first.order != second.order) {
            throw new IllegalArgumentException("Illegal comparison "
                    + "of n-gram models with different n");
        }
        for (String s : first.occur.keySet()) {
            if (s.length() > 0) {
                int val1 = first.occur.get(s).getValue();
                int val2 = second.occur.containsKey(s)
                        ? second.occur.get(s).getValue() : 0;
                sum += val1 * val2;
                norm1 += val1 * val1;
                norm2 += val2 * val2;
            }
        }
        for (String s : second.occur.keySet()) {
            if (s.length() > 0 && !first.occur.containsKey(s)) {
                int val2 = second.occur.get(s).getValue();
                norm2 += val2 * val2;
            }
        }
        if (norm1 > 0 && norm2 > 0) {
            return sum / (Math.sqrt(norm1) * Math.sqrt(norm2));
        } else {
            return 0;
        }
    }
public static double cosine ( NgramModel first , NgramModel second ) { int sum = 0 ; int norm1 = 0 ; int norm2 = 0 ; if ( first . order != second . order ) { throw new IllegalArgumentException ( "Illegal comparison " + "of n-gram models with different n" ) ; } for ( String s : first . occur . keySet ( ) ) { if ( s . length ( ) > 0 ) { int val1 = first . occur . get ( s ) . getValue ( ) ; int val2 = second . occur . containsKey ( s ) ? second . occur . get ( s ) . getValue ( ) : 0 ; sum += val1 * val2 ; norm1 += val1 * val1 ; norm2 += val2 * val2 ; } } for ( String s : second . occur . keySet ( ) ) { if ( s . length ( ) > 0 && ! first . occur . containsKey ( s ) ) { int val2 = second . occur . get ( s ) . getValue ( ) ; norm2 += val2 * val2 ; } } if ( norm1 > 0 && norm2 > 0 ) { return sum / ( Math . sqrt ( norm1 ) * Math . sqrt ( norm2 ) ) ; } else { return 0 ; } }
@param first an NgramModel
 @param second a second NgramModel (its order must be identical to
 first's)
 @return
@param first an NgramModel @param second a second NgramModel ( its order must be identical to first ' s ) @return