@Test public void testGetLevenshteinDistance() { try { StringUtils.getLevenshteinDistance(null, null); Assert.fail(); } catch (IllegalArgumentException e) { } try { StringUtils.getLevenshteinDistance("", null); Assert.fail(); } catch (IllegalArgumentException e) { } try { StringUtils.getLevenshteinDistance(null, ""); Assert.fail(); } catch (IllegalArgumentException e) { } try { StringUtils.getLevenshteinDistance("foo", null); Assert.fail(); } catch (IllegalArgumentException e) { } Assert.assertEquals(3, StringUtils.getLevenshteinDistance("foo", "")); try { StringUtils.getLevenshteinDistance(null, "bar"); Assert.fail(); } catch (IllegalArgumentException e) { } Assert.assertEquals(0, StringUtils.getLevenshteinDistance("", "")); Assert.assertEquals(3, StringUtils.getLevenshteinDistance("", "bar")); Assert.assertEquals(3, StringUtils.getLevenshteinDistance("foo", "bar")); Assert.assertEquals(4, StringUtils.getLevenshteinDistance("foo1", "bar")); Assert.assertEquals(4, StringUtils.getLevenshteinDistance("foo", "bar1")); }
public static int getLevenshteinDistance(CharSequence s, CharSequence t) { if (s == null || t == null) throw new IllegalArgumentException("Strings must not be null"); int n = s.length(); int m = t.length(); if (n == 0) return m; else if (m == 0) return n; if (n > m) { CharSequence tmp = s; s = t; t = tmp; n = m; m = t.length(); } int p[] = new int[n + 1]; int d[] = new int[n + 1]; int pd[]; int i; int j; for (i = 0; i <= n; i++) p[i] = i; int cost; char tj; for (j = 1; j <= m; j++) { tj = t.charAt(j - 1); d[0] = j; for (i = 1; i <= n; i++) { cost = s.charAt(i - 1) == tj ? 0 : 1; d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost); } pd = p; p = d; d = pd; } return p[n]; }
StringUtils { public static int getLevenshteinDistance(CharSequence s, CharSequence t) { if (s == null || t == null) throw new IllegalArgumentException("Strings must not be null"); int n = s.length(); int m = t.length(); if (n == 0) return m; else if (m == 0) return n; if (n > m) { CharSequence tmp = s; s = t; t = tmp; n = m; m = t.length(); } int p[] = new int[n + 1]; int d[] = new int[n + 1]; int pd[]; int i; int j; for (i = 0; i <= n; i++) p[i] = i; int cost; char tj; for (j = 1; j <= m; j++) { tj = t.charAt(j - 1); d[0] = j; for (i = 1; i <= n; i++) { cost = s.charAt(i - 1) == tj ? 0 : 1; d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost); } pd = p; p = d; d = pd; } return p[n]; } }
StringUtils { public static int getLevenshteinDistance(CharSequence s, CharSequence t) { if (s == null || t == null) throw new IllegalArgumentException("Strings must not be null"); int n = s.length(); int m = t.length(); if (n == 0) return m; else if (m == 0) return n; if (n > m) { CharSequence tmp = s; s = t; t = tmp; n = m; m = t.length(); } int p[] = new int[n + 1]; int d[] = new int[n + 1]; int pd[]; int i; int j; for (i = 0; i <= n; i++) p[i] = i; int cost; char tj; for (j = 1; j <= m; j++) { tj = t.charAt(j - 1); d[0] = j; for (i = 1; i <= n; i++) { cost = s.charAt(i - 1) == tj ? 0 : 1; d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost); } pd = p; p = d; d = pd; } return p[n]; } private  StringUtils(); }
StringUtils { public static int getLevenshteinDistance(CharSequence s, CharSequence t) { if (s == null || t == null) throw new IllegalArgumentException("Strings must not be null"); int n = s.length(); int m = t.length(); if (n == 0) return m; else if (m == 0) return n; if (n > m) { CharSequence tmp = s; s = t; t = tmp; n = m; m = t.length(); } int p[] = new int[n + 1]; int d[] = new int[n + 1]; int pd[]; int i; int j; for (i = 0; i <= n; i++) p[i] = i; int cost; char tj; for (j = 1; j <= m; j++) { tj = t.charAt(j - 1); d[0] = j; for (i = 1; i <= n; i++) { cost = s.charAt(i - 1) == tj ? 0 : 1; d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost); } pd = p; p = d; d = pd; } return p[n]; } private  StringUtils(); static String byteToHex(byte[] data); static String replaceLineBreaksWithSpace(String string); static String collapseLineBreaks(String string); static String replaceLineBreaks(String string, String replace); static String removeWhiteSpaces(String string); static String tryTrim(String string); static int getLevenshteinDistance(CharSequence s, CharSequence t); static String join(Object[] array, char separator); static String join(Object[] array, char separator, int startIndex, int endIndex); static String join(Iterator<T> iterator, String separator); static String join(Collection<T> collection, String separator); }
StringUtils { public static int getLevenshteinDistance(CharSequence s, CharSequence t) { if (s == null || t == null) throw new IllegalArgumentException("Strings must not be null"); int n = s.length(); int m = t.length(); if (n == 0) return m; else if (m == 0) return n; if (n > m) { CharSequence tmp = s; s = t; t = tmp; n = m; m = t.length(); } int p[] = new int[n + 1]; int d[] = new int[n + 1]; int pd[]; int i; int j; for (i = 0; i <= n; i++) p[i] = i; int cost; char tj; for (j = 1; j <= m; j++) { tj = t.charAt(j - 1); d[0] = j; for (i = 1; i <= n; i++) { cost = s.charAt(i - 1) == tj ? 0 : 1; d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost); } pd = p; p = d; d = pd; } return p[n]; } private  StringUtils(); static String byteToHex(byte[] data); static String replaceLineBreaksWithSpace(String string); static String collapseLineBreaks(String string); static String replaceLineBreaks(String string, String replace); static String removeWhiteSpaces(String string); static String tryTrim(String string); static int getLevenshteinDistance(CharSequence s, CharSequence t); static String join(Object[] array, char separator); static String join(Object[] array, char separator, int startIndex, int endIndex); static String join(Iterator<T> iterator, String separator); static String join(Collection<T> collection, String separator); static final String EMPTY; static final String SPACE; }