Create a way to pass the proximity info to the dictionary

This is a preparative change for inserting the spell checker.

Change-Id: Ie441879cac4f67078ec27a95f1fcbbf3ef373df7
main
Jean Chalard 2011-08-04 12:08:22 +09:00
parent 3889462439
commit 043f784198
14 changed files with 78 additions and 42 deletions

View File

@ -57,6 +57,7 @@ public class RecorrectionSuggestionEntries {
private static SuggestedWords.Builder getTypedSuggestions( private static SuggestedWords.Builder getTypedSuggestions(
Suggest suggest, KeyboardSwitcher keyboardSwitcher, WordComposer word) { Suggest suggest, KeyboardSwitcher keyboardSwitcher, WordComposer word) {
return suggest.getSuggestedWordBuilder(keyboardSwitcher.getKeyboardView(), word, null); return suggest.getSuggestedWordBuilder(keyboardSwitcher.getKeyboardView(), word, null,
keyboardSwitcher.getLatinKeyboard().getProximityInfo());
} }
} }

View File

@ -149,8 +149,8 @@ public class Keyboard {
mMostCommonKeyWidth, mKeys); mMostCommonKeyWidth, mKeys);
} }
public int getProximityInfo() { public ProximityInfo getProximityInfo() {
return mProximityInfo.getNativeProximityInfo(); return mProximityInfo;
} }
public boolean hasShiftLockKey() { public boolean hasShiftLockKey() {

View File

@ -19,6 +19,7 @@ package com.android.inputmethod.keyboard;
import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.Utils;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class ProximityInfo { public class ProximityInfo {
@ -54,6 +55,10 @@ public class ProximityInfo {
computeNearestNeighbors(keyWidth, keys); computeNearestNeighbors(keyWidth, keys);
} }
public static ProximityInfo getDummyProximityInfo() {
return new ProximityInfo(1, 1, 1, 1, 1, Collections.<Key>emptyList());
}
private int mNativeProximityInfo; private int mNativeProximityInfo;
static { static {
Utils.loadNativeLibrary(); Utils.loadNativeLibrary();

View File

@ -156,10 +156,11 @@ public class BinaryDictionary extends Dictionary {
} }
} }
// proximityInfo may not be null.
@Override @Override
public void getWords(final WordComposer codes, final WordCallback callback) { public void getWords(final WordComposer codes, final WordCallback callback,
final int count = getSuggestions(codes, mKeyboardSwitcher.getLatinKeyboard(), final ProximityInfo proximityInfo) {
mOutputChars, mScores); final int count = getSuggestions(codes, proximityInfo, mOutputChars, mScores);
for (int j = 0; j < count; ++j) { for (int j = 0; j < count; ++j) {
if (mScores[j] < 1) break; if (mScores[j] < 1) break;
@ -179,8 +180,9 @@ public class BinaryDictionary extends Dictionary {
return mNativeDict != 0; return mNativeDict != 0;
} }
/* package for test */ int getSuggestions(final WordComposer codes, final Keyboard keyboard, // proximityInfo may not be null.
char[] outputChars, int[] scores) { /* package for test */ int getSuggestions(final WordComposer codes,
final ProximityInfo proximityInfo, char[] outputChars, int[] scores) {
if (!isValidDictionary()) return -1; if (!isValidDictionary()) return -1;
final int codesSize = codes.size(); final int codesSize = codes.size();
@ -196,9 +198,8 @@ public class BinaryDictionary extends Dictionary {
Arrays.fill(outputChars, (char) 0); Arrays.fill(outputChars, (char) 0);
Arrays.fill(scores, 0); Arrays.fill(scores, 0);
final int proximityInfo = keyboard == null ? 0 : keyboard.getProximityInfo();
return getSuggestionsNative( return getSuggestionsNative(
mNativeDict, proximityInfo, mNativeDict, proximityInfo.getNativeProximityInfo(),
codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize, codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize,
mFlags, outputChars, scores); mFlags, outputChars, scores);
} }

View File

@ -16,6 +16,8 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import com.android.inputmethod.keyboard.ProximityInfo;
/** /**
* Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
* strokes. * strokes.
@ -61,9 +63,11 @@ public abstract class Dictionary {
* words are added through the callback object. * words are added through the callback object.
* @param composer the key sequence to match * @param composer the key sequence to match
* @param callback the callback object to send matched words to as possible candidates * @param callback the callback object to send matched words to as possible candidates
* @param proximityInfo the object for key proximity. May be ignored by some implementations.
* @see WordCallback#addWord(char[], int, int, int, int, DataType) * @see WordCallback#addWord(char[], int, int, int, int, DataType)
*/ */
abstract public void getWords(final WordComposer composer, final WordCallback callback); abstract public void getWords(final WordComposer composer, final WordCallback callback,
final ProximityInfo proximityInfo);
/** /**
* Searches for pairs in the bigram dictionary that matches the previous word and all the * Searches for pairs in the bigram dictionary that matches the previous word and all the

View File

@ -16,6 +16,8 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import com.android.inputmethod.keyboard.ProximityInfo;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -47,9 +49,10 @@ public class DictionaryCollection extends Dictionary {
} }
@Override @Override
public void getWords(final WordComposer composer, final WordCallback callback) { public void getWords(final WordComposer composer, final WordCallback callback,
final ProximityInfo proximityInfo) {
for (final Dictionary dict : mDictionaries) for (final Dictionary dict : mDictionaries)
dict.getWords(composer, callback); dict.getWords(composer, callback, proximityInfo);
} }
@Override @Override

View File

@ -20,6 +20,7 @@ import android.content.Context;
import android.os.AsyncTask; import android.os.AsyncTask;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.ProximityInfo;
import java.util.LinkedList; import java.util.LinkedList;
@ -193,7 +194,8 @@ public class ExpandableDictionary extends Dictionary {
} }
@Override @Override
public void getWords(final WordComposer codes, final WordCallback callback) { public void getWords(final WordComposer codes, final WordCallback callback,
final ProximityInfo proximityInfo) {
synchronized (mUpdatingLock) { synchronized (mUpdatingLock) {
// If we need to update, start off a background task // If we need to update, start off a background task
if (mRequiresReload) startDictionaryLoadingTaskLocked(); if (mRequiresReload) startDictionaryLoadingTaskLocked();

View File

@ -1600,7 +1600,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
// getSuggestedWordBuilder handles gracefully a null value of prevWord // getSuggestedWordBuilder handles gracefully a null value of prevWord
final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(
mKeyboardSwitcher.getKeyboardView(), wordComposer, prevWord); mKeyboardSwitcher.getKeyboardView(), wordComposer, prevWord,
mKeyboardSwitcher.getLatinKeyboard().getProximityInfo());
boolean autoCorrectionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasAutoCorrection(); boolean autoCorrectionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasAutoCorrection();
final CharSequence typedWord = wordComposer.getTypedWord(); final CharSequence typedWord = wordComposer.getTypedWord();
@ -1825,7 +1826,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final CharSequence prevWord = EditingUtils.getThisWord(getCurrentInputConnection(), final CharSequence prevWord = EditingUtils.getThisWord(getCurrentInputConnection(),
mSettingsValues.mWordSeparators); mSettingsValues.mWordSeparators);
SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(
mKeyboardSwitcher.getKeyboardView(), sEmptyWordComposer, prevWord); mKeyboardSwitcher.getKeyboardView(), sEmptyWordComposer, prevWord,
mKeyboardSwitcher.getLatinKeyboard().getProximityInfo());
if (builder.size() > 0) { if (builder.size() > 0) {
// Explicitly supply an empty typed word (the no-second-arg version of // Explicitly supply an empty typed word (the no-second-arg version of

View File

@ -22,6 +22,8 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import com.android.inputmethod.keyboard.ProximityInfo;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -263,9 +265,10 @@ public class Suggest implements Dictionary.WordCallback {
* @param prevWordForBigram previous word (used only for bigram) * @param prevWordForBigram previous word (used only for bigram)
* @return suggested words object. * @return suggested words object.
*/ */
public SuggestedWords getSuggestions(View view, WordComposer wordComposer, public SuggestedWords getSuggestions(final View view, final WordComposer wordComposer,
CharSequence prevWordForBigram) { final CharSequence prevWordForBigram, final ProximityInfo proximityInfo) {
return getSuggestedWordBuilder(view, wordComposer, prevWordForBigram).build(); return getSuggestedWordBuilder(view, wordComposer, prevWordForBigram,
proximityInfo).build();
} }
private CharSequence capitalizeWord(boolean all, boolean first, CharSequence word) { private CharSequence capitalizeWord(boolean all, boolean first, CharSequence word) {
@ -299,8 +302,9 @@ public class Suggest implements Dictionary.WordCallback {
} }
// TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder
public SuggestedWords.Builder getSuggestedWordBuilder(View view, WordComposer wordComposer, public SuggestedWords.Builder getSuggestedWordBuilder(final View view,
CharSequence prevWordForBigram) { final WordComposer wordComposer, CharSequence prevWordForBigram,
final ProximityInfo proximityInfo) {
LatinImeLogger.onStartSuggestion(prevWordForBigram); LatinImeLogger.onStartSuggestion(prevWordForBigram);
mAutoCorrection.init(); mAutoCorrection.init();
mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized(); mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized();
@ -365,7 +369,7 @@ public class Suggest implements Dictionary.WordCallback {
if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST)) if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST))
continue; continue;
final Dictionary dictionary = mUnigramDictionaries.get(key); final Dictionary dictionary = mUnigramDictionaries.get(key);
dictionary.getWords(wordComposer, this); dictionary.getWords(wordComposer, this, proximityInfo);
} }
} }
CharSequence autoText = null; CharSequence autoText = null;

View File

@ -26,6 +26,8 @@ import android.net.Uri;
import android.os.RemoteException; import android.os.RemoteException;
import android.provider.UserDictionary.Words; import android.provider.UserDictionary.Words;
import com.android.inputmethod.keyboard.ProximityInfo;
public class UserDictionary extends ExpandableDictionary { public class UserDictionary extends ExpandableDictionary {
private static final String[] PROJECTION_QUERY = { private static final String[] PROJECTION_QUERY = {
@ -150,8 +152,9 @@ public class UserDictionary extends ExpandableDictionary {
} }
@Override @Override
public synchronized void getWords(final WordComposer codes, final WordCallback callback) { public synchronized void getWords(final WordComposer codes, final WordCallback callback,
super.getWords(codes, callback); final ProximityInfo proximityInfo) {
super.getWords(codes, callback, proximityInfo);
} }
@Override @Override

View File

@ -21,6 +21,8 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import com.android.inputmethod.keyboard.ProximityInfo;
import java.util.HashMap; import java.util.HashMap;
public class WhitelistDictionary extends Dictionary { public class WhitelistDictionary extends Dictionary {
@ -89,7 +91,8 @@ public class WhitelistDictionary extends Dictionary {
// Not used for WhitelistDictionary. We use getWhitelistedWord() in Suggest.java instead // Not used for WhitelistDictionary. We use getWhitelistedWord() in Suggest.java instead
@Override @Override
public void getWords(WordComposer composer, WordCallback callback) { public void getWords(final WordComposer composer, final WordCallback callback,
final ProximityInfo proximityInfo) {
} }
@Override @Override

View File

@ -20,6 +20,7 @@ import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import com.android.inputmethod.compat.ArraysCompatUtils; import com.android.inputmethod.compat.ArraysCompatUtils;
import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.Dictionary.DataType; import com.android.inputmethod.latin.Dictionary.DataType;
import com.android.inputmethod.latin.Dictionary.WordCallback; import com.android.inputmethod.latin.Dictionary.WordCallback;
@ -109,7 +110,7 @@ public class SpellChecker {
composer.add(character, new int[] { character }, composer.add(character, new int[] { character },
WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
} }
mDictionary.getWords(composer, suggestionsGatherer); mDictionary.getWords(composer, suggestionsGatherer, ProximityInfo.getDummyProximityInfo());
return suggestionsGatherer.getGatheredSuggestions(); return suggestionsGatherer.getGatheredSuggestions();
} }
} }

View File

@ -28,7 +28,7 @@ import java.io.File;
public class SuggestHelper { public class SuggestHelper {
protected final Suggest mSuggest; protected final Suggest mSuggest;
private final LatinKeyboard mKeyboard; protected final LatinKeyboard mKeyboard;
private final KeyDetector mKeyDetector; private final KeyDetector mKeyDetector;
public SuggestHelper(Context context, int dictionaryId, KeyboardId keyboardId) { public SuggestHelper(Context context, int dictionaryId, KeyboardId keyboardId) {
@ -94,19 +94,22 @@ public class SuggestHelper {
// TODO: This may be slow, but is OK for test so far. // TODO: This may be slow, but is OK for test so far.
public SuggestedWords getSuggestions(CharSequence typed) { public SuggestedWords getSuggestions(CharSequence typed) {
return mSuggest.getSuggestions(null, createWordComposer(typed), null); return mSuggest.getSuggestions(null, createWordComposer(typed), null,
mKeyboard.getProximityInfo());
} }
public CharSequence getFirstSuggestion(CharSequence typed) { public CharSequence getFirstSuggestion(CharSequence typed) {
WordComposer word = createWordComposer(typed); WordComposer word = createWordComposer(typed);
SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null); SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null,
mKeyboard.getProximityInfo());
// Note that suggestions.getWord(0) is the word user typed. // Note that suggestions.getWord(0) is the word user typed.
return suggestions.size() > 1 ? suggestions.getWord(1) : null; return suggestions.size() > 1 ? suggestions.getWord(1) : null;
} }
public CharSequence getAutoCorrection(CharSequence typed) { public CharSequence getAutoCorrection(CharSequence typed) {
WordComposer word = createWordComposer(typed); WordComposer word = createWordComposer(typed);
SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null); SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null,
mKeyboard.getProximityInfo());
// Note that suggestions.getWord(0) is the word user typed. // Note that suggestions.getWord(0) is the word user typed.
return (suggestions.size() > 1 && mSuggest.hasAutoCorrection()) return (suggestions.size() > 1 && mSuggest.hasAutoCorrection())
? suggestions.getWord(1) : null; ? suggestions.getWord(1) : null;
@ -114,7 +117,8 @@ public class SuggestHelper {
public int getSuggestIndex(CharSequence typed, CharSequence expected) { public int getSuggestIndex(CharSequence typed, CharSequence expected) {
WordComposer word = createWordComposer(typed); WordComposer word = createWordComposer(typed);
SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null); SuggestedWords suggestions = mSuggest.getSuggestions(null, word, null,
mKeyboard.getProximityInfo());
// Note that suggestions.getWord(0) is the word user typed. // Note that suggestions.getWord(0) is the word user typed.
for (int i = 1; i < suggestions.size(); i++) { for (int i = 1; i < suggestions.size(); i++) {
if (TextUtils.equals(suggestions.getWord(i), expected)) if (TextUtils.equals(suggestions.getWord(i), expected))
@ -126,21 +130,23 @@ public class SuggestHelper {
private void getBigramSuggestions(CharSequence previous, CharSequence typed) { private void getBigramSuggestions(CharSequence previous, CharSequence typed) {
if (!TextUtils.isEmpty(previous) && (typed.length() > 1)) { if (!TextUtils.isEmpty(previous) && (typed.length() > 1)) {
WordComposer firstChar = createWordComposer(Character.toString(typed.charAt(0))); WordComposer firstChar = createWordComposer(Character.toString(typed.charAt(0)));
mSuggest.getSuggestions(null, firstChar, previous); mSuggest.getSuggestions(null, firstChar, previous, mKeyboard.getProximityInfo());
} }
} }
public CharSequence getBigramFirstSuggestion(CharSequence previous, CharSequence typed) { public CharSequence getBigramFirstSuggestion(CharSequence previous, CharSequence typed) {
WordComposer word = createWordComposer(typed); WordComposer word = createWordComposer(typed);
getBigramSuggestions(previous, typed); getBigramSuggestions(previous, typed);
SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous); SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous,
mKeyboard.getProximityInfo());
return suggestions.size() > 1 ? suggestions.getWord(1) : null; return suggestions.size() > 1 ? suggestions.getWord(1) : null;
} }
public CharSequence getBigramAutoCorrection(CharSequence previous, CharSequence typed) { public CharSequence getBigramAutoCorrection(CharSequence previous, CharSequence typed) {
WordComposer word = createWordComposer(typed); WordComposer word = createWordComposer(typed);
getBigramSuggestions(previous, typed); getBigramSuggestions(previous, typed);
SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous); SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous,
mKeyboard.getProximityInfo());
return (suggestions.size() > 1 && mSuggest.hasAutoCorrection()) return (suggestions.size() > 1 && mSuggest.hasAutoCorrection())
? suggestions.getWord(1) : null; ? suggestions.getWord(1) : null;
} }
@ -149,7 +155,8 @@ public class SuggestHelper {
CharSequence expected) { CharSequence expected) {
WordComposer word = createWordComposer(typed); WordComposer word = createWordComposer(typed);
getBigramSuggestions(previous, typed); getBigramSuggestions(previous, typed);
SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous); SuggestedWords suggestions = mSuggest.getSuggestions(null, word, previous,
mKeyboard.getProximityInfo());
for (int i = 1; i < suggestions.size(); i++) { for (int i = 1; i < suggestions.size(); i++) {
if (TextUtils.equals(suggestions.getWord(i), expected)) if (TextUtils.equals(suggestions.getWord(i), expected))
return i; return i;

View File

@ -58,7 +58,7 @@ public class UserBigramSuggestHelper extends SuggestHelper {
flushUserBigrams(); flushUserBigrams();
if (!TextUtils.isEmpty(previous) && !TextUtils.isEmpty(Character.toString(typed))) { if (!TextUtils.isEmpty(previous) && !TextUtils.isEmpty(Character.toString(typed))) {
WordComposer firstChar = createWordComposer(Character.toString(typed)); WordComposer firstChar = createWordComposer(Character.toString(typed));
mSuggest.getSuggestions(null, firstChar, previous); mSuggest.getSuggestions(null, firstChar, previous, mKeyboard.getProximityInfo());
boolean reloading = mUserBigram.reloadDictionaryIfRequired(); boolean reloading = mUserBigram.reloadDictionaryIfRequired();
if (reloading) mUserBigram.waitForDictionaryLoading(); if (reloading) mUserBigram.waitForDictionaryLoading();
mUserBigram.getBigrams(firstChar, previous, mSuggest); mUserBigram.getBigrams(firstChar, previous, mSuggest);