Merge "Tweak edit distance"

This commit is contained in:
satok 2011-04-20 00:58:08 -07:00 committed by Android (Google) Code Review
commit 0aae85fb67

View file

@ -258,6 +258,8 @@ public class Utils {
} }
} }
/* Damerau-Levenshtein distance */
public static int editDistance(CharSequence s, CharSequence t) { public static int editDistance(CharSequence s, CharSequence t) {
if (s == null || t == null) { if (s == null || t == null) {
throw new IllegalArgumentException("editDistance: Arguments should not be null."); throw new IllegalArgumentException("editDistance: Arguments should not be null.");
@ -273,14 +275,29 @@ public class Utils {
} }
for (int i = 0; i < sl; ++i) { for (int i = 0; i < sl; ++i) {
for (int j = 0; j < tl; ++j) { for (int j = 0; j < tl; ++j) {
if (Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(t.charAt(j))) { final char sc = Character.toLowerCase(s.charAt(i));
dp[i + 1][j + 1] = dp[i][j]; final char tc = Character.toLowerCase(t.charAt(j));
} else { final int cost = sc == tc ? 0 : 1;
dp[i + 1][j + 1] = 1 + Math.min(dp[i][j], dp[i + 1][j + 1] = Math.min(
Math.min(dp[i + 1][j], dp[i][j + 1])); dp[i][j + 1] + 1, Math.min(dp[i + 1][j] + 1, dp[i][j] + cost));
// Overwrite for transposition cases
if (i > 0 && j > 0
&& sc == Character.toLowerCase(t.charAt(j - 1))
&& tc == Character.toLowerCase(s.charAt(i - 1))) {
dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i - 1][j - 1] + cost);
} }
} }
} }
if (LatinImeLogger.sDBG) {
Log.d(TAG, "editDistance:" + s + "," + t);
for (int i = 0; i < dp.length; ++i) {
StringBuffer sb = new StringBuffer();
for (int j = 0; j < dp[i].length; ++j) {
sb.append(dp[i][j]).append(',');
}
Log.d(TAG, i + ":" + sb.toString());
}
}
return dp[sl][tl]; return dp[sl][tl];
} }