am e9f3e182: Add arguments for dic traverse session

* commit 'e9f3e182e4b3217282831fd8805958270b4dbba3':
  Add arguments for dic traverse session
main
Satoshi Kataoka 2012-08-09 20:54:37 -07:00 committed by Android Git Automerger
commit ab45cc6e5c
12 changed files with 124 additions and 68 deletions

View File

@ -98,6 +98,7 @@ public class BinaryDictionary extends Dictionary {
private static native float calcNormalizedScoreNative(char[] before, char[] after, int score); private static native float calcNormalizedScoreNative(char[] before, char[] after, int score);
private static native int editDistanceNative(char[] before, char[] after); 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) { private final void loadDictionary(String path, long startOffset, long length) {
mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER, mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER,
FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS, MAX_PREDICTIONS); FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS, MAX_PREDICTIONS);

View File

@ -23,8 +23,8 @@ public class DicTraverseSession {
JniUtils.loadNativeLibrary(); JniUtils.loadNativeLibrary();
} }
private native long setDicTraverseSessionNative(String locale); private native long setDicTraverseSessionNative(String locale);
private native void initDicTraverseSessionNative( private native void initDicTraverseSessionNative(long nativeDicTraverseSession,
long nativeDicTraverseSession, int[] previousWord, int previwousWordLength); long dictionary, int[] previousWord, int previwousWordLength);
private native void releaseDicTraverseSessionNative(long nativeDicTraverseSession); private native void releaseDicTraverseSessionNative(long nativeDicTraverseSession);
private long mNativeDicTraverseSession; private long mNativeDicTraverseSession;
@ -32,19 +32,19 @@ public class DicTraverseSession {
public DicTraverseSession(Locale locale) { public DicTraverseSession(Locale locale) {
mNativeDicTraverseSession = createNativeDicTraverseSession( mNativeDicTraverseSession = createNativeDicTraverseSession(
locale != null ? locale.toString() : ""); locale != null ? locale.toString() : "");
initSession();
} }
public long getSession() { public long getSession() {
return mNativeDicTraverseSession; return mNativeDicTraverseSession;
} }
public void initSession() { public void initSession(long dictionary) {
initSession(null, 0); initSession(dictionary, null, 0);
} }
public void initSession(int[] previousWord, int previousWordLength) { public void initSession(long dictionary, int[] previousWord, int previousWordLength) {
initDicTraverseSessionNative(mNativeDicTraverseSession, previousWord, previousWordLength); initDicTraverseSessionNative(
mNativeDicTraverseSession, dictionary, previousWord, previousWordLength);
} }
private final long createNativeDicTraverseSession(String locale) { private final long createNativeDicTraverseSession(String locale) {

View File

@ -47,6 +47,7 @@ LATIN_IME_CORE_SRC_FILES := \
char_utils.cpp \ char_utils.cpp \
correction.cpp \ correction.cpp \
dictionary.cpp \ dictionary.cpp \
dic_traverse_wrapper.cpp \
proximity_info.cpp \ proximity_info.cpp \
proximity_info_state.cpp \ proximity_info_state.cpp \
unigram_dictionary.cpp \ unigram_dictionary.cpp \

View File

@ -17,25 +17,23 @@
#define LOG_TAG "LatinIME: jni: Session" #define LOG_TAG "LatinIME: jni: Session"
#include "com_android_inputmethod_latin_DicTraverseSession.h" #include "com_android_inputmethod_latin_DicTraverseSession.h"
#include "dic_traverse_wrapper.h"
#include "jni.h" #include "jni.h"
#include "jni_common.h" #include "jni_common.h"
namespace latinime { namespace latinime {
void *(*DicTraverseWrapper::sDicTraverseSessionFactoryMethod)() = 0; static jlong latinime_setDicTraverseSession(JNIEnv *env, jobject object, jstring localeJStr) {
void (*DicTraverseWrapper::sDicTraverseSessionReleaseMethod)(void *) = 0; void *traverseSession = DicTraverseWrapper::getDicTraverseSession(env, localeJStr);
void (*DicTraverseWrapper::sDicTraverseSessionInitMethod)(
JNIEnv *, void *, const jintArray, const jint) = 0;
static jlong latinime_setDicTraverseSession(JNIEnv *env, jobject object,
jstring localejStr) {
void *traverseSession = DicTraverseWrapper::getDicTraverseSession();
return reinterpret_cast<jlong>(traverseSession); return reinterpret_cast<jlong>(traverseSession);
} }
static void latinime_initDicTraverseSession(JNIEnv *env, jlong traverseSession, static void latinime_initDicTraverseSession(JNIEnv *env, jlong traverseSession,
jintArray previousWord, jint previousWordLength) { jlong dictionary, jintArray previousWord, jint previousWordLength) {
void *ts = reinterpret_cast<void*>(traverseSession); void *ts = reinterpret_cast<void*>(traverseSession);
DicTraverseWrapper::initDicTraverseSession(env, ts, previousWord, previousWordLength); Dictionary *dict = reinterpret_cast<Dictionary*>(dictionary);
int prevWord[previousWordLength];
env->GetIntArrayRegion(previousWord, 0, previousWordLength, prevWord);
DicTraverseWrapper::initDicTraverseSession(ts, dict, prevWord, previousWordLength);
} }
static void latinime_DicTraverseSession_release( static void latinime_DicTraverseSession_release(
@ -46,7 +44,7 @@ static void latinime_DicTraverseSession_release(
static JNINativeMethod sMethods[] = { static JNINativeMethod sMethods[] = {
{"setDicTraverseSessionNative", "(Ljava/lang/String;)J", (void*)latinime_setDicTraverseSession}, {"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} {"releaseDicTraverseSessionNative", "(J)V", (void*)latinime_DicTraverseSession_release}
}; };

View File

@ -21,33 +21,6 @@
#include "jni.h" #include "jni.h"
namespace latinime { 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); int register_DicTraverseSession(JNIEnv *env);
} // namespace latinime } // namespace latinime
#endif // _COM_ANDROID_INPUTMETHOD_LATIN_DICTRAVERSESESSION_H #endif // _COM_ANDROID_INPUTMETHOD_LATIN_DICTRAVERSESESSION_H

View File

@ -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

View File

@ -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 <stdint.h>
#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

View File

@ -22,6 +22,7 @@
#include "binary_format.h" #include "binary_format.h"
#include "defines.h" #include "defines.h"
#include "dictionary.h" #include "dictionary.h"
#include "dic_traverse_wrapper.h"
#include "gesture_decoder_wrapper.h" #include "gesture_decoder_wrapper.h"
#include "unigram_dictionary.h" #include "unigram_dictionary.h"
@ -31,8 +32,9 @@ namespace latinime {
Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
int typedLetterMultiplier, int fullWordMultiplier, int typedLetterMultiplier, int fullWordMultiplier,
int maxWordLength, int maxWords, int maxPredictions) int maxWordLength, int maxWords, int maxPredictions)
: mDict((unsigned char*) dict), mDictSize(dictSize), : mDict((unsigned char*) dict),
mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) { mOffsetDict(((unsigned char*) dict) + BinaryFormat::getHeaderSize(mDict)),
mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) {
if (DEBUG_DICT) { if (DEBUG_DICT) {
if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
AKLOGI("Max word length (%d) is greater than %d", 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)); AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
} }
} }
const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict);
const unsigned int options = BinaryFormat::getFlags(mDict); const unsigned int options = BinaryFormat::getFlags(mDict);
mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier, mUnigramDictionary = new UnigramDictionary(mOffsetDict, typedLetterMultiplier,
fullWordMultiplier, maxWordLength, maxWords, options); fullWordMultiplier, maxWordLength, maxWords, options);
mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, maxPredictions); mBigramDictionary = new BigramDictionary(mOffsetDict, maxWordLength, maxPredictions);
mGestureDecoder = new GestureDecoderWrapper(maxWordLength, maxWords); mGestureDecoder = new GestureDecoderWrapper(maxWordLength, maxWords);
mGestureDecoder->setDict(mUnigramDictionary, mBigramDictionary, mGestureDecoder->setDict(mUnigramDictionary, mBigramDictionary,
mDict + headerSize /* dict root */, 0 /* root pos */); mOffsetDict /* dict root */, 0 /* root pos */);
} }
Dictionary::~Dictionary() { Dictionary::~Dictionary() {
@ -64,7 +65,8 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSessi
int *frequencies, int *spaceIndices, int *outputTypes) { int *frequencies, int *spaceIndices, int *outputTypes) {
int result = 0; int result = 0;
if (isGesture) { if (isGesture) {
mGestureDecoder->setPrevWord(prevWordChars, prevWordLength); DicTraverseWrapper::initDicTraverseSession(
traverseSession, this, prevWordChars, prevWordLength);
result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession, result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession,
xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint, xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint,
outWords, frequencies, spaceIndices, outputTypes); outWords, frequencies, spaceIndices, outputTypes);

View File

@ -55,7 +55,8 @@ class Dictionary {
int getFrequency(const int32_t *word, int length) const; int getFrequency(const int32_t *word, int length) const;
bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2) 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 getDictSize() const { return mDictSize; }
int getMmapFd() const { return mMmapFd; } int getMmapFd() const { return mMmapFd; }
int getDictBufAdjust() const { return mDictBufAdjust; } int getDictBufAdjust() const { return mDictBufAdjust; }
@ -68,6 +69,7 @@ class Dictionary {
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary); DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
const unsigned char *mDict; const unsigned char *mDict;
const unsigned char *mOffsetDict;
// Used only for the mmap version of dictionary loading, but we use these as dummy variables // Used only for the mmap version of dictionary loading, but we use these as dummy variables
// also for the malloc version. // also for the malloc version.

View File

@ -63,13 +63,6 @@ class GestureDecoderWrapper : public IncrementalDecoderInterface {
mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos); mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos);
} }
void setPrevWord(const int32_t *prevWord, int prevWordLength) {
if (!mIncrementalDecoderInterface) {
return;
}
mIncrementalDecoderInterface->setPrevWord(prevWord, prevWordLength);
}
static void setGestureDecoderFactoryMethod( static void setGestureDecoderFactoryMethod(
IncrementalDecoderInterface *(*factoryMethod)(int, int)) { IncrementalDecoderInterface *(*factoryMethod)(int, int)) {
sGestureDecoderFactoryMethod = factoryMethod; sGestureDecoderFactoryMethod = factoryMethod;

View File

@ -35,8 +35,9 @@ class IncrementalDecoderInterface {
virtual void reset() = 0; virtual void reset() = 0;
virtual void setDict(const UnigramDictionary *dict, const BigramDictionary *bigram, virtual void setDict(const UnigramDictionary *dict, const BigramDictionary *bigram,
const uint8_t *dictRoot, int rootPos) = 0; const uint8_t *dictRoot, int rootPos) = 0;
virtual void setPrevWord(const int32_t *prevWord, int prevWordLength) = 0;
virtual ~IncrementalDecoderInterface() { }; virtual ~IncrementalDecoderInterface() { };
private:
//DISALLOW_COPY_AND_ASSIGN(IncrementalDecoderInterface);
}; };
} // namespace latinime } // namespace latinime
#endif // LATINIME_INCREMENTAL_DECODER_INTERFACE_H #endif // LATINIME_INCREMENTAL_DECODER_INTERFACE_H

View File

@ -63,13 +63,6 @@ class IncrementalDecoderWrapper : public IncrementalDecoderInterface {
mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos); mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos);
} }
void setPrevWord(const int32_t *prevWord, int prevWordLength) {
if (!mIncrementalDecoderInterface) {
return;
}
mIncrementalDecoderInterface->setPrevWord(prevWord, prevWordLength);
}
static void setIncrementalDecoderFactoryMethod( static void setIncrementalDecoderFactoryMethod(
IncrementalDecoderInterface *(*factoryMethod)(int, int)) { IncrementalDecoderInterface *(*factoryMethod)(int, int)) {
sIncrementalDecoderFactoryMethod = factoryMethod; sIncrementalDecoderFactoryMethod = factoryMethod;