Use the whitelist as a dictionary in the spell checker.

Bug: 5402436
Change-Id: If89b8bbdebf6751697c4788270d01d4639cff665
This commit is contained in:
Jean Chalard 2011-10-06 15:57:43 +09:00
parent 266ae6964d
commit fee149abe0
3 changed files with 32 additions and 24 deletions

View file

@ -67,10 +67,10 @@ public class Suggest implements Dictionary.WordCallback {
public static final int DIC_USER_UNIGRAM = 3; public static final int DIC_USER_UNIGRAM = 3;
public static final int DIC_CONTACTS = 4; public static final int DIC_CONTACTS = 4;
public static final int DIC_USER_BIGRAM = 5; public static final int DIC_USER_BIGRAM = 5;
public static final int DIC_WHITELIST = 6;
// If you add a type of dictionary, increment DIC_TYPE_LAST_ID // If you add a type of dictionary, increment DIC_TYPE_LAST_ID
// TODO: this value seems unused. Remove it? // TODO: this value seems unused. Remove it?
public static final int DIC_TYPE_LAST_ID = 5; public static final int DIC_TYPE_LAST_ID = 6;
public static final String DICT_KEY_MAIN = "main"; public static final String DICT_KEY_MAIN = "main";
public static final String DICT_KEY_CONTACTS = "contacts"; public static final String DICT_KEY_CONTACTS = "contacts";
// User dictionary, the system-managed one. // User dictionary, the system-managed one.
@ -360,7 +360,7 @@ public class Suggest implements Dictionary.WordCallback {
final String typedWordString = typedWord == null ? null : typedWord.toString(); final String typedWordString = typedWord == null ? null : typedWord.toString();
CharSequence whitelistedWord = capitalizeWord(mIsAllUpperCase, mIsFirstCharCapitalized, CharSequence whitelistedWord = capitalizeWord(mIsAllUpperCase, mIsFirstCharCapitalized,
mWhiteListDictionary.getWhiteListedWord(typedWordString)); mWhiteListDictionary.getWhitelistedWord(typedWordString));
mAutoCorrection.updateAutoCorrectionStatus(mUnigramDictionaries, wordComposer, mAutoCorrection.updateAutoCorrectionStatus(mUnigramDictionaries, wordComposer,
mSuggestions, mScores, typedWord, mAutoCorrectionThreshold, mCorrectionMode, mSuggestions, mScores, typedWord, mAutoCorrectionThreshold, mCorrectionMode,

View file

@ -27,7 +27,7 @@ import com.android.inputmethod.keyboard.ProximityInfo;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
public class WhitelistDictionary extends Dictionary { public class WhitelistDictionary extends ExpandableDictionary {
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
private static final String TAG = WhitelistDictionary.class.getSimpleName(); private static final String TAG = WhitelistDictionary.class.getSimpleName();
@ -35,7 +35,9 @@ public class WhitelistDictionary extends Dictionary {
private final HashMap<String, Pair<Integer, String>> mWhitelistWords = private final HashMap<String, Pair<Integer, String>> mWhitelistWords =
new HashMap<String, Pair<Integer, String>>(); new HashMap<String, Pair<Integer, String>>();
// TODO: Conform to the async load contact of ExpandableDictionary
public WhitelistDictionary(final Context context, final Locale locale) { public WhitelistDictionary(final Context context, final Locale locale) {
super(context, Suggest.DIC_WHITELIST);
final Resources res = context.getResources(); final Resources res = context.getResources();
final Locale previousLocale = LocaleUtils.setSystemLocale(res, locale); final Locale previousLocale = LocaleUtils.setSystemLocale(res, locale);
if (context != null) { if (context != null) {
@ -61,6 +63,7 @@ public class WhitelistDictionary extends Dictionary {
if (before != null && after != null) { if (before != null && after != null) {
mWhitelistWords.put( mWhitelistWords.put(
before.toLowerCase(), new Pair<Integer, String>(score, after)); before.toLowerCase(), new Pair<Integer, String>(score, after));
addWord(after, score);
} }
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -70,30 +73,18 @@ public class WhitelistDictionary extends Dictionary {
} }
} }
public String getWhiteListedWord(String before) { public String getWhitelistedWord(String before) {
if (before == null) return null; if (before == null) return null;
final String lowerCaseBefore = before.toLowerCase(); final String lowerCaseBefore = before.toLowerCase();
if(mWhitelistWords.containsKey(lowerCaseBefore)) { if(mWhitelistWords.containsKey(lowerCaseBefore)) {
if (DBG) { if (DBG) {
Log.d(TAG, "--- found whiteListedWord: " + lowerCaseBefore); Log.d(TAG, "--- found whitelistedWord: " + lowerCaseBefore);
} }
return mWhitelistWords.get(lowerCaseBefore).second; return mWhitelistWords.get(lowerCaseBefore).second;
} }
return null; return null;
} }
// Not used for WhitelistDictionary. We use getWhitelistedWord() in Suggest.java instead
@Override
public void getWords(final WordComposer composer, final WordCallback callback,
final ProximityInfo proximityInfo) {
}
@Override
public boolean isValidWord(CharSequence word) {
if (TextUtils.isEmpty(word)) return false;
return !TextUtils.isEmpty(getWhiteListedWord(word.toString()));
}
// See LatinIME#updateSuggestions. This breaks in the (queer) case that the whitelist // See LatinIME#updateSuggestions. This breaks in the (queer) case that the whitelist
// lists that word a should autocorrect to word b, and word c would autocorrect to // lists that word a should autocorrect to word b, and word c would autocorrect to
// an upper-cased version of a. In this case, the way this return value is used would // an upper-cased version of a. In this case, the way this return value is used would
@ -105,8 +96,11 @@ public class WhitelistDictionary extends Dictionary {
// ever be - it doesn't make sense. But still. // ever be - it doesn't make sense. But still.
public boolean shouldForciblyAutoCorrectFrom(CharSequence word) { public boolean shouldForciblyAutoCorrectFrom(CharSequence word) {
if (TextUtils.isEmpty(word)) return false; if (TextUtils.isEmpty(word)) return false;
final String correction = getWhiteListedWord(word.toString()); final String correction = getWhitelistedWord(word.toString());
if (TextUtils.isEmpty(correction)) return false; if (TextUtils.isEmpty(correction)) return false;
return !correction.equals(word); return !correction.equals(word);
} }
// Leave implementation of getWords and isValidWord to the superclass.
// The words have been added to the ExpandableDictionary with addWord() inside initWordlist.
} }

View file

@ -37,6 +37,7 @@ import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary; import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary;
import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.WhitelistDictionary;
import com.android.inputmethod.latin.WordComposer; import com.android.inputmethod.latin.WordComposer;
import java.util.ArrayList; import java.util.ArrayList;
@ -79,6 +80,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
private Map<String, Dictionary> mUserDictionaries = private Map<String, Dictionary> mUserDictionaries =
Collections.synchronizedMap(new TreeMap<String, Dictionary>()); Collections.synchronizedMap(new TreeMap<String, Dictionary>());
private Map<String, Dictionary> mWhitelistDictionaries =
Collections.synchronizedMap(new TreeMap<String, Dictionary>());
// The threshold for a candidate to be offered as a suggestion. // The threshold for a candidate to be offered as a suggestion.
private double mSuggestionThreshold; private double mSuggestionThreshold;
@ -253,12 +256,17 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
final Map<String, Dictionary> oldUserDictionaries = mUserDictionaries; final Map<String, Dictionary> oldUserDictionaries = mUserDictionaries;
mUserDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>()); mUserDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
final Map<String, Dictionary> oldWhitelistDictionaries = mWhitelistDictionaries;
mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
for (DictionaryPool pool : oldPools.values()) { for (DictionaryPool pool : oldPools.values()) {
pool.close(); pool.close();
} }
for (Dictionary dict : oldUserDictionaries.values()) { for (Dictionary dict : oldUserDictionaries.values()) {
dict.close(); dict.close();
} }
for (Dictionary dict : oldWhitelistDictionaries.values()) {
dict.close();
}
return false; return false;
} }
@ -280,12 +288,18 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId, DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId,
USE_FULL_EDIT_DISTANCE_FLAG_ARRAY); USE_FULL_EDIT_DISTANCE_FLAG_ARRAY);
final String localeStr = locale.toString(); final String localeStr = locale.toString();
Dictionary userDict = mUserDictionaries.get(localeStr); Dictionary userDictionary = mUserDictionaries.get(localeStr);
if (null == userDict) { if (null == userDictionary) {
userDict = new SynchronouslyLoadedUserDictionary(this, localeStr, true); userDictionary = new SynchronouslyLoadedUserDictionary(this, localeStr, true);
mUserDictionaries.put(localeStr, userDict); mUserDictionaries.put(localeStr, userDictionary);
} }
dictionaryCollection.addDictionary(userDict); dictionaryCollection.addDictionary(userDictionary);
Dictionary whitelistDictionary = mWhitelistDictionaries.get(localeStr);
if (null == whitelistDictionary) {
whitelistDictionary = new WhitelistDictionary(this, locale);
mWhitelistDictionaries.put(localeStr, whitelistDictionary);
}
dictionaryCollection.addDictionary(whitelistDictionary);
return new DictAndProximity(dictionaryCollection, proximityInfo); return new DictAndProximity(dictionaryCollection, proximityInfo);
} }