Fix the safety net

Bug: 5453150

Change-Id: I5990feb2622738988bf29843c6bcdb9cbf8bbf33
This commit is contained in:
satok 2011-10-13 14:25:26 +09:00
parent ef0a7b7546
commit 2aa1dd45c4
4 changed files with 37 additions and 11 deletions

View file

@ -1722,9 +1722,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
public void showSuggestions(SuggestedWords suggestedWords, CharSequence typedWord) { public void showSuggestions(SuggestedWords suggestedWords, CharSequence typedWord) {
final boolean shouldBlockAutoCorrectionBySafetyNet =
Utils.shouldBlockAutoCorrectionBySafetyNet(suggestedWords, mSuggest);
if (shouldBlockAutoCorrectionBySafetyNet) {
suggestedWords.setShouldBlockAutoCorrection();
}
setSuggestions(suggestedWords); setSuggestions(suggestedWords);
if (suggestedWords.size() > 0) { if (suggestedWords.size() > 0) {
if (Utils.shouldBlockedBySafetyNetForAutoCorrection(suggestedWords, mSuggest)) { if (shouldBlockAutoCorrectionBySafetyNet) {
mBestWord = typedWord; mBestWord = typedWord;
} else if (suggestedWords.hasAutoCorrectionWord()) { } else if (suggestedWords.hasAutoCorrectionWord()) {
mBestWord = suggestedWords.getWord(1); mBestWord = suggestedWords.getWord(1);

View file

@ -29,12 +29,13 @@ public class SuggestedWords {
public final List<CharSequence> mWords; public final List<CharSequence> mWords;
public final boolean mTypedWordValid; public final boolean mTypedWordValid;
public final boolean mHasMinimalSuggestion; public final boolean mHasAutoCorrectionCandidate;
public final boolean mIsPunctuationSuggestions; public final boolean mIsPunctuationSuggestions;
private final List<SuggestedWordInfo> mSuggestedWordInfoList; private final List<SuggestedWordInfo> mSuggestedWordInfoList;
private boolean mShouldBlockAutoCorrection;
private SuggestedWords(List<CharSequence> words, boolean typedWordValid, private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
boolean hasMinimalSuggestion, boolean isPunctuationSuggestions, boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
List<SuggestedWordInfo> suggestedWordInfoList) { List<SuggestedWordInfo> suggestedWordInfoList) {
if (words != null) { if (words != null) {
mWords = words; mWords = words;
@ -42,9 +43,10 @@ public class SuggestedWords {
mWords = Collections.emptyList(); mWords = Collections.emptyList();
} }
mTypedWordValid = typedWordValid; mTypedWordValid = typedWordValid;
mHasMinimalSuggestion = hasMinimalSuggestion; mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate;
mIsPunctuationSuggestions = isPunctuationSuggestions; mIsPunctuationSuggestions = isPunctuationSuggestions;
mSuggestedWordInfoList = suggestedWordInfoList; mSuggestedWordInfoList = suggestedWordInfoList;
mShouldBlockAutoCorrection = false;
} }
public int size() { public int size() {
@ -60,17 +62,25 @@ public class SuggestedWords {
} }
public boolean hasAutoCorrectionWord() { public boolean hasAutoCorrectionWord() {
return mHasMinimalSuggestion && size() > 1 && !mTypedWordValid; return mHasAutoCorrectionCandidate && size() > 1 && !mTypedWordValid;
} }
public boolean hasWordAboveAutoCorrectionScoreThreshold() { public boolean hasWordAboveAutoCorrectionScoreThreshold() {
return mHasMinimalSuggestion && ((size() > 1 && !mTypedWordValid) || mTypedWordValid); return mHasAutoCorrectionCandidate && ((size() > 1 && !mTypedWordValid) || mTypedWordValid);
} }
public boolean isPunctuationSuggestions() { public boolean isPunctuationSuggestions() {
return mIsPunctuationSuggestions; return mIsPunctuationSuggestions;
} }
public void setShouldBlockAutoCorrection() {
mShouldBlockAutoCorrection = true;
}
public boolean shouldBlockAutoCorrection() {
return mShouldBlockAutoCorrection;
}
public static class Builder { public static class Builder {
private List<CharSequence> mWords = new ArrayList<CharSequence>(); private List<CharSequence> mWords = new ArrayList<CharSequence>();
private boolean mTypedWordValid; private boolean mTypedWordValid;
@ -176,6 +186,7 @@ public class SuggestedWords {
return mWords.get(pos); return mWords.get(pos);
} }
@Override
public String toString() { public String toString() {
// Pretty-print method to help debug // Pretty-print method to help debug
final StringBuilder sb = new StringBuilder("StringBuilder: mTypedWordValid = " final StringBuilder sb = new StringBuilder("StringBuilder: mTypedWordValid = "

View file

@ -303,6 +303,12 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener,
} else { } else {
color = mColorTypedWord; color = mColorTypedWord;
} }
if (LatinImeLogger.sDBG) {
if (index == mCenterSuggestionIndex && suggestions.mHasAutoCorrectionCandidate
&& suggestions.shouldBlockAutoCorrection()) {
return 0xFFFF0000;
}
}
final SuggestedWordInfo info = (pos < suggestions.size()) final SuggestedWordInfo info = (pos < suggestions.size())
? suggestions.getInfo(pos) : null; ? suggestions.getInfo(pos) : null;

View file

@ -167,7 +167,9 @@ public class Utils {
throw new RuntimeException("Can not find input method id for " + packageName); 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) { Suggest suggest) {
// Safety net for auto correction. // Safety net for auto correction.
// Actually if we hit this safety net, it's actually a bug. // 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; if (typedWord.length() < MINIMUM_SAFETY_NET_CHAR_LENGTH) return false;
final CharSequence suggestionWord = suggestions.getWord(1); final CharSequence suggestionWord = suggestions.getWord(1);
final int typedWordLength = typedWord.length(); 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); final int distance = Utils.editDistance(typedWord, suggestionWord);
if (DBG) { if (DBG) {
Log.d(TAG, "Autocorrected edit distance = " + distance Log.d(TAG, "Autocorrected edit distance = " + distance
@ -189,8 +192,8 @@ public class Utils {
} }
if (distance > maxEditDistanceOfNativeDictionary) { if (distance > maxEditDistanceOfNativeDictionary) {
if (DBG) { if (DBG) {
Log.d(TAG, "Safety net: before = " + typedWord + ", after = " + suggestionWord); Log.e(TAG, "Safety net: before = " + typedWord + ", after = " + suggestionWord);
Log.w(TAG, "(Error) The edit distance of this correction exceeds limit. " Log.e(TAG, "(Error) The edit distance of this correction exceeds limit. "
+ "Turning off auto-correction."); + "Turning off auto-correction.");
} }
return true; return true;
@ -792,6 +795,7 @@ public class Utils {
} }
public static boolean willAutoCorrect(SuggestedWords suggestions) { public static boolean willAutoCorrect(SuggestedWords suggestions) {
return !suggestions.mTypedWordValid && suggestions.mHasMinimalSuggestion; return !suggestions.mTypedWordValid && suggestions.mHasAutoCorrectionCandidate
&& !suggestions.shouldBlockAutoCorrection();
} }
} }