CharacterNGramMeasure extends TextSimilarityMeasureBase { @Override public double getSimilarity(String text1, String text2) throws SimilarityException { Set<String> ngrams1 = getNGrams(text1); Set<String> ngrams2 = getNGrams(text2); ngrams = new HashSet<String>(); ngrams.addAll(ngrams1); ngrams.addAll(ngrams2); Map<String, Double> tf1 = getTF(ngrams1); Map<String, Double> tf2 = getTF(ngrams2); double[] tfidf1 = new double[ngrams.size()]; double[] tfidf2 = new double[ngrams.size()]; int idx = 0; for (String ngram : ngrams) { if (tf1.containsKey(ngram) && idf.containsKey(ngram)) { tfidf1[idx] = tf1.get(ngram) * idf.get(ngram); } else { tfidf1[idx] = 0.0; } if (tf2.containsKey(ngram) && idf.containsKey(ngram)) { tfidf2[idx] = tf2.get(ngram) * idf.get(ngram); } else { tfidf2[idx] = 0.0; } idx++; } double dotprod = 0.0; for (int i = 0; i < ngrams.size(); i++) { dotprod += tfidf1[i] * tfidf2[i]; } double mag1 = 0.0; for (int i = 0; i < ngrams.size(); i++) { mag1 += Math.pow(tfidf1[i], 2); } double mag2 = 0.0; for (int i = 0; i < ngrams.size(); i++) { mag2 += Math.pow(tfidf2[i], 2); } return dotprod / (Math.sqrt(mag1) * Math.sqrt(mag2)); } CharacterNGramMeasure(int n, Map<String, Double> idfValues);  CharacterNGramMeasure(int n, String idfValuesFile); @Override double getSimilarity(String text1, String text2); @Override double getSimilarity(Collection<String> stringList1,
            Collection<String> stringList2); Set<String> getNGrams(String text); @Override String getName(); @Override boolean isDistanceMeasure();  }
@Test public void test() throws Exception { Map<String,Double> idfValues = new HashMap<String,Double>(); idfValues.put("the", 0.01); idfValues.put("ick", 0.3); idfValues.put("fox", 2.5); idfValues.put("dog", 2.1); idfValues.put("laz", 0.28); idfValues.put("azy", 0.27); String a1 = "The quick brown fox jumps over the lazy dog."; String a2 = "The quick brown dog jumps."; String b1 = "The quick brown fox jumps over the lazy dog."; String b2 = "The quick brown fox jumps."; TermSimilarityMeasure measure = new CharacterNGramMeasure(3, idfValues); assertEquals(0.117, measure.getSimilarity(a1, a2), epsilon); assertEquals(0.988, measure.getSimilarity(b1, b2), epsilon); }