BinaryDictionary.java revision 2e04770adfc16344f69d316efd3ed0a617ede330
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 com.android.inputmethod.keyboard.Keyboard; 20import com.android.inputmethod.keyboard.KeyboardSwitcher; 21import com.android.inputmethod.keyboard.ProximityInfo; 22 23import android.content.Context; 24import android.content.res.AssetFileDescriptor; 25import android.util.Log; 26 27import java.io.File; 28import java.util.Arrays; 29 30/** 31 * Implements a static, compacted, binary dictionary of standard words. 32 */ 33public class BinaryDictionary extends Dictionary { 34 35 /** 36 * There is difference between what java and native code can handle. 37 * This value should only be used in BinaryDictionary.java 38 * It is necessary to keep it at this value because some languages e.g. German have 39 * really long words. 40 */ 41 public static final int MAX_WORD_LENGTH = 48; 42 public static final int MAX_WORDS = 18; 43 44 private static final String TAG = "BinaryDictionary"; 45 private static final int MAX_PROXIMITY_CHARS_SIZE = ProximityInfo.MAX_PROXIMITY_CHARS_SIZE; 46 private static final int MAX_BIGRAMS = 60; 47 48 private static final int TYPED_LETTER_MULTIPLIER = 2; 49 50 private static final BinaryDictionary sInstance = new BinaryDictionary(); 51 private int mDicTypeId; 52 private int mNativeDict; 53 private long mDictLength; 54 private final int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_PROXIMITY_CHARS_SIZE]; 55 private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS]; 56 private final char[] mOutputChars_bigrams = new char[MAX_WORD_LENGTH * MAX_BIGRAMS]; 57 private final int[] mFrequencies = new int[MAX_WORDS]; 58 private final int[] mFrequencies_bigrams = new int[MAX_BIGRAMS]; 59 60 private final KeyboardSwitcher mKeyboardSwitcher = KeyboardSwitcher.getInstance(); 61 62 private BinaryDictionary() { 63 } 64 65 /** 66 * Initialize a dictionary from a raw resource file 67 * @param context application context for reading resources 68 * @param resId the resource containing the raw binary dictionary 69 * @return initialized instance of BinaryDictionary 70 */ 71 public static BinaryDictionary initDictionary(Context context, int resId, int dicTypeId) { 72 synchronized (sInstance) { 73 sInstance.closeInternal(); 74 try { 75 final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId); 76 if (afd == null) { 77 Log.e(TAG, "Found the resource but it is compressed. resId=" + resId); 78 return null; 79 } 80 final String sourceDir = context.getApplicationInfo().sourceDir; 81 final File packagePath = new File(sourceDir); 82 // TODO: Come up with a way to handle a directory. 83 if (!packagePath.isFile()) { 84 Log.e(TAG, "sourceDir is not a file: " + sourceDir); 85 return null; 86 } 87 sInstance.loadDictionary(sourceDir, afd.getStartOffset(), afd.getLength()); 88 sInstance.mDicTypeId = dicTypeId; 89 } catch (android.content.res.Resources.NotFoundException e) { 90 Log.e(TAG, "Could not find the resource. resId=" + resId); 91 return null; 92 } 93 } 94 return sInstance; 95 } 96 97 /* package for test */ static BinaryDictionary initDictionary(File dictionary, long startOffset, 98 long length, int dicTypeId) { 99 synchronized (sInstance) { 100 sInstance.closeInternal(); 101 if (dictionary.isFile()) { 102 sInstance.loadDictionary(dictionary.getAbsolutePath(), startOffset, length); 103 sInstance.mDicTypeId = dicTypeId; 104 } else { 105 Log.e(TAG, "Could not find the file. path=" + dictionary.getAbsolutePath()); 106 return null; 107 } 108 } 109 return sInstance; 110 } 111 112 static { 113 Utils.loadNativeLibrary(); 114 } 115 private native int openNative(String sourceDir, long dictOffset, long dictSize, 116 int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, 117 int maxWords, int maxAlternatives); 118 private native void closeNative(int dict); 119 private native boolean isValidWordNative(int nativeData, char[] word, int wordLength); 120 private native int getSuggestionsNative(int dict, int proximityInfo, int[] xCoordinates, 121 int[] yCoordinates, int[] inputCodes, int codesSize, char[] outputChars, 122 int[] frequencies); 123 private native int getBigramsNative(int dict, char[] prevWord, int prevWordLength, 124 int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies, 125 int maxWordLength, int maxBigrams, int maxAlternatives); 126 127 private final void loadDictionary(String path, long startOffset, long length) { 128 mNativeDict = openNative(path, startOffset, length, 129 TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER, 130 MAX_WORD_LENGTH, MAX_WORDS, MAX_PROXIMITY_CHARS_SIZE); 131 mDictLength = length; 132 } 133 134 @Override 135 public void getBigrams(final WordComposer codes, final CharSequence previousWord, 136 final WordCallback callback) { 137 if (mNativeDict == 0) return; 138 139 char[] chars = previousWord.toString().toCharArray(); 140 Arrays.fill(mOutputChars_bigrams, (char) 0); 141 Arrays.fill(mFrequencies_bigrams, 0); 142 143 int codesSize = codes.size(); 144 Arrays.fill(mInputCodes, -1); 145 int[] alternatives = codes.getCodesAt(0); 146 System.arraycopy(alternatives, 0, mInputCodes, 0, 147 Math.min(alternatives.length, MAX_PROXIMITY_CHARS_SIZE)); 148 149 int count = getBigramsNative(mNativeDict, chars, chars.length, mInputCodes, codesSize, 150 mOutputChars_bigrams, mFrequencies_bigrams, MAX_WORD_LENGTH, MAX_BIGRAMS, 151 MAX_PROXIMITY_CHARS_SIZE); 152 153 for (int j = 0; j < count; ++j) { 154 if (mFrequencies_bigrams[j] < 1) break; 155 final int start = j * MAX_WORD_LENGTH; 156 int len = 0; 157 while (len < MAX_WORD_LENGTH && mOutputChars_bigrams[start + len] != 0) { 158 ++len; 159 } 160 if (len > 0) { 161 callback.addWord(mOutputChars_bigrams, start, len, mFrequencies_bigrams[j], 162 mDicTypeId, DataType.BIGRAM); 163 } 164 } 165 } 166 167 @Override 168 public void getWords(final WordComposer codes, final WordCallback callback) { 169 final int count = getSuggestions(codes, mKeyboardSwitcher.getLatinKeyboard(), 170 mOutputChars, mFrequencies); 171 172 for (int j = 0; j < count; ++j) { 173 if (mFrequencies[j] < 1) break; 174 final int start = j * MAX_WORD_LENGTH; 175 int len = 0; 176 while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) { 177 ++len; 178 } 179 if (len > 0) { 180 callback.addWord(mOutputChars, start, len, mFrequencies[j], mDicTypeId, 181 DataType.UNIGRAM); 182 } 183 } 184 } 185 186 /* package for test */ boolean isValidDictionary() { 187 return mNativeDict != 0; 188 } 189 190 /* package for test */ int getSuggestions(final WordComposer codes, final Keyboard keyboard, 191 char[] outputChars, int[] frequencies) { 192 if (!isValidDictionary()) return -1; 193 194 final int codesSize = codes.size(); 195 // Won't deal with really long words. 196 if (codesSize > MAX_WORD_LENGTH - 1) return -1; 197 198 Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE); 199 for (int i = 0; i < codesSize; i++) { 200 int[] alternatives = codes.getCodesAt(i); 201 System.arraycopy(alternatives, 0, mInputCodes, i * MAX_PROXIMITY_CHARS_SIZE, 202 Math.min(alternatives.length, MAX_PROXIMITY_CHARS_SIZE)); 203 } 204 Arrays.fill(outputChars, (char) 0); 205 Arrays.fill(frequencies, 0); 206 207 return getSuggestionsNative( 208 mNativeDict, keyboard.getProximityInfo(), 209 codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize, 210 outputChars, frequencies); 211 } 212 213 @Override 214 public boolean isValidWord(CharSequence word) { 215 if (word == null) return false; 216 char[] chars = word.toString().toCharArray(); 217 return isValidWordNative(mNativeDict, chars, chars.length); 218 } 219 220 public long getSize() { 221 return mDictLength; // This value is initialized in loadDictionary() 222 } 223 224 @Override 225 public synchronized void close() { 226 closeInternal(); 227 } 228 229 private void closeInternal() { 230 if (mNativeDict != 0) { 231 closeNative(mNativeDict); 232 mNativeDict = 0; 233 mDictLength = 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