ModifiedLevenshteinDistance { public static Match levenshteinMatch(String needle, String haystack){ return new Match(needle, calculateModifiedLevenshteinDistance(needle, nullToEmptyString(haystack).replace(' ', '_'))); }  static Match levenshteinMatch(String needle, String haystack); static int calculateModifiedLevenshteinDistance(String needle, String haystack); static int calculateModifiedLevenshteinDistance(String needle, String haystack, char space);  }
@Test public void testBasicRules() { String needle = "needle"; String neele = "neele"; String needDle = "needDle"; assertThat(levenshteinMatch(needle,needle).getDistance(), is(0)); assertThat(levenshteinMatch(needle,needle + "a").getDistance(), is(1)); assertThat(levenshteinMatch(needle, "a" + needle ).getDistance(), is(1)); assertThat(levenshteinMatch(needle, "a" + needle + "a").getDistance(), is(2)); assertThat(levenshteinMatch(needle, needDle).getDistance(), is(1)); assertThat(levenshteinMatch(needDle, needle).getDistance(), is(1)); assertThat(levenshteinMatch(needle, neele).getDistance(), is(1)); assertThat(levenshteinMatch(neele, needle).getDistance(), is(1)); assertThat(levenshteinMatch(needle, needle + " " + neele).getDistance(), is(0)); assertThat(levenshteinMatch(needle, neele + " " + needle).getDistance(), is(0)); assertThat(levenshteinMatch(needle,needle + " a").getDistance(), is(0)); assertThat(levenshteinMatch(needle, "a " + needle ).getDistance(), is(0)); assertThat(levenshteinMatch(needle, "a" + needle + " a").getDistance(), is(1)); assertThat(levenshteinMatch(needle, "a " + needle + "a").getDistance(), is(1)); assertThat(levenshteinMatch(needle, "a " + needle + " a").getDistance(), is(0)); }
@Test public void getDistancesEmptyNeedle(){ assertThat(levenshteinMatch("", "haystack").getDistance(), is(Integer.MAX_VALUE)); assertThat(levenshteinMatch("", "haystack").getDistance(), is(Integer.MAX_VALUE)); }
@Test public void getDistancesEmptyHaystack(){ assertThat(levenshteinMatch("needle", "").getDistance(), is(Integer.MAX_VALUE)); }
@Test public void getDistanceXtoSomethingWithoutX() { assertThat(levenshteinMatch("x","y").getDistance(), is(Integer.MAX_VALUE)); assertThat(levenshteinMatch("x","y ").getDistance(), is(Integer.MAX_VALUE)); assertThat(levenshteinMatch("x"," y").getDistance(), is(Integer.MAX_VALUE)); assertThat(levenshteinMatch("x"," y ").getDistance(), is(Integer.MAX_VALUE)); assertThat(levenshteinMatch("x","lorem ipsum").getDistance(), is(Integer.MAX_VALUE)); }
@Test public void getDistanceXToXX() { assertThat(levenshteinMatch("xx","x").getDistance(), is(1)); assertThat(levenshteinMatch("xx","x ").getDistance(), is(1)); assertThat(levenshteinMatch("xx"," x").getDistance(), is(1)); assertThat(levenshteinMatch("xx"," x ").getDistance(), is(1)); assertThat(levenshteinMatch("xx","y x ").getDistance(), is(1)); assertThat(levenshteinMatch("xx"," x y").getDistance(), is(1)); assertThat(levenshteinMatch("xx","y x y").getDistance(), is(1)); assertThat(levenshteinMatch("Xx","x").getDistance(), is(1)); assertThat(levenshteinMatch("xx","X").getDistance(), is(1)); }
@Test public void getDistanceTest() { String needle = "needle"; String haystack = "haystack"; Match match = levenshteinMatch(needle,haystack); assertThat(match.getNeedle(), is(needle)); assertThat(match.getDistance(), is(greaterThan(0))); }
@Test public void getDistanceTestFullMatch() { String needle = "needle"; String haystack = "needle"; Match match = levenshteinMatch(needle,haystack); assertThat(match.getDistance(), is(0)); }
@Test public void getDistanceTestPartialMatch() { String needle = "needle"; String haystack = "ndle"; Match match = levenshteinMatch(needle,haystack); assertThat(match.getDistance(), is(2)); }
@Test public void getDistanceTestUpToPrefixMatch() { String needle = "needle"; String haystack = "prefix needle"; Match match = levenshteinMatch(needle,haystack); assertThat(match.getDistance(), is(0)); }
@Test public void getDistanceTestUpToPostfixMatch() { String needle = "needle"; String haystack = "needle postfix"; Match match = levenshteinMatch(needle,haystack); assertThat(match.getDistance(), is(0)); }
@Test public void getDistanceTestFullSubstringMatch() { String needle = "needle"; String noise = "bla"; String haystack = "prefix needle " + noise + " postfix"; Match match = levenshteinMatch(needle,haystack); assertThat(match.getDistance(), is(0)); }
@Test public void getDistanceTestPartialSubstringMatch() { String needle = "needle"; String noise = "bla"; String haystack = "prefix needle" + noise + " postfix"; Match match = levenshteinMatch(needle,haystack); assertThat(match.getDistance(), is(noise.length())) ; }