FileDocCategorySizeDatePackage
BinaryDictionary.javaAPI DocAndroid 1.5 API4540Wed May 06 22:42:48 BST 2009com.android.inputmethod.latin

BinaryDictionary

public class BinaryDictionary extends Dictionary
Implements a static, compacted, binary dictionary of standard words.

Fields Summary
public static final int
MAX_WORD_LENGTH
private static final int
MAX_ALTERNATIVES
private static final int
MAX_WORDS
private static final int
TYPED_LETTER_MULTIPLIER
private int
mNativeDict
private int[]
mInputCodes
private WordCallback
mWordCallback
private char[]
mOutputChars
private int[]
mFrequencies
Constructors Summary
public BinaryDictionary(android.content.Context context, int resId)
Create a dictionary from a raw resource file

param
context application context for reading resources
param
resId the resource containing the raw binary dictionary


     
        try {
            System.loadLibrary("jni_latinime");
        } catch (UnsatisfiedLinkError ule) {
            Log.e("BinaryDictionary", "Could not load native library jni_latinime");
        }
    
        if (resId != 0) {
            loadDictionary(context, resId);
        }
    
Methods Summary
public synchronized voidclose()

        if (mNativeDict != 0) {
            closeNative(mNativeDict);
            mNativeDict = 0;
        }
    
private native voidcloseNative(int dict)

protected voidfinalize()

        close();
        super.finalize();
    
private native intgetSuggestionsNative(int dict, int[] inputCodes, int codesSize, char[] outputChars, int[] frequencies, int maxWordLength, int maxWords, int maxAlternatives)

public voidgetWords(WordComposer codes, WordCallback callback)

        mWordCallback = callback;
        final int codesSize = codes.size();
        // Wont deal with really long words.
        if (codesSize > MAX_WORD_LENGTH - 1) return;
        
        Arrays.fill(mInputCodes, -1);
        for (int i = 0; i < codesSize; i++) {
            int[] alternatives = codes.getCodesAt(i);
            System.arraycopy(alternatives, 0, mInputCodes, i * MAX_ALTERNATIVES,
                    Math.min(alternatives.length, MAX_ALTERNATIVES));
        }
        Arrays.fill(mOutputChars, (char) 0);

        int count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize, mOutputChars, mFrequencies,
                MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);

        for (int j = 0; j < count; j++) {
            if (mFrequencies[j] < 1) break;
            int start = j * MAX_WORD_LENGTH;
            int len = 0;
            while (mOutputChars[start + len] != 0) {
                len++;
            }
            if (len > 0) {
                callback.addWord(mOutputChars, start, len, mFrequencies[j]);
            }
        }
    
public booleanisValidWord(java.lang.CharSequence word)

        if (word == null) return false;
        char[] chars = word.toString().toLowerCase().toCharArray();
        return isValidWordNative(mNativeDict, chars, chars.length);
    
private native booleanisValidWordNative(int nativeData, char[] word, int wordLength)

private final voidloadDictionary(android.content.Context context, int resId)

        AssetManager am = context.getResources().getAssets();
        String assetName = context.getResources().getString(resId);
        mNativeDict = openNative(am, assetName, TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER);
    
private native intopenNative(android.content.res.AssetManager am, java.lang.String resourcePath, int typedLetterMultiplier, int fullWordMultiplier)

private native voidsetParamsNative(int typedLetterMultiplier, int fullWordMultiplier)