BinaryDictionary.java revision f3b62900c7bcb0d6434f45ec7b467b7b4bad6f9a
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.utils.AdditionalFeaturesSettingUtils;
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 int[] mInputCodePoints = new int[MAX_WORD_LENGTH];
47    private final int[] mOutputCodePoints = new int[MAX_WORD_LENGTH * MAX_RESULTS];
48    private final int[] mSpaceIndices = new int[MAX_RESULTS];
49    private final int[] mOutputScores = new int[MAX_RESULTS];
50    private final int[] mOutputTypes = new int[MAX_RESULTS];
51
52    private final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions();
53
54    private final SparseArray<DicTraverseSession> mDicTraverseSessions =
55            CollectionUtils.newSparseArray();
56
57    // TODO: There should be a way to remove used DicTraverseSession objects from
58    // {@code mDicTraverseSessions}.
59    private DicTraverseSession getTraverseSession(final int traverseSessionId) {
60        synchronized(mDicTraverseSessions) {
61            DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId);
62            if (traverseSession == null) {
63                traverseSession = mDicTraverseSessions.get(traverseSessionId);
64                if (traverseSession == null) {
65                    traverseSession = new DicTraverseSession(mLocale, mNativeDict);
66                    mDicTraverseSessions.put(traverseSessionId, traverseSession);
67                }
68            }
69            return traverseSession;
70        }
71    }
72
73    /**
74     * Constructor for the binary dictionary. This is supposed to be called from the
75     * dictionary factory.
76     * @param filename the name of the file to read through native code.
77     * @param offset the offset of the dictionary data within the file.
78     * @param length the length of the binary data.
79     * @param useFullEditDistance whether to use the full edit distance in suggestions
80     * @param dictType the dictionary type, as a human-readable string
81     */
82    public BinaryDictionary(final String filename, final long offset, final long length,
83            final boolean useFullEditDistance, final Locale locale, final String dictType) {
84        super(dictType);
85        mLocale = locale;
86        mNativeSuggestOptions.setUseFullEditDistance(useFullEditDistance);
87        loadDictionary(filename, offset, length);
88    }
89
90    static {
91        JniUtils.loadNativeLibrary();
92    }
93
94    private static native long openNative(String sourceDir, long dictOffset, long dictSize,
95            boolean isUpdatable);
96    private static native void closeNative(long dict);
97    private static native int getProbabilityNative(long dict, int[] word);
98    private static native boolean isValidBigramNative(long dict, int[] word1, int[] word2);
99    private static native int getSuggestionsNative(long dict, long proximityInfo,
100            long traverseSession, int[] xCoordinates, int[] yCoordinates, int[] times,
101            int[] pointerIds, int[] inputCodePoints, int inputSize, int commitPoint,
102            int[] suggestOptions, int[] prevWordCodePointArray,
103            int[] outputCodePoints, int[] outputScores, int[] outputIndices, int[] outputTypes);
104    private static native float calcNormalizedScoreNative(int[] before, int[] after, int score);
105    private static native int editDistanceNative(int[] before, int[] after);
106
107    // TODO: Move native dict into session
108    private final void loadDictionary(final String path, final long startOffset,
109            final long length) {
110        mNativeDict = openNative(path, startOffset, length, false /* isUpdatable */);
111    }
112
113    @Override
114    public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
115            final String prevWord, final ProximityInfo proximityInfo,
116            final boolean blockOffensiveWords) {
117        return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, blockOffensiveWords,
118                0 /* sessionId */);
119    }
120
121    @Override
122    public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
123            final String prevWord, final ProximityInfo proximityInfo,
124            final boolean blockOffensiveWords, final int sessionId) {
125        if (!isValidDictionary()) return null;
126
127        Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE);
128        // TODO: toLowerCase in the native code
129        final int[] prevWordCodePointArray = (null == prevWord)
130                ? null : StringUtils.toCodePointArray(prevWord);
131        final int composerSize = composer.size();
132
133        final boolean isGesture = composer.isBatchMode();
134        if (composerSize <= 1 || !isGesture) {
135            if (composerSize > MAX_WORD_LENGTH - 1) return null;
136            for (int i = 0; i < composerSize; i++) {
137                mInputCodePoints[i] = composer.getCodeAt(i);
138            }
139        }
140
141        final InputPointers ips = composer.getInputPointers();
142        final int inputSize = isGesture ? ips.getPointerSize() : composerSize;
143        mNativeSuggestOptions.setIsGesture(isGesture);
144        mNativeSuggestOptions.setAdditionalFeaturesOptions(
145                AdditionalFeaturesSettingUtils.getAdditionalNativeSuggestOptions());
146        // proximityInfo and/or prevWordForBigrams may not be null.
147        final int count = getSuggestionsNative(mNativeDict, proximityInfo.getNativeProximityInfo(),
148                getTraverseSession(sessionId).getSession(), ips.getXCoordinates(),
149                ips.getYCoordinates(), ips.getTimes(), ips.getPointerIds(), mInputCodePoints,
150                inputSize, 0 /* commitPoint */, mNativeSuggestOptions.getOptions(),
151                prevWordCodePointArray, mOutputCodePoints, mOutputScores, mSpaceIndices,
152                mOutputTypes);
153        final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
154        for (int j = 0; j < count; ++j) {
155            final int start = j * MAX_WORD_LENGTH;
156            int len = 0;
157            while (len < MAX_WORD_LENGTH && mOutputCodePoints[start + len] != 0) {
158                ++len;
159            }
160            if (len > 0) {
161                final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS;
162                if (blockOffensiveWords
163                        && 0 != (flags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE)
164                        && 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) {
165                    // If we block potentially offensive words, and if the word is possibly
166                    // offensive, then we don't output it unless it's also an exact match.
167                    continue;
168                }
169                final int kind = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_KIND;
170                final int score = SuggestedWordInfo.KIND_WHITELIST == kind
171                        ? SuggestedWordInfo.MAX_SCORE : mOutputScores[j];
172                // TODO: check that all users of the `kind' parameter are ready to accept
173                // flags too and pass mOutputTypes[j] instead of kind
174                suggestions.add(new SuggestedWordInfo(new String(mOutputCodePoints, start, len),
175                        score, kind, mDictType));
176            }
177        }
178        return suggestions;
179    }
180
181    public boolean isValidDictionary() {
182        return mNativeDict != 0;
183    }
184
185    public static float calcNormalizedScore(final String before, final String after,
186            final int score) {
187        return calcNormalizedScoreNative(StringUtils.toCodePointArray(before),
188                StringUtils.toCodePointArray(after), score);
189    }
190
191    public static int editDistance(final String before, final String after) {
192        if (before == null || after == null) {
193            throw new IllegalArgumentException();
194        }
195        return editDistanceNative(StringUtils.toCodePointArray(before),
196                StringUtils.toCodePointArray(after));
197    }
198
199    @Override
200    public boolean isValidWord(final String word) {
201        return getFrequency(word) >= 0;
202    }
203
204    @Override
205    public int getFrequency(final String word) {
206        if (word == null) return -1;
207        int[] codePoints = StringUtils.toCodePointArray(word);
208        return getProbabilityNative(mNativeDict, codePoints);
209    }
210
211    // TODO: Add a batch process version (isValidBigramMultiple?) to avoid excessive numbers of jni
212    // calls when checking for changes in an entire dictionary.
213    public boolean isValidBigram(final String word1, final String word2) {
214        if (TextUtils.isEmpty(word1) || TextUtils.isEmpty(word2)) return false;
215        final int[] codePoints1 = StringUtils.toCodePointArray(word1);
216        final int[] codePoints2 = StringUtils.toCodePointArray(word2);
217        return isValidBigramNative(mNativeDict, codePoints1, codePoints2);
218    }
219
220    @Override
221    public void close() {
222        synchronized (mDicTraverseSessions) {
223            final int sessionsSize = mDicTraverseSessions.size();
224            for (int index = 0; index < sessionsSize; ++index) {
225                final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index);
226                if (traverseSession != null) {
227                    traverseSession.close();
228                }
229            }
230        }
231        closeInternal();
232    }
233
234    private synchronized void closeInternal() {
235        if (mNativeDict != 0) {
236            closeNative(mNativeDict);
237            mNativeDict = 0;
238        }
239    }
240
241    @Override
242    protected void finalize() throws Throwable {
243        try {
244            closeInternal();
245        } finally {
246            super.finalize();
247        }
248    }
249}
250