diff options
Diffstat (limited to 'tests/src/com/android/inputmethod/latin/SuggestHelper.java')
-rw-r--r-- | tests/src/com/android/inputmethod/latin/SuggestHelper.java | 277 |
1 files changed, 85 insertions, 192 deletions
diff --git a/tests/src/com/android/inputmethod/latin/SuggestHelper.java b/tests/src/com/android/inputmethod/latin/SuggestHelper.java index 759bfa18a..87ea011fa 100644 --- a/tests/src/com/android/inputmethod/latin/SuggestHelper.java +++ b/tests/src/com/android/inputmethod/latin/SuggestHelper.java @@ -16,253 +16,146 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.KeyDetector; +import com.android.inputmethod.keyboard.KeyboardId; +import com.android.inputmethod.keyboard.LatinKeyboard; + import android.content.Context; 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; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.channels.Channels; +import java.io.File; 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; + protected final Suggest mSuggest; + private final LatinKeyboard mKeyboard; + private final KeyDetector mKeyDetector; - /** Uses main dictionary only **/ - public SuggestHelper(String tag, Context context, int[] resId) { - TAG = tag; - InputStream[] is = null; - try { - // merging separated dictionary into one if dictionary is separated - int total = 0; - is = new InputStream[resId.length]; - for (int i = 0; i < resId.length; i++) { - is[i] = context.getResources().openRawResource(resId[i]); - total += is[i].available(); - } + public SuggestHelper(Context context, int dictionaryId, KeyboardId keyboardId) { + // Use null as the locale for Suggest so as to force it to use the internal dictionary + // (and not try to find a dictionary provider for a specified locale) + mSuggest = new Suggest(context, dictionaryId, null); + mKeyboard = new LatinKeyboard(context, keyboardId, keyboardId.mWidth); + mKeyDetector = new KeyDetector(); + init(); + } - ByteBuffer byteBuffer = - ByteBuffer.allocateDirect(total).order(ByteOrder.nativeOrder()); - int got = 0; - for (int i = 0; i < resId.length; i++) { - got += Channels.newChannel(is[i]).read(byteBuffer); - } - if (got != total) { - Log.w(TAG, "Read " + got + " bytes, expected " + total); - } else { - mSuggest = new Suggest(context, byteBuffer); - Log.i(TAG, "Created mSuggest " + total + " bytes"); - } - } catch (IOException e) { - Log.w(TAG, "No available memory for binary dictionary"); - } finally { - try { - 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"); - } - } - mSuggest.setAutoTextEnabled(false); - mSuggest.setCorrectionMode(Suggest.CORRECTION_FULL_BIGRAM); + protected SuggestHelper(Context context, File dictionaryPath, long startOffset, long length, + KeyboardId keyboardId) { + mSuggest = new Suggest(context, dictionaryPath, startOffset, length, null); + mKeyboard = new LatinKeyboard(context, keyboardId, keyboardId.mWidth); + mKeyDetector = new KeyDetector(); + init(); } - /** 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); + private void init() { + mSuggest.setQuickFixesEnabled(false); + mSuggest.setCorrectionMode(Suggest.CORRECTION_FULL); + mKeyDetector.setKeyboard(mKeyboard, 0, 0); + mKeyDetector.setProximityCorrectionEnabled(true); + mKeyDetector.setProximityThreshold(mKeyboard.getMostCommonKeyWidth()); } - 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); + public void setCorrectionMode(int correctionMode) { + mSuggest.setCorrectionMode(correctionMode); + } + + public boolean hasMainDictionary() { + return mSuggest.hasMainDictionary(); + } + + private void addKeyInfo(WordComposer word, char c) { + final List<Key> keys = mKeyboard.getKeys(); + for (final Key key : keys) { + if (key.mCode == c) { + final int x = key.mX + key.mWidth / 2; + final int y = key.mY + key.mHeight / 2; + final int[] codes = mKeyDetector.newCodeArray(); + mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes); + word.add(c, codes, x, y); + return; + } } + word.add(c, new int[] { c }, WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); } - private WordComposer createWordComposer(CharSequence s) { + protected WordComposer createWordComposer(CharSequence s) { WordComposer word = new WordComposer(); for (int i = 0; i < s.length(); i++) { final char c = s.charAt(i); - int[] codes; - // If it's not a lowercase letter, don't find adjacent letters - if (c < 'a' || c > 'z') { - codes = new int[] { c }; - } else { - codes = adjacents[c - 'a']; - } - word.add(c, codes); + addKeyInfo(word, c); } return word; } - private void showList(String title, List<CharSequence> suggestions) { - Log.i(TAG, title); - for (int i = 0; i < suggestions.size(); i++) { - Log.i(title, suggestions.get(i) + ", "); - } + public boolean isValidWord(CharSequence typed) { + return AutoCorrection.isValidWordForAutoCorrection(mSuggest.getUnigramDictionaries(), + typed, false); } - private boolean isDefaultSuggestion(List<CharSequence> suggestions, CharSequence word) { - // Check if either the word is what you typed or the first alternative - return suggestions.size() > 0 && - (/*TextUtils.equals(suggestions.get(0), word) || */ - (suggestions.size() > 1 && TextUtils.equals(suggestions.get(1), word))); + // TODO: This may be slow, but is OK for test so far. + public SuggestedWords getSuggestions(CharSequence typed) { + return mSuggest.getSuggestions(null, createWordComposer(typed), null); } - boolean isDefaultSuggestion(CharSequence typed, CharSequence expected) { + public CharSequence getFirstSuggestion(CharSequence typed) { WordComposer word = createWordComposer(typed); - List<CharSequence> suggestions = mSuggest.getSuggestions(null, word, false, null); - return isDefaultSuggestion(suggestions, expected); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null); + // Note that suggestions.getWord(0) is the word user typed. + return suggestions.size() > 1 ? suggestions.getWord(1) : null; } - boolean isDefaultCorrection(CharSequence typed, CharSequence expected) { + public CharSequence getAutoCorrection(CharSequence typed) { WordComposer word = createWordComposer(typed); - List<CharSequence> suggestions = mSuggest.getSuggestions(null, word, false, null); - return isDefaultSuggestion(suggestions, expected) && mSuggest.hasMinimalCorrection(); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null); + // Note that suggestions.getWord(0) is the word user typed. + return (suggestions.size() > 1 && mSuggest.hasAutoCorrection()) + ? suggestions.getWord(1) : null; } - boolean isASuggestion(CharSequence typed, CharSequence expected) { + public int getSuggestIndex(CharSequence typed, CharSequence expected) { WordComposer word = createWordComposer(typed); - List<CharSequence> suggestions = mSuggest.getSuggestions(null, word, false, null); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null); + // Note that suggestions.getWord(0) is the word user typed. for (int i = 1; i < suggestions.size(); i++) { - if (TextUtils.equals(suggestions.get(i), expected)) return true; + if (TextUtils.equals(suggestions.getWord(i), expected)) + return i; } - return false; + return -1; } private void getBigramSuggestions(CharSequence previous, CharSequence typed) { if (!TextUtils.isEmpty(previous) && (typed.length() > 1)) { WordComposer firstChar = createWordComposer(Character.toString(typed.charAt(0))); - mSuggest.getSuggestions(null, firstChar, false, previous); + mSuggest.getSuggestions(null, firstChar, previous); } } - boolean isDefaultNextSuggestion(CharSequence previous, CharSequence typed, - CharSequence expected) { + public CharSequence getBigramFirstSuggestion(CharSequence previous, CharSequence typed) { WordComposer word = createWordComposer(typed); getBigramSuggestions(previous, typed); - List<CharSequence> suggestions = mSuggest.getSuggestions(null, word, false, previous); - return isDefaultSuggestion(suggestions, expected); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous); + return suggestions.size() > 1 ? suggestions.getWord(1) : null; } - boolean isDefaultNextCorrection(CharSequence previous, CharSequence typed, - CharSequence expected) { + public CharSequence getBigramAutoCorrection(CharSequence previous, CharSequence typed) { WordComposer word = createWordComposer(typed); getBigramSuggestions(previous, typed); - List<CharSequence> suggestions = mSuggest.getSuggestions(null, word, false, previous); - return isDefaultSuggestion(suggestions, expected) && mSuggest.hasMinimalCorrection(); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous); + return (suggestions.size() > 1 && mSuggest.hasAutoCorrection()) + ? suggestions.getWord(1) : null; } - boolean isASuggestion(CharSequence previous, CharSequence typed, + public int searchBigramSuggestion(CharSequence previous, CharSequence typed, CharSequence expected) { WordComposer word = createWordComposer(typed); getBigramSuggestions(previous, typed); - List<CharSequence> suggestions = mSuggest.getSuggestions(null, word, false, previous); + SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous); for (int i = 1; i < suggestions.size(); i++) { - if (TextUtils.equals(suggestions.get(i), expected)) return true; + if (TextUtils.equals(suggestions.getWord(i), expected)) + return i; } - return false; - } - - boolean isValid(CharSequence typed) { - return mSuggest.isValidWord(typed); + return -1; } - - 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}, - {'c','v','f','x','g',}, - {'d','f','r','e','s','x',-1}, - {'e','w','r','s','d',-1}, - {'f','g','d','c','t','r',-1}, - {'g','h','f','y','t','v',-1}, - {'h','j','u','g','b','y',-1}, - {'i','o','u','k',-1}, - {'j','k','i','h','u','n',-1}, - {'k','l','o','j','i','m',-1}, - {'l','k','o','p',-1}, - {'m','k','n','l',-1}, - {'n','m','j','k','b',-1}, - {'o','p','i','l',-1}, - {'p','o',-1}, - {'q','w',-1}, - {'r','t','e','f',-1}, - {'s','d','e','w','a','z',-1}, - {'t','y','r',-1}, - {'u','y','i','h','j',-1}, - {'v','b','g','c','h',-1}, - {'w','e','q',-1}, - {'x','c','d','z','f',-1}, - {'y','u','t','h','g',-1}, - {'z','s','x','a','d',-1}, - }; } |