Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4c6db059 authored by Jean Chalard's avatar Jean Chalard Committed by Android (Google) Code Review
Browse files

Merge "Remove the whitelist dictionary." into jb-mr1-dev

parents 04d6649d 46fc768e
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ public class AutoCorrection {
        }
        final CharSequence lowerCasedWord = word.toString().toLowerCase();
        for (final String key : dictionaries.keySet()) {
            if (key.equals(Dictionary.TYPE_WHITELIST)) continue;
            final Dictionary dictionary = dictionaries.get(key);
            // 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
@@ -64,7 +63,6 @@ public class AutoCorrection {
        }
        int maxFreq = -1;
        for (final String key : dictionaries.keySet()) {
            if (key.equals(Dictionary.TYPE_WHITELIST)) continue;
            final Dictionary dictionary = dictionaries.get(key);
            if (null == dictionary) continue;
            final int tempFreq = dictionary.getFrequency(word);
+0 −1
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ public abstract class Dictionary {
    public static final String TYPE_USER = "user";
    // User history dictionary internal to LatinIME.
    public static final String TYPE_USER_HISTORY = "history";
    public static final String TYPE_WHITELIST ="whitelist";
    protected final String mDictType;

    public Dictionary(final String dictType) {
+4 −19
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ public class Suggest {

    private Dictionary mMainDictionary;
    private ContactsBinaryDictionary mContactsDict;
    private WhitelistDictionary mWhiteListDictionary;
    private final ConcurrentHashMap<String, Dictionary> mDictionaries =
            new ConcurrentHashMap<String, Dictionary>();

@@ -74,21 +73,11 @@ public class Suggest {
        mLocale = locale;
        mMainDictionary = 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,
            final SuggestInitializationListener 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(
@@ -225,11 +214,6 @@ public class Suggest {
            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
                && !whitelistedWord.equals(consideredWord))
                || AutoCorrection.isNotAWord(mDictionaries, consideredWord,
@@ -311,9 +295,10 @@ public class Suggest {

        // At second character typed, search the unigrams (scores being affected by bigrams)
        for (final String key : mDictionaries.keySet()) {
            // Skip UserUnigramDictionary and WhitelistDictionary to lookup
            if (key.equals(Dictionary.TYPE_USER_HISTORY)
                    || key.equals(Dictionary.TYPE_WHITELIST)) {
            // Skip User history dictionary for lookup
            // TODO: The user history dictionary should just override getSuggestionsWithSessionId
            // to make sure it doesn't return anything and we should remove this test
            if (key.equals(Dictionary.TYPE_USER_HISTORY)) {
                continue;
            }
            final Dictionary dictionary = mDictionaries.get(key);
+0 −119
Original line number Diff line number Diff line
/*
 * 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.
}
+0 −14
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary;
import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary;
import com.android.inputmethod.latin.UserBinaryDictionary;
import com.android.inputmethod.latin.WhitelistDictionary;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
@@ -67,8 +66,6 @@ public class AndroidSpellCheckerService extends SpellCheckerService
            Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
    private Map<String, UserBinaryDictionary> mUserDictionaries =
            Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>());
    private Map<String, Dictionary> mWhitelistDictionaries =
            Collections.synchronizedMap(new TreeMap<String, Dictionary>());
    private ContactsBinaryDictionary mContactsDictionary;

    // 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;
        mUserDictionaries =
                Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>());
        final Map<String, Dictionary> oldWhitelistDictionaries = mWhitelistDictionaries;
        mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
        new Thread("spellchecker_close_dicts") {
            @Override
            public void run() {
@@ -377,9 +372,6 @@ public class AndroidSpellCheckerService extends SpellCheckerService
                for (Dictionary dict : oldUserDictionaries.values()) {
                    dict.close();
                }
                for (Dictionary dict : oldWhitelistDictionaries.values()) {
                    dict.close();
                }
                synchronized (mUseContactsLock) {
                    if (null != mContactsDictionary) {
                        // The synchronously loaded contacts dictionary should have been in one
@@ -423,12 +415,6 @@ public class AndroidSpellCheckerService extends SpellCheckerService
            mUserDictionaries.put(localeStr, 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) {
            if (mUseContactsDictionary) {
                if (null == mContactsDictionary) {