BinaryDictionary.java revision eaef1c500703b4ee378821884c7b108815ed2983
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import com.android.inputmethod.keyboard.KeyboardSwitcher;
20import com.android.inputmethod.keyboard.ProximityInfo;
21
22import android.content.Context;
23import android.content.res.AssetFileDescriptor;
24import android.util.Log;
25
26import java.io.File;
27import java.util.Arrays;
28
29/**
30 * Implements a static, compacted, binary dictionary of standard words.
31 */
32public class BinaryDictionary extends Dictionary {
33
34    /**
35     * There is difference between what java and native code can handle.
36     * This value should only be used in BinaryDictionary.java
37     * It is necessary to keep it at this value because some languages e.g. German have
38     * really long words.
39     */
40    public static final int MAX_WORD_LENGTH = 48;
41
42    private static final String TAG = "BinaryDictionary";
43    private static final int MAX_PROXIMITY_CHARS_SIZE = ProximityInfo.MAX_PROXIMITY_CHARS_SIZE;
44    private static final int MAX_WORDS = 18;
45    private static final int MAX_BIGRAMS = 60;
46
47    private static final int TYPED_LETTER_MULTIPLIER = 2;
48
49    private static final BinaryDictionary sInstance = new BinaryDictionary();
50    private int mDicTypeId;
51    private int mNativeDict;
52    private long mDictLength;
53    private final int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_PROXIMITY_CHARS_SIZE];
54    private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS];
55    private final char[] mOutputChars_bigrams = new char[MAX_WORD_LENGTH * MAX_BIGRAMS];
56    private final int[] mFrequencies = new int[MAX_WORDS];
57    private final int[] mFrequencies_bigrams = new int[MAX_BIGRAMS];
58
59    private final KeyboardSwitcher mKeyboardSwitcher = KeyboardSwitcher.getInstance();
60
61    private BinaryDictionary() {
62    }
63
64    /**
65     * Initialize a dictionary from a raw resource file
66     * @param context application context for reading resources
67     * @param resId the resource containing the raw binary dictionary
68     * @return initialized instance of BinaryDictionary
69     */
70    public static BinaryDictionary initDictionary(Context context, int resId, int dicTypeId) {
71        synchronized (sInstance) {
72            sInstance.closeInternal();
73            try {
74                final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
75                if (afd == null) {
76                    Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
77                    return null;
78                }
79                final String sourceDir = context.getApplicationInfo().sourceDir;
80                final File packagePath = new File(sourceDir);
81                // TODO: Come up with a way to handle a directory.
82                if (!packagePath.isFile()) {
83                    Log.e(TAG, "sourceDir is not a file: " + sourceDir);
84                    return null;
85                }
86                sInstance.loadDictionary(sourceDir, afd.getStartOffset(), afd.getLength());
87                sInstance.mDicTypeId = dicTypeId;
88            } catch (android.content.res.Resources.NotFoundException e) {
89                Log.e(TAG, "Could not find the resource. resId=" + resId);
90                return null;
91            }
92        }
93        return sInstance;
94    }
95
96    // For unit test
97    /* package */ static BinaryDictionary initDictionary(File dictionary, long startOffset,
98            long length, int dicTypeId) {
99        synchronized (sInstance) {
100            sInstance.closeInternal();
101            if (dictionary.isFile()) {
102                sInstance.loadDictionary(dictionary.getAbsolutePath(), startOffset, length);
103                sInstance.mDicTypeId = dicTypeId;
104            } else {
105                Log.e(TAG, "Could not find the file. path=" + dictionary.getAbsolutePath());
106                return null;
107            }
108        }
109        return sInstance;
110    }
111
112    static {
113        Utils.loadNativeLibrary();
114    }
115    private native int openNative(String sourceDir, long dictOffset, long dictSize,
116            int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength,
117            int maxWords, int maxAlternatives);
118    private native void closeNative(int dict);
119    private native boolean isValidWordNative(int nativeData, char[] word, int wordLength);
120    private native int getSuggestionsNative(int dict, int proximityInfo, int[] xCoordinates,
121            int[] yCoordinates, int[] inputCodes, int codesSize, char[] outputChars,
122            int[] frequencies);
123    private native int getBigramsNative(int dict, char[] prevWord, int prevWordLength,
124            int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies,
125            int maxWordLength, int maxBigrams, int maxAlternatives);
126
127    private final void loadDictionary(String path, long startOffset, long length) {
128        mNativeDict = openNative(path, startOffset, length,
129                    TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
130                    MAX_WORD_LENGTH, MAX_WORDS, MAX_PROXIMITY_CHARS_SIZE);
131        mDictLength = length;
132    }
133
134    @Override
135    public void getBigrams(final WordComposer codes, final CharSequence previousWord,
136            final WordCallback callback) {
137        if (mNativeDict == 0) return;
138
139        char[] chars = previousWord.toString().toCharArray();
140        Arrays.fill(mOutputChars_bigrams, (char) 0);
141        Arrays.fill(mFrequencies_bigrams, 0);
142
143        int codesSize = codes.size();
144        Arrays.fill(mInputCodes, -1);
145        int[] alternatives = codes.getCodesAt(0);
146        System.arraycopy(alternatives, 0, mInputCodes, 0,
147                Math.min(alternatives.length, MAX_PROXIMITY_CHARS_SIZE));
148
149        int count = getBigramsNative(mNativeDict, chars, chars.length, mInputCodes, codesSize,
150                mOutputChars_bigrams, mFrequencies_bigrams, MAX_WORD_LENGTH, MAX_BIGRAMS,
151                MAX_PROXIMITY_CHARS_SIZE);
152
153        for (int j = 0; j < count; ++j) {
154            if (mFrequencies_bigrams[j] < 1) break;
155            final int start = j * MAX_WORD_LENGTH;
156            int len = 0;
157            while (len <  MAX_WORD_LENGTH && mOutputChars_bigrams[start + len] != 0) {
158                ++len;
159            }
160            if (len > 0) {
161                callback.addWord(mOutputChars_bigrams, start, len, mFrequencies_bigrams[j],
162                        mDicTypeId, DataType.BIGRAM);
163            }
164        }
165    }
166
167    @Override
168    public void getWords(final WordComposer codes, final WordCallback callback) {
169        if (mNativeDict == 0) return;
170
171        final int codesSize = codes.size();
172        // Won't deal with really long words.
173        if (codesSize > MAX_WORD_LENGTH - 1) return;
174
175        Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE);
176        for (int i = 0; i < codesSize; i++) {
177            int[] alternatives = codes.getCodesAt(i);
178            System.arraycopy(alternatives, 0, mInputCodes, i * MAX_PROXIMITY_CHARS_SIZE,
179                    Math.min(alternatives.length, MAX_PROXIMITY_CHARS_SIZE));
180        }
181        Arrays.fill(mOutputChars, (char) 0);
182        Arrays.fill(mFrequencies, 0);
183
184        int count = getSuggestionsNative(
185                mNativeDict, mKeyboardSwitcher.getLatinKeyboard().getProximityInfo(),
186                codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize,
187                mOutputChars, mFrequencies);
188
189        for (int j = 0; j < count; ++j) {
190            if (mFrequencies[j] < 1) break;
191            final int start = j * MAX_WORD_LENGTH;
192            int len = 0;
193            while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) {
194                ++len;
195            }
196            if (len > 0) {
197                callback.addWord(mOutputChars, start, len, mFrequencies[j], mDicTypeId,
198                        DataType.UNIGRAM);
199            }
200        }
201    }
202
203    @Override
204    public boolean isValidWord(CharSequence word) {
205        if (word == null) return false;
206        char[] chars = word.toString().toCharArray();
207        return isValidWordNative(mNativeDict, chars, chars.length);
208    }
209
210    public long getSize() {
211        return mDictLength; // This value is initialized in loadDictionary()
212    }
213
214    @Override
215    public synchronized void close() {
216        closeInternal();
217    }
218
219    private void closeInternal() {
220        if (mNativeDict != 0) {
221            closeNative(mNativeDict);
222            mNativeDict = 0;
223            mDictLength = 0;
224        }
225    }
226
227    @Override
228    protected void finalize() throws Throwable {
229        try {
230            closeInternal();
231        } finally {
232            super.finalize();
233        }
234    }
235}
236