Make access to the safety net calculation easier

Change-Id: If33897b38af94ae81b05e5ef140a542724b9f34a
This commit is contained in:
Jean Chalard 2012-03-09 21:44:40 +09:00
parent 151d5cb532
commit 682c2d53cc

View File

@ -455,7 +455,7 @@ public class Suggest implements Dictionary.WordCallback {
builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion( builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion(
autoCorrectionAvailable); autoCorrectionAvailable);
if (allowsToBeAutoCorrected && builder.size() > 1 && mAutoCorrectionThreshold > 0 if (allowsToBeAutoCorrected && builder.size() > 1 && mAutoCorrectionThreshold > 0
&& Suggest.shouldBlockAutoCorrectionBySafetyNet(builder)) { && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord, builder.getWord(1))) {
builder.setShouldBlockAutoCorrectionBySafetyNet(); builder.setShouldBlockAutoCorrectionBySafetyNet();
} }
return builder; return builder;
@ -608,31 +608,28 @@ public class Suggest implements Dictionary.WordCallback {
// TODO: Resolve the inconsistencies between the native auto correction algorithms and // TODO: Resolve the inconsistencies between the native auto correction algorithms and
// this safety net // this safety net
public static boolean shouldBlockAutoCorrectionBySafetyNet( public static boolean shouldBlockAutoCorrectionBySafetyNet(final String typedWord,
final SuggestedWords.Builder suggestedWordsBuilder) { final CharSequence suggestion) {
// Safety net for auto correction. // Safety net for auto correction.
// Actually if we hit this safety net, it's a bug. // Actually if we hit this safety net, it's a bug.
// If user selected aggressive auto correction mode, there is no need to use the safety // If user selected aggressive auto correction mode, there is no need to use the safety
// net. // net.
final CharSequence typedWord = suggestedWordsBuilder.getWord(0);
// If the length of typed word is less than MINIMUM_SAFETY_NET_CHAR_LENGTH, // If the length of typed word is less than MINIMUM_SAFETY_NET_CHAR_LENGTH,
// we should not use net because relatively edit distance can be big. // we should not use net because relatively edit distance can be big.
if (typedWord.length() < Suggest.MINIMUM_SAFETY_NET_CHAR_LENGTH) { final int typedWordLength = typedWord.length();
if (typedWordLength < Suggest.MINIMUM_SAFETY_NET_CHAR_LENGTH) {
return false; return false;
} }
final CharSequence suggestionWord = suggestedWordsBuilder.getWord(1);
final int typedWordLength = typedWord.length();
final int maxEditDistanceOfNativeDictionary = final int maxEditDistanceOfNativeDictionary =
(typedWordLength < 5 ? 2 : typedWordLength / 2) + 1; (typedWordLength < 5 ? 2 : typedWordLength / 2) + 1;
final int distance = BinaryDictionary.editDistance( final int distance = BinaryDictionary.editDistance(typedWord, suggestion.toString());
typedWord.toString(), suggestionWord.toString());
if (DBG) { if (DBG) {
Log.d(TAG, "Autocorrected edit distance = " + distance Log.d(TAG, "Autocorrected edit distance = " + distance
+ ", " + maxEditDistanceOfNativeDictionary); + ", " + maxEditDistanceOfNativeDictionary);
} }
if (distance > maxEditDistanceOfNativeDictionary) { if (distance > maxEditDistanceOfNativeDictionary) {
if (DBG) { if (DBG) {
Log.e(TAG, "Safety net: before = " + typedWord + ", after = " + suggestionWord); Log.e(TAG, "Safety net: before = " + typedWord + ", after = " + suggestion);
Log.e(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.");
} }