Skip to content

Commit

Permalink
still adjust multilingual typing confidences on slow input connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 committed Dec 20, 2023
1 parent 8d3a32b commit 228f859
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 228f859

Please sign in to comment.