2011-02-08 09:30:26 +00:00
|
|
|
/*
|
|
|
|
* 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 com.android.inputmethod.keyboard.KeyboardId;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
|
|
|
public class UserBigramSuggestHelper extends SuggestHelper {
|
|
|
|
private final Context mContext;
|
|
|
|
private UserBigramDictionary mUserBigram;
|
|
|
|
|
2011-10-06 06:57:43 +00:00
|
|
|
public UserBigramSuggestHelper(final Context context, final File dictionaryPath,
|
|
|
|
final long startOffset, final long length, final int userBigramMax,
|
|
|
|
final int userBigramDelete, final KeyboardId keyboardId, final Locale locale) {
|
|
|
|
super(context, dictionaryPath, startOffset, length, keyboardId, locale);
|
2011-02-08 09:30:26 +00:00
|
|
|
mContext = context;
|
2011-10-06 06:57:43 +00:00
|
|
|
mUserBigram = new UserBigramDictionary(context, null, locale.toString(),
|
2011-02-08 09:30:26 +00:00
|
|
|
Suggest.DIC_USER);
|
|
|
|
mUserBigram.setDatabaseMax(userBigramMax);
|
|
|
|
mUserBigram.setDatabaseDelete(userBigramDelete);
|
|
|
|
mSuggest.setCorrectionMode(Suggest.CORRECTION_FULL_BIGRAM);
|
|
|
|
mSuggest.setUserBigramDictionary(mUserBigram);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changeUserBigramLocale(Locale locale) {
|
|
|
|
if (mUserBigram != null) {
|
|
|
|
flushUserBigrams();
|
|
|
|
mUserBigram.close();
|
|
|
|
mUserBigram = new UserBigramDictionary(mContext, null, locale.toString(),
|
|
|
|
Suggest.DIC_USER);
|
|
|
|
mSuggest.setUserBigramDictionary(mUserBigram);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int searchUserBigramSuggestion(CharSequence previous, char typed,
|
|
|
|
CharSequence expected) {
|
|
|
|
if (mUserBigram == null) return -1;
|
|
|
|
|
|
|
|
flushUserBigrams();
|
|
|
|
if (!TextUtils.isEmpty(previous) && !TextUtils.isEmpty(Character.toString(typed))) {
|
|
|
|
WordComposer firstChar = createWordComposer(Character.toString(typed));
|
2011-10-03 05:28:40 +00:00
|
|
|
mSuggest.getSuggestions(firstChar, previous, mKeyboard.getProximityInfo());
|
2011-02-08 09:30:26 +00:00
|
|
|
boolean reloading = mUserBigram.reloadDictionaryIfRequired();
|
|
|
|
if (reloading) mUserBigram.waitForDictionaryLoading();
|
2011-02-10 11:53:58 +00:00
|
|
|
mUserBigram.getBigrams(firstChar, previous, mSuggest);
|
2011-02-08 09:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mSuggest.mBigramSuggestions.size(); i++) {
|
|
|
|
final CharSequence word = mSuggest.mBigramSuggestions.get(i);
|
|
|
|
if (TextUtils.equals(word, expected))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addToUserBigram(String sentence) {
|
|
|
|
StringTokenizer st = new StringTokenizer(sentence);
|
|
|
|
String previous = null;
|
|
|
|
while (st.hasMoreTokens()) {
|
|
|
|
String current = st.nextToken();
|
|
|
|
if (previous != null) {
|
|
|
|
addToUserBigram(new String[] {previous, current});
|
|
|
|
}
|
|
|
|
previous = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addToUserBigram(String[] pair) {
|
|
|
|
if (mUserBigram != null && pair.length == 2) {
|
|
|
|
mUserBigram.addBigrams(pair[0], pair[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void flushUserBigrams() {
|
|
|
|
if (mUserBigram != null) {
|
|
|
|
mUserBigram.flushPendingWrites();
|
|
|
|
mUserBigram.waitUntilUpdateDBDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|