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

Commit d67fe0e7 authored by Amith Yamasani's avatar Amith Yamasani Committed by Jean-Baptiste Queru
Browse files

Improve auto-add heuristics.

Also add auto-switch back to alphabet mode on pressing enter key.
parent 4a7ff90d
Loading
Loading
Loading
Loading
+6 −4
Original line number Original line Diff line number Diff line
@@ -245,14 +245,16 @@ public class KeyboardSwitcher {
     * Returns true if the keyboard needs to switch back 
     * Returns true if the keyboard needs to switch back 
     */
     */
    boolean onKey(int key) {
    boolean onKey(int key) {
        // Switch back to alpha mode if user types one or more non-space characters followed by
        // Switch back to alpha mode if user types one or more non-space/enter characters
        // a space.
        // followed by a space/enter
        switch (mSymbolsModeState) {
        switch (mSymbolsModeState) {
            case SYMBOLS_MODE_STATE_BEGIN:
            case SYMBOLS_MODE_STATE_BEGIN:
                if (key != ' ' && key > 0) mSymbolsModeState = SYMBOLS_MODE_STATE_SYMBOL;
                if (key != LatinIME.KEYCODE_SPACE && key != LatinIME.KEYCODE_ENTER && key > 0) {
                    mSymbolsModeState = SYMBOLS_MODE_STATE_SYMBOL;
                }
                break;
                break;
            case SYMBOLS_MODE_STATE_SYMBOL:
            case SYMBOLS_MODE_STATE_SYMBOL:
                if (key == ' ') return true;
                if (key == LatinIME.KEYCODE_ENTER || key == LatinIME.KEYCODE_SPACE) return true;
                break;
                break;
        }
        }
        return false;
        return false;
+21 −11
Original line number Original line Diff line number Diff line
@@ -77,9 +77,16 @@ public class LatinIME extends InputMethodService
    private static final int DELETE_ACCELERATE_AT = 20;
    private static final int DELETE_ACCELERATE_AT = 20;
    // Key events coming any faster than this are long-presses.
    // Key events coming any faster than this are long-presses.
    private static final int QUICK_PRESS = 200;
    private static final int QUICK_PRESS = 200;
    // Weight added to a user picking a new word from the suggestion strip
    static final int FREQUENCY_FOR_PICKED = 3;
    // Weight added to a user typing a new word that doesn't get corrected (or is reverted)
    static final int FREQUENCY_FOR_TYPED = 1;
    // A word that is frequently typed and get's promoted to the user dictionary, uses this
    // frequency.
    static final int FREQUENCY_FOR_AUTO_ADD = 250;
    
    
    private static final int KEYCODE_ENTER = 10;
    static final int KEYCODE_ENTER = '\n';
    private static final int KEYCODE_SPACE = ' ';
    static final int KEYCODE_SPACE = ' ';


    // Contextual menu positions
    // Contextual menu positions
    private static final int POS_SETTINGS = 0;
    private static final int POS_SETTINGS = 0;
@@ -481,7 +488,7 @@ public class LatinIME extends InputMethodService
                }
                }
                mCommittedLength = mComposing.length();
                mCommittedLength = mComposing.length();
                TextEntryState.acceptedTyped(mComposing);
                TextEntryState.acceptedTyped(mComposing);
                mAutoDictionary.addWord(mComposing.toString(), 1);
                mAutoDictionary.addWord(mComposing.toString(), FREQUENCY_FOR_TYPED);
            }
            }
            updateSuggestions();
            updateSuggestions();
        }
        }
@@ -845,7 +852,7 @@ public class LatinIME extends InputMethodService
        }
        }
        // Add the word to the auto dictionary if it's not a known word
        // Add the word to the auto dictionary if it's not a known word
        if (mAutoDictionary.isValidWord(suggestion) || !mSuggest.isValidWord(suggestion)) {
        if (mAutoDictionary.isValidWord(suggestion) || !mSuggest.isValidWord(suggestion)) {
            mAutoDictionary.addWord(suggestion.toString(), 1);
            mAutoDictionary.addWord(suggestion.toString(), FREQUENCY_FOR_PICKED);
        }
        }
        mPredicting = false;
        mPredicting = false;
        mCommittedLength = suggestion.length();
        mCommittedLength = suggestion.length();
@@ -1141,8 +1148,10 @@ public class LatinIME extends InputMethodService
    }
    }


    class AutoDictionary extends ExpandableDictionary {
    class AutoDictionary extends ExpandableDictionary {
        private static final int VALIDITY_THRESHOLD = 3;
        // If the user touches a typed word 2 times or more, it will become valid.
        private static final int PROMOTION_THRESHOLD = 6;
        private static final int VALIDITY_THRESHOLD = 2 * FREQUENCY_FOR_PICKED;
        // If the user touches a typed word 5 times or more, it will be added to the user dict.
        private static final int PROMOTION_THRESHOLD = 5 * FREQUENCY_FOR_PICKED;


        public AutoDictionary(Context context) {
        public AutoDictionary(Context context) {
            super(context);
            super(context);
@@ -1155,10 +1164,11 @@ public class LatinIME extends InputMethodService
        }
        }


        @Override
        @Override
        public void addWord(String word, int frequency) {
        public void addWord(String word, int addFrequency) {
            super.addWord(word, 1);
            super.addWord(word, addFrequency);
            if (getWordFrequency(word) > PROMOTION_THRESHOLD) {
            final int freq = getWordFrequency(word);
                LatinIME.this.promoteToUserDictionary(word, frequency);
            if (freq > PROMOTION_THRESHOLD) {
                LatinIME.this.promoteToUserDictionary(word, FREQUENCY_FOR_AUTO_ADD);
            }
            }
        }
        }
    }
    }