From c2c44f94e705e74598ec944ab51f3bd13eb50dbf Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 3 Mar 2011 16:02:28 -0800 Subject: [PATCH] Cleanup Suggest to be able to be unit test ready Bug: 3414081 Change-Id: Ia76afac4b1a61b8953a215b7cbdb7557736f7b9c --- .../inputmethod/latin/AutoCorrection.java | 19 ++- .../android/inputmethod/latin/Suggest.java | 111 ++++++++---------- 2 files changed, 66 insertions(+), 64 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/AutoCorrection.java b/java/src/com/android/inputmethod/latin/AutoCorrection.java index d1154d9ed..f177f6a71 100644 --- a/java/src/com/android/inputmethod/latin/AutoCorrection.java +++ b/java/src/com/android/inputmethod/latin/AutoCorrection.java @@ -19,6 +19,7 @@ package com.android.inputmethod.latin; import android.util.Log; import java.util.ArrayList; +import java.util.Collection; public class AutoCorrection { private static final boolean DBG = LatinImeLogger.sDBG; @@ -45,12 +46,12 @@ public class AutoCorrection { return mNormalizedScore; } - public void updateAutoCorrectionStatus(Suggest suggest, + public void updateAutoCorrectionStatus(Collection dictionaries, WordComposer wordComposer, ArrayList suggestions, int[] priorities, CharSequence typedWord, double autoCorrectionThreshold, int correctionMode, CharSequence quickFixedWord) { if (hasAutoCorrectionForTypedWord( - suggest, wordComposer, suggestions, typedWord, correctionMode)) { + dictionaries, wordComposer, suggestions, typedWord, correctionMode)) { mHasAutoCorrection = true; mAutoCorrectionWord = typedWord; } else if (hasAutoCorrectForBinaryDictionary(wordComposer, suggestions, correctionMode, @@ -63,9 +64,17 @@ public class AutoCorrection { } } - private boolean hasAutoCorrectionForTypedWord(Suggest suggest, WordComposer wordComposer, - ArrayList suggestions, CharSequence typedWord, int correctionMode) { - return wordComposer.size() > 1 && suggestions.size() > 0 && suggest.isValidWord(typedWord) + private boolean hasAutoCorrectionForTypedWord(Collection dictionaries, + WordComposer wordComposer, ArrayList suggestions, CharSequence typedWord, + int correctionMode) { + boolean isValidWord = false; + for (final Dictionary dictionary : dictionaries) { + if (dictionary.isValidWord(typedWord)) { + isValidWord = true; + break; + } + } + return wordComposer.size() > 1 && suggestions.size() > 0 && isValidWord && (correctionMode == Suggest.CORRECTION_FULL || correctionMode == Suggest.CORRECTION_FULL_BIGRAM); } diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 95a2d631b..ec86a39bb 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -25,6 +25,10 @@ import android.view.View; import java.io.File; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; /** * This class loads a dictionary and provides a list of suggestions for a given sequence of @@ -70,14 +74,13 @@ public class Suggest implements Dictionary.WordCallback { private AutoCorrection mAutoCorrection; private BinaryDictionary mMainDict; - - private Dictionary mUserDictionary; - - private Dictionary mAutoDictionary; - - private Dictionary mContactsDictionary; - - private Dictionary mUserBigramDictionary; + 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 final Map mUnigramDictionaries = new HashMap(); + private final Map mBigramDictionaries = new HashMap(); private int mPrefMaxSuggestions = 12; @@ -101,16 +104,19 @@ public class Suggest implements Dictionary.WordCallback { private int mCorrectionMode = CORRECTION_BASIC; public Suggest(Context context, int dictionaryResId) { - mMainDict = BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN); - init(); + init(BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN)); } /* package for test */ Suggest(File dictionary, long startOffset, long length) { - mMainDict = BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN); - init(); + init(BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN)); } - private void init() { + private void init(BinaryDictionary mainDict) { + if (mainDict != null) { + mMainDict = mainDict; + mUnigramDictionaries.put(DICT_KEY_MAIN, mainDict); + mBigramDictionaries.put(DICT_KEY_MAIN, mainDict); + } mAutoCorrection = new AutoCorrection(); initPool(); } @@ -147,22 +153,28 @@ public class Suggest implements Dictionary.WordCallback { * before the main dictionary, if set. */ public void setUserDictionary(Dictionary userDictionary) { - mUserDictionary = userDictionary; + if (userDictionary != null) + mUnigramDictionaries.put(DICT_KEY_USER, userDictionary); } /** * Sets an optional contacts dictionary resource to be loaded. */ - public void setContactsDictionary(Dictionary userDictionary) { - mContactsDictionary = userDictionary; + public void setContactsDictionary(Dictionary contactsDictionary) { + if (contactsDictionary != null) { + mUnigramDictionaries.put(DICT_KEY_CONTACTS, contactsDictionary); + mBigramDictionaries.put(DICT_KEY_CONTACTS, contactsDictionary); + } } public void setAutoDictionary(Dictionary autoDictionary) { - mAutoDictionary = autoDictionary; + if (autoDictionary != null) + mUnigramDictionaries.put(DICT_KEY_AUTO, autoDictionary); } public void setUserBigramDictionary(Dictionary userBigramDictionary) { - mUserBigramDictionary = userBigramDictionary; + if (userBigramDictionary != null) + mBigramDictionaries.put(DICT_KEY_USER_BIGRAM, userBigramDictionary); } public void setAutoCorrectionThreshold(double threshold) { @@ -240,14 +252,8 @@ public class Suggest implements Dictionary.WordCallback { if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) { prevWordForBigram = lowerPrevWord; } - if (mUserBigramDictionary != null) { - mUserBigramDictionary.getBigrams(wordComposer, prevWordForBigram, this); - } - if (mContactsDictionary != null) { - mContactsDictionary.getBigrams(wordComposer, prevWordForBigram, this); - } - if (mMainDict != null) { - mMainDict.getBigrams(wordComposer, prevWordForBigram, this); + for (final Dictionary dictionary : mBigramDictionaries.values()) { + dictionary.getBigrams(wordComposer, prevWordForBigram, this); } char currentChar = wordComposer.getTypedWord().charAt(0); char currentCharUpper = Character.toUpperCase(currentChar); @@ -270,15 +276,13 @@ public class Suggest implements Dictionary.WordCallback { } else if (wordComposer.size() > 1) { // At second character typed, search the unigrams (scores being affected by bigrams) - if (mUserDictionary != null || mContactsDictionary != null) { - if (mUserDictionary != null) { - mUserDictionary.getWords(wordComposer, this); - } - if (mContactsDictionary != null) { - mContactsDictionary.getWords(wordComposer, this); - } + for (final String key : mUnigramDictionaries.keySet()) { + // Skip AutoDictionary to lookup + if (key.equals(DICT_KEY_AUTO)) + continue; + final Dictionary dictionary = mUnigramDictionaries.get(key); + dictionary.getWords(wordComposer, this); } - if (mMainDict != null) mMainDict.getWords(wordComposer, this); } CharSequence autoText = null; final String typedWordString = typedWord == null ? null : typedWord.toString(); @@ -324,8 +328,9 @@ public class Suggest implements Dictionary.WordCallback { } } - mAutoCorrection.updateAutoCorrectionStatus(this, wordComposer, mSuggestions, mPriorities, - typedWord, mAutoCorrectionThreshold, mCorrectionMode, autoText); + mAutoCorrection.updateAutoCorrectionStatus(mUnigramDictionaries.values(), wordComposer, + mSuggestions, mPriorities, typedWord, mAutoCorrectionThreshold, mCorrectionMode, + autoText); if (autoText != null) { mSuggestions.add(0, autoText); @@ -515,10 +520,11 @@ public class Suggest implements Dictionary.WordCallback { if (word == null || word.length() == 0 || mMainDict == null) { return false; } - return mMainDict.isValidWord(word) - || (mUserDictionary != null && mUserDictionary.isValidWord(word)) - || (mAutoDictionary != null && mAutoDictionary.isValidWord(word)) - || (mContactsDictionary != null && mContactsDictionary.isValidWord(word)); + for (final Dictionary dictionary : mUnigramDictionaries.values()) { + if (dictionary.isValidWord(word)) + return true; + } + return false; } private void collectGarbage(ArrayList suggestions, int prefMaxSuggestions) { @@ -539,25 +545,12 @@ public class Suggest implements Dictionary.WordCallback { } public void close() { - if (mMainDict != null) { - mMainDict.close(); - mMainDict = null; - } - if (mUserDictionary != null) { - mUserDictionary.close(); - mUserDictionary = null; - } - if (mUserBigramDictionary != null) { - mUserBigramDictionary.close(); - mUserBigramDictionary = null; - } - if (mContactsDictionary != null) { - mContactsDictionary.close(); - mContactsDictionary = null; - } - if (mAutoDictionary != null) { - mAutoDictionary.close(); - mAutoDictionary = null; + final Set dictionaries = new HashSet(); + dictionaries.addAll(mUnigramDictionaries.values()); + dictionaries.addAll(mBigramDictionaries.values()); + for (final Dictionary dictionary : dictionaries) { + dictionary.close(); } + mMainDict = null; } }