diff options
author | 2011-02-08 18:30:26 +0900 | |
---|---|---|
committer | 2011-02-14 12:00:42 +0900 | |
commit | 5cb4eaeec01eee8eed2c8746a95bb53151ef4e74 (patch) | |
tree | bd35d57f22b28b8f308d8d68134d7aa6e02f4009 /tests/src/com/android/inputmethod/latin/UserBigramSuggestHelper.java | |
parent | d94de2076039edfe6255dded49d648380b497e1e (diff) | |
download | latinime-5cb4eaeec01eee8eed2c8746a95bb53151ef4e74.tar.gz latinime-5cb4eaeec01eee8eed2c8746a95bb53151ef4e74.tar.xz latinime-5cb4eaeec01eee8eed2c8746a95bb53151ef4e74.zip |
Reorganize suggestion related unit test
Bug: 3414081
Change-Id: Ie98c7935b25d17f1547955f8fb6ba2c5c1edb997
Diffstat (limited to 'tests/src/com/android/inputmethod/latin/UserBigramSuggestHelper.java')
-rw-r--r-- | tests/src/com/android/inputmethod/latin/UserBigramSuggestHelper.java | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/src/com/android/inputmethod/latin/UserBigramSuggestHelper.java b/tests/src/com/android/inputmethod/latin/UserBigramSuggestHelper.java new file mode 100644 index 000000000..a6fb6e1e1 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/UserBigramSuggestHelper.java @@ -0,0 +1,100 @@ +/* + * 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; + + public UserBigramSuggestHelper(Context context, File dictionaryPath, long startOffset, + long length, int userBigramMax, int userBigramDelete, KeyboardId keyboardId) { + super(context, dictionaryPath, startOffset, length, keyboardId); + mContext = context; + mUserBigram = new UserBigramDictionary(context, null, Locale.US.toString(), + 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)); + mSuggest.getSuggestions(null, firstChar, previous); + boolean reloading = mUserBigram.reloadDictionaryIfRequired(); + if (reloading) mUserBigram.waitForDictionaryLoading(); + mUserBigram.getBigrams(firstChar, previous, mSuggest, null); + } + + 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(); + } + } +} |