From 880624838611a69f20f39ae762181ea4639dd071 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Tue, 16 Sep 2014 20:52:32 +0900 Subject: [PATCH] Pass prevWordCount to native code for getSuggestion. Bug: 14425059 Change-Id: Id2b49a448982c997e5c0a22445b115e675156c3e --- .../inputmethod/latin/BinaryDictionary.java | 14 +++++++------- ...droid_inputmethod_latin_BinaryDictionary.cpp | 17 ++++++++++------- native/jni/src/utils/jni_data_utils.h | 10 +++------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 8b13bdb02..2ae54348a 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -186,9 +186,9 @@ public final class BinaryDictionary extends Dictionary { long traverseSession, int[] xCoordinates, int[] yCoordinates, int[] times, int[] pointerIds, int[] inputCodePoints, int inputSize, int[] suggestOptions, int[][] prevWordCodePointArrays, boolean[] isBeginningOfSentenceArray, - int[] outputSuggestionCount, int[] outputCodePoints, int[] outputScores, - int[] outputIndices, int[] outputTypes, int[] outputAutoCommitFirstWordConfidence, - float[] inOutLanguageWeight); + int prevWordCount, int[] outputSuggestionCount, int[] outputCodePoints, + int[] outputScores, int[] outputIndices, int[] outputTypes, + int[] outputAutoCommitFirstWordConfidence, float[] inOutLanguageWeight); private static native boolean addUnigramEntryNative(long dict, int[] word, int probability, int[] shortcutTarget, int shortcutProbability, boolean isBeginningOfSentence, boolean isNotAWord, boolean isBlacklisted, int timestamp); @@ -295,10 +295,10 @@ public final class BinaryDictionary extends Dictionary { inputPointers.getYCoordinates(), inputPointers.getTimes(), inputPointers.getPointerIds(), session.mInputCodePoints, inputSize, session.mNativeSuggestOptions.getOptions(), session.mPrevWordCodePointArrays, - session.mIsBeginningOfSentenceArray, session.mOutputSuggestionCount, - session.mOutputCodePoints, session.mOutputScores, session.mSpaceIndices, - session.mOutputTypes, session.mOutputAutoCommitFirstWordConfidence, - session.mInputOutputLanguageWeight); + session.mIsBeginningOfSentenceArray, prevWordsInfo.getPrevWordCount(), + session.mOutputSuggestionCount, session.mOutputCodePoints, session.mOutputScores, + session.mSpaceIndices, session.mOutputTypes, + session.mOutputAutoCommitFirstWordConfidence, session.mInputOutputLanguageWeight); if (inOutLanguageWeight != null) { inOutLanguageWeight[0] = session.mInputOutputLanguageWeight[0]; } diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index 440b963d9..e65dc4c06 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -180,8 +180,8 @@ static void latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, jintArray yCoordinatesArray, jintArray timesArray, jintArray pointerIdsArray, jintArray inputCodePointsArray, jint inputSize, jintArray suggestOptions, jobjectArray prevWordCodePointArrays, jbooleanArray isBeginningOfSentenceArray, - jintArray outSuggestionCount, jintArray outCodePointsArray, jintArray outScoresArray, - jintArray outSpaceIndicesArray, jintArray outTypesArray, + jint prevWordCount, jintArray outSuggestionCount, jintArray outCodePointsArray, + jintArray outScoresArray, jintArray outSpaceIndicesArray, jintArray outTypesArray, jintArray outAutoCommitFirstWordConfidenceArray, jfloatArray inOutLanguageWeight) { Dictionary *dictionary = reinterpret_cast(dict); // Assign 0 to outSuggestionCount here in case of returning earlier in this method. @@ -241,7 +241,7 @@ static void latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, env->GetFloatArrayRegion(inOutLanguageWeight, 0, 1 /* len */, &languageWeight); SuggestionResults suggestionResults(MAX_RESULTS); const PrevWordsInfo prevWordsInfo = JniDataUtils::constructPrevWordsInfo(env, - prevWordCodePointArrays, isBeginningOfSentenceArray); + prevWordCodePointArrays, isBeginningOfSentenceArray, prevWordCount); if (givenSuggestOptions.isGesture() || inputSize > 0) { // TODO: Use SuggestionResults to return suggestions. dictionary->getSuggestions(pInfo, traverseSession, xCoordinates, yCoordinates, @@ -285,7 +285,8 @@ static jint latinime_BinaryDictionary_getNgramProbability(JNIEnv *env, jclass cl int wordCodePoints[wordLength]; env->GetIntArrayRegion(word, 0, wordLength, wordCodePoints); const PrevWordsInfo prevWordsInfo = JniDataUtils::constructPrevWordsInfo(env, - prevWordCodePointArrays, isBeginningOfSentenceArray); + prevWordCodePointArrays, isBeginningOfSentenceArray, + env->GetArrayLength(prevWordCodePointArrays)); return dictionary->getNgramProbability(&prevWordsInfo, CodePointArrayView(wordCodePoints, wordLength)); } @@ -393,7 +394,8 @@ static bool latinime_BinaryDictionary_addNgramEntry(JNIEnv *env, jclass clazz, j return false; } const PrevWordsInfo prevWordsInfo = JniDataUtils::constructPrevWordsInfo(env, - prevWordCodePointArrays, isBeginningOfSentenceArray); + prevWordCodePointArrays, isBeginningOfSentenceArray, + env->GetArrayLength(prevWordCodePointArrays)); jsize wordLength = env->GetArrayLength(word); int wordCodePoints[wordLength]; env->GetIntArrayRegion(word, 0, wordLength, wordCodePoints); @@ -413,7 +415,8 @@ static bool latinime_BinaryDictionary_removeNgramEntry(JNIEnv *env, jclass clazz return false; } const PrevWordsInfo prevWordsInfo = JniDataUtils::constructPrevWordsInfo(env, - prevWordCodePointArrays, isBeginningOfSentenceArray); + prevWordCodePointArrays, isBeginningOfSentenceArray, + env->GetArrayLength(prevWordCodePointArrays)); jsize codePointCount = env->GetArrayLength(word); int wordCodePoints[codePointCount]; env->GetIntArrayRegion(word, 0, codePointCount, wordCodePoints); @@ -667,7 +670,7 @@ static const JNINativeMethod sMethods[] = { }, { const_cast("getSuggestionsNative"), - const_cast("(JJJ[I[I[I[I[II[I[[I[Z[I[I[I[I[I[I[F)V"), + const_cast("(JJJ[I[I[I[I[II[I[[I[ZI[I[I[I[I[I[I[F)V"), reinterpret_cast(latinime_BinaryDictionary_getSuggestions) }, { diff --git a/native/jni/src/utils/jni_data_utils.h b/native/jni/src/utils/jni_data_utils.h index cb82d3c3b..235a03bba 100644 --- a/native/jni/src/utils/jni_data_utils.h +++ b/native/jni/src/utils/jni_data_utils.h @@ -97,17 +97,13 @@ class JniDataUtils { } static PrevWordsInfo constructPrevWordsInfo(JNIEnv *env, jobjectArray prevWordCodePointArrays, - jbooleanArray isBeginningOfSentenceArray) { + jbooleanArray isBeginningOfSentenceArray, const size_t prevWordCount) { int prevWordCodePoints[MAX_PREV_WORD_COUNT_FOR_N_GRAM][MAX_WORD_LENGTH]; int prevWordCodePointCount[MAX_PREV_WORD_COUNT_FOR_N_GRAM]; bool isBeginningOfSentence[MAX_PREV_WORD_COUNT_FOR_N_GRAM]; - jsize prevWordsCount = env->GetArrayLength(prevWordCodePointArrays); - for (size_t i = 0; i < NELEMS(prevWordCodePoints); ++i) { + for (size_t i = 0; i < prevWordCount; ++i) { prevWordCodePointCount[i] = 0; isBeginningOfSentence[i] = false; - if (prevWordsCount <= static_cast(i)) { - continue; - } jintArray prevWord = (jintArray)env->GetObjectArrayElement(prevWordCodePointArrays, i); if (!prevWord) { continue; @@ -124,7 +120,7 @@ class JniDataUtils { isBeginningOfSentence[i] = isBeginningOfSentenceBoolean == JNI_TRUE; } return PrevWordsInfo(prevWordCodePoints, prevWordCodePointCount, isBeginningOfSentence, - MAX_PREV_WORD_COUNT_FOR_N_GRAM); + prevWordCount); } static void putBooleanToArray(JNIEnv *env, jbooleanArray array, const int index,