diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitator.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitator.java index dfcba0f70..99722446a 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitator.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitator.java @@ -149,6 +149,8 @@ void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized, @NonNull final NgramContext ngramContext, final long timeStampInSeconds, final boolean blockPotentiallyOffensive); + void adjustConfidences(final String word, final boolean wasAutoCapitalized); + void unlearnFromUserHistory(final String word, @NonNull final NgramContext ngramContext, final long timeStampInSeconds, final int eventType); diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java index c0d79430f..265b20d06 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java @@ -610,21 +610,7 @@ public void addToUserHistory(final String suggestion, final boolean wasAutoCapit // increase / decrease confidence if we have more than one dictionary group boolean[] validWordForDictionary; // store results to avoid unnecessary duplicate lookups if (mDictionaryGroups.size() > 1 && words.length == 1) { // ignore if more than a single word, this only happens with (badly working) spaceAwareGesture - validWordForDictionary = new boolean[mDictionaryGroups.size()]; - // if suggestion was auto-capitalized, check against both the suggestion and the de-capitalized suggestion - final String decapitalizedSuggestion; - if (wasAutoCapitalized) - decapitalizedSuggestion = suggestion.substring(0, 1).toLowerCase() + suggestion.substring(1); - else - decapitalizedSuggestion = suggestion; - for (int i = 0; i < mDictionaryGroups.size(); i ++) { - final DictionaryGroup dictionaryGroup = mDictionaryGroups.get(i); - final boolean isValidWord = isValidWord(suggestion, ALL_DICTIONARY_TYPES, dictionaryGroup); - if (isValidWord || (wasAutoCapitalized && isValidWord(decapitalizedSuggestion, ALL_DICTIONARY_TYPES, dictionaryGroup))) - dictionaryGroup.increaseConfidence(); - else dictionaryGroup.decreaseConfidence(); - validWordForDictionary[i] = isValidWord; - } + validWordForDictionary = adjustConfidencesInternal(suggestion, wasAutoCapitalized); } else validWordForDictionary = null; @@ -656,6 +642,30 @@ public void addToUserHistory(final String suggestion, final boolean wasAutoCapit } } + @Override public void adjustConfidences(final String word, final boolean wasAutoCapitalized) { + if (mDictionaryGroups.size() > 1 && !word.contains(Constants.WORD_SEPARATOR)) + adjustConfidencesInternal(word, wasAutoCapitalized); + } + + private boolean[] adjustConfidencesInternal(final String word, final boolean wasAutoCapitalized) { + final boolean[] validWordForDictionary = new boolean[mDictionaryGroups.size()]; + // if suggestion was auto-capitalized, check against both the suggestion and the de-capitalized suggestion + final String decapitalizedSuggestion; + if (wasAutoCapitalized) + decapitalizedSuggestion = word.substring(0, 1).toLowerCase() + word.substring(1); + else + decapitalizedSuggestion = word; + for (int i = 0; i < mDictionaryGroups.size(); i ++) { + final DictionaryGroup dictionaryGroup = mDictionaryGroups.get(i); + final boolean isValidWord = isValidWord(word, ALL_DICTIONARY_TYPES, dictionaryGroup); + if (isValidWord || (wasAutoCapitalized && isValidWord(decapitalizedSuggestion, ALL_DICTIONARY_TYPES, dictionaryGroup))) + dictionaryGroup.increaseConfidence(); + else dictionaryGroup.decreaseConfidence(); + validWordForDictionary[i] = isValidWord; + } + return validWordForDictionary; + } + // main and secondary isValid provided to avoid duplicate lookups private void addToPersonalDictionaryIfInvalidButInHistory(String suggestion, boolean[] validWordForDictionary) { final DictionaryGroup dictionaryGroup = getClearlyPreferredDictionaryGroupOrNull(); diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java index 83bbe055c..7eaa5b977 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java @@ -1535,20 +1535,21 @@ private void performAdditionToUserHistoryDictionary(final SettingsValues setting // That's to avoid unintended additions in some sensitive fields, or fields that // expect to receive non-words. // mInputTypeNoAutoCorrect changed to !mShouldShowSuggestions because this was cancelling learning way too often - if (!settingsValues.mInputAttributes.mShouldShowSuggestions || settingsValues.mIncognitoModeEnabled) + if (!settingsValues.mInputAttributes.mShouldShowSuggestions || settingsValues.mIncognitoModeEnabled || TextUtils.isEmpty(suggestion)) return; + final boolean wasAutoCapitalized = mWordComposer.wasAutoCapitalized() && !mWordComposer.isMostlyCaps(); + final String word = stripWordSeparatorsFromEnd(suggestion, settingsValues); if (mConnection.hasSlowInputConnection()) { // Since we don't unlearn when the user backspaces on a slow InputConnection, // turn off learning to guard against adding typos that the user later deletes. Log.w(TAG, "Skipping learning due to slow InputConnection."); + // but we still want to adjust confidences for multilingual typing + mDictionaryFacilitator.adjustConfidences(word, wasAutoCapitalized); return; } - - if (TextUtils.isEmpty(suggestion)) return; - final boolean wasAutoCapitalized = mWordComposer.wasAutoCapitalized() && !mWordComposer.isMostlyCaps(); final int timeStampInSeconds = (int)TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); - mDictionaryFacilitator.addToUserHistory(stripWordSeparatorsFromEnd(suggestion, settingsValues), wasAutoCapitalized, - ngramContext, timeStampInSeconds, settingsValues.mBlockPotentiallyOffensive); + mDictionaryFacilitator.addToUserHistory(word, wasAutoCapitalized, ngramContext, + timeStampInSeconds, settingsValues.mBlockPotentiallyOffensive); } // strip word separators from end (may be necessary for urls, e.g. when the user has typed