1 /* 2 * Copyright (C) 2011 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 17 package com.android.inputmethod.latin; 18 19 import com.android.inputmethod.keyboard.KeyboardId; 20 21 import android.content.Context; 22 import android.text.TextUtils; 23 24 import java.io.File; 25 import java.util.Locale; 26 import java.util.StringTokenizer; 27 28 public class UserBigramSuggestHelper extends SuggestHelper { 29 private final Context mContext; 30 private UserBigramDictionary mUserBigram; 31 32 public UserBigramSuggestHelper(final Context context, final File dictionaryPath, 33 final long startOffset, final long length, final int userBigramMax, 34 final int userBigramDelete, final KeyboardId keyboardId, final Locale locale) { 35 super(context, dictionaryPath, startOffset, length, keyboardId, locale); 36 mContext = context; 37 mUserBigram = new UserBigramDictionary(context, null, locale.toString(), 38 Suggest.DIC_USER); 39 mUserBigram.setDatabaseMax(userBigramMax); 40 mUserBigram.setDatabaseDelete(userBigramDelete); 41 mSuggest.setCorrectionMode(Suggest.CORRECTION_FULL_BIGRAM); 42 mSuggest.setUserBigramDictionary(mUserBigram); 43 } 44 45 public void changeUserBigramLocale(Locale locale) { 46 if (mUserBigram != null) { 47 flushUserBigrams(); 48 mUserBigram.close(); 49 mUserBigram = new UserBigramDictionary(mContext, null, locale.toString(), 50 Suggest.DIC_USER); 51 mSuggest.setUserBigramDictionary(mUserBigram); 52 } 53 } 54 55 public int searchUserBigramSuggestion(CharSequence previous, char typed, 56 CharSequence expected) { 57 if (mUserBigram == null) return -1; 58 59 flushUserBigrams(); 60 if (!TextUtils.isEmpty(previous) && !TextUtils.isEmpty(Character.toString(typed))) { 61 WordComposer firstChar = createWordComposer(Character.toString(typed)); 62 mSuggest.getSuggestions(firstChar, previous, mKeyboard.getProximityInfo()); 63 boolean reloading = mUserBigram.reloadDictionaryIfRequired(); 64 if (reloading) mUserBigram.waitForDictionaryLoading(); 65 mUserBigram.getBigrams(firstChar, previous, mSuggest); 66 } 67 68 for (int i = 0; i < mSuggest.mBigramSuggestions.size(); i++) { 69 final CharSequence word = mSuggest.mBigramSuggestions.get(i); 70 if (TextUtils.equals(word, expected)) 71 return i; 72 } 73 74 return -1; 75 } 76 77 public void addToUserBigram(String sentence) { 78 StringTokenizer st = new StringTokenizer(sentence); 79 String previous = null; 80 while (st.hasMoreTokens()) { 81 String current = st.nextToken(); 82 if (previous != null) { 83 addToUserBigram(new String[] {previous, current}); 84 } 85 previous = current; 86 } 87 } 88 89 public void addToUserBigram(String[] pair) { 90 if (mUserBigram != null && pair.length == 2) { 91 mUserBigram.addBigrams(pair[0], pair[1]); 92 } 93 } 94 95 public void flushUserBigrams() { 96 if (mUserBigram != null) { 97 mUserBigram.flushPendingWrites(); 98 mUserBigram.waitUntilUpdateDBDone(); 99 } 100 } 101 } 102