diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/Utils.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/Utils.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index 753e5d64f..d2582b115 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -36,6 +36,8 @@ import java.text.SimpleDateFormat; import java.util.Date; public class Utils { + private static final String TAG = Utils.class.getSimpleName(); + private static boolean DBG = LatinImeLogger.sDBG; /** * Cancel an {@link AsyncTask}. @@ -95,6 +97,29 @@ public class Utils { || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; } + + public static boolean shouldBlockedBySafetyNetForAutoCorrection(SuggestedWords suggestions) { + // Safety net for auto correction. + // Actually if we hit this safety net, it's actually a bug. + if (suggestions.size() <= 1 || suggestions.mTypedWordValid) return false; + CharSequence typedWord = suggestions.getWord(0); + CharSequence candidateWord = suggestions.getWord(1); + final int typedWordLength = typedWord.length(); + final int maxEditDistanceOfNativeDictionary = typedWordLength < 5 ? 2 : typedWordLength / 2; + final int distance = Utils.editDistance(typedWord, candidateWord); + if (DBG) { + Log.d(TAG, "Autocorrected edit distance = " + distance + + ", " + maxEditDistanceOfNativeDictionary); + } + if (distance > maxEditDistanceOfNativeDictionary) { + Log.w(TAG, "(Error) The edit distance of this correction exceeds limit. " + + "Turning off auto-correction."); + return true; + } else { + return false; + } + } + /* package */ static class RingCharBuffer { private static RingCharBuffer sRingCharBuffer = new RingCharBuffer(); private static final char PLACEHOLDER_DELIMITER_CHAR = '\uFFFC'; |