From 80111f08e284655808380663f0b68547b981da72 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 17 Apr 2012 15:55:17 +0900 Subject: [PATCH] Pass the previous word down to native code in getSuggestions Change-Id: I477b631d81ef58461e44954f3ae5fd895928bb97 --- .../inputmethod/latin/BinaryDictionary.java | 9 ++++++--- .../android/inputmethod/latin/StringUtils.java | 15 +++++++++++++++ ...android_inputmethod_latin_BinaryDictionary.cpp | 12 +++++++++--- native/jni/src/bigram_dictionary.cpp | 1 - 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index f4c8e61ed..9429ef411 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -85,8 +85,8 @@ public class BinaryDictionary extends Dictionary { private native void closeNative(long dict); private native boolean isValidWordNative(long dict, char[] word, int wordLength); private native int getSuggestionsNative(long dict, long proximityInfo, int[] xCoordinates, - int[] yCoordinates, int[] inputCodes, int codesSize, boolean useFullEditDistance, - char[] outputChars, int[] scores); + int[] yCoordinates, int[] inputCodes, int codesSize, int[] prevWordForBigrams, + boolean useFullEditDistance, char[] outputChars, int[] scores); private native int getBigramsNative(long dict, char[] prevWord, int prevWordLength, int[] inputCodes, int inputCodesLength, char[] outputChars, int[] scores, int maxWordLength, int maxBigrams); @@ -177,11 +177,14 @@ public class BinaryDictionary extends Dictionary { Arrays.fill(outputChars, (char) 0); Arrays.fill(scores, 0); + final int[] prevWordCodePointArray = null == prevWordForBigrams + ? null : StringUtils.toCodePointArray(prevWordForBigrams.toString()); + // TODO: pass the previous word to native code return getSuggestionsNative( mNativeDict, proximityInfo.getNativeProximityInfo(), codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize, - mUseFullEditDistance, outputChars, scores); + prevWordCodePointArray, mUseFullEditDistance, outputChars, scores); } public static double calcNormalizedScore(String before, String after, int score) { diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java index 649cd650a..a599933d8 100644 --- a/java/src/com/android/inputmethod/latin/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/StringUtils.java @@ -166,4 +166,19 @@ public class StringUtils { // - It also does not work with unicode surrogate code points. return s.toUpperCase(locale).charAt(0) + s.substring(1); } + + public static int[] toCodePointArray(final String string) { + final char[] characters = string.toCharArray(); + final int length = characters.length; + final int[] codePoints = new int[Character.codePointCount(characters, 0, length)]; + int codePoint = Character.codePointAt(characters, 0); + int dsti = 0; + for (int srci = Character.charCount(codePoint); + srci < length; srci += Character.charCount(codePoint), ++dsti) { + codePoints[dsti] = codePoint; + codePoint = Character.codePointAt(characters, srci); + } + codePoints[dsti] = codePoint; + return codePoints; + } } diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index 613fbc480..2ef72e1e8 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -127,8 +127,8 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jobject object, static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object, jlong dict, jlong proximityInfo, jintArray xCoordinatesArray, jintArray yCoordinatesArray, - jintArray inputArray, jint arraySize, jboolean useFullEditDistance, - jcharArray outputArray, jintArray frequencyArray) { + jintArray inputArray, jint arraySize, jintArray prevWordForBigrams, + jboolean useFullEditDistance, jcharArray outputArray, jintArray frequencyArray) { Dictionary *dictionary = (Dictionary*)dict; if (!dictionary) return 0; ProximityInfo *pInfo = (ProximityInfo*)proximityInfo; @@ -137,6 +137,11 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object, int *frequencies = env->GetIntArrayElements(frequencyArray, 0); int *inputCodes = env->GetIntArrayElements(inputArray, 0); jchar *outputChars = env->GetCharArrayElements(outputArray, 0); + // Deactivated to prevent unused variable errors. + // TODO: use the following variables. + // jint *prevWordChars = prevWordForBigrams + // ? env->GetIntArrayElements(prevWordForBigrams, 0) : NULL; + // jsize prevWordLength = prevWordChars ? env->GetArrayLength(prevWordForBigrams) : 0; int count = dictionary->getSuggestions(pInfo, xCoordinates, yCoordinates, inputCodes, arraySize, useFullEditDistance, (unsigned short*) outputChars, frequencies); env->ReleaseCharArrayElements(outputArray, outputChars, 0); @@ -229,7 +234,8 @@ void releaseDictBuf(void* dictBuf, const size_t length, int fd) { static JNINativeMethod sMethods[] = { {"openNative", "(Ljava/lang/String;JJIIII)J", (void*)latinime_BinaryDictionary_open}, {"closeNative", "(J)V", (void*)latinime_BinaryDictionary_close}, - {"getSuggestionsNative", "(JJ[I[I[IIZ[C[I)I", (void*)latinime_BinaryDictionary_getSuggestions}, + {"getSuggestionsNative", "(JJ[I[I[II[IZ[C[I)I", + (void*)latinime_BinaryDictionary_getSuggestions}, {"isValidWordNative", "(J[CI)Z", (void*)latinime_BinaryDictionary_isValidWord}, {"getBigramsNative", "(J[CI[II[C[III)I", (void*)latinime_BinaryDictionary_getBigrams}, {"calcNormalizedScoreNative", "([CI[CII)D", diff --git a/native/jni/src/bigram_dictionary.cpp b/native/jni/src/bigram_dictionary.cpp index 87ca1e9ed..f283cb5c7 100644 --- a/native/jni/src/bigram_dictionary.cpp +++ b/native/jni/src/bigram_dictionary.cpp @@ -30,7 +30,6 @@ BigramDictionary::BigramDictionary(const unsigned char *dict, int maxWordLength, : DICT(dict), MAX_WORD_LENGTH(maxWordLength), mParentDictionary(parentDictionary) { if (DEBUG_DICT) { AKLOGI("BigramDictionary - constructor"); - AKLOGI("Has Bigram : %d", hasBigram); } }