From 2aa1dd45c44295e2f7e8ece1b520032d86b9f908 Mon Sep 17 00:00:00 2001 From: satok Date: Thu, 13 Oct 2011 14:25:26 +0900 Subject: [PATCH] Fix the safety net Bug: 5453150 Change-Id: I5990feb2622738988bf29843c6bcdb9cbf8bbf33 --- .../android/inputmethod/latin/LatinIME.java | 7 ++++++- .../inputmethod/latin/SuggestedWords.java | 21 ++++++++++++++----- .../inputmethod/latin/SuggestionsView.java | 6 ++++++ .../com/android/inputmethod/latin/Utils.java | 14 ++++++++----- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 3c878d0a8..2fd85894c 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1722,9 +1722,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } public void showSuggestions(SuggestedWords suggestedWords, CharSequence typedWord) { + final boolean shouldBlockAutoCorrectionBySafetyNet = + Utils.shouldBlockAutoCorrectionBySafetyNet(suggestedWords, mSuggest); + if (shouldBlockAutoCorrectionBySafetyNet) { + suggestedWords.setShouldBlockAutoCorrection(); + } setSuggestions(suggestedWords); if (suggestedWords.size() > 0) { - if (Utils.shouldBlockedBySafetyNetForAutoCorrection(suggestedWords, mSuggest)) { + if (shouldBlockAutoCorrectionBySafetyNet) { mBestWord = typedWord; } else if (suggestedWords.hasAutoCorrectionWord()) { mBestWord = suggestedWords.getWord(1); diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index 005db36bd..ed6359cfa 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -29,12 +29,13 @@ public class SuggestedWords { public final List mWords; public final boolean mTypedWordValid; - public final boolean mHasMinimalSuggestion; + public final boolean mHasAutoCorrectionCandidate; public final boolean mIsPunctuationSuggestions; private final List mSuggestedWordInfoList; + private boolean mShouldBlockAutoCorrection; private SuggestedWords(List words, boolean typedWordValid, - boolean hasMinimalSuggestion, boolean isPunctuationSuggestions, + boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions, List suggestedWordInfoList) { if (words != null) { mWords = words; @@ -42,9 +43,10 @@ public class SuggestedWords { mWords = Collections.emptyList(); } mTypedWordValid = typedWordValid; - mHasMinimalSuggestion = hasMinimalSuggestion; + mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate; mIsPunctuationSuggestions = isPunctuationSuggestions; mSuggestedWordInfoList = suggestedWordInfoList; + mShouldBlockAutoCorrection = false; } public int size() { @@ -60,17 +62,25 @@ public class SuggestedWords { } public boolean hasAutoCorrectionWord() { - return mHasMinimalSuggestion && size() > 1 && !mTypedWordValid; + return mHasAutoCorrectionCandidate && size() > 1 && !mTypedWordValid; } public boolean hasWordAboveAutoCorrectionScoreThreshold() { - return mHasMinimalSuggestion && ((size() > 1 && !mTypedWordValid) || mTypedWordValid); + return mHasAutoCorrectionCandidate && ((size() > 1 && !mTypedWordValid) || mTypedWordValid); } public boolean isPunctuationSuggestions() { return mIsPunctuationSuggestions; } + public void setShouldBlockAutoCorrection() { + mShouldBlockAutoCorrection = true; + } + + public boolean shouldBlockAutoCorrection() { + return mShouldBlockAutoCorrection; + } + public static class Builder { private List mWords = new ArrayList(); private boolean mTypedWordValid; @@ -176,6 +186,7 @@ public class SuggestedWords { return mWords.get(pos); } + @Override public String toString() { // Pretty-print method to help debug final StringBuilder sb = new StringBuilder("StringBuilder: mTypedWordValid = " diff --git a/java/src/com/android/inputmethod/latin/SuggestionsView.java b/java/src/com/android/inputmethod/latin/SuggestionsView.java index 3271b8253..937c2c9ff 100644 --- a/java/src/com/android/inputmethod/latin/SuggestionsView.java +++ b/java/src/com/android/inputmethod/latin/SuggestionsView.java @@ -303,6 +303,12 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener, } else { color = mColorTypedWord; } + if (LatinImeLogger.sDBG) { + if (index == mCenterSuggestionIndex && suggestions.mHasAutoCorrectionCandidate + && suggestions.shouldBlockAutoCorrection()) { + return 0xFFFF0000; + } + } final SuggestedWordInfo info = (pos < suggestions.size()) ? suggestions.getInfo(pos) : null; diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index de2930460..f6343f1d7 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -167,7 +167,9 @@ public class Utils { throw new RuntimeException("Can not find input method id for " + packageName); } - public static boolean shouldBlockedBySafetyNetForAutoCorrection(SuggestedWords suggestions, + // TODO: Resolve the inconsistencies between the native auto correction algorithms and + // this safety net + public static boolean shouldBlockAutoCorrectionBySafetyNet(SuggestedWords suggestions, Suggest suggest) { // Safety net for auto correction. // Actually if we hit this safety net, it's actually a bug. @@ -181,7 +183,8 @@ public class Utils { if (typedWord.length() < MINIMUM_SAFETY_NET_CHAR_LENGTH) return false; final CharSequence suggestionWord = suggestions.getWord(1); final int typedWordLength = typedWord.length(); - final int maxEditDistanceOfNativeDictionary = typedWordLength < 5 ? 2 : typedWordLength / 2; + final int maxEditDistanceOfNativeDictionary = + (typedWordLength < 5 ? 2 : typedWordLength / 2) + 1; final int distance = Utils.editDistance(typedWord, suggestionWord); if (DBG) { Log.d(TAG, "Autocorrected edit distance = " + distance @@ -189,8 +192,8 @@ public class Utils { } if (distance > maxEditDistanceOfNativeDictionary) { if (DBG) { - Log.d(TAG, "Safety net: before = " + typedWord + ", after = " + suggestionWord); - Log.w(TAG, "(Error) The edit distance of this correction exceeds limit. " + Log.e(TAG, "Safety net: before = " + typedWord + ", after = " + suggestionWord); + Log.e(TAG, "(Error) The edit distance of this correction exceeds limit. " + "Turning off auto-correction."); } return true; @@ -792,6 +795,7 @@ public class Utils { } public static boolean willAutoCorrect(SuggestedWords suggestions) { - return !suggestions.mTypedWordValid && suggestions.mHasMinimalSuggestion; + return !suggestions.mTypedWordValid && suggestions.mHasAutoCorrectionCandidate + && !suggestions.shouldBlockAutoCorrection(); } }