Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df9fce5d authored by Jean Chalard's avatar Jean Chalard
Browse files

Export some logic out of LatinIME

Hopefully that will end in Suggest/SuggestedWords being autonomous
and won't need the logic spoon-fed to them

Change-Id: I915661bce13c69c8a5b8e5d4a8c41e18fea594cf
parent 0cb7b007
Loading
Loading
Loading
Loading
+3 −20
Original line number Diff line number Diff line
@@ -1823,39 +1823,22 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
        final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(mWordComposer,
                prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), mCorrectionMode);

        // Here, we want to promote a whitelisted word if exists.
        // 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(
                mSuggest.getUnigramDictionaries(),
                // If the typed string ends with a single quote, for dictionary lookup purposes
                // we behave as if the single quote was not here. Here, we are looking up the
                // typed string in the dictionary (to avoid autocorrecting from an existing
                // word, so for consistency this lookup should be made WITHOUT the trailing
                // single quote.
                quotesCount > 0
                        ? typedWord.subSequence(0, typedWord.length() - quotesCount) : typedWord,
                preferCapitalization());

        // Basically, we update the suggestion strip only when suggestion count > 1.  However,
        // there is an exception: We update the suggestion strip whenever typed word's length
        // is 1 or typed word is found in dictionary, regardless of suggestion count.  Actually,
        // in most cases, suggestion count is 1 when typed word's length is 1, but we do always
        // need to clear the previous state when the user starts typing a word (i.e. typed word's
        // length == 1).
        if (builder.size() > 1 || typedWord.length() == 1 || (!allowsToBeAutoCorrected)
        if (builder.size() > 1 || typedWord.length() == 1 || !builder.allowsToBeAutoCorrected()
                || mSuggestionsView.isShowingAddToDictionaryHint()) {
            boolean autoCorrectionAvailable = mSuggest.hasAutoCorrection();
            if (mCorrectionMode == Suggest.CORRECTION_FULL
                    || mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM) {
                autoCorrectionAvailable |= (!allowsToBeAutoCorrected);
                autoCorrectionAvailable |= !builder.allowsToBeAutoCorrected();
            }
            // Don't auto-correct words with multiple capital letter
            autoCorrectionAvailable &= !mWordComposer.isMostlyCaps();
            builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion(
            builder.setTypedWordValid(!builder.allowsToBeAutoCorrected()).setHasMinimalSuggestion(
                    autoCorrectionAvailable);
            if (Suggest.shouldBlockAutoCorrectionBySafetyNet(builder, mSuggest,
                    mSettingsValues.mAutoCorrectionThreshold)) {
+12 −2
Original line number Diff line number Diff line
@@ -282,6 +282,14 @@ public class Suggest implements Dictionary.WordCallback {
                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);
@@ -393,9 +401,11 @@ public class Suggest implements Dictionary.WordCallback {
            for (int i = mScores.length; i < mSuggestions.size(); ++i) {
                scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("--", false));
            }
            return new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList);
            return new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList)
                    .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected);
        }
        return new SuggestedWords.Builder().addWords(mSuggestions, null);
        return new SuggestedWords.Builder().addWords(mSuggestions, null)
                .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected);
    }

    public boolean hasAutoCorrection() {
+10 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ public class SuggestedWords {
        private boolean mHasMinimalSuggestion;
        private boolean mIsPunctuationSuggestions;
        private boolean mShouldBlockAutoCorrectionBySafetyNet;
        private boolean mAllowsToBeAutoCorrected;
        private List<SuggestedWordInfo> mSuggestedWordInfoList =
                new ArrayList<SuggestedWordInfo>();

@@ -159,6 +160,11 @@ public class SuggestedWords {
            return this;
        }

        public Builder setAllowsToBeAutoCorrected(final boolean allowsToBeAutoCorrected) {
            mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
            return this;
        }

        // Should get rid of the first one (what the user typed previously) from suggestions
        // and replace it with what the user currently typed.
        public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
@@ -200,6 +206,10 @@ public class SuggestedWords {
            return mTypedWordValid;
        }

        public boolean allowsToBeAutoCorrected() {
            return mAllowsToBeAutoCorrected;
        }

        @Override
        public String toString() {
            // Pretty-print method to help debug