get secondary dictionary suggestions in separate thread

This commit is contained in:
Helium 2022-03-25 07:33:10 +01:00
parent b237346dad
commit 6afc90c014

View file

@ -719,47 +719,67 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
false /* firstSuggestionExceedsConfidenceThreshold */); false /* firstSuggestionExceedsConfidenceThreshold */);
final float[] weightOfLangModelVsSpatialModel = final float[] weightOfLangModelVsSpatialModel =
new float[] { Dictionary.NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL }; new float[] { Dictionary.NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL };
for (final String dictType : ALL_DICTIONARY_TYPES) {
final Dictionary dictionary = mDictionaryGroup.getDict(dictType); // start getting suggestions for secondary locale first, but in separate thread
if (null == dictionary) continue; final ArrayList<SuggestedWordInfo> dictionarySuggestionsSecondary = new ArrayList<>();
final float weightForLocale = composedData.mIsBatchMode final CountDownLatch waitForSecondaryDictionary = new CountDownLatch(1);
? mDictionaryGroup.mWeightForGesturingInLocale if (mSecondaryDictionaryGroup != null) {
: mDictionaryGroup.mWeightForTypingInLocale; ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD).execute(new Runnable() {
final ArrayList<SuggestedWordInfo> dictionarySuggestions = @Override
dictionary.getSuggestions(composedData, ngramContext, public void run() {
proximityInfoHandle, settingsValuesForSuggestion, sessionId, dictionarySuggestionsSecondary.addAll(getSuggestions(composedData,
weightForLocale, weightOfLangModelVsSpatialModel); ngramContext, settingsValuesForSuggestion, sessionId, proximityInfoHandle,
if (null == dictionarySuggestions) continue; weightOfLangModelVsSpatialModel, mSecondaryDictionaryGroup));
waitForSecondaryDictionary.countDown();
}
});
}
// get main locale suggestions
final ArrayList<SuggestedWordInfo> dictionarySuggestions = getSuggestions(composedData,
ngramContext, settingsValuesForSuggestion, sessionId, proximityInfoHandle,
weightOfLangModelVsSpatialModel, mDictionaryGroup);
suggestionResults.addAll(dictionarySuggestions); suggestionResults.addAll(dictionarySuggestions);
if (null != suggestionResults.mRawSuggestions) { if (null != suggestionResults.mRawSuggestions) {
suggestionResults.mRawSuggestions.addAll(dictionarySuggestions); suggestionResults.mRawSuggestions.addAll(dictionarySuggestions);
} }
// wait for secondary locale suggestions
if (mSecondaryDictionaryGroup != null) {
try { waitForSecondaryDictionary.await(); }
catch (InterruptedException e) {
Log.w(TAG, "Interrupted while trying to get secondary locale suggestions", e);
} }
// now same for secondary dictionary group suggestionResults.addAll(dictionarySuggestionsSecondary);
// todo: performance... second check is usually faster than 1st one
// (or it's just dictionary size)
// but still may add up to 100 ms on S4 mini (average ca 30)
// relative: +70% compared to only one dictionary group
if (mSecondaryDictionaryGroup != null)
for (final String dictType : ALL_DICTIONARY_TYPES) {
final Dictionary dictionary = mSecondaryDictionaryGroup.getDict(dictType);
if (null == dictionary) continue;
final float weightForLocale = composedData.mIsBatchMode
? mSecondaryDictionaryGroup.mWeightForGesturingInLocale
: mSecondaryDictionaryGroup.mWeightForTypingInLocale;
final ArrayList<SuggestedWordInfo> dictionarySuggestions =
dictionary.getSuggestions(composedData, ngramContext,
proximityInfoHandle, settingsValuesForSuggestion, sessionId,
weightForLocale, weightOfLangModelVsSpatialModel);
if (null == dictionarySuggestions) continue;
suggestionResults.addAll(dictionarySuggestions);
if (null != suggestionResults.mRawSuggestions) { if (null != suggestionResults.mRawSuggestions) {
suggestionResults.mRawSuggestions.addAll(dictionarySuggestions); suggestionResults.mRawSuggestions.addAll(dictionarySuggestionsSecondary);
} }
} }
return suggestionResults; return suggestionResults;
} }
private ArrayList<SuggestedWordInfo> getSuggestions(ComposedData composedData,
NgramContext ngramContext, SettingsValuesForSuggestion settingsValuesForSuggestion,
int sessionId, long proximityInfoHandle, float[] weightOfLangModelVsSpatialModel,
DictionaryGroup dictGroup) {
final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<>();
for (final String dictType : ALL_DICTIONARY_TYPES) {
final Dictionary dictionary = dictGroup.getDict(dictType);
if (null == dictionary) continue;
final float weightForLocale = composedData.mIsBatchMode
? dictGroup.mWeightForGesturingInLocale
: dictGroup.mWeightForTypingInLocale;
final ArrayList<SuggestedWordInfo> dictionarySuggestions =
dictionary.getSuggestions(composedData, ngramContext,
proximityInfoHandle, settingsValuesForSuggestion, sessionId,
weightForLocale, weightOfLangModelVsSpatialModel);
if (null == dictionarySuggestions) continue;
suggestions.addAll(dictionarySuggestions);
}
return suggestions;
}
// Spell checker is using this, and has its own instance of DictionaryFacilitatorImpl, // Spell checker is using this, and has its own instance of DictionaryFacilitatorImpl,
// meaning that it always has default mConfidence. So we cannot choose to only check preferred // meaning that it always has default mConfidence. So we cannot choose to only check preferred
// locale, and instead simply return true if word is in any of the available dictionaries // locale, and instead simply return true if word is in any of the available dictionaries