Fix the safety net

Bug: 5453150

Change-Id: I5990feb2622738988bf29843c6bcdb9cbf8bbf33
main
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) {
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);

View File

@ -29,12 +29,13 @@ public class SuggestedWords {
public final List<CharSequence> mWords;
public final boolean mTypedWordValid;
public final boolean mHasMinimalSuggestion;
public final boolean mHasAutoCorrectionCandidate;
public final boolean mIsPunctuationSuggestions;
private final List<SuggestedWordInfo> mSuggestedWordInfoList;
private boolean mShouldBlockAutoCorrection;
private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
boolean hasMinimalSuggestion, boolean isPunctuationSuggestions,
boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
List<SuggestedWordInfo> 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<CharSequence> mWords = new ArrayList<CharSequence>();
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 = "

View File

@ -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;

View File

@ -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();
}
}