Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "LatinIME: jni: BinaryDictionaryUtils"
     18 
     19 #include "com_android_inputmethod_latin_BinaryDictionaryUtils.h"
     20 
     21 #include "defines.h"
     22 #include "jni.h"
     23 #include "jni_common.h"
     24 #include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
     25 #include "utils/autocorrection_threshold_utils.h"
     26 #include "utils/char_utils.h"
     27 #include "utils/jni_data_utils.h"
     28 #include "utils/time_keeper.h"
     29 
     30 namespace latinime {
     31 
     32 static jboolean latinime_BinaryDictionaryUtils_createEmptyDictFile(JNIEnv *env, jclass clazz,
     33         jstring filePath, jlong dictVersion, jstring locale, jobjectArray attributeKeyStringArray,
     34         jobjectArray attributeValueStringArray) {
     35     const jsize filePathUtf8Length = env->GetStringUTFLength(filePath);
     36     char filePathChars[filePathUtf8Length + 1];
     37     env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
     38     filePathChars[filePathUtf8Length] = '\0';
     39 
     40     const jsize localeUtf8Length = env->GetStringUTFLength(locale);
     41     char localeChars[localeUtf8Length + 1];
     42     env->GetStringUTFRegion(locale, 0, env->GetStringLength(locale), localeChars);
     43     localeChars[localeUtf8Length] = '\0';
     44     std::vector<int> localeCodePoints;
     45     HeaderReadWriteUtils::insertCharactersIntoVector(localeChars, &localeCodePoints);
     46 
     47     const int keyCount = env->GetArrayLength(attributeKeyStringArray);
     48     const int valueCount = env->GetArrayLength(attributeValueStringArray);
     49     if (keyCount != valueCount) {
     50         return false;
     51     }
     52     DictionaryHeaderStructurePolicy::AttributeMap attributeMap =
     53             JniDataUtils::constructAttributeMap(env, attributeKeyStringArray,
     54                     attributeValueStringArray);
     55     return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast<int>(dictVersion),
     56             localeCodePoints, &attributeMap);
     57 }
     58 
     59 static jfloat latinime_BinaryDictionaryUtils_calcNormalizedScore(JNIEnv *env, jclass clazz,
     60         jintArray before, jintArray after, jint score) {
     61     jsize beforeLength = env->GetArrayLength(before);
     62     jsize afterLength = env->GetArrayLength(after);
     63     int beforeCodePoints[beforeLength];
     64     int afterCodePoints[afterLength];
     65     env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints);
     66     env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints);
     67     return AutocorrectionThresholdUtils::calcNormalizedScore(beforeCodePoints, beforeLength,
     68             afterCodePoints, afterLength, score);
     69 }
     70 
     71 static jint latinime_BinaryDictionaryUtils_editDistance(JNIEnv *env, jclass clazz, jintArray before,
     72         jintArray after) {
     73     jsize beforeLength = env->GetArrayLength(before);
     74     jsize afterLength = env->GetArrayLength(after);
     75     int beforeCodePoints[beforeLength];
     76     int afterCodePoints[afterLength];
     77     env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints);
     78     env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints);
     79     return AutocorrectionThresholdUtils::editDistance(beforeCodePoints, beforeLength,
     80             afterCodePoints, afterLength);
     81 }
     82 
     83 static int latinime_BinaryDictionaryUtils_setCurrentTimeForTest(JNIEnv *env, jclass clazz,
     84         jint currentTime) {
     85     if (currentTime >= 0) {
     86         TimeKeeper::startTestModeWithForceCurrentTime(currentTime);
     87     } else {
     88         TimeKeeper::stopTestMode();
     89     }
     90     TimeKeeper::setCurrentTime();
     91     return TimeKeeper::peekCurrentTime();
     92 }
     93 
     94 static const JNINativeMethod sMethods[] = {
     95     {
     96         const_cast<char *>("createEmptyDictFileNative"),
     97         const_cast<char *>(
     98                 "(Ljava/lang/String;JLjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)Z"),
     99         reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_createEmptyDictFile)
    100     },
    101     {
    102         const_cast<char *>("calcNormalizedScoreNative"),
    103         const_cast<char *>("([I[II)F"),
    104         reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_calcNormalizedScore)
    105     },
    106     {
    107         const_cast<char *>("editDistanceNative"),
    108         const_cast<char *>("([I[I)I"),
    109         reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_editDistance)
    110     },
    111     {
    112         const_cast<char *>("setCurrentTimeForTestNative"),
    113         const_cast<char *>("(I)I"),
    114         reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_setCurrentTimeForTest)
    115     }
    116 };
    117 
    118 int register_BinaryDictionaryUtils(JNIEnv *env) {
    119     const char *const kClassPathName = "com/android/inputmethod/latin/utils/BinaryDictionaryUtils";
    120     return registerNativeMethods(env, kClassPathName, sMethods, NELEMS(sMethods));
    121 }
    122 } // namespace latinime
    123