mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-06-10 00:27:45 +00:00
prepare for adding customizable weights for user-provided dictionaries
This commit is contained in:
parent
27a2300631
commit
4289e487e9
2 changed files with 17 additions and 29 deletions
|
@ -6,30 +6,35 @@
|
||||||
|
|
||||||
package helium314.keyboard.latin;
|
package helium314.keyboard.latin;
|
||||||
|
|
||||||
import helium314.keyboard.latin.utils.Log;
|
|
||||||
|
|
||||||
import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo;
|
import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo;
|
||||||
import helium314.keyboard.latin.common.ComposedData;
|
import helium314.keyboard.latin.common.ComposedData;
|
||||||
import helium314.keyboard.latin.settings.SettingsValuesForSuggestion;
|
import helium314.keyboard.latin.settings.SettingsValuesForSuggestion;
|
||||||
|
import helium314.keyboard.latin.utils.Log;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for a collection of dictionaries that behave like one dictionary.
|
* Class for a collection of dictionaries that behave like one dictionary.
|
||||||
*/
|
*/
|
||||||
public final class DictionaryCollection extends Dictionary {
|
public final class DictionaryCollection extends Dictionary {
|
||||||
private final String TAG = DictionaryCollection.class.getSimpleName();
|
private final String TAG = DictionaryCollection.class.getSimpleName();
|
||||||
private final CopyOnWriteArrayList<Dictionary> mDictionaries;
|
private final ArrayList<Dictionary> mDictionaries;
|
||||||
|
private final float[] mWeights;
|
||||||
|
|
||||||
public DictionaryCollection(final String dictType, final Locale locale,
|
public DictionaryCollection(final String dictType, final Locale locale,
|
||||||
final Collection<Dictionary> dictionaries) {
|
final Collection<Dictionary> dictionaries, final float[] weights) {
|
||||||
super(dictType, locale);
|
super(dictType, locale);
|
||||||
mDictionaries = new CopyOnWriteArrayList<>(dictionaries);
|
mDictionaries = new ArrayList<>(dictionaries);
|
||||||
mDictionaries.removeAll(Collections.singleton(null));
|
mDictionaries.removeAll(Collections.singleton(null));
|
||||||
|
if (mDictionaries.size() > weights.length) {
|
||||||
|
mWeights = new float[mDictionaries.size()];
|
||||||
|
Arrays.fill(mWeights, 1f);
|
||||||
|
Log.w(TAG, "got weights array of length " + weights.length + ", expected "+mDictionaries.size());
|
||||||
|
} else mWeights = weights;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,19 +43,19 @@ public final class DictionaryCollection extends Dictionary {
|
||||||
final SettingsValuesForSuggestion settingsValuesForSuggestion,
|
final SettingsValuesForSuggestion settingsValuesForSuggestion,
|
||||||
final int sessionId, final float weightForLocale,
|
final int sessionId, final float weightForLocale,
|
||||||
final float[] inOutWeightOfLangModelVsSpatialModel) {
|
final float[] inOutWeightOfLangModelVsSpatialModel) {
|
||||||
final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
|
final ArrayList<Dictionary> dictionaries = mDictionaries;
|
||||||
if (dictionaries.isEmpty()) return null;
|
if (dictionaries.isEmpty()) return null;
|
||||||
// To avoid creating unnecessary objects, we get the list out of the first
|
// To avoid creating unnecessary objects, we get the list out of the first
|
||||||
// dictionary and add the rest to it if not null, hence the get(0)
|
// dictionary and add the rest to it if not null, hence the get(0)
|
||||||
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composedData,
|
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composedData,
|
||||||
ngramContext, proximityInfoHandle, settingsValuesForSuggestion, sessionId,
|
ngramContext, proximityInfoHandle, settingsValuesForSuggestion, sessionId,
|
||||||
weightForLocale, inOutWeightOfLangModelVsSpatialModel);
|
weightForLocale * mWeights[0], inOutWeightOfLangModelVsSpatialModel);
|
||||||
if (null == suggestions) suggestions = new ArrayList<>();
|
if (null == suggestions) suggestions = new ArrayList<>();
|
||||||
final int length = dictionaries.size();
|
final int length = dictionaries.size();
|
||||||
for (int i = 1; i < length; ++ i) {
|
for (int i = 1; i < length; ++ i) {
|
||||||
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(
|
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(
|
||||||
composedData, ngramContext, proximityInfoHandle, settingsValuesForSuggestion,
|
composedData, ngramContext, proximityInfoHandle, settingsValuesForSuggestion,
|
||||||
sessionId, weightForLocale, inOutWeightOfLangModelVsSpatialModel);
|
sessionId, weightForLocale * mWeights[i], inOutWeightOfLangModelVsSpatialModel);
|
||||||
if (null != sugg) suggestions.addAll(sugg);
|
if (null != sugg) suggestions.addAll(sugg);
|
||||||
}
|
}
|
||||||
return suggestions;
|
return suggestions;
|
||||||
|
@ -93,22 +98,4 @@ public final class DictionaryCollection extends Dictionary {
|
||||||
for (final Dictionary dict : mDictionaries)
|
for (final Dictionary dict : mDictionaries)
|
||||||
dict.close();
|
dict.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warning: this is not thread-safe. Take necessary precaution when calling.
|
|
||||||
public void addDictionary(final Dictionary newDict) {
|
|
||||||
if (null == newDict) return;
|
|
||||||
if (mDictionaries.contains(newDict)) {
|
|
||||||
Log.w(TAG, "This collection already contains this dictionary: " + newDict);
|
|
||||||
}
|
|
||||||
mDictionaries.add(newDict);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warning: this is not thread-safe. Take necessary precaution when calling.
|
|
||||||
public void removeDictionary(final Dictionary dict) {
|
|
||||||
if (mDictionaries.contains(dict)) {
|
|
||||||
mDictionaries.remove(dict);
|
|
||||||
} else {
|
|
||||||
Log.w(TAG, "This collection does not contain this dictionary: " + dict);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import java.util.Locale
|
||||||
* @param locale the locale for which to create the dictionary
|
* @param locale the locale for which to create the dictionary
|
||||||
* @return an initialized instance of DictionaryCollection
|
* @return an initialized instance of DictionaryCollection
|
||||||
*/
|
*/
|
||||||
|
// todo: this needs updating, and then we can expose the weight for custom dictionaries (useful for addons like emoji dict)
|
||||||
fun createMainDictionary(context: Context, locale: Locale): DictionaryCollection {
|
fun createMainDictionary(context: Context, locale: Locale): DictionaryCollection {
|
||||||
val cacheDir = DictionaryInfoUtils.getAndCreateCacheDirectoryForLocale(locale, context)
|
val cacheDir = DictionaryInfoUtils.getAndCreateCacheDirectoryForLocale(locale, context)
|
||||||
val dictList = LinkedList<Dictionary>()
|
val dictList = LinkedList<Dictionary>()
|
||||||
|
@ -36,7 +37,7 @@ fun createMainDictionary(context: Context, locale: Locale): DictionaryCollection
|
||||||
// add extracted dicts to list (after userDicts, to skip extracted dicts of same type)
|
// add extracted dicts to list (after userDicts, to skip extracted dicts of same type)
|
||||||
extractedDicts.forEach { checkAndAddDictionaryToListIfNotExisting(it, dictList, locale) }
|
extractedDicts.forEach { checkAndAddDictionaryToListIfNotExisting(it, dictList, locale) }
|
||||||
if (dictList.any { it.mDictType == Dictionary.TYPE_MAIN })
|
if (dictList.any { it.mDictType == Dictionary.TYPE_MAIN })
|
||||||
return DictionaryCollection(Dictionary.TYPE_MAIN, locale, dictList)
|
return DictionaryCollection(Dictionary.TYPE_MAIN, locale, dictList, FloatArray(dictList.size) { 1f })
|
||||||
|
|
||||||
// no main dict found -> check assets
|
// no main dict found -> check assets
|
||||||
val assetsDicts = DictionaryInfoUtils.getAssetsDictionaryList(context)
|
val assetsDicts = DictionaryInfoUtils.getAssetsDictionaryList(context)
|
||||||
|
@ -57,7 +58,7 @@ fun createMainDictionary(context: Context, locale: Locale): DictionaryCollection
|
||||||
// If the list is empty, that means we should not use any dictionary (for example, the user
|
// If the list is empty, that means we should not use any dictionary (for example, the user
|
||||||
// explicitly disabled the main dictionary), so the following is okay. dictList is never
|
// explicitly disabled the main dictionary), so the following is okay. dictList is never
|
||||||
// null, but if for some reason it is, DictionaryCollection handles it gracefully.
|
// null, but if for some reason it is, DictionaryCollection handles it gracefully.
|
||||||
return DictionaryCollection(Dictionary.TYPE_MAIN, locale, dictList)
|
return DictionaryCollection(Dictionary.TYPE_MAIN, locale, dictList, FloatArray(dictList.size) { 1f })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue