diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 6ac6e83a3..7ba565c31 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -98,6 +98,7 @@ public class BinaryDictionary extends Dictionary { private static native float calcNormalizedScoreNative(char[] before, char[] after, int score); private static native int editDistanceNative(char[] before, char[] after); + // TODO: Move native dict into session private final void loadDictionary(String path, long startOffset, long length) { mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER, FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS, MAX_PREDICTIONS); diff --git a/java/src/com/android/inputmethod/latin/DicTraverseSession.java b/java/src/com/android/inputmethod/latin/DicTraverseSession.java index 437876e05..160752896 100644 --- a/java/src/com/android/inputmethod/latin/DicTraverseSession.java +++ b/java/src/com/android/inputmethod/latin/DicTraverseSession.java @@ -23,8 +23,8 @@ public class DicTraverseSession { JniUtils.loadNativeLibrary(); } private native long setDicTraverseSessionNative(String locale); - private native void initDicTraverseSessionNative( - long nativeDicTraverseSession, int[] previousWord, int previwousWordLength); + private native void initDicTraverseSessionNative(long nativeDicTraverseSession, + long dictionary, int[] previousWord, int previwousWordLength); private native void releaseDicTraverseSessionNative(long nativeDicTraverseSession); private long mNativeDicTraverseSession; @@ -32,19 +32,19 @@ public class DicTraverseSession { public DicTraverseSession(Locale locale) { mNativeDicTraverseSession = createNativeDicTraverseSession( locale != null ? locale.toString() : ""); - initSession(); } public long getSession() { return mNativeDicTraverseSession; } - public void initSession() { - initSession(null, 0); + public void initSession(long dictionary) { + initSession(dictionary, null, 0); } - public void initSession(int[] previousWord, int previousWordLength) { - initDicTraverseSessionNative(mNativeDicTraverseSession, previousWord, previousWordLength); + public void initSession(long dictionary, int[] previousWord, int previousWordLength) { + initDicTraverseSessionNative( + mNativeDicTraverseSession, dictionary, previousWord, previousWordLength); } private final long createNativeDicTraverseSession(String locale) { diff --git a/native/jni/Android.mk b/native/jni/Android.mk index 1b7301b19..86ad8576e 100644 --- a/native/jni/Android.mk +++ b/native/jni/Android.mk @@ -47,6 +47,7 @@ LATIN_IME_CORE_SRC_FILES := \ char_utils.cpp \ correction.cpp \ dictionary.cpp \ + dic_traverse_wrapper.cpp \ proximity_info.cpp \ proximity_info_state.cpp \ unigram_dictionary.cpp \ diff --git a/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp b/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp index 0f5c39642..f0fa2db2a 100644 --- a/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp +++ b/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp @@ -17,25 +17,23 @@ #define LOG_TAG "LatinIME: jni: Session" #include "com_android_inputmethod_latin_DicTraverseSession.h" +#include "dic_traverse_wrapper.h" #include "jni.h" #include "jni_common.h" namespace latinime { -void *(*DicTraverseWrapper::sDicTraverseSessionFactoryMethod)() = 0; -void (*DicTraverseWrapper::sDicTraverseSessionReleaseMethod)(void *) = 0; -void (*DicTraverseWrapper::sDicTraverseSessionInitMethod)( - JNIEnv *, void *, const jintArray, const jint) = 0; - -static jlong latinime_setDicTraverseSession(JNIEnv *env, jobject object, - jstring localejStr) { - void *traverseSession = DicTraverseWrapper::getDicTraverseSession(); +static jlong latinime_setDicTraverseSession(JNIEnv *env, jobject object, jstring localeJStr) { + void *traverseSession = DicTraverseWrapper::getDicTraverseSession(env, localeJStr); return reinterpret_cast(traverseSession); } static void latinime_initDicTraverseSession(JNIEnv *env, jlong traverseSession, - jintArray previousWord, jint previousWordLength) { + jlong dictionary, jintArray previousWord, jint previousWordLength) { void *ts = reinterpret_cast(traverseSession); - DicTraverseWrapper::initDicTraverseSession(env, ts, previousWord, previousWordLength); + Dictionary *dict = reinterpret_cast(dictionary); + int prevWord[previousWordLength]; + env->GetIntArrayRegion(previousWord, 0, previousWordLength, prevWord); + DicTraverseWrapper::initDicTraverseSession(ts, dict, prevWord, previousWordLength); } static void latinime_DicTraverseSession_release( @@ -46,7 +44,7 @@ static void latinime_DicTraverseSession_release( static JNINativeMethod sMethods[] = { {"setDicTraverseSessionNative", "(Ljava/lang/String;)J", (void*)latinime_setDicTraverseSession}, - {"initDicTraverseSessionNative", "(J[II)V", (void*)latinime_initDicTraverseSession}, + {"initDicTraverseSessionNative", "(JJ[II)V", (void*)latinime_initDicTraverseSession}, {"releaseDicTraverseSessionNative", "(J)V", (void*)latinime_DicTraverseSession_release} }; diff --git a/native/jni/com_android_inputmethod_latin_DicTraverseSession.h b/native/jni/com_android_inputmethod_latin_DicTraverseSession.h index a76815d7f..37531e96b 100644 --- a/native/jni/com_android_inputmethod_latin_DicTraverseSession.h +++ b/native/jni/com_android_inputmethod_latin_DicTraverseSession.h @@ -21,33 +21,6 @@ #include "jni.h" namespace latinime { - -// TODO: Remove -class DicTraverseWrapper { - public: - static void *getDicTraverseSession() { - if (sDicTraverseSessionFactoryMethod) { - return sDicTraverseSessionFactoryMethod(); - } - return 0; - } - static void initDicTraverseSession(JNIEnv *env, void *traverseSession, - const jintArray prevWord, const jint prevWordLength) { - if (sDicTraverseSessionInitMethod) { - sDicTraverseSessionInitMethod(env, traverseSession, prevWord, prevWordLength); - } - } - static void releaseDicTraverseSession(void *traverseSession) { - if (sDicTraverseSessionReleaseMethod) { - sDicTraverseSessionReleaseMethod(traverseSession); - } - } - private: - DISALLOW_IMPLICIT_CONSTRUCTORS(DicTraverseWrapper); - static void *(*sDicTraverseSessionFactoryMethod)(); - static void (*sDicTraverseSessionInitMethod)(JNIEnv *, void *, const jintArray, const jint); - static void (*sDicTraverseSessionReleaseMethod)(void *); -}; int register_DicTraverseSession(JNIEnv *env); } // namespace latinime #endif // _COM_ANDROID_INPUTMETHOD_LATIN_DICTRAVERSESESSION_H diff --git a/native/jni/src/dic_traverse_wrapper.cpp b/native/jni/src/dic_traverse_wrapper.cpp new file mode 100644 index 000000000..1f7dcbfb2 --- /dev/null +++ b/native/jni/src/dic_traverse_wrapper.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2012, 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. + */ + +#define LOG_TAG "LatinIME: jni: Session" + +#include "dic_traverse_wrapper.h" + +namespace latinime { +void *(*DicTraverseWrapper::sDicTraverseSessionFactoryMethod)(JNIEnv *env, jstring locale) = 0; +void (*DicTraverseWrapper::sDicTraverseSessionReleaseMethod)(void *) = 0; +void (*DicTraverseWrapper::sDicTraverseSessionInitMethod)( + void *, Dictionary *, const int *, const int) = 0; +} // namespace latinime diff --git a/native/jni/src/dic_traverse_wrapper.h b/native/jni/src/dic_traverse_wrapper.h new file mode 100644 index 000000000..8396d0027 --- /dev/null +++ b/native/jni/src/dic_traverse_wrapper.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2012, 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_DIC_TRAVERSE_WRAPPER_H +#define LATINIME_DIC_TRAVERSE_WRAPPER_H + +#include + +#include "defines.h" +#include "jni.h" + +namespace latinime { +class Dictionary; +// TODO: Remove +class DicTraverseWrapper { + public: + static void *getDicTraverseSession(JNIEnv *env, jstring locale) { + if (sDicTraverseSessionFactoryMethod) { + return sDicTraverseSessionFactoryMethod(env, locale); + } + return 0; + } + static void initDicTraverseSession(void *traverseSession, + Dictionary *dictionary, const int *prevWord, const int prevWordLength) { + if (sDicTraverseSessionInitMethod) { + sDicTraverseSessionInitMethod(traverseSession, dictionary, prevWord, prevWordLength); + } + } + static void releaseDicTraverseSession(void *traverseSession) { + if (sDicTraverseSessionReleaseMethod) { + sDicTraverseSessionReleaseMethod(traverseSession); + } + } + static void setTraverseSessionFactoryMethod( + void *(*factoryMethod)(JNIEnv *env, jstring locale)) { + sDicTraverseSessionFactoryMethod = factoryMethod; + } + static void setTraverseSessionInitMethod( + void (*initMethod)(void *, Dictionary *, const int *, const int)) { + sDicTraverseSessionInitMethod = initMethod; + } + static void setTraverseSessionReleaseMethod(void (*releaseMethod)(void *)) { + sDicTraverseSessionReleaseMethod = releaseMethod; + } + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(DicTraverseWrapper); + static void *(*sDicTraverseSessionFactoryMethod)(JNIEnv *, jstring); + static void (*sDicTraverseSessionInitMethod)(void *, Dictionary *, const int *, const int); + static void (*sDicTraverseSessionReleaseMethod)(void *); +}; +int register_DicTraverseSession(JNIEnv *env); +} // namespace latinime +#endif // LATINIME_DIC_TRAVERSE_WRAPPER_H diff --git a/native/jni/src/dictionary.cpp b/native/jni/src/dictionary.cpp index 8c785a263..f3bdb310d 100644 --- a/native/jni/src/dictionary.cpp +++ b/native/jni/src/dictionary.cpp @@ -22,6 +22,7 @@ #include "binary_format.h" #include "defines.h" #include "dictionary.h" +#include "dic_traverse_wrapper.h" #include "gesture_decoder_wrapper.h" #include "unigram_dictionary.h" @@ -31,8 +32,9 @@ namespace latinime { Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords, int maxPredictions) - : mDict((unsigned char*) dict), mDictSize(dictSize), - mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) { + : mDict((unsigned char*) dict), + mOffsetDict(((unsigned char*) dict) + BinaryFormat::getHeaderSize(mDict)), + mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) { if (DEBUG_DICT) { if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { AKLOGI("Max word length (%d) is greater than %d", @@ -40,14 +42,13 @@ Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF)); } } - const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict); const unsigned int options = BinaryFormat::getFlags(mDict); - mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier, + mUnigramDictionary = new UnigramDictionary(mOffsetDict, typedLetterMultiplier, fullWordMultiplier, maxWordLength, maxWords, options); - mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, maxPredictions); + mBigramDictionary = new BigramDictionary(mOffsetDict, maxWordLength, maxPredictions); mGestureDecoder = new GestureDecoderWrapper(maxWordLength, maxWords); mGestureDecoder->setDict(mUnigramDictionary, mBigramDictionary, - mDict + headerSize /* dict root */, 0 /* root pos */); + mOffsetDict /* dict root */, 0 /* root pos */); } Dictionary::~Dictionary() { @@ -64,7 +65,8 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSessi int *frequencies, int *spaceIndices, int *outputTypes) { int result = 0; if (isGesture) { - mGestureDecoder->setPrevWord(prevWordChars, prevWordLength); + DicTraverseWrapper::initDicTraverseSession( + traverseSession, this, prevWordChars, prevWordLength); result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession, xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint, outWords, frequencies, spaceIndices, outputTypes); diff --git a/native/jni/src/dictionary.h b/native/jni/src/dictionary.h index 2c79527be..b550ba4df 100644 --- a/native/jni/src/dictionary.h +++ b/native/jni/src/dictionary.h @@ -55,7 +55,8 @@ class Dictionary { int getFrequency(const int32_t *word, int length) const; bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2) const; - void *getDict() const { return (void *)mDict; } + void *getDict() const { return (void *)mDict; } // required to release dictionary buffer + void *getOffsetDict() const { return (void *)mOffsetDict; } int getDictSize() const { return mDictSize; } int getMmapFd() const { return mMmapFd; } int getDictBufAdjust() const { return mDictBufAdjust; } @@ -68,6 +69,7 @@ class Dictionary { private: DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary); const unsigned char *mDict; + const unsigned char *mOffsetDict; // Used only for the mmap version of dictionary loading, but we use these as dummy variables // also for the malloc version. diff --git a/native/jni/src/gesture/gesture_decoder_wrapper.h b/native/jni/src/gesture/gesture_decoder_wrapper.h index b70c8e0a3..14a0705f7 100644 --- a/native/jni/src/gesture/gesture_decoder_wrapper.h +++ b/native/jni/src/gesture/gesture_decoder_wrapper.h @@ -63,13 +63,6 @@ class GestureDecoderWrapper : public IncrementalDecoderInterface { mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos); } - void setPrevWord(const int32_t *prevWord, int prevWordLength) { - if (!mIncrementalDecoderInterface) { - return; - } - mIncrementalDecoderInterface->setPrevWord(prevWord, prevWordLength); - } - static void setGestureDecoderFactoryMethod( IncrementalDecoderInterface *(*factoryMethod)(int, int)) { sGestureDecoderFactoryMethod = factoryMethod; diff --git a/native/jni/src/gesture/incremental_decoder_interface.h b/native/jni/src/gesture/incremental_decoder_interface.h index e8d3a5333..cc219bf50 100644 --- a/native/jni/src/gesture/incremental_decoder_interface.h +++ b/native/jni/src/gesture/incremental_decoder_interface.h @@ -35,8 +35,9 @@ class IncrementalDecoderInterface { virtual void reset() = 0; virtual void setDict(const UnigramDictionary *dict, const BigramDictionary *bigram, const uint8_t *dictRoot, int rootPos) = 0; - virtual void setPrevWord(const int32_t *prevWord, int prevWordLength) = 0; virtual ~IncrementalDecoderInterface() { }; + private: + //DISALLOW_COPY_AND_ASSIGN(IncrementalDecoderInterface); }; } // namespace latinime #endif // LATINIME_INCREMENTAL_DECODER_INTERFACE_H diff --git a/native/jni/src/gesture/incremental_decoder_wrapper.h b/native/jni/src/gesture/incremental_decoder_wrapper.h index 8d66b4ec1..2f9602853 100644 --- a/native/jni/src/gesture/incremental_decoder_wrapper.h +++ b/native/jni/src/gesture/incremental_decoder_wrapper.h @@ -63,13 +63,6 @@ class IncrementalDecoderWrapper : public IncrementalDecoderInterface { mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos); } - void setPrevWord(const int32_t *prevWord, int prevWordLength) { - if (!mIncrementalDecoderInterface) { - return; - } - mIncrementalDecoderInterface->setPrevWord(prevWord, prevWordLength); - } - static void setIncrementalDecoderFactoryMethod( IncrementalDecoderInterface *(*factoryMethod)(int, int)) { sIncrementalDecoderFactoryMethod = factoryMethod;