Cleanup Suggest to be able to be unit test ready

Bug: 3414081
Change-Id: Ia76afac4b1a61b8953a215b7cbdb7557736f7b9c
main
Tadashi G. Takaoka 2011-03-03 16:02:28 -08:00
parent dca305dd71
commit c2c44f94e7
2 changed files with 66 additions and 64 deletions

View File

@ -19,6 +19,7 @@ package com.android.inputmethod.latin;
import android.util.Log; import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
public class AutoCorrection { public class AutoCorrection {
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
@ -45,12 +46,12 @@ public class AutoCorrection {
return mNormalizedScore; return mNormalizedScore;
} }
public void updateAutoCorrectionStatus(Suggest suggest, public void updateAutoCorrectionStatus(Collection<Dictionary> dictionaries,
WordComposer wordComposer, ArrayList<CharSequence> suggestions, int[] priorities, WordComposer wordComposer, ArrayList<CharSequence> suggestions, int[] priorities,
CharSequence typedWord, double autoCorrectionThreshold, int correctionMode, CharSequence typedWord, double autoCorrectionThreshold, int correctionMode,
CharSequence quickFixedWord) { CharSequence quickFixedWord) {
if (hasAutoCorrectionForTypedWord( if (hasAutoCorrectionForTypedWord(
suggest, wordComposer, suggestions, typedWord, correctionMode)) { dictionaries, wordComposer, suggestions, typedWord, correctionMode)) {
mHasAutoCorrection = true; mHasAutoCorrection = true;
mAutoCorrectionWord = typedWord; mAutoCorrectionWord = typedWord;
} else if (hasAutoCorrectForBinaryDictionary(wordComposer, suggestions, correctionMode, } else if (hasAutoCorrectForBinaryDictionary(wordComposer, suggestions, correctionMode,
@ -63,9 +64,17 @@ public class AutoCorrection {
} }
} }
private boolean hasAutoCorrectionForTypedWord(Suggest suggest, WordComposer wordComposer, private boolean hasAutoCorrectionForTypedWord(Collection<Dictionary> dictionaries,
ArrayList<CharSequence> suggestions, CharSequence typedWord, int correctionMode) { WordComposer wordComposer, ArrayList<CharSequence> suggestions, CharSequence typedWord,
return wordComposer.size() > 1 && suggestions.size() > 0 && suggest.isValidWord(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
|| correctionMode == Suggest.CORRECTION_FULL_BIGRAM); || correctionMode == Suggest.CORRECTION_FULL_BIGRAM);
} }

View File

@ -25,6 +25,10 @@ import android.view.View;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; 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 * 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 AutoCorrection mAutoCorrection;
private BinaryDictionary mMainDict; private BinaryDictionary mMainDict;
private static final String DICT_KEY_MAIN = "main";
private Dictionary mUserDictionary; private static final String DICT_KEY_CONTACTS = "contacts";
private static final String DICT_KEY_AUTO = "auto";
private Dictionary mAutoDictionary; private static final String DICT_KEY_USER = "user";
private static final String DICT_KEY_USER_BIGRAM = "user_bigram";
private Dictionary mContactsDictionary; private final Map<String, Dictionary> mUnigramDictionaries = new HashMap<String, Dictionary>();
private final Map<String, Dictionary> mBigramDictionaries = new HashMap<String, Dictionary>();
private Dictionary mUserBigramDictionary;
private int mPrefMaxSuggestions = 12; private int mPrefMaxSuggestions = 12;
@ -101,16 +104,19 @@ public class Suggest implements Dictionary.WordCallback {
private int mCorrectionMode = CORRECTION_BASIC; private int mCorrectionMode = CORRECTION_BASIC;
public Suggest(Context context, int dictionaryResId) { public Suggest(Context context, int dictionaryResId) {
mMainDict = BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN); init(BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN));
init();
} }
/* package for test */ Suggest(File dictionary, long startOffset, long length) { /* package for test */ Suggest(File dictionary, long startOffset, long length) {
mMainDict = BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN); init(BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN));
init();
} }
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(); mAutoCorrection = new AutoCorrection();
initPool(); initPool();
} }
@ -147,22 +153,28 @@ public class Suggest implements Dictionary.WordCallback {
* before the main dictionary, if set. * before the main dictionary, if set.
*/ */
public void setUserDictionary(Dictionary userDictionary) { 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. * Sets an optional contacts dictionary resource to be loaded.
*/ */
public void setContactsDictionary(Dictionary userDictionary) { public void setContactsDictionary(Dictionary contactsDictionary) {
mContactsDictionary = userDictionary; if (contactsDictionary != null) {
mUnigramDictionaries.put(DICT_KEY_CONTACTS, contactsDictionary);
mBigramDictionaries.put(DICT_KEY_CONTACTS, contactsDictionary);
}
} }
public void setAutoDictionary(Dictionary autoDictionary) { public void setAutoDictionary(Dictionary autoDictionary) {
mAutoDictionary = autoDictionary; if (autoDictionary != null)
mUnigramDictionaries.put(DICT_KEY_AUTO, autoDictionary);
} }
public void setUserBigramDictionary(Dictionary userBigramDictionary) { public void setUserBigramDictionary(Dictionary userBigramDictionary) {
mUserBigramDictionary = userBigramDictionary; if (userBigramDictionary != null)
mBigramDictionaries.put(DICT_KEY_USER_BIGRAM, userBigramDictionary);
} }
public void setAutoCorrectionThreshold(double threshold) { public void setAutoCorrectionThreshold(double threshold) {
@ -240,14 +252,8 @@ public class Suggest implements Dictionary.WordCallback {
if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) { if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) {
prevWordForBigram = lowerPrevWord; prevWordForBigram = lowerPrevWord;
} }
if (mUserBigramDictionary != null) { for (final Dictionary dictionary : mBigramDictionaries.values()) {
mUserBigramDictionary.getBigrams(wordComposer, prevWordForBigram, this); dictionary.getBigrams(wordComposer, prevWordForBigram, this);
}
if (mContactsDictionary != null) {
mContactsDictionary.getBigrams(wordComposer, prevWordForBigram, this);
}
if (mMainDict != null) {
mMainDict.getBigrams(wordComposer, prevWordForBigram, this);
} }
char currentChar = wordComposer.getTypedWord().charAt(0); char currentChar = wordComposer.getTypedWord().charAt(0);
char currentCharUpper = Character.toUpperCase(currentChar); char currentCharUpper = Character.toUpperCase(currentChar);
@ -270,15 +276,13 @@ public class Suggest implements Dictionary.WordCallback {
} else if (wordComposer.size() > 1) { } else if (wordComposer.size() > 1) {
// At second character typed, search the unigrams (scores being affected by bigrams) // At second character typed, search the unigrams (scores being affected by bigrams)
if (mUserDictionary != null || mContactsDictionary != null) { for (final String key : mUnigramDictionaries.keySet()) {
if (mUserDictionary != null) { // Skip AutoDictionary to lookup
mUserDictionary.getWords(wordComposer, this); if (key.equals(DICT_KEY_AUTO))
} continue;
if (mContactsDictionary != null) { final Dictionary dictionary = mUnigramDictionaries.get(key);
mContactsDictionary.getWords(wordComposer, this); dictionary.getWords(wordComposer, this);
}
} }
if (mMainDict != null) mMainDict.getWords(wordComposer, this);
} }
CharSequence autoText = null; CharSequence autoText = null;
final String typedWordString = typedWord == null ? null : typedWord.toString(); final String typedWordString = typedWord == null ? null : typedWord.toString();
@ -324,8 +328,9 @@ public class Suggest implements Dictionary.WordCallback {
} }
} }
mAutoCorrection.updateAutoCorrectionStatus(this, wordComposer, mSuggestions, mPriorities, mAutoCorrection.updateAutoCorrectionStatus(mUnigramDictionaries.values(), wordComposer,
typedWord, mAutoCorrectionThreshold, mCorrectionMode, autoText); mSuggestions, mPriorities, typedWord, mAutoCorrectionThreshold, mCorrectionMode,
autoText);
if (autoText != null) { if (autoText != null) {
mSuggestions.add(0, autoText); mSuggestions.add(0, autoText);
@ -515,10 +520,11 @@ public class Suggest implements Dictionary.WordCallback {
if (word == null || word.length() == 0 || mMainDict == null) { if (word == null || word.length() == 0 || mMainDict == null) {
return false; return false;
} }
return mMainDict.isValidWord(word) for (final Dictionary dictionary : mUnigramDictionaries.values()) {
|| (mUserDictionary != null && mUserDictionary.isValidWord(word)) if (dictionary.isValidWord(word))
|| (mAutoDictionary != null && mAutoDictionary.isValidWord(word)) return true;
|| (mContactsDictionary != null && mContactsDictionary.isValidWord(word)); }
return false;
} }
private void collectGarbage(ArrayList<CharSequence> suggestions, int prefMaxSuggestions) { private void collectGarbage(ArrayList<CharSequence> suggestions, int prefMaxSuggestions) {
@ -539,25 +545,12 @@ public class Suggest implements Dictionary.WordCallback {
} }
public void close() { public void close() {
if (mMainDict != null) { final Set<Dictionary> dictionaries = new HashSet<Dictionary>();
mMainDict.close(); dictionaries.addAll(mUnigramDictionaries.values());
mMainDict = null; dictionaries.addAll(mBigramDictionaries.values());
} for (final Dictionary dictionary : dictionaries) {
if (mUserDictionary != null) { dictionary.close();
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;
} }
mMainDict = null;
} }
} }