2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2011-08-04 03:08:22 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-08-04 03:08:22 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-08-04 03:08:22 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2011-08-04 03:08:22 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2012-06-12 21:57:40 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2014-07-08 07:36:06 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion;
|
2012-06-12 21:57:40 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2014-09-01 11:08:19 +00:00
|
|
|
import java.util.Locale;
|
2014-08-18 18:00:35 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
2011-08-04 03:08:22 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
|
|
|
|
* strokes.
|
|
|
|
*/
|
2010-12-10 06:24:28 +00:00
|
|
|
public abstract class Dictionary {
|
2012-05-29 10:07:22 +00:00
|
|
|
public static final int NOT_A_PROBABILITY = -1;
|
2014-09-12 13:26:09 +00:00
|
|
|
public static final float NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL = -1.0f;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2013-08-20 07:11:03 +00:00
|
|
|
// The following types do not actually come from real dictionary instances, so we create
|
|
|
|
// corresponding instances.
|
2012-06-27 09:08:08 +00:00
|
|
|
public static final String TYPE_USER_TYPED = "user_typed";
|
2013-08-20 07:11:03 +00:00
|
|
|
public static final Dictionary DICTIONARY_USER_TYPED = new PhonyDictionary(TYPE_USER_TYPED);
|
|
|
|
|
2012-06-27 09:17:28 +00:00
|
|
|
public static final String TYPE_APPLICATION_DEFINED = "application_defined";
|
2013-08-20 07:11:03 +00:00
|
|
|
public static final Dictionary DICTIONARY_APPLICATION_DEFINED =
|
|
|
|
new PhonyDictionary(TYPE_APPLICATION_DEFINED);
|
|
|
|
|
2012-06-27 09:17:28 +00:00
|
|
|
public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
|
2013-08-20 07:11:03 +00:00
|
|
|
public static final Dictionary DICTIONARY_HARDCODED =
|
|
|
|
new PhonyDictionary(TYPE_HARDCODED);
|
|
|
|
|
|
|
|
// Spawned by resuming suggestions. Comes from a span that was in the TextView.
|
|
|
|
public static final String TYPE_RESUMED = "resumed";
|
|
|
|
public static final Dictionary DICTIONARY_RESUMED =
|
|
|
|
new PhonyDictionary(TYPE_RESUMED);
|
|
|
|
|
|
|
|
// The following types of dictionary have actual functional instances. We don't need final
|
|
|
|
// phony dictionary instances for them.
|
2012-06-27 09:08:08 +00:00
|
|
|
public static final String TYPE_MAIN = "main";
|
|
|
|
public static final String TYPE_CONTACTS = "contacts";
|
|
|
|
// User dictionary, the system-managed one.
|
|
|
|
public static final String TYPE_USER = "user";
|
2013-12-13 08:09:16 +00:00
|
|
|
// User history dictionary internal to LatinIME.
|
2012-06-27 09:08:08 +00:00
|
|
|
public static final String TYPE_USER_HISTORY = "history";
|
2013-12-13 08:09:16 +00:00
|
|
|
// Personalization dictionary.
|
2013-07-26 05:38:52 +00:00
|
|
|
public static final String TYPE_PERSONALIZATION = "personalization";
|
2014-04-28 13:01:51 +00:00
|
|
|
// Contextual dictionary.
|
|
|
|
public static final String TYPE_CONTEXTUAL = "contextual";
|
2013-08-20 07:11:03 +00:00
|
|
|
public final String mDictType;
|
2014-09-01 11:08:19 +00:00
|
|
|
// The locale for this dictionary. May be null if unknown (phony dictionary for example).
|
|
|
|
public final Locale mLocale;
|
2012-06-27 08:31:09 +00:00
|
|
|
|
2014-08-18 18:00:35 +00:00
|
|
|
/**
|
|
|
|
* Set out of the dictionary types listed above that are based on data specific to the user,
|
|
|
|
* e.g., the user's contacts.
|
|
|
|
*/
|
|
|
|
private static final HashSet<String> sUserSpecificDictionaryTypes =
|
|
|
|
new HashSet(Arrays.asList(new String[] { TYPE_USER_TYPED, TYPE_USER, TYPE_CONTACTS,
|
|
|
|
TYPE_USER_HISTORY, TYPE_PERSONALIZATION, TYPE_CONTEXTUAL }));
|
|
|
|
|
2014-09-01 11:08:19 +00:00
|
|
|
public Dictionary(final String dictType, final Locale locale) {
|
2012-06-27 08:31:09 +00:00
|
|
|
mDictType = dictType;
|
2014-09-01 11:08:19 +00:00
|
|
|
mLocale = locale;
|
2012-06-27 08:31:09 +00:00
|
|
|
}
|
|
|
|
|
2012-07-09 07:34:49 +00:00
|
|
|
/**
|
2014-09-29 01:52:18 +00:00
|
|
|
* Searches for suggestions for a given context.
|
2012-07-09 07:34:49 +00:00
|
|
|
* @param composer the key sequence to match with coordinate info, as a WordComposer
|
2014-09-29 01:52:18 +00:00
|
|
|
* @param ngramContext the context for n-gram.
|
2012-07-09 07:34:49 +00:00
|
|
|
* @param proximityInfo the object for key proximity. May be ignored by some implementations.
|
2014-07-08 07:36:06 +00:00
|
|
|
* @param settingsValuesForSuggestion the settings values used for the suggestion.
|
2014-05-16 08:57:03 +00:00
|
|
|
* @param sessionId the session id.
|
2014-09-12 13:26:09 +00:00
|
|
|
* @param weightForLocale the weight given to this locale, to multiply the output scores for
|
|
|
|
* multilingual input.
|
|
|
|
* @param inOutWeightOfLangModelVsSpatialModel the weight of the language model as a ratio of
|
|
|
|
* the spatial model, used for generating suggestions. inOutWeightOfLangModelVsSpatialModel is
|
|
|
|
* a float array that has only one element. This can be updated when a different value is used.
|
2012-07-09 07:34:49 +00:00
|
|
|
* @return the list of suggestions (possibly null if none)
|
|
|
|
*/
|
2012-07-09 09:25:27 +00:00
|
|
|
abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
2014-09-29 01:52:18 +00:00
|
|
|
final NgramContext ngramContext, final ProximityInfo proximityInfo,
|
2014-07-08 07:36:06 +00:00
|
|
|
final SettingsValuesForSuggestion settingsValuesForSuggestion,
|
2014-09-12 13:26:09 +00:00
|
|
|
final int sessionId, final float weightForLocale,
|
|
|
|
final float[] inOutWeightOfLangModelVsSpatialModel);
|
2012-08-14 13:49:59 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
2014-06-09 02:04:28 +00:00
|
|
|
* Checks if the given word has to be treated as a valid word. Please note that some
|
|
|
|
* dictionaries have entries that should be treated as invalid words.
|
2009-03-13 22:11:42 +00:00
|
|
|
* @param word the word to search for. The search should be case-insensitive.
|
2014-06-09 02:04:28 +00:00
|
|
|
* @return true if the word is valid, false otherwise
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2014-06-09 02:04:28 +00:00
|
|
|
public boolean isValidWord(final String word) {
|
|
|
|
return isInDictionary(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given word is in the dictionary regardless of it being valid or not.
|
|
|
|
*/
|
|
|
|
abstract public boolean isInDictionary(final String word);
|
2011-08-04 03:08:22 +00:00
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public int getFrequency(final String word) {
|
2012-05-29 10:07:22 +00:00
|
|
|
return NOT_A_PROBABILITY;
|
|
|
|
}
|
|
|
|
|
2014-06-05 10:05:58 +00:00
|
|
|
public int getMaxFrequencyOfExactMatches(final String word) {
|
|
|
|
return NOT_A_PROBABILITY;
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Compares the contents of the character array with the typed word and returns true if they
|
|
|
|
* are the same.
|
|
|
|
* @param word the array of characters that make up the word
|
|
|
|
* @param length the number of valid characters in the character array
|
|
|
|
* @param typedWord the word to compare with
|
|
|
|
* @return true if they are the same, false otherwise.
|
|
|
|
*/
|
2012-10-03 06:19:43 +00:00
|
|
|
protected boolean same(final char[] word, final int length, final String typedWord) {
|
2009-03-13 22:11:42 +00:00
|
|
|
if (typedWord.length() != length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (word[i] != typedWord.charAt(i)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-12 20:48:35 +00:00
|
|
|
/**
|
|
|
|
* Override to clean up any resources.
|
|
|
|
*/
|
|
|
|
public void close() {
|
2010-12-10 06:24:28 +00:00
|
|
|
// empty base implementation
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
2012-06-16 00:49:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclasses may override to indicate that this Dictionary is not yet properly initialized.
|
|
|
|
*/
|
|
|
|
public boolean isInitialized() {
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-20 07:11:03 +00:00
|
|
|
|
2013-08-20 07:21:27 +00:00
|
|
|
/**
|
2013-08-20 09:00:21 +00:00
|
|
|
* Whether we think this suggestion should trigger an auto-commit. prevWord is the word
|
|
|
|
* before the suggestion, so that we can use n-gram frequencies.
|
|
|
|
* @param candidate The candidate suggestion, in whole (not only the first part).
|
|
|
|
* @return whether we should auto-commit or not.
|
2013-08-20 07:21:27 +00:00
|
|
|
*/
|
|
|
|
public boolean shouldAutoCommit(final SuggestedWordInfo candidate) {
|
|
|
|
// If we don't have support for auto-commit, or if we don't know, we return false to
|
|
|
|
// avoid auto-committing stuff. Implementations of the Dictionary class that know to
|
|
|
|
// determine whether we should auto-commit will override this.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:00:35 +00:00
|
|
|
/**
|
|
|
|
* Whether this dictionary is based on data specific to the user, e.g., the user's contacts.
|
|
|
|
* @return Whether this dictionary is specific to the user.
|
|
|
|
*/
|
|
|
|
public boolean isUserSpecific() {
|
|
|
|
return sUserSpecificDictionaryTypes.contains(mDictType);
|
|
|
|
}
|
|
|
|
|
2013-08-20 07:11:03 +00:00
|
|
|
/**
|
|
|
|
* Not a true dictionary. A placeholder used to indicate suggestions that don't come from any
|
|
|
|
* real dictionary.
|
|
|
|
*/
|
|
|
|
private static class PhonyDictionary extends Dictionary {
|
|
|
|
// This class is not publicly instantiable.
|
|
|
|
private PhonyDictionary(final String type) {
|
2014-09-01 11:08:19 +00:00
|
|
|
super(type, null);
|
2013-08-20 07:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
2014-09-29 01:52:18 +00:00
|
|
|
final NgramContext ngramContext, final ProximityInfo proximityInfo,
|
2014-07-08 07:36:06 +00:00
|
|
|
final SettingsValuesForSuggestion settingsValuesForSuggestion,
|
2014-09-12 13:26:09 +00:00
|
|
|
final int sessionId, final float weightForLocale,
|
|
|
|
final float[] inOutWeightOfLangModelVsSpatialModel) {
|
2013-08-20 07:11:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-09 02:04:28 +00:00
|
|
|
public boolean isInDictionary(String word) {
|
2013-08-20 07:11:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|