BinaryDictionary.java revision 4c2767857a02c9cf18a9579aa0391fd09b3fe411
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.text.TextUtils;
20import android.util.SparseArray;
21
22import com.android.inputmethod.keyboard.ProximityInfo;
23import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
24import com.android.inputmethod.latin.settings.NativeSuggestOptions;
25import com.android.inputmethod.latin.utils.CollectionUtils;
26import com.android.inputmethod.latin.utils.JniUtils;
27import com.android.inputmethod.latin.utils.StringUtils;
28
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.Locale;
32
33/**
34 * Implements a static, compacted, binary dictionary of standard words.
35 */
36public final class BinaryDictionary extends Dictionary {
37    private static final String TAG = BinaryDictionary.class.getSimpleName();
38
39    // Must be equal to MAX_WORD_LENGTH in native/jni/src/defines.h
40    private static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
41    // Must be equal to MAX_RESULTS in native/jni/src/defines.h
42    private static final int MAX_RESULTS = 18;
43
44    private long mNativeDict;
45    private final Locale mLocale;
46    private final long mDictSize;
47    private final int[] mInputCodePoints = new int[MAX_WORD_LENGTH];
48    private final int[] mOutputCodePoints = new int[MAX_WORD_LENGTH * MAX_RESULTS];
49    private final int[] mSpaceIndices = new int[MAX_RESULTS];
50    private final int[] mOutputScores = new int[MAX_RESULTS];
51    private final int[] mOutputTypes = new int[MAX_RESULTS];
52
53    private final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions();
54
55    private final SparseArray<DicTraverseSession> mDicTraverseSessions =
56            CollectionUtils.newSparseArray();
57
58    // TODO: There should be a way to remove used DicTraverseSession objects from
59    // {@code mDicTraverseSessions}.
60    private DicTraverseSession getTraverseSession(final int traverseSessionId) {
61        synchronized(mDicTraverseSessions) {
62            DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId);
63            if (traverseSession == null) {
64                traverseSession = mDicTraverseSessions.get(traverseSessionId);
65                if (traverseSession == null) {
66                    traverseSession = new DicTraverseSession(mLocale, mNativeDict, mDictSize);
67                    mDicTraverseSessions.put(traverseSessionId, traverseSession);
68                }
69            }
70            return traverseSession;
71        }
72    }
73
74    /**
75     * Constructor for the binary dictionary. This is supposed to be called from the
76     * dictionary factory.
77     * @param filename the name of the file to read through native code.
78     * @param offset the offset of the dictionary data within the file.
79     * @param length the length of the binary data.
80     * @param useFullEditDistance whether to use the full edit distance in suggestions
81     * @param dictType the dictionary type, as a human-readable string
82     * @param isUpdatable whether to open the dictionary file in writable mode.
83     */
84    public BinaryDictionary(final String filename, final long offset, final long length,
85            final boolean useFullEditDistance, final Locale locale, final String dictType,
86            final boolean isUpdatable) {
87        super(dictType);
88        mLocale = locale;
89        mDictSize = length;
90        mNativeSuggestOptions.setUseFullEditDistance(useFullEditDistance);
91        loadDictionary(filename, offset, length, isUpdatable);
92    }
93
94    static {
95        JniUtils.loadNativeLibrary();
96    }
97
98    private static native long openNative(String sourceDir, long dictOffset, long dictSize,
99            boolean isUpdatable);
100    private static native void closeNative(long dict);
101    private static native int getProbabilityNative(long dict, int[] word);
102    private static native boolean isValidBigramNative(long dict, int[] word0, int[] word1);
103    private static native int getSuggestionsNative(long dict, long proximityInfo,
104            long traverseSession, int[] xCoordinates, int[] yCoordinates, int[] times,
105            int[] pointerIds, int[] inputCodePoints, int inputSize, int commitPoint,
106            int[] suggestOptions, int[] prevWordCodePointArray,
107            int[] outputCodePoints, int[] outputScores, int[] outputIndices, int[] outputTypes);
108    private static native float calcNormalizedScoreNative(int[] before, int[] after, int score);
109    private static native int editDistanceNative(int[] before, int[] after);
110    private static native void addUnigramWordNative(long dict, int[] word, int probability);
111    private static native void addBigramWordsNative(long dict, int[] word0, int[] word1,
112            int probability);
113    private static native void removeBigramWordsNative(long dict, int[] word0, int[] word1);
114
115    // TODO: Move native dict into session
116    private final void loadDictionary(final String path, final long startOffset,
117            final long length, final boolean isUpdatable) {
118        mNativeDict = openNative(path, startOffset, length, isUpdatable);
119    }
120
121    @Override
122    public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
123            final String prevWord, final ProximityInfo proximityInfo,
124            final boolean blockOffensiveWords, final int[] additionalFeaturesOptions) {
125        return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, blockOffensiveWords,
126                additionalFeaturesOptions, 0 /* sessionId */);
127    }
128
129    @Override
130    public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
131            final String prevWord, final ProximityInfo proximityInfo,
132            final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
133            final int sessionId) {
134        if (!isValidDictionary()) return null;
135
136        Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE);
137        // TODO: toLowerCase in the native code
138        final int[] prevWordCodePointArray = (null == prevWord)
139                ? null : StringUtils.toCodePointArray(prevWord);
140        final int composerSize = composer.size();
141
142        final boolean isGesture = composer.isBatchMode();
143        if (composerSize <= 1 || !isGesture) {
144            if (composerSize > MAX_WORD_LENGTH - 1) return null;
145            for (int i = 0; i < composerSize; i++) {
146                mInputCodePoints[i] = composer.getCodeAt(i);
147            }
148        }
149
150        final InputPointers ips = composer.getInputPointers();
151        final int inputSize = isGesture ? ips.getPointerSize() : composerSize;
152        mNativeSuggestOptions.setIsGesture(isGesture);
153        mNativeSuggestOptions.setAdditionalFeaturesOptions(additionalFeaturesOptions);
154        // proximityInfo and/or prevWordForBigrams may not be null.
155        final int count = getSuggestionsNative(mNativeDict, proximityInfo.getNativeProximityInfo(),
156                getTraverseSession(sessionId).getSession(), ips.getXCoordinates(),
157                ips.getYCoordinates(), ips.getTimes(), ips.getPointerIds(), mInputCodePoints,
158                inputSize, 0 /* commitPoint */, mNativeSuggestOptions.getOptions(),
159                prevWordCodePointArray, mOutputCodePoints, mOutputScores, mSpaceIndices,
160                mOutputTypes);
161        final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
162        for (int j = 0; j < count; ++j) {
163            final int start = j * MAX_WORD_LENGTH;
164            int len = 0;
165            while (len < MAX_WORD_LENGTH && mOutputCodePoints[start + len] != 0) {
166                ++len;
167            }
168            if (len > 0) {
169                final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS;
170                if (blockOffensiveWords
171                        && 0 != (flags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE)
172                        && 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) {
173                    // If we block potentially offensive words, and if the word is possibly
174                    // offensive, then we don't output it unless it's also an exact match.
175                    continue;
176                }
177                final int kind = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_KIND;
178                final int score = SuggestedWordInfo.KIND_WHITELIST == kind
179                        ? SuggestedWordInfo.MAX_SCORE : mOutputScores[j];
180                // TODO: check that all users of the `kind' parameter are ready to accept
181                // flags too and pass mOutputTypes[j] instead of kind
182                suggestions.add(new SuggestedWordInfo(new String(mOutputCodePoints, start, len),
183                        score, kind, this /* sourceDict */,
184                        mSpaceIndices[0] /* indexOfTouchPointOfSecondWord */));
185            }
186        }
187        return suggestions;
188    }
189
190    public boolean isValidDictionary() {
191        return mNativeDict != 0;
192    }
193
194    public static float calcNormalizedScore(final String before, final String after,
195            final int score) {
196        return calcNormalizedScoreNative(StringUtils.toCodePointArray(before),
197                StringUtils.toCodePointArray(after), score);
198    }
199
200    public static int editDistance(final String before, final String after) {
201        if (before == null || after == null) {
202            throw new IllegalArgumentException();
203        }
204        return editDistanceNative(StringUtils.toCodePointArray(before),
205                StringUtils.toCodePointArray(after));
206    }
207
208    @Override
209    public boolean isValidWord(final String word) {
210        return getFrequency(word) >= 0;
211    }
212
213    @Override
214    public int getFrequency(final String word) {
215        if (word == null) return -1;
216        int[] codePoints = StringUtils.toCodePointArray(word);
217        return getProbabilityNative(mNativeDict, codePoints);
218    }
219
220    // TODO: Add a batch process version (isValidBigramMultiple?) to avoid excessive numbers of jni
221    // calls when checking for changes in an entire dictionary.
222    public boolean isValidBigram(final String word0, final String word1) {
223        if (TextUtils.isEmpty(word0) || TextUtils.isEmpty(word1)) return false;
224        final int[] codePoints0 = StringUtils.toCodePointArray(word0);
225        final int[] codePoints1 = StringUtils.toCodePointArray(word1);
226        return isValidBigramNative(mNativeDict, codePoints0, codePoints1);
227    }
228
229    // Add a unigram entry to binary dictionary in native code.
230    public void addUnigramWord(final String word, final int probability) {
231        if (TextUtils.isEmpty(word)) {
232            return;
233        }
234        final int[] codePoints = StringUtils.toCodePointArray(word);
235        addUnigramWordNative(mNativeDict, codePoints, probability);
236    }
237
238    // Add a bigram entry to binary dictionary in native code.
239    public void addBigramWords(final String word0, final String word1, final int probability) {
240        if (TextUtils.isEmpty(word0) || TextUtils.isEmpty(word1)) {
241            return;
242        }
243        final int[] codePoints0 = StringUtils.toCodePointArray(word0);
244        final int[] codePoints1 = StringUtils.toCodePointArray(word1);
245        addBigramWordsNative(mNativeDict, codePoints0, codePoints1, probability);
246    }
247
248    // Remove a bigram entry form binary dictionary in native code.
249    public void removeBigramWords(final String word0, final String word1) {
250        if (TextUtils.isEmpty(word0) || TextUtils.isEmpty(word1)) {
251            return;
252        }
253        final int[] codePoints0 = StringUtils.toCodePointArray(word0);
254        final int[] codePoints1 = StringUtils.toCodePointArray(word1);
255        removeBigramWordsNative(mNativeDict, codePoints0, codePoints1);
256    }
257
258    @Override
259    public void close() {
260        synchronized (mDicTraverseSessions) {
261            final int sessionsSize = mDicTraverseSessions.size();
262            for (int index = 0; index < sessionsSize; ++index) {
263                final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index);
264                if (traverseSession != null) {
265                    traverseSession.close();
266                }
267            }
268        }
269        closeInternal();
270    }
271
272    private synchronized void closeInternal() {
273        if (mNativeDict != 0) {
274            closeNative(mNativeDict);
275            mNativeDict = 0;
276        }
277    }
278
279    @Override
280    protected void finalize() throws Throwable {
281        try {
282            closeInternal();
283        } finally {
284            super.finalize();
285        }
286    }
287}
288