BinaryDictionary.java revision 887f11ee43ad621aa6ad93d535ab7f48dec73fc7
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 android.content.Context;
20import android.content.res.AssetFileDescriptor;
21import android.util.Log;
22
23import java.io.File;
24import java.util.Arrays;
25
26/**
27 * Implements a static, compacted, binary dictionary of standard words.
28 */
29public class BinaryDictionary extends Dictionary {
30
31    /**
32     * There is difference between what java and native code can handle.
33     * This value should only be used in BinaryDictionary.java
34     * It is necessary to keep it at this value because some languages e.g. German have
35     * really long words.
36     */
37    protected static final int MAX_WORD_LENGTH = 48;
38
39    private static final String TAG = "BinaryDictionary";
40    private static final int MAX_ALTERNATIVES = 16;
41    private static final int MAX_WORDS = 18;
42    private static final int MAX_BIGRAMS = 60;
43
44    private static final int TYPED_LETTER_MULTIPLIER = 2;
45
46    private static final BinaryDictionary sInstance = new BinaryDictionary();
47    private int mDicTypeId;
48    private int mNativeDict;
49    private long mDictLength;
50    private final int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_ALTERNATIVES];
51    private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS];
52    private final char[] mOutputChars_bigrams = new char[MAX_WORD_LENGTH * MAX_BIGRAMS];
53    private final int[] mFrequencies = new int[MAX_WORDS];
54    private final int[] mFrequencies_bigrams = new int[MAX_BIGRAMS];
55
56    static {
57        try {
58            System.loadLibrary("jni_latinime");
59        } catch (UnsatisfiedLinkError ule) {
60            Log.e(TAG, "Could not load native library jni_latinime");
61        }
62    }
63
64    private BinaryDictionary() {
65    }
66
67    /**
68     * Initialize a dictionary from a raw resource file
69     * @param context application context for reading resources
70     * @param resId the resource containing the raw binary dictionary
71     * @return initialized instance of BinaryDictionary
72     */
73    public static BinaryDictionary initDictionary(Context context, int resId, int dicTypeId) {
74        synchronized (sInstance) {
75            sInstance.closeInternal();
76            try {
77                final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
78                if (afd == null) {
79                    Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
80                    return null;
81                }
82                final String sourceDir = context.getApplicationInfo().sourceDir;
83                final File packagePath = new File(sourceDir);
84                // TODO: Come up with a way to handle a directory.
85                if (!packagePath.isFile()) {
86                    Log.e(TAG, "sourceDir is not a file: " + sourceDir);
87                    return null;
88                }
89                sInstance.loadDictionary(sourceDir, afd.getStartOffset(), afd.getLength());
90                sInstance.mDicTypeId = dicTypeId;
91            } catch (android.content.res.Resources.NotFoundException e) {
92                Log.e(TAG, "Could not find the resource. resId=" + resId);
93                return null;
94            }
95        }
96        return sInstance;
97    }
98
99    // For unit test
100    /* package */ static BinaryDictionary initDictionary(File dictionary, long startOffset,
101            long length, int dicTypeId) {
102        synchronized (sInstance) {
103            sInstance.closeInternal();
104            if (dictionary.isFile()) {
105                sInstance.loadDictionary(dictionary.getAbsolutePath(), startOffset, length);
106                sInstance.mDicTypeId = dicTypeId;
107            } else {
108                Log.e(TAG, "Could not find the file. path=" + dictionary.getAbsolutePath());
109                return null;
110            }
111        }
112        return sInstance;
113    }
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[] inputCodes, int codesSize,
121            char[] outputChars, int[] frequencies);
122    private native int getBigramsNative(int dict, char[] prevWord, int prevWordLength,
123            int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies,
124            int maxWordLength, int maxBigrams, int maxAlternatives);
125
126    private final void loadDictionary(String path, long startOffset, long length) {
127        mNativeDict = openNative(path, startOffset, length,
128                    TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
129                    MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
130        mDictLength = length;
131    }
132
133    @Override
134    public void getBigrams(final WordComposer codes, final CharSequence previousWord,
135            final WordCallback callback) {
136        if (mNativeDict == 0) return;
137
138        char[] chars = previousWord.toString().toCharArray();
139        Arrays.fill(mOutputChars_bigrams, (char) 0);
140        Arrays.fill(mFrequencies_bigrams, 0);
141
142        int codesSize = codes.size();
143        Arrays.fill(mInputCodes, -1);
144        int[] alternatives = codes.getCodesAt(0);
145        System.arraycopy(alternatives, 0, mInputCodes, 0,
146                Math.min(alternatives.length, MAX_ALTERNATIVES));
147
148        int count = getBigramsNative(mNativeDict, chars, chars.length, mInputCodes, codesSize,
149                mOutputChars_bigrams, mFrequencies_bigrams, MAX_WORD_LENGTH, MAX_BIGRAMS,
150                MAX_ALTERNATIVES);
151
152        for (int j = 0; j < count; ++j) {
153            if (mFrequencies_bigrams[j] < 1) break;
154            final int start = j * MAX_WORD_LENGTH;
155            int len = 0;
156            while (len <  MAX_WORD_LENGTH && mOutputChars_bigrams[start + len] != 0) {
157                ++len;
158            }
159            if (len > 0) {
160                callback.addWord(mOutputChars_bigrams, start, len, mFrequencies_bigrams[j],
161                        mDicTypeId, DataType.BIGRAM);
162            }
163        }
164    }
165
166    @Override
167    public void getWords(final WordComposer codes, final WordCallback callback) {
168        if (mNativeDict == 0) return;
169
170        final int codesSize = codes.size();
171        // Won't deal with really long words.
172        if (codesSize > MAX_WORD_LENGTH - 1) return;
173
174        Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE);
175        for (int i = 0; i < codesSize; i++) {
176            int[] alternatives = codes.getCodesAt(i);
177            System.arraycopy(alternatives, 0, mInputCodes, i * MAX_ALTERNATIVES,
178                    Math.min(alternatives.length, MAX_ALTERNATIVES));
179        }
180        Arrays.fill(mOutputChars, (char) 0);
181        Arrays.fill(mFrequencies, 0);
182
183        int count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize, mOutputChars,
184                mFrequencies);
185
186        for (int j = 0; j < count; ++j) {
187            if (mFrequencies[j] < 1) break;
188            final int start = j * MAX_WORD_LENGTH;
189            int len = 0;
190            while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) {
191                ++len;
192            }
193            if (len > 0) {
194                callback.addWord(mOutputChars, start, len, mFrequencies[j], mDicTypeId,
195                        DataType.UNIGRAM);
196            }
197        }
198    }
199
200    @Override
201    public boolean isValidWord(CharSequence word) {
202        if (word == null) return false;
203        char[] chars = word.toString().toCharArray();
204        return isValidWordNative(mNativeDict, chars, chars.length);
205    }
206
207    public long getSize() {
208        return mDictLength; // This value is initialized in loadDictionary()
209    }
210
211    @Override
212    public synchronized void close() {
213        closeInternal();
214    }
215
216    private void closeInternal() {
217        if (mNativeDict != 0) {
218            closeNative(mNativeDict);
219            mNativeDict = 0;
220            mDictLength = 0;
221        }
222    }
223
224    @Override
225    protected void finalize() throws Throwable {
226        try {
227            closeInternal();
228        } finally {
229            super.finalize();
230        }
231    }
232}
233