Suggest.java revision 043f7841985916717f4fa821fe3e423daf3ff2f5
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.content.Context;
20import android.text.AutoText;
21import android.text.TextUtils;
22import android.util.Log;
23import android.view.View;
24
25import com.android.inputmethod.keyboard.ProximityInfo;
26
27import java.io.File;
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.HashMap;
31import java.util.HashSet;
32import java.util.Locale;
33import java.util.Map;
34import java.util.Set;
35
36/**
37 * This class loads a dictionary and provides a list of suggestions for a given sequence of
38 * characters. This includes corrections and completions.
39 */
40public class Suggest implements Dictionary.WordCallback {
41
42    public static final String TAG = Suggest.class.getSimpleName();
43
44    public static final int APPROX_MAX_WORD_LENGTH = 32;
45
46    public static final int CORRECTION_NONE = 0;
47    public static final int CORRECTION_BASIC = 1;
48    public static final int CORRECTION_FULL = 2;
49    public static final int CORRECTION_FULL_BIGRAM = 3;
50
51    /**
52     * Words that appear in both bigram and unigram data gets multiplier ranging from
53     * BIGRAM_MULTIPLIER_MIN to BIGRAM_MULTIPLIER_MAX depending on the score from
54     * bigram data.
55     */
56    public static final double BIGRAM_MULTIPLIER_MIN = 1.2;
57    public static final double BIGRAM_MULTIPLIER_MAX = 1.5;
58
59    /**
60     * Maximum possible bigram frequency. Will depend on how many bits are being used in data
61     * structure. Maximum bigram frequency will get the BIGRAM_MULTIPLIER_MAX as the multiplier.
62     */
63    public static final int MAXIMUM_BIGRAM_FREQUENCY = 127;
64
65    // It seems the following values are only used for logging.
66    public static final int DIC_USER_TYPED = 0;
67    public static final int DIC_MAIN = 1;
68    public static final int DIC_USER = 2;
69    public static final int DIC_USER_UNIGRAM = 3;
70    public static final int DIC_CONTACTS = 4;
71    public static final int DIC_USER_BIGRAM = 5;
72    // If you add a type of dictionary, increment DIC_TYPE_LAST_ID
73    // TODO: this value seems unused. Remove it?
74    public static final int DIC_TYPE_LAST_ID = 5;
75
76    public static final String DICT_KEY_MAIN = "main";
77    public static final String DICT_KEY_CONTACTS = "contacts";
78    // User dictionary, the system-managed one.
79    public static final String DICT_KEY_USER = "user";
80    // User unigram dictionary, internal to LatinIME
81    public static final String DICT_KEY_USER_UNIGRAM = "user_unigram";
82    // User bigram dictionary, internal to LatinIME
83    public static final String DICT_KEY_USER_BIGRAM = "user_bigram";
84    public static final String DICT_KEY_WHITELIST ="whitelist";
85
86    private static final boolean DBG = LatinImeLogger.sDBG;
87
88    private AutoCorrection mAutoCorrection;
89
90    private Dictionary mMainDict;
91    private WhitelistDictionary mWhiteListDictionary;
92    private final Map<String, Dictionary> mUnigramDictionaries = new HashMap<String, Dictionary>();
93    private final Map<String, Dictionary> mBigramDictionaries = new HashMap<String, Dictionary>();
94
95    private int mPrefMaxSuggestions = 18;
96
97    private static final int PREF_MAX_BIGRAMS = 60;
98
99    private boolean mQuickFixesEnabled;
100
101    private double mAutoCorrectionThreshold;
102    private int[] mScores = new int[mPrefMaxSuggestions];
103    private int[] mBigramScores = new int[PREF_MAX_BIGRAMS];
104
105    private ArrayList<CharSequence> mSuggestions = new ArrayList<CharSequence>();
106    ArrayList<CharSequence> mBigramSuggestions  = new ArrayList<CharSequence>();
107    // TODO: maybe this should be synchronized, it's quite scary as it is.
108    // TODO: if it becomes synchronized, also move initPool in the thread in initAsynchronously
109    private ArrayList<CharSequence> mStringPool = new ArrayList<CharSequence>();
110    private CharSequence mTypedWord;
111
112    // TODO: Remove these member variables by passing more context to addWord() callback method
113    private boolean mIsFirstCharCapitalized;
114    private boolean mIsAllUpperCase;
115
116    private int mCorrectionMode = CORRECTION_BASIC;
117
118    public Suggest(final Context context, final int dictionaryResId, final Locale locale) {
119        initAsynchronously(context, dictionaryResId, locale);
120    }
121
122    /* package for test */ Suggest(Context context, File dictionary, long startOffset, long length,
123            Flag[] flagArray) {
124        initSynchronously(null, DictionaryFactory.createDictionaryForTest(context, dictionary,
125                startOffset, length, flagArray));
126    }
127
128    private void initWhitelistAndAutocorrectAndPool(final Context context) {
129        mWhiteListDictionary = WhitelistDictionary.init(context);
130        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
131        mAutoCorrection = new AutoCorrection();
132        initPool();
133    }
134
135    private void initAsynchronously(final Context context, final int dictionaryResId,
136            final Locale locale) {
137        resetMainDict(context, dictionaryResId, locale);
138
139        // TODO: read the whitelist and init the pool asynchronously too.
140        // initPool should be done asynchronously but the pool is not thread-safe at the moment.
141        initWhitelistAndAutocorrectAndPool(context);
142    }
143
144    private void initSynchronously(Context context, Dictionary mainDict) {
145        mMainDict = mainDict;
146        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict);
147        addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict);
148        initWhitelistAndAutocorrectAndPool(context);
149    }
150
151    private void addOrReplaceDictionary(Map<String, Dictionary> dictionaries, String key,
152            Dictionary dict) {
153        final Dictionary oldDict = (dict == null)
154                ? dictionaries.remove(key)
155                : dictionaries.put(key, dict);
156        if (oldDict != null && dict != oldDict) {
157            oldDict.close();
158        }
159    }
160
161    public void resetMainDict(final Context context, final int dictionaryResId,
162            final Locale locale) {
163        mMainDict = null;
164        new Thread("InitializeBinaryDictionary") {
165            public void run() {
166                final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager(
167                        context, locale, dictionaryResId);
168                mMainDict = newMainDict;
169                addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict);
170                addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict);
171            }
172        }.start();
173    }
174
175    private void initPool() {
176        for (int i = 0; i < mPrefMaxSuggestions; i++) {
177            StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
178            mStringPool.add(sb);
179        }
180    }
181
182    public void setQuickFixesEnabled(boolean enabled) {
183        mQuickFixesEnabled = enabled;
184    }
185
186    public int getCorrectionMode() {
187        return mCorrectionMode;
188    }
189
190    public void setCorrectionMode(int mode) {
191        mCorrectionMode = mode;
192    }
193
194    public boolean hasMainDictionary() {
195        return mMainDict != null;
196    }
197
198    public Map<String, Dictionary> getUnigramDictionaries() {
199        return mUnigramDictionaries;
200    }
201
202    public int getApproxMaxWordLength() {
203        return APPROX_MAX_WORD_LENGTH;
204    }
205
206    /**
207     * Sets an optional user dictionary resource to be loaded. The user dictionary is consulted
208     * before the main dictionary, if set. This refers to the system-managed user dictionary.
209     */
210    public void setUserDictionary(Dictionary userDictionary) {
211        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER, userDictionary);
212    }
213
214    /**
215     * Sets an optional contacts dictionary resource to be loaded. It is also possible to remove
216     * the contacts dictionary by passing null to this method. In this case no contacts dictionary
217     * won't be used.
218     */
219    public void setContactsDictionary(Dictionary contactsDictionary) {
220        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
221        addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
222    }
223
224    public void setUserUnigramDictionary(Dictionary userUnigramDictionary) {
225        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER_UNIGRAM, userUnigramDictionary);
226    }
227
228    public void setUserBigramDictionary(Dictionary userBigramDictionary) {
229        addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_USER_BIGRAM, userBigramDictionary);
230    }
231
232    public void setAutoCorrectionThreshold(double threshold) {
233        mAutoCorrectionThreshold = threshold;
234    }
235
236    public boolean isAggressiveAutoCorrectionMode() {
237        return (mAutoCorrectionThreshold == 0);
238    }
239
240    /**
241     * Number of suggestions to generate from the input key sequence. This has
242     * to be a number between 1 and 100 (inclusive).
243     * @param maxSuggestions
244     * @throws IllegalArgumentException if the number is out of range
245     */
246    public void setMaxSuggestions(int maxSuggestions) {
247        if (maxSuggestions < 1 || maxSuggestions > 100) {
248            throw new IllegalArgumentException("maxSuggestions must be between 1 and 100");
249        }
250        mPrefMaxSuggestions = maxSuggestions;
251        mScores = new int[mPrefMaxSuggestions];
252        mBigramScores = new int[PREF_MAX_BIGRAMS];
253        collectGarbage(mSuggestions, mPrefMaxSuggestions);
254        while (mStringPool.size() < mPrefMaxSuggestions) {
255            StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
256            mStringPool.add(sb);
257        }
258    }
259
260    /**
261     * Returns a object which represents suggested words that match the list of character codes
262     * passed in. This object contents will be overwritten the next time this function is called.
263     * @param view a view for retrieving the context for AutoText
264     * @param wordComposer contains what is currently being typed
265     * @param prevWordForBigram previous word (used only for bigram)
266     * @return suggested words object.
267     */
268    public SuggestedWords getSuggestions(final View view, final WordComposer wordComposer,
269            final CharSequence prevWordForBigram, final ProximityInfo proximityInfo) {
270        return getSuggestedWordBuilder(view, wordComposer, prevWordForBigram,
271                proximityInfo).build();
272    }
273
274    private CharSequence capitalizeWord(boolean all, boolean first, CharSequence word) {
275        if (TextUtils.isEmpty(word) || !(all || first)) return word;
276        final int wordLength = word.length();
277        final int poolSize = mStringPool.size();
278        final StringBuilder sb =
279                poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1)
280                        : new StringBuilder(getApproxMaxWordLength());
281        sb.setLength(0);
282        // TODO: Must pay attention to locale when changing case.
283        if (all) {
284            sb.append(word.toString().toUpperCase());
285        } else if (first) {
286            sb.append(Character.toUpperCase(word.charAt(0)));
287            if (wordLength > 1) {
288                sb.append(word.subSequence(1, wordLength));
289            }
290        }
291        return sb;
292    }
293
294    protected void addBigramToSuggestions(CharSequence bigram) {
295        final int poolSize = mStringPool.size();
296        final StringBuilder sb = poolSize > 0 ?
297                (StringBuilder) mStringPool.remove(poolSize - 1)
298                        : new StringBuilder(getApproxMaxWordLength());
299        sb.setLength(0);
300        sb.append(bigram);
301        mSuggestions.add(sb);
302    }
303
304    // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder
305    public SuggestedWords.Builder getSuggestedWordBuilder(final View view,
306            final WordComposer wordComposer, CharSequence prevWordForBigram,
307            final ProximityInfo proximityInfo) {
308        LatinImeLogger.onStartSuggestion(prevWordForBigram);
309        mAutoCorrection.init();
310        mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized();
311        mIsAllUpperCase = wordComposer.isAllUpperCase();
312        collectGarbage(mSuggestions, mPrefMaxSuggestions);
313        Arrays.fill(mScores, 0);
314
315        // Save a lowercase version of the original word
316        CharSequence typedWord = wordComposer.getTypedWord();
317        if (typedWord != null) {
318            final String typedWordString = typedWord.toString();
319            typedWord = typedWordString;
320            // Treating USER_TYPED as UNIGRAM suggestion for logging now.
321            LatinImeLogger.onAddSuggestedWord(typedWordString, Suggest.DIC_USER_TYPED,
322                    Dictionary.DataType.UNIGRAM);
323        }
324        mTypedWord = typedWord;
325
326        if (wordComposer.size() <= 1 && (mCorrectionMode == CORRECTION_FULL_BIGRAM
327                || mCorrectionMode == CORRECTION_BASIC)) {
328            // At first character typed, search only the bigrams
329            Arrays.fill(mBigramScores, 0);
330            collectGarbage(mBigramSuggestions, PREF_MAX_BIGRAMS);
331
332            if (!TextUtils.isEmpty(prevWordForBigram)) {
333                CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase();
334                if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) {
335                    prevWordForBigram = lowerPrevWord;
336                }
337                for (final Dictionary dictionary : mBigramDictionaries.values()) {
338                    dictionary.getBigrams(wordComposer, prevWordForBigram, this);
339                }
340                if (TextUtils.isEmpty(typedWord)) {
341                    // Nothing entered: return all bigrams for the previous word
342                    int insertCount = Math.min(mBigramSuggestions.size(), mPrefMaxSuggestions);
343                    for (int i = 0; i < insertCount; ++i) {
344                        addBigramToSuggestions(mBigramSuggestions.get(i));
345                    }
346                } else {
347                    // Word entered: return only bigrams that match the first char of the typed word
348                    final char currentChar = typedWord.charAt(0);
349                    // TODO: Must pay attention to locale when changing case.
350                    final char currentCharUpper = Character.toUpperCase(currentChar);
351                    int count = 0;
352                    final int bigramSuggestionSize = mBigramSuggestions.size();
353                    for (int i = 0; i < bigramSuggestionSize; i++) {
354                        final CharSequence bigramSuggestion = mBigramSuggestions.get(i);
355                        final char bigramSuggestionFirstChar = bigramSuggestion.charAt(0);
356                        if (bigramSuggestionFirstChar == currentChar
357                                || bigramSuggestionFirstChar == currentCharUpper) {
358                            addBigramToSuggestions(bigramSuggestion);
359                            if (++count > mPrefMaxSuggestions) break;
360                        }
361                    }
362                }
363            }
364
365        } else if (wordComposer.size() > 1) {
366            // At second character typed, search the unigrams (scores being affected by bigrams)
367            for (final String key : mUnigramDictionaries.keySet()) {
368                // Skip UserUnigramDictionary and WhitelistDictionary to lookup
369                if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST))
370                    continue;
371                final Dictionary dictionary = mUnigramDictionaries.get(key);
372                dictionary.getWords(wordComposer, this, proximityInfo);
373            }
374        }
375        CharSequence autoText = null;
376        final String typedWordString = typedWord == null ? null : typedWord.toString();
377        if (typedWord != null) {
378            // Apply quick fix only for the typed word.
379            if (mQuickFixesEnabled) {
380                final String lowerCaseTypedWord = typedWordString.toLowerCase();
381                CharSequence tempAutoText = capitalizeWord(
382                        mIsAllUpperCase, mIsFirstCharCapitalized, AutoText.get(
383                                lowerCaseTypedWord, 0, lowerCaseTypedWord.length(), view));
384                // TODO: cleanup canAdd
385                // Is there an AutoText (also known as Quick Fixes) correction?
386                // Capitalize as needed
387                boolean canAdd = tempAutoText != null;
388                // Is that correction already the current prediction (or original word)?
389                canAdd &= !TextUtils.equals(tempAutoText, typedWord);
390                // Is that correction already the next predicted word?
391                if (canAdd && mSuggestions.size() > 0 && mCorrectionMode != CORRECTION_BASIC) {
392                    canAdd &= !TextUtils.equals(tempAutoText, mSuggestions.get(0));
393                }
394                if (canAdd) {
395                    if (DBG) {
396                        Log.d(TAG, "Auto corrected by AUTOTEXT.");
397                    }
398                    autoText = tempAutoText;
399                }
400            }
401        }
402
403        CharSequence whitelistedWord = capitalizeWord(mIsAllUpperCase, mIsFirstCharCapitalized,
404                mWhiteListDictionary.getWhiteListedWord(typedWordString));
405
406        mAutoCorrection.updateAutoCorrectionStatus(mUnigramDictionaries, wordComposer,
407                mSuggestions, mScores, typedWord, mAutoCorrectionThreshold, mCorrectionMode,
408                autoText, whitelistedWord);
409
410        if (autoText != null) {
411            mSuggestions.add(0, autoText);
412        }
413
414        if (whitelistedWord != null) {
415            mSuggestions.add(0, whitelistedWord);
416        }
417
418        if (typedWord != null) {
419            mSuggestions.add(0, typedWordString);
420        }
421        removeDupes();
422
423        if (DBG) {
424            double normalizedScore = mAutoCorrection.getNormalizedScore();
425            ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList =
426                    new ArrayList<SuggestedWords.SuggestedWordInfo>();
427            scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("+", false));
428            for (int i = 0; i < mScores.length; ++i) {
429                if (normalizedScore > 0) {
430                    final String scoreThreshold = String.format("%d (%4.2f)", mScores[i],
431                            normalizedScore);
432                    scoreInfoList.add(
433                            new SuggestedWords.SuggestedWordInfo(scoreThreshold, false));
434                    normalizedScore = 0.0;
435                } else {
436                    final String score = Integer.toString(mScores[i]);
437                    scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(score, false));
438                }
439            }
440            for (int i = mScores.length; i < mSuggestions.size(); ++i) {
441                scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("--", false));
442            }
443            return new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList);
444        }
445        return new SuggestedWords.Builder().addWords(mSuggestions, null);
446    }
447
448    private void removeDupes() {
449        final ArrayList<CharSequence> suggestions = mSuggestions;
450        if (suggestions.size() < 2) return;
451        int i = 1;
452        // Don't cache suggestions.size(), since we may be removing items
453        while (i < suggestions.size()) {
454            final CharSequence cur = suggestions.get(i);
455            // Compare each candidate with each previous candidate
456            for (int j = 0; j < i; j++) {
457                CharSequence previous = suggestions.get(j);
458                if (TextUtils.equals(cur, previous)) {
459                    removeFromSuggestions(i);
460                    i--;
461                    break;
462                }
463            }
464            i++;
465        }
466    }
467
468    private void removeFromSuggestions(int index) {
469        CharSequence garbage = mSuggestions.remove(index);
470        if (garbage != null && garbage instanceof StringBuilder) {
471            mStringPool.add(garbage);
472        }
473    }
474
475    public boolean hasAutoCorrection() {
476        return mAutoCorrection.hasAutoCorrection();
477    }
478
479    @Override
480    public boolean addWord(final char[] word, final int offset, final int length, int score,
481            final int dicTypeId, final Dictionary.DataType dataType) {
482        Dictionary.DataType dataTypeForLog = dataType;
483        final ArrayList<CharSequence> suggestions;
484        final int[] sortedScores;
485        final int prefMaxSuggestions;
486        if(dataType == Dictionary.DataType.BIGRAM) {
487            suggestions = mBigramSuggestions;
488            sortedScores = mBigramScores;
489            prefMaxSuggestions = PREF_MAX_BIGRAMS;
490        } else {
491            suggestions = mSuggestions;
492            sortedScores = mScores;
493            prefMaxSuggestions = mPrefMaxSuggestions;
494        }
495
496        int pos = 0;
497
498        // Check if it's the same word, only caps are different
499        if (Utils.equalsIgnoreCase(mTypedWord, word, offset, length)) {
500            // TODO: remove this surrounding if clause and move this logic to
501            // getSuggestedWordBuilder.
502            if (suggestions.size() > 0) {
503                final String currentHighestWord = suggestions.get(0).toString();
504                // If the current highest word is also equal to typed word, we need to compare
505                // frequency to determine the insertion position. This does not ensure strictly
506                // correct ordering, but ensures the top score is on top which is enough for
507                // removing duplicates correctly.
508                if (Utils.equalsIgnoreCase(currentHighestWord, word, offset, length)
509                        && score <= sortedScores[0]) {
510                    pos = 1;
511                }
512            }
513        } else {
514            if (dataType == Dictionary.DataType.UNIGRAM) {
515                // Check if the word was already added before (by bigram data)
516                int bigramSuggestion = searchBigramSuggestion(word,offset,length);
517                if(bigramSuggestion >= 0) {
518                    dataTypeForLog = Dictionary.DataType.BIGRAM;
519                    // turn freq from bigram into multiplier specified above
520                    double multiplier = (((double) mBigramScores[bigramSuggestion])
521                            / MAXIMUM_BIGRAM_FREQUENCY)
522                            * (BIGRAM_MULTIPLIER_MAX - BIGRAM_MULTIPLIER_MIN)
523                            + BIGRAM_MULTIPLIER_MIN;
524                    /* Log.d(TAG,"bigram num: " + bigramSuggestion
525                            + "  wordB: " + mBigramSuggestions.get(bigramSuggestion).toString()
526                            + "  currentScore: " + score + "  bigramScore: "
527                            + mBigramScores[bigramSuggestion]
528                            + "  multiplier: " + multiplier); */
529                    score = (int)Math.round((score * multiplier));
530                }
531            }
532
533            // Check the last one's score and bail
534            if (sortedScores[prefMaxSuggestions - 1] >= score) return true;
535            while (pos < prefMaxSuggestions) {
536                if (sortedScores[pos] < score
537                        || (sortedScores[pos] == score && length < suggestions.get(pos).length())) {
538                    break;
539                }
540                pos++;
541            }
542        }
543        if (pos >= prefMaxSuggestions) {
544            return true;
545        }
546
547        System.arraycopy(sortedScores, pos, sortedScores, pos + 1, prefMaxSuggestions - pos - 1);
548        sortedScores[pos] = score;
549        int poolSize = mStringPool.size();
550        StringBuilder sb = poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1)
551                : new StringBuilder(getApproxMaxWordLength());
552        sb.setLength(0);
553        // TODO: Must pay attention to locale when changing case.
554        if (mIsAllUpperCase) {
555            sb.append(new String(word, offset, length).toUpperCase());
556        } else if (mIsFirstCharCapitalized) {
557            sb.append(Character.toUpperCase(word[offset]));
558            if (length > 1) {
559                sb.append(word, offset + 1, length - 1);
560            }
561        } else {
562            sb.append(word, offset, length);
563        }
564        suggestions.add(pos, sb);
565        if (suggestions.size() > prefMaxSuggestions) {
566            CharSequence garbage = suggestions.remove(prefMaxSuggestions);
567            if (garbage instanceof StringBuilder) {
568                mStringPool.add(garbage);
569            }
570        } else {
571            LatinImeLogger.onAddSuggestedWord(sb.toString(), dicTypeId, dataTypeForLog);
572        }
573        return true;
574    }
575
576    private int searchBigramSuggestion(final char[] word, final int offset, final int length) {
577        // TODO This is almost O(n^2). Might need fix.
578        // search whether the word appeared in bigram data
579        int bigramSuggestSize = mBigramSuggestions.size();
580        for(int i = 0; i < bigramSuggestSize; i++) {
581            if(mBigramSuggestions.get(i).length() == length) {
582                boolean chk = true;
583                for(int j = 0; j < length; j++) {
584                    if(mBigramSuggestions.get(i).charAt(j) != word[offset+j]) {
585                        chk = false;
586                        break;
587                    }
588                }
589                if(chk) return i;
590            }
591        }
592
593        return -1;
594    }
595
596    private void collectGarbage(ArrayList<CharSequence> suggestions, int prefMaxSuggestions) {
597        int poolSize = mStringPool.size();
598        int garbageSize = suggestions.size();
599        while (poolSize < prefMaxSuggestions && garbageSize > 0) {
600            CharSequence garbage = suggestions.get(garbageSize - 1);
601            if (garbage != null && garbage instanceof StringBuilder) {
602                mStringPool.add(garbage);
603                poolSize++;
604            }
605            garbageSize--;
606        }
607        if (poolSize == prefMaxSuggestions + 1) {
608            Log.w("Suggest", "String pool got too big: " + poolSize);
609        }
610        suggestions.clear();
611    }
612
613    public void close() {
614        final Set<Dictionary> dictionaries = new HashSet<Dictionary>();
615        dictionaries.addAll(mUnigramDictionaries.values());
616        dictionaries.addAll(mBigramDictionaries.values());
617        for (final Dictionary dictionary : dictionaries) {
618            dictionary.close();
619        }
620        mMainDict = null;
621    }
622}
623