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