2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2013-01-08 08:57:26 +00:00
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
2012-07-25 08:51:43 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2013-01-08 08:57:26 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-07-25 08:51:43 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2011-02-22 08:28:55 +00:00
|
|
|
#define LOG_TAG "LatinIME: jni: BinaryDictionary"
|
2011-01-07 06:01:51 +00:00
|
|
|
|
2013-06-04 10:16:47 +00:00
|
|
|
#include "com_android_inputmethod_latin_BinaryDictionary.h"
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2013-06-04 10:16:47 +00:00
|
|
|
#include <cstring> // for memset()
|
2013-05-30 15:02:57 +00:00
|
|
|
|
2013-06-04 10:16:47 +00:00
|
|
|
#include "defines.h"
|
2012-08-10 08:16:39 +00:00
|
|
|
#include "jni.h"
|
|
|
|
#include "jni_common.h"
|
2013-05-30 15:02:57 +00:00
|
|
|
#include "suggest/core/dictionary/dictionary.h"
|
2013-06-03 01:36:00 +00:00
|
|
|
#include "suggest/core/suggest_options.h"
|
2013-10-10 10:25:37 +00:00
|
|
|
#include "suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h"
|
2013-09-26 03:59:02 +00:00
|
|
|
#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
|
2013-06-11 08:04:40 +00:00
|
|
|
#include "utils/autocorrection_threshold_utils.h"
|
2012-08-10 08:16:39 +00:00
|
|
|
|
2011-02-22 08:28:55 +00:00
|
|
|
namespace latinime {
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2012-08-01 09:03:55 +00:00
|
|
|
class ProximityInfo;
|
|
|
|
|
2013-09-26 03:59:02 +00:00
|
|
|
// TODO: Move to makedict.
|
|
|
|
static jboolean latinime_BinaryDictionary_createEmptyDictFile(JNIEnv *env, jclass clazz,
|
|
|
|
jstring filePath, jlong dictVersion, 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';
|
|
|
|
|
|
|
|
const int keyCount = env->GetArrayLength(attributeKeyStringArray);
|
|
|
|
const int valueCount = env->GetArrayLength(attributeValueStringArray);
|
|
|
|
if (keyCount != valueCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeaderReadWriteUtils::AttributeMap attributeMap;
|
|
|
|
for (int i = 0; i < keyCount; i++) {
|
|
|
|
jstring keyString = static_cast<jstring>(
|
|
|
|
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';
|
|
|
|
HeaderReadWriteUtils::AttributeMap::key_type key;
|
|
|
|
HeaderReadWriteUtils::insertCharactersIntoVector(keyChars, &key);
|
|
|
|
|
|
|
|
jstring valueString = static_cast<jstring>(
|
|
|
|
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';
|
|
|
|
HeaderReadWriteUtils::AttributeMap::mapped_type value;
|
|
|
|
HeaderReadWriteUtils::insertCharactersIntoVector(valueChars, &value);
|
|
|
|
attributeMap[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast<int>(dictVersion),
|
|
|
|
&attributeMap);
|
|
|
|
}
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring sourceDir,
|
2013-06-25 05:25:01 +00:00
|
|
|
jlong dictOffset, jlong dictSize, jboolean isUpdatable) {
|
2011-01-07 06:01:51 +00:00
|
|
|
PROF_OPEN;
|
|
|
|
PROF_START(66);
|
2012-08-09 14:23:35 +00:00
|
|
|
const jsize sourceDirUtf8Length = env->GetStringUTFLength(sourceDir);
|
|
|
|
if (sourceDirUtf8Length <= 0) {
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGE("DICT: Can't get sourceDir string");
|
2011-01-07 06:01:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-09 14:23:35 +00:00
|
|
|
char sourceDirChars[sourceDirUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(sourceDir, 0, env->GetStringLength(sourceDir), sourceDirChars);
|
|
|
|
sourceDirChars[sourceDirUtf8Length] = '\0';
|
2013-11-12 06:36:02 +00:00
|
|
|
DictionaryStructureWithBufferPolicy::StructurePoilcyPtr dictionaryStructureWithBufferPolicy =
|
2013-08-16 03:52:04 +00:00
|
|
|
DictionaryStructureWithBufferPolicyFactory::newDictionaryStructureWithBufferPolicy(
|
2013-09-17 09:07:16 +00:00
|
|
|
sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize),
|
2013-11-12 06:36:02 +00:00
|
|
|
isUpdatable == JNI_TRUE);
|
2013-10-25 00:28:30 +00:00
|
|
|
if (!dictionaryStructureWithBufferPolicy.get()) {
|
2013-08-15 11:37:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-16 03:52:04 +00:00
|
|
|
|
|
|
|
Dictionary *const dictionary = new Dictionary(env, dictionaryStructureWithBufferPolicy);
|
2011-01-07 06:01:51 +00:00
|
|
|
PROF_END(66);
|
|
|
|
PROF_CLOSE;
|
2013-01-29 16:04:42 +00:00
|
|
|
return reinterpret_cast<jlong>(dictionary);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 09:07:16 +00:00
|
|
|
static void latinime_BinaryDictionary_flush(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jstring filePath) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return;
|
|
|
|
const jsize filePathUtf8Length = env->GetStringUTFLength(filePath);
|
|
|
|
char filePathChars[filePathUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
|
|
|
|
filePathChars[filePathUtf8Length] = '\0';
|
|
|
|
dictionary->flush(filePathChars);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool latinime_BinaryDictionary_needsToRunGC(JNIEnv *env, jclass clazz,
|
2013-09-30 04:57:54 +00:00
|
|
|
jlong dict, jboolean mindsBlockByGC) {
|
2013-09-17 09:07:16 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return false;
|
2013-09-30 04:57:54 +00:00
|
|
|
return dictionary->needsToRunGC(mindsBlockByGC == JNI_TRUE);
|
2013-09-17 09:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void latinime_BinaryDictionary_flushWithGC(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jstring filePath) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return;
|
|
|
|
const jsize filePathUtf8Length = env->GetStringUTFLength(filePath);
|
|
|
|
char filePathChars[filePathUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
|
|
|
|
filePathChars[filePathUtf8Length] = '\0';
|
|
|
|
dictionary->flushWithGC(filePathChars);
|
|
|
|
}
|
|
|
|
|
2013-06-26 14:06:14 +00:00
|
|
|
static void latinime_BinaryDictionary_close(JNIEnv *env, jclass clazz, jlong dict) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return;
|
|
|
|
delete dictionary;
|
|
|
|
}
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, jlong dict,
|
2012-08-08 12:23:25 +00:00
|
|
|
jlong proximityInfo, jlong dicTraverseSession, jintArray xCoordinatesArray,
|
2012-08-10 08:16:39 +00:00
|
|
|
jintArray yCoordinatesArray, jintArray timesArray, jintArray pointerIdsArray,
|
2013-05-24 17:21:04 +00:00
|
|
|
jintArray inputCodePointsArray, jint inputSize, jint commitPoint, jintArray suggestOptions,
|
|
|
|
jintArray prevWordCodePointsForBigrams, jintArray outputCodePointsArray,
|
2013-08-20 09:00:21 +00:00
|
|
|
jintArray scoresArray, jintArray spaceIndicesArray, jintArray outputTypesArray,
|
2013-09-30 12:39:43 +00:00
|
|
|
jintArray outputAutoCommitFirstWordConfidenceArray) {
|
2012-08-14 05:22:27 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
2011-01-07 06:01:51 +00:00
|
|
|
if (!dictionary) return 0;
|
2012-08-14 05:22:27 +00:00
|
|
|
ProximityInfo *pInfo = reinterpret_cast<ProximityInfo *>(proximityInfo);
|
2013-06-03 07:12:20 +00:00
|
|
|
DicTraverseSession *traverseSession =
|
|
|
|
reinterpret_cast<DicTraverseSession *>(dicTraverseSession);
|
2012-08-10 08:16:39 +00:00
|
|
|
|
|
|
|
// Input values
|
2013-01-11 16:18:00 +00:00
|
|
|
int xCoordinates[inputSize];
|
|
|
|
int yCoordinates[inputSize];
|
|
|
|
int times[inputSize];
|
|
|
|
int pointerIds[inputSize];
|
2012-08-10 08:16:39 +00:00
|
|
|
const jsize inputCodePointsLength = env->GetArrayLength(inputCodePointsArray);
|
|
|
|
int inputCodePoints[inputCodePointsLength];
|
|
|
|
const jsize prevWordCodePointsLength =
|
|
|
|
prevWordCodePointsForBigrams ? env->GetArrayLength(prevWordCodePointsForBigrams) : 0;
|
|
|
|
int prevWordCodePointsInternal[prevWordCodePointsLength];
|
|
|
|
int *prevWordCodePoints = 0;
|
2013-01-11 16:18:00 +00:00
|
|
|
env->GetIntArrayRegion(xCoordinatesArray, 0, inputSize, xCoordinates);
|
|
|
|
env->GetIntArrayRegion(yCoordinatesArray, 0, inputSize, yCoordinates);
|
|
|
|
env->GetIntArrayRegion(timesArray, 0, inputSize, times);
|
|
|
|
env->GetIntArrayRegion(pointerIdsArray, 0, inputSize, pointerIds);
|
2012-08-10 08:16:39 +00:00
|
|
|
env->GetIntArrayRegion(inputCodePointsArray, 0, inputCodePointsLength, inputCodePoints);
|
|
|
|
if (prevWordCodePointsForBigrams) {
|
|
|
|
env->GetIntArrayRegion(prevWordCodePointsForBigrams, 0, prevWordCodePointsLength,
|
|
|
|
prevWordCodePointsInternal);
|
|
|
|
prevWordCodePoints = prevWordCodePointsInternal;
|
|
|
|
}
|
|
|
|
|
2013-05-24 17:21:04 +00:00
|
|
|
const jsize numberOfOptions = env->GetArrayLength(suggestOptions);
|
|
|
|
int options[numberOfOptions];
|
|
|
|
env->GetIntArrayRegion(suggestOptions, 0, numberOfOptions, options);
|
2013-05-30 00:18:03 +00:00
|
|
|
SuggestOptions givenSuggestOptions(options, numberOfOptions);
|
2013-05-24 17:21:04 +00:00
|
|
|
|
2012-08-10 08:16:39 +00:00
|
|
|
// Output values
|
2013-01-11 16:18:00 +00:00
|
|
|
/* By the way, let's check the output array length here to make sure */
|
2012-10-29 09:06:22 +00:00
|
|
|
const jsize outputCodePointsLength = env->GetArrayLength(outputCodePointsArray);
|
2013-01-11 16:18:00 +00:00
|
|
|
if (outputCodePointsLength != (MAX_WORD_LENGTH * MAX_RESULTS)) {
|
|
|
|
AKLOGE("Invalid outputCodePointsLength: %d", outputCodePointsLength);
|
|
|
|
ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-10 08:16:39 +00:00
|
|
|
const jsize scoresLength = env->GetArrayLength(scoresArray);
|
2013-01-11 16:18:00 +00:00
|
|
|
if (scoresLength != MAX_RESULTS) {
|
|
|
|
AKLOGE("Invalid scoresLength: %d", scoresLength);
|
2013-01-11 09:59:01 +00:00
|
|
|
ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-11 16:18:00 +00:00
|
|
|
int outputCodePoints[outputCodePointsLength];
|
2012-08-10 08:16:39 +00:00
|
|
|
int scores[scoresLength];
|
|
|
|
const jsize spaceIndicesLength = env->GetArrayLength(spaceIndicesArray);
|
|
|
|
int spaceIndices[spaceIndicesLength];
|
|
|
|
const jsize outputTypesLength = env->GetArrayLength(outputTypesArray);
|
|
|
|
int outputTypes[outputTypesLength];
|
2013-09-30 12:39:43 +00:00
|
|
|
const jsize outputAutoCommitFirstWordConfidenceLength =
|
|
|
|
env->GetArrayLength(outputAutoCommitFirstWordConfidenceArray);
|
|
|
|
// We only use the first result, as obviously we will only ever autocommit the first one
|
|
|
|
ASSERT(outputAutoCommitFirstWordConfidenceLength == 1);
|
|
|
|
int outputAutoCommitFirstWordConfidence[outputAutoCommitFirstWordConfidenceLength];
|
2012-10-29 09:06:22 +00:00
|
|
|
memset(outputCodePoints, 0, sizeof(outputCodePoints));
|
2012-08-24 05:51:15 +00:00
|
|
|
memset(scores, 0, sizeof(scores));
|
|
|
|
memset(spaceIndices, 0, sizeof(spaceIndices));
|
|
|
|
memset(outputTypes, 0, sizeof(outputTypes));
|
2013-09-30 12:39:43 +00:00
|
|
|
memset(outputAutoCommitFirstWordConfidence, 0, sizeof(outputAutoCommitFirstWordConfidence));
|
2012-07-11 03:07:44 +00:00
|
|
|
|
|
|
|
int count;
|
2013-05-30 00:18:03 +00:00
|
|
|
if (givenSuggestOptions.isGesture() || inputSize > 0) {
|
2012-08-08 12:23:25 +00:00
|
|
|
count = dictionary->getSuggestions(pInfo, traverseSession, xCoordinates, yCoordinates,
|
2013-01-11 16:18:00 +00:00
|
|
|
times, pointerIds, inputCodePoints, inputSize, prevWordCodePoints,
|
2013-05-30 00:18:03 +00:00
|
|
|
prevWordCodePointsLength, commitPoint, &givenSuggestOptions, outputCodePoints,
|
2013-09-30 12:39:43 +00:00
|
|
|
scores, spaceIndices, outputTypes, outputAutoCommitFirstWordConfidence);
|
2012-07-11 03:07:44 +00:00
|
|
|
} else {
|
2012-08-10 08:16:39 +00:00
|
|
|
count = dictionary->getBigrams(prevWordCodePoints, prevWordCodePointsLength,
|
2013-07-31 04:47:23 +00:00
|
|
|
outputCodePoints, scores, outputTypes);
|
2012-07-11 03:07:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 08:16:39 +00:00
|
|
|
// Copy back the output values
|
2012-10-29 09:06:22 +00:00
|
|
|
env->SetIntArrayRegion(outputCodePointsArray, 0, outputCodePointsLength, outputCodePoints);
|
2012-08-10 08:16:39 +00:00
|
|
|
env->SetIntArrayRegion(scoresArray, 0, scoresLength, scores);
|
|
|
|
env->SetIntArrayRegion(spaceIndicesArray, 0, spaceIndicesLength, spaceIndices);
|
|
|
|
env->SetIntArrayRegion(outputTypesArray, 0, outputTypesLength, outputTypes);
|
2013-09-30 12:39:43 +00:00
|
|
|
env->SetIntArrayRegion(outputAutoCommitFirstWordConfidenceArray, 0,
|
|
|
|
outputAutoCommitFirstWordConfidenceLength, outputAutoCommitFirstWordConfidence);
|
2012-08-10 08:16:39 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-03-18 04:08:31 +00:00
|
|
|
static jint latinime_BinaryDictionary_getProbability(JNIEnv *env, jclass clazz, jlong dict,
|
2013-06-26 03:51:33 +00:00
|
|
|
jintArray word) {
|
2012-08-14 05:22:27 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
2013-07-03 11:12:38 +00:00
|
|
|
if (!dictionary) return NOT_A_PROBABILITY;
|
2013-06-26 03:51:33 +00:00
|
|
|
const jsize wordLength = env->GetArrayLength(word);
|
|
|
|
int codePoints[wordLength];
|
|
|
|
env->GetIntArrayRegion(word, 0, wordLength, codePoints);
|
|
|
|
return dictionary->getProbability(codePoints, wordLength);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 02:18:28 +00:00
|
|
|
static jint latinime_BinaryDictionary_getBigramProbability(JNIEnv *env, jclass clazz,
|
|
|
|
jlong dict, jintArray word0, jintArray word1) {
|
2012-08-14 05:22:27 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
2013-01-29 16:04:42 +00:00
|
|
|
if (!dictionary) return JNI_FALSE;
|
2013-06-26 03:51:33 +00:00
|
|
|
const jsize word0Length = env->GetArrayLength(word0);
|
|
|
|
const jsize word1Length = env->GetArrayLength(word1);
|
|
|
|
int word0CodePoints[word0Length];
|
|
|
|
int word1CodePoints[word1Length];
|
|
|
|
env->GetIntArrayRegion(word0, 0, word0Length, word0CodePoints);
|
|
|
|
env->GetIntArrayRegion(word1, 0, word1Length, word1CodePoints);
|
2013-09-18 02:18:28 +00:00
|
|
|
return dictionary->getBigramProbability(word0CodePoints, word0Length, word1CodePoints,
|
|
|
|
word1Length);
|
2012-04-27 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
static jfloat latinime_BinaryDictionary_calcNormalizedScore(JNIEnv *env, jclass clazz,
|
2012-10-29 09:06:22 +00:00
|
|
|
jintArray before, jintArray after, jint score) {
|
2012-08-09 15:40:34 +00:00
|
|
|
jsize beforeLength = env->GetArrayLength(before);
|
|
|
|
jsize afterLength = env->GetArrayLength(after);
|
2012-10-29 09:06:22 +00:00
|
|
|
int beforeCodePoints[beforeLength];
|
|
|
|
int afterCodePoints[afterLength];
|
|
|
|
env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints);
|
|
|
|
env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints);
|
2013-06-11 08:04:40 +00:00
|
|
|
return AutocorrectionThresholdUtils::calcNormalizedScore(beforeCodePoints, beforeLength,
|
2012-10-29 09:06:22 +00:00
|
|
|
afterCodePoints, afterLength, score);
|
2012-01-12 09:44:40 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
static jint latinime_BinaryDictionary_editDistance(JNIEnv *env, jclass clazz, jintArray before,
|
2012-10-29 09:06:22 +00:00
|
|
|
jintArray after) {
|
2012-08-09 15:40:34 +00:00
|
|
|
jsize beforeLength = env->GetArrayLength(before);
|
|
|
|
jsize afterLength = env->GetArrayLength(after);
|
2012-10-29 09:06:22 +00:00
|
|
|
int beforeCodePoints[beforeLength];
|
|
|
|
int afterCodePoints[afterLength];
|
|
|
|
env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints);
|
|
|
|
env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints);
|
2013-06-11 08:04:40 +00:00
|
|
|
return AutocorrectionThresholdUtils::editDistance(beforeCodePoints, beforeLength,
|
2012-10-29 09:06:22 +00:00
|
|
|
afterCodePoints, afterLength);
|
2012-01-12 09:44:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 03:51:33 +00:00
|
|
|
static void latinime_BinaryDictionary_addUnigramWord(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jintArray word, jint probability) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jsize wordLength = env->GetArrayLength(word);
|
|
|
|
int codePoints[wordLength];
|
2013-09-10 09:45:39 +00:00
|
|
|
env->GetIntArrayRegion(word, 0, wordLength, codePoints);
|
2013-06-26 03:51:33 +00:00
|
|
|
dictionary->addUnigramWord(codePoints, wordLength, probability);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void latinime_BinaryDictionary_addBigramWords(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jintArray word0, jintArray word1, jint probability) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jsize word0Length = env->GetArrayLength(word0);
|
|
|
|
int word0CodePoints[word0Length];
|
2013-09-10 09:45:39 +00:00
|
|
|
env->GetIntArrayRegion(word0, 0, word0Length, word0CodePoints);
|
2013-06-26 03:51:33 +00:00
|
|
|
jsize word1Length = env->GetArrayLength(word1);
|
|
|
|
int word1CodePoints[word1Length];
|
2013-09-10 09:45:39 +00:00
|
|
|
env->GetIntArrayRegion(word1, 0, word1Length, word1CodePoints);
|
2013-06-26 03:51:33 +00:00
|
|
|
dictionary->addBigramWords(word0CodePoints, word0Length, word1CodePoints,
|
|
|
|
word1Length, probability);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void latinime_BinaryDictionary_removeBigramWords(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jintArray word0, jintArray word1) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jsize word0Length = env->GetArrayLength(word0);
|
|
|
|
int word0CodePoints[word0Length];
|
2013-09-10 09:45:39 +00:00
|
|
|
env->GetIntArrayRegion(word0, 0, word0Length, word0CodePoints);
|
2013-06-26 03:51:33 +00:00
|
|
|
jsize word1Length = env->GetArrayLength(word1);
|
|
|
|
int word1CodePoints[word1Length];
|
2013-09-10 09:45:39 +00:00
|
|
|
env->GetIntArrayRegion(word1, 0, word1Length, word1CodePoints);
|
2013-06-26 03:51:33 +00:00
|
|
|
dictionary->removeBigramWords(word0CodePoints, word0Length, word1CodePoints,
|
|
|
|
word1Length);
|
|
|
|
}
|
|
|
|
|
2013-11-26 07:03:10 +00:00
|
|
|
|
|
|
|
// Returns how many language model params are processed.
|
|
|
|
static int latinime_BinaryDictionary_addMultipleDictionaryEntries(JNIEnv *env, jclass clazz,
|
|
|
|
jlong dict, jobjectArray languageModelParams, jint startIndex) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
jsize languageModelParamCount = env->GetArrayLength(languageModelParams);
|
|
|
|
if (languageModelParamCount == 0 || startIndex >= languageModelParamCount) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
jobject languageModelParam = env->GetObjectArrayElement(languageModelParams, 0);
|
|
|
|
jclass languageModelParamClass = env->GetObjectClass(languageModelParam);
|
|
|
|
env->DeleteLocalRef(languageModelParam);
|
|
|
|
jfieldID word0FieldId = env->GetFieldID(languageModelParamClass, "mWord0", "[I");
|
|
|
|
jfieldID word1FieldId = env->GetFieldID(languageModelParamClass, "mWord1", "[I");
|
|
|
|
jfieldID unigramProbabilityFieldId =
|
|
|
|
env->GetFieldID(languageModelParamClass, "mUnigramProbability", "I");
|
|
|
|
jfieldID bigramProbabilityFieldId =
|
|
|
|
env->GetFieldID(languageModelParamClass, "mBigramProbability", "I");
|
|
|
|
env->DeleteLocalRef(languageModelParamClass);
|
|
|
|
|
|
|
|
for (int i = startIndex; i < languageModelParamCount; ++i) {
|
|
|
|
jobject languageModelParam = env->GetObjectArrayElement(languageModelParams, i);
|
|
|
|
jintArray word0 = static_cast<jintArray>(
|
|
|
|
env->GetObjectField(languageModelParam, word0FieldId));
|
|
|
|
jsize word0Length = word0 ? env->GetArrayLength(word0) : 0;
|
|
|
|
int word0CodePoints[word0Length];
|
|
|
|
if (word0) {
|
|
|
|
env->GetIntArrayRegion(word0, 0, word0Length, word0CodePoints);
|
|
|
|
}
|
|
|
|
jintArray word1 = static_cast<jintArray>(
|
|
|
|
env->GetObjectField(languageModelParam, word1FieldId));
|
|
|
|
jsize word1Length = env->GetArrayLength(word1);
|
|
|
|
int word1CodePoints[word1Length];
|
|
|
|
env->GetIntArrayRegion(word1, 0, word1Length, word1CodePoints);
|
|
|
|
jint unigramProbability = env->GetIntField(languageModelParam, unigramProbabilityFieldId);
|
|
|
|
dictionary->addUnigramWord(word1CodePoints, word1Length, unigramProbability);
|
|
|
|
if (word0) {
|
|
|
|
jint bigramProbability = env->GetIntField(languageModelParam, bigramProbabilityFieldId);
|
|
|
|
dictionary->addBigramWords(word0CodePoints, word0Length, word1CodePoints, word1Length,
|
|
|
|
bigramProbability);
|
|
|
|
}
|
|
|
|
if (dictionary->needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(word0);
|
|
|
|
env->DeleteLocalRef(word1);
|
|
|
|
env->DeleteLocalRef(languageModelParam);
|
|
|
|
}
|
|
|
|
return languageModelParamCount;
|
|
|
|
}
|
|
|
|
|
2013-09-18 02:18:28 +00:00
|
|
|
static int latinime_BinaryDictionary_calculateProbabilityNative(JNIEnv *env, jclass clazz,
|
|
|
|
jlong dict, jint unigramProbability, jint bigramProbability) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return NOT_A_PROBABILITY;
|
|
|
|
}
|
|
|
|
return dictionary->getDictionaryStructurePolicy()->getProbability(unigramProbability,
|
|
|
|
bigramProbability);
|
|
|
|
}
|
|
|
|
|
2013-09-27 14:12:12 +00:00
|
|
|
static jstring latinime_BinaryDictionary_getProperty(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jstring query) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return env->NewStringUTF("");
|
|
|
|
}
|
|
|
|
const jsize queryUtf8Length = env->GetStringUTFLength(query);
|
|
|
|
char queryChars[queryUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(query, 0, env->GetStringLength(query), queryChars);
|
|
|
|
queryChars[queryUtf8Length] = '\0';
|
|
|
|
static const int GET_PROPERTY_RESULT_LENGTH = 100;
|
|
|
|
char resultChars[GET_PROPERTY_RESULT_LENGTH];
|
|
|
|
resultChars[0] = '\0';
|
2013-11-25 05:57:41 +00:00
|
|
|
dictionary->getProperty(queryChars, queryUtf8Length, resultChars, GET_PROPERTY_RESULT_LENGTH);
|
2013-09-27 14:12:12 +00:00
|
|
|
return env->NewStringUTF(resultChars);
|
|
|
|
}
|
|
|
|
|
2013-06-04 10:16:47 +00:00
|
|
|
static const JNINativeMethod sMethods[] = {
|
2013-09-26 03:59:02 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("createEmptyDictFileNative"),
|
|
|
|
const_cast<char *>("(Ljava/lang/String;J[Ljava/lang/String;[Ljava/lang/String;)Z"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_createEmptyDictFile)
|
|
|
|
},
|
2013-06-04 10:16:47 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("openNative"),
|
2013-06-25 05:25:01 +00:00
|
|
|
const_cast<char *>("(Ljava/lang/String;JJZ)J"),
|
2013-06-04 10:16:47 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_open)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("closeNative"),
|
|
|
|
const_cast<char *>("(J)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_close)
|
|
|
|
},
|
2013-09-17 09:07:16 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("flushNative"),
|
|
|
|
const_cast<char *>("(JLjava/lang/String;)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_flush)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("needsToRunGCNative"),
|
2013-09-30 04:57:54 +00:00
|
|
|
const_cast<char *>("(JZ)Z"),
|
2013-09-17 09:07:16 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_needsToRunGC)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("flushWithGCNative"),
|
|
|
|
const_cast<char *>("(JLjava/lang/String;)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_flushWithGC)
|
|
|
|
},
|
2013-06-04 10:16:47 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("getSuggestionsNative"),
|
2013-08-20 09:00:21 +00:00
|
|
|
const_cast<char *>("(JJJ[I[I[I[I[III[I[I[I[I[I[I[I)I"),
|
2013-06-04 10:16:47 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getSuggestions)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("getProbabilityNative"),
|
|
|
|
const_cast<char *>("(J[I)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getProbability)
|
|
|
|
},
|
|
|
|
{
|
2013-09-18 02:18:28 +00:00
|
|
|
const_cast<char *>("getBigramProbabilityNative"),
|
|
|
|
const_cast<char *>("(J[I[I)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getBigramProbability)
|
2013-06-04 10:16:47 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("calcNormalizedScoreNative"),
|
|
|
|
const_cast<char *>("([I[II)F"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_calcNormalizedScore)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("editDistanceNative"),
|
|
|
|
const_cast<char *>("([I[I)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_editDistance)
|
2013-06-26 03:51:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("addUnigramWordNative"),
|
|
|
|
const_cast<char *>("(J[II)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_addUnigramWord)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("addBigramWordsNative"),
|
|
|
|
const_cast<char *>("(J[I[II)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_addBigramWords)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("removeBigramWordsNative"),
|
|
|
|
const_cast<char *>("(J[I[I)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_removeBigramWords)
|
2013-09-18 02:18:28 +00:00
|
|
|
},
|
2013-11-26 07:03:10 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("addMultipleDictionaryEntriesNative"),
|
|
|
|
const_cast<char *>(
|
|
|
|
"(J[Lcom/android/inputmethod/latin/BinaryDictionary$LanguageModelParam;I)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_addMultipleDictionaryEntries)
|
|
|
|
},
|
2013-09-18 02:18:28 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("calculateProbabilityNative"),
|
|
|
|
const_cast<char *>("(JII)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_calculateProbabilityNative)
|
2013-09-27 14:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("getPropertyNative"),
|
|
|
|
const_cast<char *>("(JLjava/lang/String;)Ljava/lang/String;"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getProperty)
|
2013-06-04 10:16:47 +00:00
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
};
|
|
|
|
|
2011-02-22 08:28:55 +00:00
|
|
|
int register_BinaryDictionary(JNIEnv *env) {
|
2012-07-25 08:51:43 +00:00
|
|
|
const char *const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
|
2012-10-08 02:46:14 +00:00
|
|
|
return registerNativeMethods(env, kClassPathName, sMethods, NELEMS(sMethods));
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2011-06-18 04:09:55 +00:00
|
|
|
} // namespace latinime
|