src/main/java/eu/digitisation/ngram/Distance.java
public static double[] delta(NgramModel first, NgramModel second) {
        double[] result = new double[first.order];
        int[] deltas = new int[first.order];
        int[] tot = new int[first.order];

        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;
                deltas[s.length() - 1] += Math.abs(val1 - val2);
                tot[s.length() - 1] += (val1 + val2);
            }
        }
        for (String s : second.occur.keySet()) {
            if (s.length() > 0 && !first.occur.containsKey(s)) {
                int val2 = second.occur.get(s).getValue();
                deltas[s.length() - 1] += val2;
                tot[s.length() - 1] += val2;
            }
        }
        for (int n = 0; n < first.order; ++n) {
            //result += Math.log(deltas[n] / (double) tot[n]);
            result[n] = deltas[n] / (double) tot[n];
        }
//            return Math.exp(result / first.order);
        return result;
    }
/**
     *
     * @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[] delta(NgramModel first, NgramModel second) {
        double[] result = new double[first.order];
        int[] deltas = new int[first.order];
        int[] tot = new int[first.order];
        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;
                deltas[s.length() - 1] += Math.abs(val1 - val2);
                tot[s.length() - 1] += (val1 + val2);
            }
        }
        for (String s : second.occur.keySet()) {
            if (s.length() > 0 && !first.occur.containsKey(s)) {
                int val2 = second.occur.get(s).getValue();
                deltas[s.length() - 1] += val2;
                tot[s.length() - 1] += val2;
            }
        }
        for (int n = 0; n < first.order; ++n) {
            result[n] = deltas[n] / (double) tot[n];
        }
        return result;
    }
public static double [ ] delta ( NgramModel first , NgramModel second ) { double [ ] result = new double [ first . order ] ; int [ ] deltas = new int [ first . order ] ; int [ ] tot = new int [ first . order ] ; 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 ; deltas [ s . length ( ) - 1 ] += Math . abs ( val1 - val2 ) ; tot [ s . length ( ) - 1 ] += ( val1 + val2 ) ; } } for ( String s : second . occur . keySet ( ) ) { if ( s . length ( ) > 0 && ! first . occur . containsKey ( s ) ) { int val2 = second . occur . get ( s ) . getValue ( ) ; deltas [ s . length ( ) - 1 ] += val2 ; tot [ s . length ( ) - 1 ] += val2 ; } } for ( int n = 0 ; n < first . order ; ++ n ) { result [ n ] = deltas [ n ] / ( double ) tot [ n ] ; } return result ; }
@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
//result += Math.log(deltas[n] / (double) tot[n]); //            return Math.exp(result / first.order);