diff options
Diffstat (limited to 'native')
-rw-r--r-- | native/Android.mk | 2 | ||||
-rw-r--r-- | native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp | 36 | ||||
-rw-r--r-- | native/jni/com_android_inputmethod_keyboard_ProximityInfo.h | 2 | ||||
-rw-r--r-- | native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp | 36 | ||||
-rw-r--r-- | native/jni/com_android_inputmethod_latin_BinaryDictionary.h | 2 | ||||
-rw-r--r-- | native/jni/jni_common.cpp (renamed from native/jni/onload.cpp) | 23 | ||||
-rw-r--r-- | native/jni/jni_common.h | 30 | ||||
-rw-r--r-- | native/src/bigram_dictionary.h | 5 | ||||
-rw-r--r-- | native/src/char_utils.h | 2 | ||||
-rw-r--r-- | native/src/debug.h | 3 | ||||
-rw-r--r-- | native/src/dictionary.h | 5 | ||||
-rw-r--r-- | native/src/proximity_info.cpp | 4 | ||||
-rw-r--r-- | native/src/proximity_info.h | 4 | ||||
-rw-r--r-- | native/src/unigram_dictionary.cpp | 74 | ||||
-rw-r--r-- | native/src/unigram_dictionary.h | 20 |
15 files changed, 125 insertions, 123 deletions
diff --git a/native/Android.mk b/native/Android.mk index 4727b1e39..d9f4f1d38 100644 --- a/native/Android.mk +++ b/native/Android.mk @@ -11,7 +11,7 @@ LOCAL_CFLAGS += -Wno-unused-parameter -Wno-unused-function LOCAL_SRC_FILES := \ jni/com_android_inputmethod_keyboard_ProximityInfo.cpp \ jni/com_android_inputmethod_latin_BinaryDictionary.cpp \ - jni/onload.cpp \ + jni/jni_common.cpp \ src/bigram_dictionary.cpp \ src/char_utils.cpp \ src/dictionary.cpp \ diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp index 3db89edf1..f3e2a7e60 100644 --- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp +++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp @@ -19,28 +19,15 @@ #include "com_android_inputmethod_keyboard_ProximityInfo.h" #include "jni.h" +#include "jni_common.h" #include "proximity_info.h" #include <assert.h> #include <errno.h> #include <stdio.h> -// ---------------------------------------------------------------------------- - namespace latinime { -// -// helper function to throw an exception -// -static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) { - if (jclass cls = env->FindClass(ex)) { - char msg[1000]; - snprintf(msg, sizeof(msg), fmt, data); - env->ThrowNew(cls, msg); - env->DeleteLocalRef(cls); - } -} - static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object, jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth, jint gridHeight, jintArray proximityCharsArray) { @@ -57,34 +44,15 @@ static void latinime_Keyboard_release(JNIEnv *env, jobject object, jint proximit delete pi; } -// ---------------------------------------------------------------------------- - static JNINativeMethod sKeyboardMethods[] = { {"setProximityInfoNative", "(IIIII[I)I", (void*)latinime_Keyboard_setProximityInfo}, {"releaseProximityInfoNative", "(I)V", (void*)latinime_Keyboard_release} }; -static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, - int numMethods) { - jclass clazz; - - clazz = env->FindClass(className); - if (clazz == NULL) { - LOGE("Native registration unable to find class '%s'", className); - return JNI_FALSE; - } - if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { - LOGE("RegisterNatives failed for '%s'", className); - return JNI_FALSE; - } - - return JNI_TRUE; -} - int register_ProximityInfo(JNIEnv *env) { const char* const kClassPathName = "com/android/inputmethod/keyboard/ProximityInfo"; return registerNativeMethods(env, kClassPathName, sKeyboardMethods, sizeof(sKeyboardMethods) / sizeof(sKeyboardMethods[0])); } -}; // namespace latinime +} // namespace latinime diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h index bdeeb8f37..4a1e83b09 100644 --- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h +++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h @@ -21,7 +21,9 @@ #include "jni.h" namespace latinime { + int register_ProximityInfo(JNIEnv *env); + } #endif // _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index 4b61c1414..ce874d8d4 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -20,6 +20,7 @@ #include "com_android_inputmethod_latin_BinaryDictionary.h" #include "dictionary.h" #include "jni.h" +#include "jni_common.h" #include "proximity_info.h" #include <assert.h> @@ -35,22 +36,8 @@ #include <stdlib.h> #endif // USE_MMAP_FOR_DICTIONARY -// ---------------------------------------------------------------------------- - namespace latinime { -// -// helper function to throw an exception -// -static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) { - if (jclass cls = env->FindClass(ex)) { - char msg[1000]; - snprintf(msg, sizeof(msg), fmt, data); - env->ThrowNew(cls, msg); - env->DeleteLocalRef(cls); - } -} - static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object, jstring sourceDir, jlong dictOffset, jlong dictSize, jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords, @@ -208,8 +195,6 @@ static void latinime_BinaryDictionary_close(JNIEnv *env, jobject object, jint di delete dictionary; } -// ---------------------------------------------------------------------------- - static JNINativeMethod sMethods[] = { {"openNative", "(Ljava/lang/String;JJIIIII)I", (void*)latinime_BinaryDictionary_open}, {"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close}, @@ -218,27 +203,10 @@ static JNINativeMethod sMethods[] = { {"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams} }; -static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, - int numMethods) { - jclass clazz; - - clazz = env->FindClass(className); - if (clazz == NULL) { - LOGE("Native registration unable to find class '%s'", className); - return JNI_FALSE; - } - if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { - LOGE("RegisterNatives failed for '%s'", className); - return JNI_FALSE; - } - - return JNI_TRUE; -} - int register_BinaryDictionary(JNIEnv *env) { const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary"; return registerNativeMethods(env, kClassPathName, sMethods, sizeof(sMethods) / sizeof(sMethods[0])); } -}; // namespace latinime +} // namespace latinime diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.h b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h index f7cd81fa7..1b1ba7f0f 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.h +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h @@ -21,7 +21,9 @@ #include "jni.h" namespace latinime { + int register_BinaryDictionary(JNIEnv *env); + } #endif // _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H diff --git a/native/jni/onload.cpp b/native/jni/jni_common.cpp index f02c9a052..8643f723f 100644 --- a/native/jni/onload.cpp +++ b/native/jni/jni_common.cpp @@ -26,11 +26,8 @@ #include <errno.h> #include <stdio.h> -// ---------------------------------------------------------------------------- - using namespace latinime; - /* * Returns the JNI version on success, -1 on failure. */ @@ -60,3 +57,23 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { bail: return result; } + +namespace latinime { + +int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods, + int numMethods) { + jclass clazz = env->FindClass(className); + if (clazz == NULL) { + LOGE("Native registration unable to find class '%s'", className); + return JNI_FALSE; + } + if (env->RegisterNatives(clazz, methods, numMethods) < 0) { + LOGE("RegisterNatives failed for '%s'", className); + env->DeleteLocalRef(clazz); + return JNI_FALSE; + } + env->DeleteLocalRef(clazz); + return JNI_TRUE; +} + +} // namespace latinime diff --git a/native/jni/jni_common.h b/native/jni/jni_common.h new file mode 100644 index 000000000..c502fa3a8 --- /dev/null +++ b/native/jni/jni_common.h @@ -0,0 +1,30 @@ +/* +** +** Copyright 2011, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#ifndef LATINIME_JNI_COMMON_H +#define LATINIME_JNI_COMMON_H + +#include "jni.h" + +namespace latinime { + +int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods, + int numMethods); + +} // namespace latinime + +#endif // LATINIME_JNI_COMMON_H diff --git a/native/src/bigram_dictionary.h b/native/src/bigram_dictionary.h index d658b93e6..c07458a38 100644 --- a/native/src/bigram_dictionary.h +++ b/native/src/bigram_dictionary.h @@ -50,6 +50,7 @@ private: int *mInputCodes; int mInputLength; }; -// ---------------------------------------------------------------------------- -}; // namespace latinime + +} // namespace latinime + #endif // LATINIME_BIGRAM_DICTIONARY_H diff --git a/native/src/char_utils.h b/native/src/char_utils.h index 921ecb4a5..a69a35e7a 100644 --- a/native/src/char_utils.h +++ b/native/src/char_utils.h @@ -21,6 +21,6 @@ namespace latinime { unsigned short latin_tolower(unsigned short c); -}; // namespace latinime +} // namespace latinime #endif // LATINIME_CHAR_UTILS_H diff --git a/native/src/debug.h b/native/src/debug.h index ae629b222..38b2f107a 100644 --- a/native/src/debug.h +++ b/native/src/debug.h @@ -28,6 +28,7 @@ static inline unsigned char* convertToUnibyteString(unsigned short* input, unsig output[i] = 0; return output; } + static inline unsigned char* convertToUnibyteStringAndReplaceLastChar(unsigned short* input, unsigned char* output, const unsigned int length, unsigned char c) { int i = 0; @@ -37,6 +38,7 @@ static inline unsigned char* convertToUnibyteStringAndReplaceLastChar(unsigned s output[i] = 0; return output; } + static inline void LOGI_S16(unsigned short* string, const unsigned int length) { unsigned char tmp_buffer[length]; convertToUnibyteString(string, tmp_buffer, length); @@ -46,6 +48,7 @@ static inline void LOGI_S16(unsigned short* string, const unsigned int length) { // TODO : refactor this in a blocking log or something. // usleep(10); } + static inline void LOGI_S16_PLUS(unsigned short* string, const unsigned int length, unsigned char c) { unsigned char tmp_buffer[length+1]; diff --git a/native/src/dictionary.h b/native/src/dictionary.h index 1b41f69dd..3dc577a56 100644 --- a/native/src/dictionary.h +++ b/native/src/dictionary.h @@ -81,7 +81,6 @@ private: BigramDictionary *mBigramDictionary; }; -// ---------------------------------------------------------------------------- // public static utility methods // static inline methods should be defined in the header file inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) { @@ -134,7 +133,6 @@ inline int Dictionary::getFreq(const unsigned char *dict, return freq; } - inline int Dictionary::wideStrLen(unsigned short *str) { if (!str) return 0; unsigned short *end = str; @@ -158,5 +156,6 @@ inline int Dictionary::setDictionaryValues(const unsigned char *dict, return position; } -}; // namespace latinime +} // namespace latinime + #endif // LATINIME_DICTIONARY_H diff --git a/native/src/proximity_info.cpp b/native/src/proximity_info.cpp index 102123c3c..209c31e6e 100644 --- a/native/src/proximity_info.cpp +++ b/native/src/proximity_info.cpp @@ -22,6 +22,7 @@ #include "proximity_info.h" namespace latinime { + ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, const int keyboardHeight, const int gridWidth, const int gridHeight, const uint32_t *proximityCharsArray) @@ -61,4 +62,5 @@ bool ProximityInfo::hasSpaceProximity(const int x, const int y) const { } return false; } -} // namespace latinime + +} // namespace latinime diff --git a/native/src/proximity_info.h b/native/src/proximity_info.h index c2062e8c5..327cd0940 100644 --- a/native/src/proximity_info.h +++ b/native/src/proximity_info.h @@ -41,5 +41,7 @@ private: const int CELL_HEIGHT; uint32_t *mProximityCharsArray; }; -}; // namespace latinime + +} // namespace latinime + #endif // LATINIME_PROXIMITY_INFO_H diff --git a/native/src/unigram_dictionary.cpp b/native/src/unigram_dictionary.cpp index c294cf3b5..ef52ceb7c 100644 --- a/native/src/unigram_dictionary.cpp +++ b/native/src/unigram_dictionary.cpp @@ -272,6 +272,7 @@ static inline void registerNextLetter(unsigned short c, int *nextLetters, int ne } // TODO: We need to optimize addWord by using STL or something +// TODO: This needs to take an const unsigned short* and not tinker with its contents bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) { word[length] = 0; if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) { @@ -321,6 +322,16 @@ bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) return false; } +inline void UnigramDictionary::addWordAlternatesSpellings(const uint8_t* const root, int pos, + int depth, int finalFreq) { + // TODO: actually add alternates when the format supports it. +} + +static inline bool hasAlternateSpellings(uint8_t flags) { + // TODO: when the format supports it, return the actual value. + return false; +} + static inline unsigned short toBaseLowerCase(unsigned short c) { if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) { c = BASE_CHARS[c]; @@ -333,7 +344,7 @@ static inline unsigned short toBaseLowerCase(unsigned short c) { return c; } -bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) { +bool UnigramDictionary::sameAsTyped(const unsigned short *word, int length) const { if (length != mInputLength) { return false; } @@ -579,7 +590,6 @@ void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, cons for (int i = 0; i < childrenCount; ++i) { int newCount; int newChildPosition; - const int newDepth = depth + 1; bool newTraverseAllNodes; int newMatchRate; int newInputIndex; @@ -595,7 +605,7 @@ void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, cons siblingPos = newSiblingPos; if (needsToTraverseChildrenNodes) { - getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes, + getWordsRec(newCount, newChildPosition, newOutputIndex, maxDepth, newTraverseAllNodes, newMatchRate, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos, nextLetters, nextLettersSize); } @@ -657,28 +667,6 @@ inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int return finalFreq; } -inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength( - unsigned short *word, const int inputIndex, const int depth, const int matchWeight, - int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos, - const int transposedPos, const int freq) { - const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, excessivePos, - transposedPos, freq, false); - if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq); - if (depth >= mInputLength && skipPos < 0) { - registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize); - } -} - -inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength( - unsigned short *word, const int inputIndex, const int depth, const int matchWeight, - const int skipPos, const int excessivePos, const int transposedPos, const int freq) { - if (sameAsTyped(word, depth + 1)) return; - const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, - excessivePos, transposedPos, freq, true); - // Proximity collection will promote a word of the same length as what user typed. - if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq); -} - inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c, const int inputIndex, const int skipPos, const int depth) { const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0]; @@ -709,7 +697,6 @@ inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex return false; } - // In the following function, c is the current character of the dictionary word // currently examined. // currentChars is an array containing the keys close to the character the @@ -752,6 +739,30 @@ inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId return UNRELATED_CHAR; } +inline void UnigramDictionary::onTerminal(unsigned short int* word, const int depth, + const uint8_t* const root, const uint8_t flags, int pos, + const int inputIndex, const int matchWeight, const int skipPos, + const int excessivePos, const int transposedPos, const int freq, const bool sameLength, + int* nextLetters, const int nextLettersSize) { + + const bool isSameAsTyped = sameLength ? sameAsTyped(word, depth + 1) : false; + const bool hasAlternates = hasAlternateSpellings(flags); + if (isSameAsTyped && !hasAlternates) return; + + if (depth >= MIN_SUGGEST_DEPTH) { + const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, + excessivePos, transposedPos, freq, sameLength); + if (!isSameAsTyped) + addWord(word, depth + 1, finalFreq); + if (hasAlternates) + addWordAlternatesSpellings(DICT_ROOT, pos, flags, finalFreq); + } + + if (sameLength && depth >= mInputLength && skipPos < 0) { + registerNextLetter(word[mInputLength], nextLetters, nextLettersSize); + } +} + inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth, const int maxDepth, const bool traverseAllNodes, int matchWeight, int inputIndex, const int diffs, const int skipPos, const int excessivePos, const int transposedPos, @@ -771,6 +782,8 @@ inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth int freq; bool isSameAsUserTypedLength = false; + const uint8_t flags = 0; // No flags for now + if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex; *nextSiblingPosition = Dictionary::setDictionaryValues(DICT_ROOT, IS_LATEST_DICT_VERSION, pos, @@ -783,9 +796,8 @@ inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) { mWord[depth] = c; if (traverseAllNodes && terminal) { - onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth, - matchWeight, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos, - freq); + onTerminal(mWord, depth, DICT_ROOT, flags, pos, inputIndex, matchWeight, skipPos, + excessivePos, transposedPos, freq, false, nextLetters, nextLettersSize); } if (!needsToTraverseChildrenNodes) return false; *newTraverseAllNodes = traverseAllNodes; @@ -812,8 +824,8 @@ inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth bool isSameAsUserTypedLength = mInputLength == inputIndex + 1 || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2); if (isSameAsUserTypedLength && terminal) { - onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, matchWeight, - skipPos, excessivePos, transposedPos, freq); + onTerminal(mWord, depth, DICT_ROOT, flags, pos, inputIndex, matchWeight, skipPos, + excessivePos, transposedPos, freq, true, nextLetters, nextLettersSize); } if (!needsToTraverseChildrenNodes) return false; // Start traversing all nodes after the index exceeds the user typed length diff --git a/native/src/unigram_dictionary.h b/native/src/unigram_dictionary.h index c47db1ad2..154ac9b36 100644 --- a/native/src/unigram_dictionary.h +++ b/native/src/unigram_dictionary.h @@ -64,9 +64,9 @@ private: bool checkIfDictVersionIsLatest(); int getAddress(int *pos); int getFreq(int *pos); - int wideStrLen(unsigned short *str); - bool sameAsTyped(unsigned short *word, int length); + bool sameAsTyped(const unsigned short *word, int length) const; bool addWord(unsigned short *word, int length, int frequency); + void addWordAlternatesSpellings(const uint8_t* const root, int pos, int depth, int finalFreq); void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs, const int skipPos, const int excessivePos, const int transposedPos, int *nextLetters, @@ -83,13 +83,11 @@ private: int calculateFinalFreq(const int inputIndex, const int depth, const int snr, const int skipPos, const int excessivePos, const int transposedPos, const int freq, const bool sameLength) const; - void onTerminalWhenUserTypedLengthIsGreaterThanInputLength(unsigned short *word, - const int inputIndex, const int depth, const int snr, int *nextLetters, - const int nextLettersSize, const int skipPos, const int excessivePos, - const int transposedPos, const int freq); - void onTerminalWhenUserTypedLengthIsSameAsInputLength(unsigned short *word, - const int inputIndex, const int depth, const int snr, const int skipPos, - const int excessivePos, const int transposedPos, const int freq); + void onTerminal(unsigned short int* word, const int depth, + const uint8_t* const root, const uint8_t flags, int pos, + const int inputIndex, const int matchWeight, const int skipPos, + const int excessivePos, const int transposedPos, const int freq, const bool sameLength, + int *nextLetters, const int nextLettersSize); bool needsToSkipCurrentNode(const unsigned short c, const int inputIndex, const int skipPos, const int depth); ProximityType getMatchedProximityId(const int *currentChars, const unsigned short c, @@ -149,8 +147,6 @@ private: int mNextLettersFrequency[NEXT_LETTERS_SIZE]; }; -// ---------------------------------------------------------------------------- - -}; // namespace latinime +} // namespace latinime #endif // LATINIME_UNIGRAM_DICTIONARY_H |