/tags/20100815/src/com/menny/android/anysoftkeyboard/dictionary/BinaryDictionary.java

http://softkeyboard.googlecode.com/ · Java · 162 lines · 110 code · 23 blank · 29 comment · 17 complexity · 41956c0ce6ea00b50370ae07a27e41d9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008-2009 Google Inc.
  3. * Copyright (C) 2009 Spiros Papadimitriou <spapadim@cs.cmu.edu>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. package com.menny.android.anysoftkeyboard.dictionary;
  18. import java.io.FileDescriptor;
  19. import java.util.Arrays;
  20. import android.content.res.AssetFileDescriptor;
  21. import android.util.Log;
  22. import com.menny.android.anysoftkeyboard.WordComposer;
  23. /**
  24. * Implements a static, compacted, binary dictionary of standard words.
  25. */
  26. class BinaryDictionary extends Dictionary {
  27. private static final String TAG = "ASK_BinaryDictionary";
  28. public static final int MAX_WORD_LENGTH = 20;
  29. private static final int MAX_ALTERNATIVES = 16;
  30. private static final int MAX_WORDS = 16;
  31. private final AssetFileDescriptor mAfd;
  32. private static final int TYPED_LETTER_MULTIPLIER = 2;
  33. private static final boolean ENABLE_MISSED_CHARACTERS = true;
  34. private int mNativeDict;
  35. private int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_ALTERNATIVES];
  36. private char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS];
  37. private int[] mFrequencies = new int[MAX_WORDS];
  38. static {
  39. try {
  40. System.loadLibrary("nativeime");
  41. }
  42. catch (UnsatisfiedLinkError ule) {
  43. Log.e(TAG, "******** Could not load native library nativeim ********");
  44. Log.e(TAG, "******** Could not load native library nativeim ********", ule);
  45. Log.e(TAG, "******** Could not load native library nativeim ********");
  46. }
  47. catch (Throwable t) {
  48. Log.e(TAG, "******** Failed to load native dictionary library ********");
  49. Log.e(TAG, "******** Failed to load native dictionary library *******", t);
  50. Log.e(TAG, "******** Failed to load native dictionary library ********");
  51. }
  52. }
  53. /**
  54. * Create a dictionary from a raw resource file
  55. * @param context application context for reading resources
  56. * @param resId the resource containing the raw binary dictionary
  57. */
  58. public BinaryDictionary(AssetFileDescriptor afd) {
  59. mAfd = afd;
  60. }
  61. @Override
  62. public void loadDictionary() throws Exception {
  63. if (mAfd != null) {
  64. loadDictionary(mAfd);
  65. }
  66. }
  67. private native int openNative(FileDescriptor fd, long offset, long length,
  68. int typedLetterMultiplier, int fullWordMultiplier);
  69. private native void closeNative(int dict);
  70. private native boolean isValidWordNative(int nativeData, char[] word, int wordLength);
  71. private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
  72. char[] outputChars, int[] frequencies,
  73. int maxWordLength, int maxWords, int maxAlternatives, int skipPos);
  74. private final void loadDictionary(AssetFileDescriptor afd) {
  75. long startTime = System.currentTimeMillis();
  76. mNativeDict = openNative(afd.getFileDescriptor(),
  77. afd.getStartOffset(), afd.getLength(),
  78. TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER);
  79. Log.i(TAG, "Loaded dictionary in " + (System.currentTimeMillis() - startTime) + "msec");
  80. }
  81. @Override
  82. public void getWords(final WordComposer codes, final WordCallback callback) {
  83. final int codesSize = codes.size();
  84. // Wont deal with really long words.
  85. if (codesSize > MAX_WORD_LENGTH - 1) return;
  86. Arrays.fill(mInputCodes, -1);
  87. for (int i = 0; i < codesSize; i++) {
  88. int[] alternatives = codes.getCodesAt(i);
  89. System.arraycopy(alternatives, 0, mInputCodes, i * MAX_ALTERNATIVES,
  90. Math.min(alternatives.length, MAX_ALTERNATIVES));
  91. }
  92. Arrays.fill(mOutputChars, (char) 0);
  93. Arrays.fill(mFrequencies, 0);
  94. int count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize,
  95. mOutputChars, mFrequencies,
  96. MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES, -1);
  97. // If there aren't sufficient suggestions, search for words by allowing wild cards at
  98. // the different character positions. This feature is not ready for prime-time as we need
  99. // to figure out the best ranking for such words compared to proximity corrections and
  100. // completions.
  101. if (ENABLE_MISSED_CHARACTERS && count < 5) {
  102. for (int skip = 0; skip < codesSize; skip++) {
  103. int tempCount = getSuggestionsNative(mNativeDict, mInputCodes, codesSize,
  104. mOutputChars, mFrequencies,
  105. MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES, skip);
  106. count = Math.max(count, tempCount);
  107. if (tempCount > 0) break;
  108. }
  109. }
  110. for (int j = 0; j < count; j++) {
  111. if (mFrequencies[j] < 1) break;
  112. int start = j * MAX_WORD_LENGTH;
  113. int len = 0;
  114. while (mOutputChars[start + len] != 0) {
  115. len++;
  116. }
  117. if (len > 0) {
  118. callback.addWord(mOutputChars, start, len, mFrequencies[j]);
  119. }
  120. }
  121. }
  122. @Override
  123. public boolean isValidWord(CharSequence word) {
  124. if (word == null) return false;
  125. char[] chars = word.toString().toLowerCase().toCharArray();
  126. return isValidWordNative(mNativeDict, chars, chars.length);
  127. }
  128. public synchronized void close() {
  129. if (mNativeDict != 0) {
  130. closeNative(mNativeDict);
  131. mNativeDict = 0;
  132. }
  133. }
  134. @Override
  135. protected void finalize() throws Throwable {
  136. close();
  137. super.finalize();
  138. }
  139. }