### Question:
CharacterNGramSimilarity extends AbstractStringMetric { @Override public double getSimilarity(String s1, String s2) { Validate.notNull(s1, "s1 must not be null"); Validate.notNull(s2, "s2 must not be null"); String s1lower = s1.toLowerCase(); String s2lower = s2.toLowerCase(); if (s1lower.equals(s2lower)) { return 1; } Set<String> nGrams1 = createNGrams(s1lower, n); Set<String> nGrams2 = createNGrams(s2lower, n); return setSimilarity.getSimilarity(nGrams1, nGrams2); } CharacterNGramSimilarity(int n, SetSimilarity setSimilarity);  CharacterNGramSimilarity(int n); @Override double getSimilarity(String s1, String s2); @Override String toString();  }### Answer:
@Test public void testNGramSimilarity() { CharacterNGramSimilarity similarity = new CharacterNGramSimilarity(3, SetSimilarities.DICE); assertEquals(1.0, similarity.getSimilarity("", ""), DELTA); assertEquals(0.0, similarity.getSimilarity("", "string"), DELTA); assertEquals(1.0, similarity.getSimilarity("a", "a"), DELTA); assertEquals(1.0, similarity.getSimilarity("cat", "cat"), DELTA); assertEquals(0.727, similarity.getSimilarity("cat", "caat"), DELTA); assertEquals(0.0, similarity.getSimilarity("cat", "dog"), DELTA); assertEquals(0.824, similarity.getSimilarity("philipp", "philip"), DELTA); assertEquals(0.333, similarity.getSimilarity("philipp", "p"), DELTA); assertEquals(0.5, similarity.getSimilarity("word", "wort"), DELTA); }
@Test public void testNGramSimilarityJaccard() { CharacterNGramSimilarity similarity = new CharacterNGramSimilarity(3, SetSimilarities.JACCARD); assertEquals(1.0, similarity.getSimilarity("", ""), DELTA); assertEquals(0.0, similarity.getSimilarity("", "string"), DELTA); assertEquals(1.0, similarity.getSimilarity("a", "a"), DELTA); assertEquals(1.0, similarity.getSimilarity("cat", "cat"), DELTA); assertEquals(0.571, similarity.getSimilarity("cat", "caat"), DELTA); assertEquals(0.0, similarity.getSimilarity("cat", "dog"), DELTA); assertEquals(0.7, similarity.getSimilarity("philipp", "philip"), DELTA); assertEquals(0.2, similarity.getSimilarity("philipp", "p"), DELTA); assertEquals(0.333, similarity.getSimilarity("word", "wort"), DELTA); }
@Test public void testNGramSimilarityOverlap() { CharacterNGramSimilarity similarity = new CharacterNGramSimilarity(3, SetSimilarities.OVERLAP); assertEquals(1.0, similarity.getSimilarity("", ""), DELTA); assertEquals(0.0, similarity.getSimilarity("", "string"), DELTA); assertEquals(1.0, similarity.getSimilarity("a", "a"), DELTA); assertEquals(1.0, similarity.getSimilarity("cat", "cat"), DELTA); assertEquals(0.8, similarity.getSimilarity("cat", "caat"), DELTA); assertEquals(0.0, similarity.getSimilarity("cat", "dog"), DELTA); assertEquals(0.875, similarity.getSimilarity("philipp", "philip"), DELTA); assertEquals(0.666, similarity.getSimilarity("philipp", "p"), DELTA); assertEquals(0.5, similarity.getSimilarity("word", "wort"), DELTA); }