BinaryDictionary.java revision ecfbf4625c8afd9cde7b79e0c7846b87e20f79e9
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.text.TextUtils;
21import android.util.SparseArray;
22
23import com.android.inputmethod.keyboard.ProximityInfo;
24import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
25
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.Locale;
29
30/**
31 * Implements a static, compacted, binary dictionary of standard words.
32 */
33public class BinaryDictionary extends Dictionary {
34
35    public static final String DICTIONARY_PACK_AUTHORITY =
36            "com.android.inputmethod.latin.dictionarypack";
37
38    /**
39     * There is a difference between what java and native code can handle.
40     * This value should only be used in BinaryDictionary.java
41     * It is necessary to keep it at this value because some languages e.g. German have
42     * really long words.
43     */
44    public static final int MAX_WORD_LENGTH = 48;
45    public static final int MAX_WORDS = 18;
46    public static final int MAX_SPACES = 16;
47
48    private static final String TAG = "BinaryDictionary";
49    private static final int MAX_PREDICTIONS = 60;
50    private static final int MAX_RESULTS = Math.max(MAX_PREDICTIONS, MAX_WORDS);
51
52    private static final int TYPED_LETTER_MULTIPLIER = 2;
53
54    private long mNativeDict;
55    private final Locale mLocale;
56    private final int[] mInputCodePoints = new int[MAX_WORD_LENGTH];
57    // TODO: The below should be int[] mOutputCodePoints
58    private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_RESULTS];
59    private final int[] mSpaceIndices = new int[MAX_SPACES];
60    private final int[] mOutputScores = new int[MAX_RESULTS];
61    private final int[] mOutputTypes = new int[MAX_RESULTS];
62
63    private final boolean mUseFullEditDistance;
64
65    private final SparseArray<DicTraverseSession> mDicTraverseSessions =
66            CollectionUtils.newSparseArray();
67
68    // TODO: There should be a way to remove used DicTraverseSession objects from
69    // {@code mDicTraverseSessions}.
70    private DicTraverseSession getTraverseSession(int traverseSessionId) {
71        synchronized(mDicTraverseSessions) {
72            DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId);
73            if (traverseSession == null) {
74                traverseSession = mDicTraverseSessions.get(traverseSessionId);
75                if (traverseSession == null) {
76                    traverseSession = new DicTraverseSession(mLocale, mNativeDict);
77                    mDicTraverseSessions.put(traverseSessionId, traverseSession);
78                }
79            }
80            return traverseSession;
81        }
82    }
83
84    /**
85     * Constructor for the binary dictionary. This is supposed to be called from the
86     * dictionary factory.
87     * All implementations should pass null into flagArray, except for testing purposes.
88     * @param context the context to access the environment from.
89     * @param filename the name of the file to read through native code.
90     * @param offset the offset of the dictionary data within the file.
91     * @param length the length of the binary data.
92     * @param useFullEditDistance whether to use the full edit distance in suggestions
93     * @param dictType the dictionary type, as a human-readable string
94     */
95    public BinaryDictionary(final Context context,
96            final String filename, final long offset, final long length,
97            final boolean useFullEditDistance, final Locale locale, final String dictType) {
98        super(dictType);
99        mLocale = locale;
100        mUseFullEditDistance = useFullEditDistance;
101        loadDictionary(filename, offset, length);
102    }
103
104    static {
105        JniUtils.loadNativeLibrary();
106    }
107
108    private native long openNative(String sourceDir, long dictOffset, long dictSize,
109            int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords,
110            int maxPredictions);
111    private native void closeNative(long dict);
112    private native int getFrequencyNative(long dict, int[] word);
113    private native boolean isValidBigramNative(long dict, int[] word1, int[] word2);
114    private native int getSuggestionsNative(long dict, long proximityInfo, long traverseSession,
115            int[] xCoordinates, int[] yCoordinates, int[] times, int[] pointerIds,
116            int[] inputCodePoints, int codesSize, int commitPoint, boolean isGesture,
117            int[] prevWordCodePointArray, boolean useFullEditDistance, char[] outputChars,
118            int[] outputScores, int[] outputIndices, int[] outputTypes);
119    private static native float calcNormalizedScoreNative(char[] before, char[] after, int score);
120    private static native int editDistanceNative(char[] before, char[] after);
121
122    // TODO: Move native dict into session
123    private final void loadDictionary(String path, long startOffset, long length) {
124        mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER,
125                FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS, MAX_PREDICTIONS);
126    }
127
128    @Override
129    public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
130            final CharSequence prevWord, final ProximityInfo proximityInfo) {
131        return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, 0);
132    }
133
134    @Override
135    public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
136            final CharSequence prevWord, final ProximityInfo proximityInfo, int sessionId) {
137        if (!isValidDictionary()) return null;
138
139        Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE);
140        // TODO: toLowerCase in the native code
141        final int[] prevWordCodePointArray = (null == prevWord)
142                ? null : StringUtils.toCodePointArray(prevWord.toString());
143        final int composerSize = composer.size();
144
145        final boolean isGesture = composer.isBatchMode();
146        if (composerSize <= 1 || !isGesture) {
147            if (composerSize > MAX_WORD_LENGTH - 1) return null;
148            for (int i = 0; i < composerSize; i++) {
149                mInputCodePoints[i] = composer.getCodeAt(i);
150            }
151        }
152
153        final InputPointers ips = composer.getInputPointers();
154        final int codesSize = isGesture ? ips.getPointerSize() : composerSize;
155        // proximityInfo and/or prevWordForBigrams may not be null.
156        final int tmpCount = getSuggestionsNative(mNativeDict,
157                proximityInfo.getNativeProximityInfo(), getTraverseSession(sessionId).getSession(),
158                ips.getXCoordinates(), ips.getYCoordinates(), ips.getTimes(), ips.getPointerIds(),
159                mInputCodePoints, codesSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray,
160                mUseFullEditDistance, mOutputChars, mOutputScores, mSpaceIndices, mOutputTypes);
161        final int count = Math.min(tmpCount, MAX_PREDICTIONS);
162
163        final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
164        for (int j = 0; j < count; ++j) {
165            if (composerSize > 0 && mOutputScores[j] < 1) break;
166            final int start = j * MAX_WORD_LENGTH;
167            int len = 0;
168            while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) {
169                ++len;
170            }
171            if (len > 0) {
172                final int score = SuggestedWordInfo.KIND_WHITELIST == mOutputTypes[j]
173                        ? SuggestedWordInfo.MAX_SCORE : mOutputScores[j];
174                suggestions.add(new SuggestedWordInfo(
175                        new String(mOutputChars, start, len), score, mOutputTypes[j], mDictType));
176            }
177        }
178        return suggestions;
179    }
180
181    /* package for test */ boolean isValidDictionary() {
182        return mNativeDict != 0;
183    }
184
185    public static float calcNormalizedScore(String before, String after, int score) {
186        return calcNormalizedScoreNative(before.toCharArray(), after.toCharArray(), score);
187    }
188
189    public static int editDistance(String before, String after) {
190        return editDistanceNative(before.toCharArray(), after.toCharArray());
191    }
192
193    @Override
194    public boolean isValidWord(CharSequence word) {
195        return getFrequency(word) >= 0;
196    }
197
198    @Override
199    public int getFrequency(CharSequence word) {
200        if (word == null) return -1;
201        int[] codePoints = StringUtils.toCodePointArray(word.toString());
202        return getFrequencyNative(mNativeDict, codePoints);
203    }
204
205    // TODO: Add a batch process version (isValidBigramMultiple?) to avoid excessive numbers of jni
206    // calls when checking for changes in an entire dictionary.
207    public boolean isValidBigram(CharSequence word1, CharSequence word2) {
208        if (TextUtils.isEmpty(word1) || TextUtils.isEmpty(word2)) return false;
209        int[] chars1 = StringUtils.toCodePointArray(word1.toString());
210        int[] chars2 = StringUtils.toCodePointArray(word2.toString());
211        return isValidBigramNative(mNativeDict, chars1, chars2);
212    }
213
214    @Override
215    public void close() {
216        synchronized (mDicTraverseSessions) {
217            final int sessionsSize = mDicTraverseSessions.size();
218            for (int index = 0; index < sessionsSize; ++index) {
219                final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index);
220                if (traverseSession != null) {
221                    traverseSession.close();
222                }
223            }
224        }
225        closeInternal();
226    }
227
228    private synchronized void closeInternal() {
229        if (mNativeDict != 0) {
230            closeNative(mNativeDict);
231            mNativeDict = 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