diff options
author | 2011-03-07 22:45:09 -0800 | |
---|---|---|
committer | 2011-03-07 22:45:09 -0800 | |
commit | b55fcea6fc23bf44966ffb278d976c54995be72e (patch) | |
tree | 2be2dbe33d4fa17c07e34da1f72cf161ea2c0822 /java/src/com/android/inputmethod/latin/Suggest.java | |
parent | e07f93d3ab60f88b154740186795efaf802c7051 (diff) | |
parent | bcfce3b3b9dbd4f5db736948b74bd820fc639a08 (diff) | |
download | latinime-b55fcea6fc23bf44966ffb278d976c54995be72e.tar.gz latinime-b55fcea6fc23bf44966ffb278d976c54995be72e.tar.xz latinime-b55fcea6fc23bf44966ffb278d976c54995be72e.zip |
Merge "Add whitelist dictionary" into honeycomb-mr1
Diffstat (limited to 'java/src/com/android/inputmethod/latin/Suggest.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/Suggest.java | 96 |
1 files changed, 53 insertions, 43 deletions
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index ec86a39bb..9051bf762 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -67,6 +67,13 @@ public class Suggest implements Dictionary.WordCallback { // If you add a type of dictionary, increment DIC_TYPE_LAST_ID public static final int DIC_TYPE_LAST_ID = 4; + public static final String DICT_KEY_MAIN = "main"; + public static final String DICT_KEY_CONTACTS = "contacts"; + public static final String DICT_KEY_AUTO = "auto"; + public static final String DICT_KEY_USER = "user"; + public static final String DICT_KEY_USER_BIGRAM = "user_bigram"; + public static final String DICT_KEY_WHITELIST ="whitelist"; + static final int LARGE_DICTIONARY_THRESHOLD = 200 * 1000; private static final boolean DBG = LatinImeLogger.sDBG; @@ -74,11 +81,7 @@ public class Suggest implements Dictionary.WordCallback { private AutoCorrection mAutoCorrection; private BinaryDictionary mMainDict; - private static final String DICT_KEY_MAIN = "main"; - private static final String DICT_KEY_CONTACTS = "contacts"; - private static final String DICT_KEY_AUTO = "auto"; - private static final String DICT_KEY_USER = "user"; - private static final String DICT_KEY_USER_BIGRAM = "user_bigram"; + private WhitelistDictionary mWhiteListDictionary; private final Map<String, Dictionary> mUnigramDictionaries = new HashMap<String, Dictionary>(); private final Map<String, Dictionary> mBigramDictionaries = new HashMap<String, Dictionary>(); @@ -104,19 +107,23 @@ public class Suggest implements Dictionary.WordCallback { private int mCorrectionMode = CORRECTION_BASIC; public Suggest(Context context, int dictionaryResId) { - init(BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN)); + init(context, BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN)); } /* package for test */ Suggest(File dictionary, long startOffset, long length) { - init(BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN)); + init(null, BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN)); } - private void init(BinaryDictionary mainDict) { + private void init(Context context, BinaryDictionary mainDict) { if (mainDict != null) { mMainDict = mainDict; mUnigramDictionaries.put(DICT_KEY_MAIN, mainDict); mBigramDictionaries.put(DICT_KEY_MAIN, mainDict); } + mWhiteListDictionary = WhitelistDictionary.init(context); + if (mWhiteListDictionary != null) { + mUnigramDictionaries.put(DICT_KEY_WHITELIST, mWhiteListDictionary); + } mAutoCorrection = new AutoCorrection(); initPool(); } @@ -144,6 +151,10 @@ public class Suggest implements Dictionary.WordCallback { return mMainDict != null && mMainDict.getSize() > LARGE_DICTIONARY_THRESHOLD; } + public Map<String, Dictionary> getUnigramDictionaries() { + return mUnigramDictionaries; + } + public int getApproxMaxWordLength() { return APPROX_MAX_WORD_LENGTH; } @@ -218,6 +229,25 @@ public class Suggest implements Dictionary.WordCallback { return getSuggestedWordBuilder(view, wordComposer, prevWordForBigram).build(); } + private CharSequence capitalizeWord(boolean all, boolean first, CharSequence word) { + if (TextUtils.isEmpty(word) || !(all || first)) return word; + final int wordLength = word.length(); + final int poolSize = mStringPool.size(); + final StringBuilder sb = + poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1) + : new StringBuilder(getApproxMaxWordLength()); + sb.setLength(0); + if (all) { + sb.append(word.toString().toUpperCase()); + } else if (first) { + sb.append(Character.toUpperCase(word.charAt(0))); + if (wordLength > 1) { + sb.append(word.subSequence(1, wordLength)); + } + } + return sb; + } + // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder public SuggestedWords.Builder getSuggestedWordBuilder(View view, WordComposer wordComposer, CharSequence prevWordForBigram) { @@ -277,8 +307,8 @@ public class Suggest implements Dictionary.WordCallback { } else if (wordComposer.size() > 1) { // At second character typed, search the unigrams (scores being affected by bigrams) for (final String key : mUnigramDictionaries.keySet()) { - // Skip AutoDictionary to lookup - if (key.equals(DICT_KEY_AUTO)) + // Skip AutoDictionary and WhitelistDictionary to lookup + if (key.equals(DICT_KEY_AUTO) || key.equals(DICT_KEY_WHITELIST)) continue; final Dictionary dictionary = mUnigramDictionaries.get(key); dictionary.getWords(wordComposer, this); @@ -290,28 +320,12 @@ public class Suggest implements Dictionary.WordCallback { // Apply quick fix only for the typed word. if (mQuickFixesEnabled) { final String lowerCaseTypedWord = typedWordString.toLowerCase(); - CharSequence tempAutoText = - AutoText.get(lowerCaseTypedWord, 0, lowerCaseTypedWord.length(), view); + CharSequence tempAutoText = capitalizeWord( + mIsAllUpperCase, mIsFirstCharCapitalized, AutoText.get( + lowerCaseTypedWord, 0, lowerCaseTypedWord.length(), view)); + // TODO: cleanup canAdd // Is there an AutoText (also known as Quick Fixes) correction? // Capitalize as needed - if (!TextUtils.isEmpty(tempAutoText) - && (mIsAllUpperCase || mIsFirstCharCapitalized)) { - final int tempAutoTextLength = tempAutoText.length(); - final int poolSize = mStringPool.size(); - final StringBuilder sb = - poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1) - : new StringBuilder(getApproxMaxWordLength()); - sb.setLength(0); - if (mIsAllUpperCase) { - sb.append(tempAutoText.toString().toUpperCase()); - } else if (mIsFirstCharCapitalized) { - sb.append(Character.toUpperCase(tempAutoText.charAt(0))); - if (tempAutoTextLength > 1) { - sb.append(tempAutoText.subSequence(1, tempAutoTextLength)); - } - } - tempAutoText = sb.toString(); - } boolean canAdd = tempAutoText != null; // Is that correction already the current prediction (or original word)? canAdd &= !TextUtils.equals(tempAutoText, typedWord); @@ -328,14 +342,21 @@ public class Suggest implements Dictionary.WordCallback { } } - mAutoCorrection.updateAutoCorrectionStatus(mUnigramDictionaries.values(), wordComposer, + CharSequence whitelistedWord = capitalizeWord(mIsAllUpperCase, mIsFirstCharCapitalized, + mWhiteListDictionary.getWhiteListedWord(typedWordString)); + + mAutoCorrection.updateAutoCorrectionStatus(mUnigramDictionaries, wordComposer, mSuggestions, mPriorities, typedWord, mAutoCorrectionThreshold, mCorrectionMode, - autoText); + autoText, whitelistedWord); if (autoText != null) { mSuggestions.add(0, autoText); } + if (whitelistedWord != null) { + mSuggestions.add(0, whitelistedWord); + } + if (typedWord != null) { mSuggestions.add(0, typedWordString); } @@ -516,17 +537,6 @@ public class Suggest implements Dictionary.WordCallback { return -1; } - public boolean isValidWord(final CharSequence word) { - if (word == null || word.length() == 0 || mMainDict == null) { - return false; - } - for (final Dictionary dictionary : mUnigramDictionaries.values()) { - if (dictionary.isValidWord(word)) - return true; - } - return false; - } - private void collectGarbage(ArrayList<CharSequence> suggestions, int prefMaxSuggestions) { int poolSize = mStringPool.size(); int garbageSize = suggestions.size(); |