diff options
Diffstat (limited to 'tests/src/com/android')
-rw-r--r-- | tests/src/com/android/inputmethod/latin/SuggestHelper.java (renamed from tests/src/com/android/inputmethod/latin/tests/SuggestHelper.java) | 97 | ||||
-rw-r--r-- | tests/src/com/android/inputmethod/latin/SuggestPerformanceTests.java (renamed from tests/src/com/android/inputmethod/latin/tests/SuggestPerformanceTests.java) | 7 | ||||
-rw-r--r-- | tests/src/com/android/inputmethod/latin/SuggestTests.java (renamed from tests/src/com/android/inputmethod/latin/tests/SuggestTests.java) | 4 | ||||
-rw-r--r-- | tests/src/com/android/inputmethod/latin/UserBigramTests.java | 100 |
4 files changed, 191 insertions, 17 deletions
diff --git a/tests/src/com/android/inputmethod/latin/tests/SuggestHelper.java b/tests/src/com/android/inputmethod/latin/SuggestHelper.java index 107f04c7c..759bfa18a 100644 --- a/tests/src/com/android/inputmethod/latin/tests/SuggestHelper.java +++ b/tests/src/com/android/inputmethod/latin/SuggestHelper.java @@ -14,13 +14,13 @@ * the License. */ -package com.android.inputmethod.latin.tests; +package com.android.inputmethod.latin; import android.content.Context; -import android.test.AndroidTestCase; import android.text.TextUtils; import android.util.Log; import com.android.inputmethod.latin.Suggest; +import com.android.inputmethod.latin.UserBigramDictionary; import com.android.inputmethod.latin.WordComposer; import java.io.IOException; @@ -29,28 +29,32 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.Channels; import java.util.List; +import java.util.Locale; +import java.util.StringTokenizer; public class SuggestHelper { private Suggest mSuggest; + private UserBigramDictionary mUserBigram; private final String TAG; + /** Uses main dictionary only **/ public SuggestHelper(String tag, Context context, int[] resId) { TAG = tag; - InputStream[] res = null; + InputStream[] is = null; try { // merging separated dictionary into one if dictionary is separated int total = 0; - res = new InputStream[resId.length]; + is = new InputStream[resId.length]; for (int i = 0; i < resId.length; i++) { - res[i] = context.getResources().openRawResource(resId[i]); - total += res[i].available(); + is[i] = context.getResources().openRawResource(resId[i]); + total += is[i].available(); } ByteBuffer byteBuffer = ByteBuffer.allocateDirect(total).order(ByteOrder.nativeOrder()); int got = 0; for (int i = 0; i < resId.length; i++) { - got += Channels.newChannel(res[i]).read(byteBuffer); + got += Channels.newChannel(is[i]).read(byteBuffer); } if (got != total) { Log.w(TAG, "Read " + got + " bytes, expected " + total); @@ -62,8 +66,10 @@ public class SuggestHelper { Log.w(TAG, "No available memory for binary dictionary"); } finally { try { - for (int i = 0;i < res.length; i++) { - res[i].close(); + if (is != null) { + for (int i = 0; i < is.length; i++) { + is[i].close(); + } } } catch (IOException e) { Log.w(TAG, "Failed to close input stream"); @@ -73,6 +79,27 @@ public class SuggestHelper { mSuggest.setCorrectionMode(Suggest.CORRECTION_FULL_BIGRAM); } + /** Uses both main dictionary and user-bigram dictionary **/ + public SuggestHelper(String tag, Context context, int[] resId, int userBigramMax, + int userBigramDelete) { + this(tag, context, resId); + mUserBigram = new UserBigramDictionary(context, null, Locale.US.toString(), + Suggest.DIC_USER); + mUserBigram.setDatabaseMax(userBigramMax); + mUserBigram.setDatabaseDelete(userBigramDelete); + mSuggest.setUserBigramDictionary(mUserBigram); + } + + void changeUserBigramLocale(Context context, Locale locale) { + if (mUserBigram != null) { + flushUserBigrams(); + mUserBigram.close(); + mUserBigram = new UserBigramDictionary(context, null, locale.toString(), + Suggest.DIC_USER); + mSuggest.setUserBigramDictionary(mUserBigram); + } + } + private WordComposer createWordComposer(CharSequence s) { WordComposer word = new WordComposer(); for (int i = 0; i < s.length(); i++) { @@ -125,8 +152,8 @@ public class SuggestHelper { } private void getBigramSuggestions(CharSequence previous, CharSequence typed) { - if(!TextUtils.isEmpty(previous) && (typed.length() > 1)) { - WordComposer firstChar = createWordComposer(typed.charAt(0) + ""); + if (!TextUtils.isEmpty(previous) && (typed.length() > 1)) { + WordComposer firstChar = createWordComposer(Character.toString(typed.charAt(0))); mSuggest.getSuggestions(null, firstChar, false, previous); } } @@ -162,6 +189,54 @@ public class SuggestHelper { return mSuggest.isValidWord(typed); } + boolean isUserBigramSuggestion(CharSequence previous, char typed, + CharSequence expected) { + WordComposer word = createWordComposer(Character.toString(typed)); + + if (mUserBigram == null) return false; + + flushUserBigrams(); + if (!TextUtils.isEmpty(previous) && !TextUtils.isEmpty(Character.toString(typed))) { + WordComposer firstChar = createWordComposer(Character.toString(typed)); + mSuggest.getSuggestions(null, firstChar, false, previous); + boolean reloading = mUserBigram.reloadDictionaryIfRequired(); + if (reloading) mUserBigram.waitForDictionaryLoading(); + mUserBigram.getBigrams(firstChar, previous, mSuggest, null); + } + + List<CharSequence> suggestions = mSuggest.mBigramSuggestions; + for (int i = 0; i < suggestions.size(); i++) { + if (TextUtils.equals(suggestions.get(i), expected)) return true; + } + + return false; + } + + 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; + } + } + + void addToUserBigram(String[] pair) { + if (mUserBigram != null && pair.length == 2) { + mUserBigram.addBigrams(pair[0], pair[1]); + } + } + + void flushUserBigrams() { + if (mUserBigram != null) { + mUserBigram.flushPendingWrites(); + mUserBigram.waitUntilUpdateDBDone(); + } + } + final int[][] adjacents = { {'a','s','w','q',-1}, {'b','h','v','n','g','j',-1}, diff --git a/tests/src/com/android/inputmethod/latin/tests/SuggestPerformanceTests.java b/tests/src/com/android/inputmethod/latin/SuggestPerformanceTests.java index 473c440f9..7eb66d502 100644 --- a/tests/src/com/android/inputmethod/latin/tests/SuggestPerformanceTests.java +++ b/tests/src/com/android/inputmethod/latin/SuggestPerformanceTests.java @@ -14,16 +14,15 @@ * the License. */ -package com.android.inputmethod.latin.tests; +package com.android.inputmethod.latin; import android.test.AndroidTestCase; import android.util.Log; - +import com.android.inputmethod.latin.tests.R; import java.io.InputStreamReader; import java.io.InputStream; import java.io.BufferedReader; import java.util.StringTokenizer; -import java.util.regex.Pattern; public class SuggestPerformanceTests extends AndroidTestCase { private static final String TAG = "SuggestPerformanceTests"; @@ -122,6 +121,6 @@ public class SuggestPerformanceTests extends AndroidTestCase { * Check the log for detail */ public void testSuggestPerformance() { - assertTrue(runText(false) < runText(true)); + assertTrue(runText(false) <= runText(true)); } } diff --git a/tests/src/com/android/inputmethod/latin/tests/SuggestTests.java b/tests/src/com/android/inputmethod/latin/SuggestTests.java index a42422b91..8463ed316 100644 --- a/tests/src/com/android/inputmethod/latin/tests/SuggestTests.java +++ b/tests/src/com/android/inputmethod/latin/SuggestTests.java @@ -14,10 +14,10 @@ * the License. */ -package com.android.inputmethod.latin.tests; +package com.android.inputmethod.latin; import android.test.AndroidTestCase; -import android.util.Log; +import com.android.inputmethod.latin.tests.R; public class SuggestTests extends AndroidTestCase { private static final String TAG = "SuggestTests"; diff --git a/tests/src/com/android/inputmethod/latin/UserBigramTests.java b/tests/src/com/android/inputmethod/latin/UserBigramTests.java new file mode 100644 index 000000000..cbf7bd8e1 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/UserBigramTests.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2010 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.test.AndroidTestCase; +import com.android.inputmethod.latin.tests.R; +import java.util.Locale; + +public class UserBigramTests extends AndroidTestCase { + private static final String TAG = "UserBigramTests"; + + private static final int SUGGESTION_STARTS = 6; + private static final int MAX_DATA = 20; + private static final int DELETE_DATA = 10; + + private SuggestHelper sh; + + @Override + protected void setUp() { + int[] resId = new int[] { R.raw.test }; + sh = new SuggestHelper(TAG, getTestContext(), resId, MAX_DATA, DELETE_DATA); + } + + /************************** Tests ************************/ + + /** + * Test suggestion started at right time + */ + public void testUserBigram() { + for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(pair1); + for (int i = 0; i < (SUGGESTION_STARTS - 1); i++) sh.addToUserBigram(pair2); + + assertTrue(sh.isUserBigramSuggestion("user", 'b', "bigram")); + assertFalse(sh.isUserBigramSuggestion("android", 'p', "platform")); + } + + /** + * Test loading correct (locale) bigrams + */ + public void testOpenAndClose() { + for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(pair1); + assertTrue(sh.isUserBigramSuggestion("user", 'b', "bigram")); + + // change to fr_FR + sh.changeUserBigramLocale(getTestContext(), Locale.FRANCE); + for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(pair3); + assertTrue(sh.isUserBigramSuggestion("locale", 'f', "france")); + assertFalse(sh.isUserBigramSuggestion("user", 'b', "bigram")); + + // change back to en_US + sh.changeUserBigramLocale(getTestContext(), Locale.US); + assertFalse(sh.isUserBigramSuggestion("locale", 'f', "france")); + assertTrue(sh.isUserBigramSuggestion("user", 'b', "bigram")); + } + + /** + * Test data gets pruned when it is over maximum + */ + public void testPruningData() { + for (int i = 0; i < SUGGESTION_STARTS; i++) sh.addToUserBigram(sentence0); + sh.flushUserBigrams(); + assertTrue(sh.isUserBigramSuggestion("Hello", 'w', "world")); + + sh.addToUserBigram(sentence1); + sh.addToUserBigram(sentence2); + assertTrue(sh.isUserBigramSuggestion("Hello", 'w', "world")); + + // pruning should happen + sh.addToUserBigram(sentence3); + sh.addToUserBigram(sentence4); + + // trying to reopen database to check pruning happened in database + sh.changeUserBigramLocale(getTestContext(), Locale.US); + assertFalse(sh.isUserBigramSuggestion("Hello", 'w', "world")); + } + + final String[] pair1 = new String[] {"user", "bigram"}; + final String[] pair2 = new String[] {"android","platform"}; + final String[] pair3 = new String[] {"locale", "france"}; + final String sentence0 = "Hello world"; + final String sentence1 = "This is a test for user input based bigram"; + final String sentence2 = "It learns phrases that contain both dictionary and nondictionary " + + "words"; + final String sentence3 = "This should give better suggestions than the previous version"; + final String sentence4 = "Android stock keyboard is improving"; +} |