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