private double distanceInterleaved(LanguageProfile that) {
        if (length != that.length) {
            throw new IllegalArgumentException("Unable to calculage distance of language profiles" +
                                               " with different ngram lengths: " + that.length + " != " + length);
        }

        double sumOfSquares = 0.0;
        double thisCount = Math.max(this.count, 1.0);
        double thatCount = Math.max(that.count, 1.0);

        Interleaved.Entry thisEntry = updateInterleaved().firstEntry();
        Interleaved.Entry thatEntry = that.updateInterleaved().firstEntry();

        // Iterate the lists in parallel, until both lists has been depleted
        while (thisEntry.hasNgram() || thatEntry.hasNgram()) {
            if (!thisEntry.hasNgram()) { // Depleted this
                sumOfSquares += square(thatEntry.count / thatCount);
                thatEntry.next();
                continue;
            }

            if (!thatEntry.hasNgram()) { // Depleted that
                sumOfSquares += square(thisEntry.count / thisCount);
                thisEntry.next();
                continue;
            }

            final int compare = thisEntry.compareTo(thatEntry);

            if (compare == 0) { // Term exists both in this and that
                double difference = thisEntry.count / thisCount - thatEntry.count / thatCount;
                sumOfSquares += square(difference);
                thisEntry.next();
                thatEntry.next();
            } else if (compare < 0) { // Term exists only in this
                sumOfSquares += square(thisEntry.count / thisCount);
                thisEntry.next();
            } else { // Term exists only in that
                sumOfSquares += square(thatEntry.count / thatCount);
                thatEntry.next();
            }
        }
        return Math.sqrt(sumOfSquares);
    }
Calculates the geometric distance between this and the given
other language profile.

@param that the other language profile
@return distance between the profiles
tika-langdetect/tika-langdetect-tika/src/main/java/org/apache/tika/langdetect/tika/LanguageProfile.java