From 9e51c6c0c62b78eaf14f92890981f9d35702b2c9 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Wed, 5 Mar 2014 12:49:04 +0900 Subject: [PATCH] Separate utility methods from BinaryDictionary in native code. Bug: 8187060 Change-Id: I099de557d2aaee95c6b809450108139550e9b0aa --- native/jni/NativeFileList.mk | 1 + ...oid_inputmethod_latin_BinaryDictionary.cpp | 101 ------------- ...nputmethod_latin_BinaryDictionaryUtils.cpp | 136 ++++++++++++++++++ ..._inputmethod_latin_BinaryDictionaryUtils.h | 25 ++++ native/jni/jni_common.cpp | 5 + 5 files changed, 167 insertions(+), 101 deletions(-) create mode 100644 native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.cpp create mode 100644 native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.h diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk index eb24df601..82237dc24 100644 --- a/native/jni/NativeFileList.mk +++ b/native/jni/NativeFileList.mk @@ -15,6 +15,7 @@ LATIN_IME_JNI_SRC_FILES := \ com_android_inputmethod_keyboard_ProximityInfo.cpp \ com_android_inputmethod_latin_BinaryDictionary.cpp \ + com_android_inputmethod_latin_BinaryDictionaryUtils.cpp \ com_android_inputmethod_latin_DicTraverseSession.cpp \ jni_common.cpp diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index bb54cbdb9..7a7816d5a 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -27,8 +27,6 @@ #include "suggest/core/dictionary/word_property.h" #include "suggest/core/suggest_options.h" #include "suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h" -#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h" -#include "utils/autocorrection_threshold_utils.h" #include "utils/char_utils.h" #include "utils/time_keeper.h" @@ -36,49 +34,6 @@ namespace latinime { class ProximityInfo; -// TODO: Move to makedict. -static jboolean latinime_BinaryDictionary_createEmptyDictFile(JNIEnv *env, jclass clazz, - jstring filePath, jlong dictVersion, jstring locale, jobjectArray attributeKeyStringArray, - jobjectArray attributeValueStringArray) { - const jsize filePathUtf8Length = env->GetStringUTFLength(filePath); - char filePathChars[filePathUtf8Length + 1]; - env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars); - filePathChars[filePathUtf8Length] = '\0'; - jsize localeLength = env->GetStringLength(locale); - jchar localeCodePoints[localeLength]; - env->GetStringRegion(locale, 0, localeLength, localeCodePoints); - const int keyCount = env->GetArrayLength(attributeKeyStringArray); - const int valueCount = env->GetArrayLength(attributeValueStringArray); - if (keyCount != valueCount) { - return false; - } - - DictionaryHeaderStructurePolicy::AttributeMap attributeMap; - for (int i = 0; i < keyCount; i++) { - jstring keyString = static_cast( - env->GetObjectArrayElement(attributeKeyStringArray, i)); - const jsize keyUtf8Length = env->GetStringUTFLength(keyString); - char keyChars[keyUtf8Length + 1]; - env->GetStringUTFRegion(keyString, 0, env->GetStringLength(keyString), keyChars); - keyChars[keyUtf8Length] = '\0'; - DictionaryHeaderStructurePolicy::AttributeMap::key_type key; - HeaderReadWriteUtils::insertCharactersIntoVector(keyChars, &key); - - jstring valueString = static_cast( - env->GetObjectArrayElement(attributeValueStringArray, i)); - const jsize valueUtf8Length = env->GetStringUTFLength(valueString); - char valueChars[valueUtf8Length + 1]; - env->GetStringUTFRegion(valueString, 0, env->GetStringLength(valueString), valueChars); - valueChars[valueUtf8Length] = '\0'; - DictionaryHeaderStructurePolicy::AttributeMap::mapped_type value; - HeaderReadWriteUtils::insertCharactersIntoVector(valueChars, &value); - attributeMap[key] = value; - } - - return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast(dictVersion), - CharUtils::convertShortArrayToIntVector(localeCodePoints, localeLength), &attributeMap); -} - static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring sourceDir, jlong dictOffset, jlong dictSize, jboolean isUpdatable) { PROF_OPEN; @@ -335,30 +290,6 @@ static void latinime_BinaryDictionary_getWordProperty(JNIEnv *env, jclass clazz, outShortcutProbabilities); } -static jfloat latinime_BinaryDictionary_calcNormalizedScore(JNIEnv *env, jclass clazz, - jintArray before, jintArray after, jint score) { - jsize beforeLength = env->GetArrayLength(before); - jsize afterLength = env->GetArrayLength(after); - int beforeCodePoints[beforeLength]; - int afterCodePoints[afterLength]; - env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints); - env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints); - return AutocorrectionThresholdUtils::calcNormalizedScore(beforeCodePoints, beforeLength, - afterCodePoints, afterLength, score); -} - -static jint latinime_BinaryDictionary_editDistance(JNIEnv *env, jclass clazz, jintArray before, - jintArray after) { - jsize beforeLength = env->GetArrayLength(before); - jsize afterLength = env->GetArrayLength(after); - int beforeCodePoints[beforeLength]; - int afterCodePoints[afterLength]; - env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints); - env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints); - return AutocorrectionThresholdUtils::editDistance(beforeCodePoints, beforeLength, - afterCodePoints, afterLength); -} - static void latinime_BinaryDictionary_addUnigramWord(JNIEnv *env, jclass clazz, jlong dict, jintArray word, jint probability, jintArray shortcutTarget, jint shortuctProbability, jboolean isNotAWord, jboolean isBlacklisted, jint timestamp) { @@ -518,17 +449,6 @@ static jstring latinime_BinaryDictionary_getProperty(JNIEnv *env, jclass clazz, return env->NewStringUTF(resultChars); } -static int latinime_BinaryDictionary_setCurrentTimeForTest(JNIEnv *env, jclass clazz, - jint currentTime) { - if (currentTime >= 0) { - TimeKeeper::startTestModeWithForceCurrentTime(currentTime); - } else { - TimeKeeper::stopTestMode(); - } - TimeKeeper::setCurrentTime(); - return TimeKeeper::peekCurrentTime(); -} - static bool latinime_BinaryDictionary_isCorruptedNative(JNIEnv *env, jclass clazz, jlong dict) { Dictionary *dictionary = reinterpret_cast(dict); if (!dictionary) { @@ -538,12 +458,6 @@ static bool latinime_BinaryDictionary_isCorruptedNative(JNIEnv *env, jclass claz } static const JNINativeMethod sMethods[] = { - { - const_cast("createEmptyDictFileNative"), - const_cast( - "(Ljava/lang/String;JLjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)Z"), - reinterpret_cast(latinime_BinaryDictionary_createEmptyDictFile) - }, { const_cast("openNative"), const_cast("(Ljava/lang/String;JJZ)J"), @@ -605,16 +519,6 @@ static const JNINativeMethod sMethods[] = { const_cast("(JI[I)I"), reinterpret_cast(latinime_BinaryDictionary_getNextWord) }, - { - const_cast("calcNormalizedScoreNative"), - const_cast("([I[II)F"), - reinterpret_cast(latinime_BinaryDictionary_calcNormalizedScore) - }, - { - const_cast("editDistanceNative"), - const_cast("([I[I)I"), - reinterpret_cast(latinime_BinaryDictionary_editDistance) - }, { const_cast("addUnigramWordNative"), const_cast("(J[II[IIZZI)V"), @@ -641,11 +545,6 @@ static const JNINativeMethod sMethods[] = { const_cast("(JII)I"), reinterpret_cast(latinime_BinaryDictionary_calculateProbabilityNative) }, - { - const_cast("setCurrentTimeForTestNative"), - const_cast("(I)I"), - reinterpret_cast(latinime_BinaryDictionary_setCurrentTimeForTest) - }, { const_cast("getPropertyNative"), const_cast("(JLjava/lang/String;)Ljava/lang/String;"), diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.cpp new file mode 100644 index 000000000..64034e78a --- /dev/null +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2014 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: BinaryDictionaryUtils" + +#include "com_android_inputmethod_latin_BinaryDictionaryUtils.h" + +#include "defines.h" +#include "jni.h" +#include "jni_common.h" +#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h" +#include "utils/autocorrection_threshold_utils.h" +#include "utils/char_utils.h" +#include "utils/time_keeper.h" + +namespace latinime { + +static jboolean latinime_BinaryDictionaryUtils_createEmptyDictFile(JNIEnv *env, jclass clazz, + jstring filePath, jlong dictVersion, jstring locale, jobjectArray attributeKeyStringArray, + jobjectArray attributeValueStringArray) { + const jsize filePathUtf8Length = env->GetStringUTFLength(filePath); + char filePathChars[filePathUtf8Length + 1]; + env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars); + filePathChars[filePathUtf8Length] = '\0'; + jsize localeLength = env->GetStringLength(locale); + jchar localeCodePoints[localeLength]; + env->GetStringRegion(locale, 0, localeLength, localeCodePoints); + const int keyCount = env->GetArrayLength(attributeKeyStringArray); + const int valueCount = env->GetArrayLength(attributeValueStringArray); + if (keyCount != valueCount) { + return false; + } + + DictionaryHeaderStructurePolicy::AttributeMap attributeMap; + for (int i = 0; i < keyCount; i++) { + jstring keyString = static_cast( + env->GetObjectArrayElement(attributeKeyStringArray, i)); + const jsize keyUtf8Length = env->GetStringUTFLength(keyString); + char keyChars[keyUtf8Length + 1]; + env->GetStringUTFRegion(keyString, 0, env->GetStringLength(keyString), keyChars); + keyChars[keyUtf8Length] = '\0'; + DictionaryHeaderStructurePolicy::AttributeMap::key_type key; + HeaderReadWriteUtils::insertCharactersIntoVector(keyChars, &key); + + jstring valueString = static_cast( + env->GetObjectArrayElement(attributeValueStringArray, i)); + const jsize valueUtf8Length = env->GetStringUTFLength(valueString); + char valueChars[valueUtf8Length + 1]; + env->GetStringUTFRegion(valueString, 0, env->GetStringLength(valueString), valueChars); + valueChars[valueUtf8Length] = '\0'; + DictionaryHeaderStructurePolicy::AttributeMap::mapped_type value; + HeaderReadWriteUtils::insertCharactersIntoVector(valueChars, &value); + attributeMap[key] = value; + } + + return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast(dictVersion), + CharUtils::convertShortArrayToIntVector(localeCodePoints, localeLength), &attributeMap); +} + +static jfloat latinime_BinaryDictionaryUtils_calcNormalizedScore(JNIEnv *env, jclass clazz, + jintArray before, jintArray after, jint score) { + jsize beforeLength = env->GetArrayLength(before); + jsize afterLength = env->GetArrayLength(after); + int beforeCodePoints[beforeLength]; + int afterCodePoints[afterLength]; + env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints); + env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints); + return AutocorrectionThresholdUtils::calcNormalizedScore(beforeCodePoints, beforeLength, + afterCodePoints, afterLength, score); +} + +static jint latinime_BinaryDictionaryUtils_editDistance(JNIEnv *env, jclass clazz, jintArray before, + jintArray after) { + jsize beforeLength = env->GetArrayLength(before); + jsize afterLength = env->GetArrayLength(after); + int beforeCodePoints[beforeLength]; + int afterCodePoints[afterLength]; + env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints); + env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints); + return AutocorrectionThresholdUtils::editDistance(beforeCodePoints, beforeLength, + afterCodePoints, afterLength); +} + +static int latinime_BinaryDictionaryUtils_setCurrentTimeForTest(JNIEnv *env, jclass clazz, + jint currentTime) { + if (currentTime >= 0) { + TimeKeeper::startTestModeWithForceCurrentTime(currentTime); + } else { + TimeKeeper::stopTestMode(); + } + TimeKeeper::setCurrentTime(); + return TimeKeeper::peekCurrentTime(); +} + +static const JNINativeMethod sMethods[] = { + { + const_cast("createEmptyDictFileNative"), + const_cast( + "(Ljava/lang/String;JLjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)Z"), + reinterpret_cast(latinime_BinaryDictionaryUtils_createEmptyDictFile) + }, + { + const_cast("calcNormalizedScoreNative"), + const_cast("([I[II)F"), + reinterpret_cast(latinime_BinaryDictionaryUtils_calcNormalizedScore) + }, + { + const_cast("editDistanceNative"), + const_cast("([I[I)I"), + reinterpret_cast(latinime_BinaryDictionaryUtils_editDistance) + }, + { + const_cast("setCurrentTimeForTestNative"), + const_cast("(I)I"), + reinterpret_cast(latinime_BinaryDictionaryUtils_setCurrentTimeForTest) + } +}; + +int register_BinaryDictionaryUtils(JNIEnv *env) { + const char *const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary"; + return registerNativeMethods(env, kClassPathName, sMethods, NELEMS(sMethods)); +} +} // namespace latinime diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.h b/native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.h new file mode 100644 index 000000000..38edcd20c --- /dev/null +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionaryUtils.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2014 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 _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARYUTILS_H +#define _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARYUTILS_H + +#include "jni.h" + +namespace latinime { +int register_BinaryDictionaryUtils(JNIEnv *env); +} // namespace latinime +#endif // _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARYUTILS_H diff --git a/native/jni/jni_common.cpp b/native/jni/jni_common.cpp index f2867d7c3..9fa7ef8f9 100644 --- a/native/jni/jni_common.cpp +++ b/native/jni/jni_common.cpp @@ -20,6 +20,7 @@ #include "com_android_inputmethod_keyboard_ProximityInfo.h" #include "com_android_inputmethod_latin_BinaryDictionary.h" +#include "com_android_inputmethod_latin_BinaryDictionaryUtils.h" #include "com_android_inputmethod_latin_DicTraverseSession.h" #include "defines.h" @@ -42,6 +43,10 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) { AKLOGE("ERROR: BinaryDictionary native registration failed"); return -1; } + if (!latinime::register_BinaryDictionaryUtils(env)) { + AKLOGE("ERROR: BinaryDictionaryUtils native registration failed"); + return -1; + } if (!latinime::register_DicTraverseSession(env)) { AKLOGE("ERROR: DicTraverseSession native registration failed"); return -1;