diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/Suggest.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/Suggest.java | 103 |
1 files changed, 52 insertions, 51 deletions
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 3089625e7..69754d769 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -22,6 +22,7 @@ import android.util.Log; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.ProximityInfo; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.io.File; import java.util.ArrayList; @@ -273,7 +274,8 @@ public class Suggest implements Dictionary.WordCallback { false /* typedWordValid */, false /* hasAutoCorrectionCandidate */, false /* allowsToBeAutoCorrected */, - false /* isPunctuationSuggestions */); + false /* isPunctuationSuggestions */, + false /* isObsoleteSuggestions */); } // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder @@ -292,18 +294,9 @@ public class Suggest implements Dictionary.WordCallback { ? typedWord.substring(0, typedWord.length() - mTrailingSingleQuotesCount) : typedWord; // Treating USER_TYPED as UNIGRAM suggestion for logging now. - LatinImeLogger.onAddSuggestedWord(typedWord, Suggest.DIC_USER_TYPED, - Dictionary.UNIGRAM); + LatinImeLogger.onAddSuggestedWord(typedWord, Suggest.DIC_USER_TYPED, Dictionary.UNIGRAM); mConsideredWord = consideredWord; - // TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid" - // but still autocorrected from - in the case the whitelist only capitalizes the word. - // The whitelist should be case-insensitive, so it's not possible to be consistent with - // a boolean flag. Right now this is handled with a slight hack in - // WhitelistDictionary#shouldForciblyAutoCorrectFrom. - final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected( - getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized()); - if (wordComposer.size() <= 1 && (correctionMode == CORRECTION_FULL_BIGRAM)) { // At first character typed, search only the bigrams Arrays.fill(mBigramScores, 0); @@ -362,12 +355,11 @@ public class Suggest implements Dictionary.WordCallback { } } - CharSequence whitelistedWord = capitalizeWord(mIsAllUpperCase, mIsFirstCharCapitalized, - mWhiteListDictionary.getWhitelistedWord(consideredWord)); + final CharSequence whitelistedWord = capitalizeWord(mIsAllUpperCase, + mIsFirstCharCapitalized, mWhiteListDictionary.getWhitelistedWord(consideredWord)); final boolean hasAutoCorrection; - if (CORRECTION_FULL == correctionMode - || CORRECTION_FULL_BIGRAM == correctionMode) { + if (CORRECTION_FULL == correctionMode || CORRECTION_FULL_BIGRAM == correctionMode) { final CharSequence autoCorrection = AutoCorrection.computeAutoCorrectionWord(mUnigramDictionaries, wordComposer, mSuggestions, mScores, consideredWord, mAutoCorrectionThreshold, @@ -392,59 +384,68 @@ public class Suggest implements Dictionary.WordCallback { mSuggestions.add(0, typedWord); StringUtils.removeDupes(mSuggestions); - final ArrayList<SuggestedWords.SuggestedWordInfo> suggestionsList; + final ArrayList<SuggestedWordInfo> suggestionsList; if (DBG) { - // TODO: this doesn't take into account the fact that removing dupes from mSuggestions - // may have made mScores[] and mSuggestions out of sync. - final CharSequence autoCorrectionSuggestion = mSuggestions.get(0); - double normalizedScore = BinaryDictionary.calcNormalizedScore( - typedWord, autoCorrectionSuggestion.toString(), mScores[0]); - suggestionsList = new ArrayList<SuggestedWords.SuggestedWordInfo>(); - suggestionsList.add(new SuggestedWords.SuggestedWordInfo(autoCorrectionSuggestion, "+", - false)); - final int suggestionsSize = mSuggestions.size(); - // Note: i here is the index in mScores[], but the index in mSuggestions is one more - // than i because we added the typed word to mSuggestions without touching mScores. - for (int i = 0; i < mScores.length && i < suggestionsSize - 1; ++i) { - final String scoreInfoString; - if (normalizedScore > 0) { - scoreInfoString = String.format("%d (%4.2f)", mScores[i], normalizedScore); - normalizedScore = 0.0; - } else { - scoreInfoString = Integer.toString(mScores[i]); - } - suggestionsList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i + 1), - scoreInfoString, false)); - } - for (int i = mScores.length; i < suggestionsSize; ++i) { - suggestionsList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i), - "--", false)); - } + suggestionsList = getSuggestionsInfoListWithDebugInfo(typedWord, mSuggestions, mScores); } else { suggestionsList = SuggestedWords.getFromCharSequenceList(mSuggestions); } + // TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid" + // but still autocorrected from - in the case the whitelist only capitalizes the word. + // The whitelist should be case-insensitive, so it's not possible to be consistent with + // a boolean flag. Right now this is handled with a slight hack in + // WhitelistDictionary#shouldForciblyAutoCorrectFrom. + final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected( + getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized()); + boolean autoCorrectionAvailable = hasAutoCorrection; - if (correctionMode == Suggest.CORRECTION_FULL - || correctionMode == Suggest.CORRECTION_FULL_BIGRAM) { + if (correctionMode == CORRECTION_FULL || correctionMode == CORRECTION_FULL_BIGRAM) { autoCorrectionAvailable |= !allowsToBeAutoCorrected; } // Don't auto-correct words with multiple capital letter autoCorrectionAvailable &= !wordComposer.isMostlyCaps(); - final boolean shouldBlockAutoCorrectionBySatefyNet; if (allowsToBeAutoCorrected && suggestionsList.size() > 1 && mAutoCorrectionThreshold > 0 && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord, suggestionsList.get(1).mWord)) { - shouldBlockAutoCorrectionBySatefyNet = true; - } else { - shouldBlockAutoCorrectionBySatefyNet = false; + autoCorrectionAvailable = false; } return new SuggestedWords(suggestionsList, !allowsToBeAutoCorrected /* typedWordValid */, - autoCorrectionAvailable & !shouldBlockAutoCorrectionBySatefyNet - /* hasAutoCorrectionCandidate */, + autoCorrectionAvailable /* hasAutoCorrectionCandidate */, allowsToBeAutoCorrected /* allowsToBeAutoCorrected */, - false /* isPunctuationSuggestions */); + false /* isPunctuationSuggestions */, + false /* isObsoleteSuggestions */); + } + + // This assumes the scores[] array is at least as long as suggestions.size() - 1. + private static ArrayList<SuggestedWordInfo> getSuggestionsInfoListWithDebugInfo( + final String typedWord, final ArrayList<CharSequence> suggestions, final int[] scores) { + // TODO: this doesn't take into account the fact that removing dupes from mSuggestions + // may have made mScores[] and mSuggestions out of sync. + final CharSequence autoCorrectionSuggestion = suggestions.get(0); + double normalizedScore = BinaryDictionary.calcNormalizedScore( + typedWord, autoCorrectionSuggestion.toString(), scores[0]); + final int suggestionsSize = suggestions.size(); + final ArrayList<SuggestedWordInfo> suggestionsList = + new ArrayList<SuggestedWordInfo>(suggestionsSize); + suggestionsList.add(new SuggestedWordInfo(autoCorrectionSuggestion, "+")); + // Note: i here is the index in mScores[], but the index in mSuggestions is one more + // than i because we added the typed word to mSuggestions without touching mScores. + for (int i = 0; i < scores.length && i < suggestionsSize - 1; ++i) { + final String scoreInfoString; + if (normalizedScore > 0) { + scoreInfoString = String.format("%d (%4.2f)", scores[i], normalizedScore); + normalizedScore = 0.0; + } else { + scoreInfoString = Integer.toString(scores[i]); + } + suggestionsList.add(new SuggestedWordInfo(suggestions.get(i + 1), scoreInfoString)); + } + for (int i = scores.length; i < suggestionsSize; ++i) { + suggestionsList.add(new SuggestedWordInfo(suggestions.get(i), "--")); + } + return suggestionsList; } @Override |