BinaryDictionary.java revision 6f4eba814a7f8426617db61f928a965209ebf359
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.Keyboard;
20import com.android.inputmethod.keyboard.KeyboardSwitcher;
21import com.android.inputmethod.keyboard.ProximityInfo;
22
23import android.content.Context;
24import android.content.res.AssetFileDescriptor;
25import android.util.Log;
26
27import java.io.File;
28import java.util.Arrays;
29
30/**
31 * Implements a static, compacted, binary dictionary of standard words.
32 */
33public class BinaryDictionary extends Dictionary {
34
35    /**
36     * There is difference between what java and native code can handle.
37     * This value should only be used in BinaryDictionary.java
38     * It is necessary to keep it at this value because some languages e.g. German have
39     * really long words.
40     */
41    public static final int MAX_WORD_LENGTH = 48;
42
43    private static final String TAG = "BinaryDictionary";
44    private static final int MAX_PROXIMITY_CHARS_SIZE = ProximityInfo.MAX_PROXIMITY_CHARS_SIZE;
45    private static final int MAX_WORDS = 18;
46    private static final int MAX_BIGRAMS = 60;
47
48    private static final int TYPED_LETTER_MULTIPLIER = 2;
49
50    private static final BinaryDictionary sInstance = new BinaryDictionary();
51    private int mDicTypeId;
52    private int mNativeDict;
53    private long mDictLength;
54    private final int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_PROXIMITY_CHARS_SIZE];
55    private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS];
56    private final char[] mOutputChars_bigrams = new char[MAX_WORD_LENGTH * MAX_BIGRAMS];
57    private final int[] mFrequencies = new int[MAX_WORDS];
58    private final int[] mFrequencies_bigrams = new int[MAX_BIGRAMS];
59
60    private final KeyboardSwitcher mKeyboardSwitcher = KeyboardSwitcher.getInstance();
61
62    private BinaryDictionary() {
63    }
64
65    /**
66     * Initialize a dictionary from a raw resource file
67     * @param context application context for reading resources
68     * @param resId the resource containing the raw binary dictionary
69     * @return initialized instance of BinaryDictionary
70     */
71    public static BinaryDictionary initDictionary(Context context, int resId, int dicTypeId) {
72        synchronized (sInstance) {
73            sInstance.closeInternal();
74            try {
75                final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
76                if (afd == null) {
77                    Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
78                    return null;
79                }
80                final String sourceDir = context.getApplicationInfo().sourceDir;
81                final File packagePath = new File(sourceDir);
82                // TODO: Come up with a way to handle a directory.
83                if (!packagePath.isFile()) {
84                    Log.e(TAG, "sourceDir is not a file: " + sourceDir);
85                    return null;
86                }
87                sInstance.loadDictionary(sourceDir, afd.getStartOffset(), afd.getLength());
88                sInstance.mDicTypeId = dicTypeId;
89            } catch (android.content.res.Resources.NotFoundException e) {
90                Log.e(TAG, "Could not find the resource. resId=" + resId);
91                return null;
92            }
93        }
94        return sInstance;
95    }
96
97    /* package for test */ 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        final int count = getSuggestions(codes, mKeyboardSwitcher.getLatinKeyboard());
170
171        for (int j = 0; j < count; ++j) {
172            if (mFrequencies[j] < 1) break;
173            final int start = j * MAX_WORD_LENGTH;
174            int len = 0;
175            while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) {
176                ++len;
177            }
178            if (len > 0) {
179                callback.addWord(mOutputChars, start, len, mFrequencies[j], mDicTypeId,
180                        DataType.UNIGRAM);
181            }
182        }
183    }
184
185    /* package for test */ boolean isValidDictionary() {
186        return mNativeDict != 0;
187    }
188
189    /* package for test */ int getSuggestions(final WordComposer codes, final Keyboard keyboard) {
190        if (!isValidDictionary()) return -1;
191
192        final int codesSize = codes.size();
193        // Won't deal with really long words.
194        if (codesSize > MAX_WORD_LENGTH - 1) return -1;
195
196        Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE);
197        for (int i = 0; i < codesSize; i++) {
198            int[] alternatives = codes.getCodesAt(i);
199            System.arraycopy(alternatives, 0, mInputCodes, i * MAX_PROXIMITY_CHARS_SIZE,
200                    Math.min(alternatives.length, MAX_PROXIMITY_CHARS_SIZE));
201        }
202        Arrays.fill(mOutputChars, (char) 0);
203        Arrays.fill(mFrequencies, 0);
204
205        return getSuggestionsNative(
206                mNativeDict, keyboard.getProximityInfo(),
207                codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize,
208                mOutputChars, mFrequencies);
209    }
210
211    @Override
212    public boolean isValidWord(CharSequence word) {
213        if (word == null) return false;
214        char[] chars = word.toString().toCharArray();
215        return isValidWordNative(mNativeDict, chars, chars.length);
216    }
217
218    public long getSize() {
219        return mDictLength; // This value is initialized in loadDictionary()
220    }
221
222    @Override
223    public synchronized void close() {
224        closeInternal();
225    }
226
227    private void closeInternal() {
228        if (mNativeDict != 0) {
229            closeNative(mNativeDict);
230            mNativeDict = 0;
231            mDictLength = 0;
232        }
233    }
234
235    @Override
236    protected void finalize() throws Throwable {
237        try {
238            closeInternal();
239        } finally {
240            super.finalize();
241        }
242    }
243}
244