Remove the whitelist dictionary.

The functionality now lives in the binary dictionary.

This finalizes work on
Bug: 6906525

Change-Id: Id106d871e8afdf9afa886d2a30bea87ff89f2d24
main
Jean Chalard 2012-08-17 14:13:13 +09:00
parent 3e43e6998e
commit 46fc768e54
5 changed files with 4 additions and 155 deletions

View File

@ -39,7 +39,6 @@ public class AutoCorrection {
} }
final CharSequence lowerCasedWord = word.toString().toLowerCase(); final CharSequence lowerCasedWord = word.toString().toLowerCase();
for (final String key : dictionaries.keySet()) { for (final String key : dictionaries.keySet()) {
if (key.equals(Dictionary.TYPE_WHITELIST)) continue;
final Dictionary dictionary = dictionaries.get(key); final Dictionary dictionary = dictionaries.get(key);
// It's unclear how realistically 'dictionary' can be null, but the monkey is somehow // It's unclear how realistically 'dictionary' can be null, but the monkey is somehow
// managing to get null in here. Presumably the language is changing to a language with // managing to get null in here. Presumably the language is changing to a language with
@ -64,7 +63,6 @@ public class AutoCorrection {
} }
int maxFreq = -1; int maxFreq = -1;
for (final String key : dictionaries.keySet()) { for (final String key : dictionaries.keySet()) {
if (key.equals(Dictionary.TYPE_WHITELIST)) continue;
final Dictionary dictionary = dictionaries.get(key); final Dictionary dictionary = dictionaries.get(key);
if (null == dictionary) continue; if (null == dictionary) continue;
final int tempFreq = dictionary.getFrequency(word); final int tempFreq = dictionary.getFrequency(word);

View File

@ -42,7 +42,6 @@ public abstract class Dictionary {
public static final String TYPE_USER = "user"; public static final String TYPE_USER = "user";
// User history dictionary internal to LatinIME. // User history dictionary internal to LatinIME.
public static final String TYPE_USER_HISTORY = "history"; public static final String TYPE_USER_HISTORY = "history";
public static final String TYPE_WHITELIST ="whitelist";
protected final String mDictType; protected final String mDictType;
public Dictionary(final String dictType) { public Dictionary(final String dictType) {

View File

@ -50,7 +50,6 @@ public class Suggest {
private Dictionary mMainDictionary; private Dictionary mMainDictionary;
private ContactsBinaryDictionary mContactsDict; private ContactsBinaryDictionary mContactsDict;
private WhitelistDictionary mWhiteListDictionary;
private final ConcurrentHashMap<String, Dictionary> mDictionaries = private final ConcurrentHashMap<String, Dictionary> mDictionaries =
new ConcurrentHashMap<String, Dictionary>(); new ConcurrentHashMap<String, Dictionary>();
@ -74,21 +73,11 @@ public class Suggest {
mLocale = locale; mLocale = locale;
mMainDictionary = mainDict; mMainDictionary = mainDict;
addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_MAIN, mainDict); addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_MAIN, mainDict);
initWhitelistAndAutocorrectAndPool(context, locale);
}
private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) {
mWhiteListDictionary = new WhitelistDictionary(context, locale);
addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_WHITELIST, mWhiteListDictionary);
} }
private void initAsynchronously(final Context context, final Locale locale, private void initAsynchronously(final Context context, final Locale locale,
final SuggestInitializationListener listener) { final SuggestInitializationListener listener) {
resetMainDict(context, locale, listener); resetMainDict(context, locale, listener);
// TODO: read the whitelist and init the pool asynchronously too.
// initPool should be done asynchronously now that the pool is thread-safe.
initWhitelistAndAutocorrectAndPool(context, locale);
} }
private static void addOrReplaceDictionary( private static void addOrReplaceDictionary(
@ -225,11 +214,6 @@ public class Suggest {
whitelistedWord = suggestionsSet.first().mWord; whitelistedWord = suggestionsSet.first().mWord;
} }
// TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid"
// but still autocorrected from - in the case the whitelist only capitalizes the word.
// The whitelist should be case-insensitive, so it's not possible to be consistent with
// a boolean flag. Right now this is handled with a slight hack in
// WhitelistDictionary#shouldForciblyAutoCorrectFrom.
final boolean allowsToBeAutoCorrected = (null != whitelistedWord final boolean allowsToBeAutoCorrected = (null != whitelistedWord
&& !whitelistedWord.equals(consideredWord)) && !whitelistedWord.equals(consideredWord))
|| AutoCorrection.isNotAWord(mDictionaries, consideredWord, || AutoCorrection.isNotAWord(mDictionaries, consideredWord,
@ -311,9 +295,10 @@ public class Suggest {
// At second character typed, search the unigrams (scores being affected by bigrams) // At second character typed, search the unigrams (scores being affected by bigrams)
for (final String key : mDictionaries.keySet()) { for (final String key : mDictionaries.keySet()) {
// Skip UserUnigramDictionary and WhitelistDictionary to lookup // Skip User history dictionary for lookup
if (key.equals(Dictionary.TYPE_USER_HISTORY) // TODO: The user history dictionary should just override getSuggestionsWithSessionId
|| key.equals(Dictionary.TYPE_WHITELIST)) { // to make sure it doesn't return anything and we should remove this test
if (key.equals(Dictionary.TYPE_USER_HISTORY)) {
continue; continue;
} }
final Dictionary dictionary = mDictionaries.get(key); final Dictionary dictionary = mDictionaries.get(key);

View File

@ -1,119 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.android.inputmethod.latin;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
public class WhitelistDictionary extends ExpandableDictionary {
private static final boolean DBG = LatinImeLogger.sDBG;
private static final String TAG = WhitelistDictionary.class.getSimpleName();
private final HashMap<String, Pair<Integer, String>> mWhitelistWords =
new HashMap<String, Pair<Integer, String>>();
// TODO: Conform to the async load contact of ExpandableDictionary
public WhitelistDictionary(final Context context, final Locale locale) {
super(context, Dictionary.TYPE_WHITELIST);
// TODO: Move whitelist dictionary into main dictionary.
final RunInLocale<Void> job = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
initWordlist(res.getStringArray(R.array.wordlist_whitelist));
return null;
}
};
job.runInLocale(context.getResources(), locale);
}
private void initWordlist(String[] wordlist) {
mWhitelistWords.clear();
final int N = wordlist.length;
if (N % 3 != 0) {
if (DBG) {
Log.d(TAG, "The number of the whitelist is invalid.");
}
return;
}
try {
for (int i = 0; i < N; i += 3) {
final int score = Integer.valueOf(wordlist[i]);
final String before = wordlist[i + 1];
final String after = wordlist[i + 2];
if (before != null && after != null) {
mWhitelistWords.put(
before.toLowerCase(), new Pair<Integer, String>(score, after));
addWord(after, null /* shortcut */, score);
}
}
} catch (NumberFormatException e) {
if (DBG) {
Log.d(TAG, "The score of the word is invalid.");
}
}
}
public String getWhitelistedWord(String before) {
if (before == null) return null;
final String lowerCaseBefore = before.toLowerCase();
if(mWhitelistWords.containsKey(lowerCaseBefore)) {
if (DBG) {
Log.d(TAG, "--- found whitelistedWord: " + lowerCaseBefore);
}
return mWhitelistWords.get(lowerCaseBefore).second;
}
return null;
}
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final CharSequence prevWord, final ProximityInfo proximityInfo) {
// Whitelist does not supply any suggestions or predictions.
return null;
}
// 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
// an upper-cased version of a. In this case, the way this return value is used would
// remove the first candidate when the user typed the upper-cased version of A.
// Example : abc -> def and xyz -> Abc
// A user typing Abc would experience it being autocorrected to something else (not
// necessarily def).
// There is no such combination in the whitelist at the time and there probably won't
// ever be - it doesn't make sense. But still.
public boolean shouldForciblyAutoCorrectFrom(CharSequence word) {
if (TextUtils.isEmpty(word)) return false;
final String correction = getWhitelistedWord(word.toString());
if (TextUtils.isEmpty(correction)) return false;
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

@ -35,7 +35,6 @@ import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary; import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary;
import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary; import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary;
import com.android.inputmethod.latin.UserBinaryDictionary; import com.android.inputmethod.latin.UserBinaryDictionary;
import com.android.inputmethod.latin.WhitelistDictionary;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
@ -67,8 +66,6 @@ public class AndroidSpellCheckerService extends SpellCheckerService
Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
private Map<String, UserBinaryDictionary> mUserDictionaries = private Map<String, UserBinaryDictionary> mUserDictionaries =
Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>()); Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>());
private Map<String, Dictionary> mWhitelistDictionaries =
Collections.synchronizedMap(new TreeMap<String, Dictionary>());
private ContactsBinaryDictionary mContactsDictionary; private ContactsBinaryDictionary mContactsDictionary;
// The threshold for a candidate to be offered as a suggestion. // The threshold for a candidate to be offered as a suggestion.
@ -366,8 +363,6 @@ public class AndroidSpellCheckerService extends SpellCheckerService
final Map<String, UserBinaryDictionary> oldUserDictionaries = mUserDictionaries; final Map<String, UserBinaryDictionary> oldUserDictionaries = mUserDictionaries;
mUserDictionaries = mUserDictionaries =
Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>()); Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>());
final Map<String, Dictionary> oldWhitelistDictionaries = mWhitelistDictionaries;
mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
new Thread("spellchecker_close_dicts") { new Thread("spellchecker_close_dicts") {
@Override @Override
public void run() { public void run() {
@ -377,9 +372,6 @@ public class AndroidSpellCheckerService extends SpellCheckerService
for (Dictionary dict : oldUserDictionaries.values()) { for (Dictionary dict : oldUserDictionaries.values()) {
dict.close(); dict.close();
} }
for (Dictionary dict : oldWhitelistDictionaries.values()) {
dict.close();
}
synchronized (mUseContactsLock) { synchronized (mUseContactsLock) {
if (null != mContactsDictionary) { if (null != mContactsDictionary) {
// The synchronously loaded contacts dictionary should have been in one // The synchronously loaded contacts dictionary should have been in one
@ -423,12 +415,6 @@ public class AndroidSpellCheckerService extends SpellCheckerService
mUserDictionaries.put(localeStr, userDictionary); mUserDictionaries.put(localeStr, userDictionary);
} }
dictionaryCollection.addDictionary(userDictionary); dictionaryCollection.addDictionary(userDictionary);
Dictionary whitelistDictionary = mWhitelistDictionaries.get(localeStr);
if (null == whitelistDictionary) {
whitelistDictionary = new WhitelistDictionary(this, locale);
mWhitelistDictionaries.put(localeStr, whitelistDictionary);
}
dictionaryCollection.addDictionary(whitelistDictionary);
synchronized (mUseContactsLock) { synchronized (mUseContactsLock) {
if (mUseContactsDictionary) { if (mUseContactsDictionary) {
if (null == mContactsDictionary) { if (null == mContactsDictionary) {