am e752aab7: Use confidence to fix whitelist

* commit 'e752aab70dc15c993a65d7db8314a72bb9e0f8b2':
  Use confidence to fix whitelist
main
Jean Chalard 2014-11-26 08:37:27 +00:00 committed by Android Git Automerger
commit 9ab179a142
2 changed files with 29 additions and 4 deletions

View File

@ -63,6 +63,9 @@ public class DictionaryFacilitator {
// HACK: This threshold is being used when adding a capitalized entry in the User History
// dictionary.
private static final int CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT = 140;
// How many words we need to type in a row ({@see mConfidenceInMostProbableLanguage}) to
// declare we are confident the user is typing in the most probable language.
private static final int CONFIDENCE_THRESHOLD = 3;
private DictionaryGroup[] mDictionaryGroups = new DictionaryGroup[] { new DictionaryGroup() };
private DictionaryGroup mMostProbableDictionaryGroup = mDictionaryGroups[0];
@ -293,6 +296,14 @@ public class DictionaryFacilitator {
}
}
public boolean isConfidentAboutCurrentLanguageBeing(final Locale mLocale) {
final DictionaryGroup mostProbableDictionaryGroup = mMostProbableDictionaryGroup;
if (!mostProbableDictionaryGroup.mLocale.equals(mLocale)) {
return false;
}
return mostProbableDictionaryGroup.mConfidence >= CONFIDENCE_THRESHOLD;
}
@Nullable
private static ExpandableBinaryDictionary getSubDict(final String dictType,
final Context context, final Locale locale, final File dictFile,
@ -634,7 +645,8 @@ public class DictionaryFacilitator {
final int timeStampInSeconds, final boolean blockPotentiallyOffensive) {
final ExpandableBinaryDictionary userHistoryDictionary =
dictionaryGroup.getSubDict(Dictionary.TYPE_USER_HISTORY);
if (userHistoryDictionary == null) {
if (userHistoryDictionary == null
|| !isConfidentAboutCurrentLanguageBeing(userHistoryDictionary.mLocale)) {
return;
}
final int maxFreq = getFrequency(word);

View File

@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
@ -117,7 +118,8 @@ public final class Suggest {
return suggestionsContainer;
}
private static String getWhitelistedWordOrNull(final ArrayList<SuggestedWordInfo> suggestions) {
private static SuggestedWordInfo getWhitelistedWordInfoOrNull(
@Nonnull final ArrayList<SuggestedWordInfo> suggestions) {
if (suggestions.isEmpty()) {
return null;
}
@ -125,7 +127,7 @@ public final class Suggest {
if (!firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) {
return null;
}
return firstSuggestedWordInfo.mWord;
return firstSuggestedWordInfo;
}
// Retrieves suggestions for non-batch input (typing, recorrection, predictions...)
@ -156,7 +158,18 @@ public final class Suggest {
SuggestedWordInfo.removeDupsAndReturnSourceOfTypedWord(wordComposer.getTypedWord(),
mostProbableLocale /* preferredLocale */, suggestionsContainer);
final String whitelistedWord = getWhitelistedWordOrNull(suggestionsContainer);
final SuggestedWordInfo whitelistedWordInfo =
getWhitelistedWordInfoOrNull(suggestionsContainer);
final String whitelistedWord;
if (null != whitelistedWordInfo &&
mDictionaryFacilitator.isConfidentAboutCurrentLanguageBeing(
whitelistedWordInfo.mSourceDict.mLocale)) {
whitelistedWord = whitelistedWordInfo.mWord;
} else {
// Even if we have a whitelist candidate, we don't use it unless we are confident
// the user is typing in the language this whitelist candidate comes from.
whitelistedWord = null;
}
final boolean resultsArePredictions = !wordComposer.isComposingWord();
// We allow auto-correction if we have a whitelisted word, or if the word had more than