// Calculates the fuzzy match of needle in haystack, // using a modified version of the Levenshtein distance algorithm. // ported from: http://ginstrom.com/scribbles/2007/12/01/fuzzy-substring-matching-with-levenshtein-distance-in-python/ static int levenSubstringIC(S needle, S haystack) { int m = l(needle), n = l(haystack); // base cases if (m == 1) ret cic(haystack, needle) ? 1 : 0; if (m == 0) ret m; int[] row1 = new int[n+1]; for i to m: { int row2 = new int[n+1]; row2[0] = i+1; for j to n: { int cost = neqic(needle.charAt(i), haystack.charAt(j)) ? 1 : 0; row2.add(min(row1[j+1]+1, # deletion row2[j]+1, #insertion row1[j]+cost) #substitution ); } row1 = row2; } ret minOfIntArray(row1); }