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()
|
2014-04-14 09:49:19 +00:00
|
|
|
#include <vector>
|
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"
|
2014-04-14 09:49:19 +00:00
|
|
|
#include "suggest/core/dictionary/property/unigram_property.h"
|
2014-04-08 09:24:06 +00:00
|
|
|
#include "suggest/core/dictionary/property/word_property.h"
|
2014-03-07 10:36:19 +00:00
|
|
|
#include "suggest/core/result/suggestion_results.h"
|
2014-05-15 09:39:23 +00:00
|
|
|
#include "suggest/core/session/prev_words_info.h"
|
2013-06-03 01:36:00 +00:00
|
|
|
#include "suggest/core/suggest_options.h"
|
2013-12-13 08:09:16 +00:00
|
|
|
#include "suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h"
|
2014-02-04 14:51:05 +00:00
|
|
|
#include "utils/char_utils.h"
|
2014-04-14 09:49:19 +00:00
|
|
|
#include "utils/jni_data_utils.h"
|
2014-05-08 03:31:32 +00:00
|
|
|
#include "utils/log_utils.h"
|
2014-01-15 22:55:57 +00:00
|
|
|
#include "utils/time_keeper.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-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';
|
2014-03-06 14:36:38 +00:00
|
|
|
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy(
|
2014-04-22 19:01:22 +00:00
|
|
|
DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
|
2013-09-17 09:07:16 +00:00
|
|
|
sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize),
|
2014-03-06 14:36:38 +00:00
|
|
|
isUpdatable == JNI_TRUE));
|
|
|
|
if (!dictionaryStructureWithBufferPolicy) {
|
2013-08-15 11:37:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-16 03:52:04 +00:00
|
|
|
|
2014-03-06 14:36:38 +00:00
|
|
|
Dictionary *const dictionary =
|
|
|
|
new Dictionary(env, std::move(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
|
|
|
}
|
|
|
|
|
2014-04-21 23:22:00 +00:00
|
|
|
static jlong latinime_BinaryDictionary_createOnMemory(JNIEnv *env, jclass clazz,
|
|
|
|
jlong formatVersion, jstring locale, jobjectArray attributeKeyStringArray,
|
|
|
|
jobjectArray attributeValueStringArray) {
|
2014-04-22 19:01:22 +00:00
|
|
|
const jsize localeUtf8Length = env->GetStringUTFLength(locale);
|
|
|
|
char localeChars[localeUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(locale, 0, env->GetStringLength(locale), localeChars);
|
|
|
|
localeChars[localeUtf8Length] = '\0';
|
|
|
|
std::vector<int> localeCodePoints;
|
|
|
|
HeaderReadWriteUtils::insertCharactersIntoVector(localeChars, &localeCodePoints);
|
|
|
|
const int keyCount = env->GetArrayLength(attributeKeyStringArray);
|
|
|
|
const int valueCount = env->GetArrayLength(attributeValueStringArray);
|
|
|
|
if (keyCount != valueCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DictionaryHeaderStructurePolicy::AttributeMap attributeMap =
|
|
|
|
JniDataUtils::constructAttributeMap(env, attributeKeyStringArray,
|
|
|
|
attributeValueStringArray);
|
|
|
|
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy =
|
|
|
|
DictionaryStructureWithBufferPolicyFactory::newPolicyForOnMemoryDict(
|
|
|
|
formatVersion, localeCodePoints, &attributeMap);
|
|
|
|
if (!dictionaryStructureWithBufferPolicy) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Dictionary *const dictionary =
|
|
|
|
new Dictionary(env, std::move(dictionaryStructureWithBufferPolicy));
|
|
|
|
return reinterpret_cast<jlong>(dictionary);
|
2014-04-21 23:22:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 08:28:29 +00:00
|
|
|
static bool latinime_BinaryDictionary_flush(JNIEnv *env, jclass clazz, jlong dict,
|
2013-09-17 09:07:16 +00:00
|
|
|
jstring filePath) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
2014-05-27 08:28:29 +00:00
|
|
|
if (!dictionary) return false;
|
2013-09-17 09:07:16 +00:00
|
|
|
const jsize filePathUtf8Length = env->GetStringUTFLength(filePath);
|
|
|
|
char filePathChars[filePathUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
|
|
|
|
filePathChars[filePathUtf8Length] = '\0';
|
2014-05-27 08:28:29 +00:00
|
|
|
return dictionary->flush(filePathChars);
|
2013-09-17 09:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-27 08:28:29 +00:00
|
|
|
static bool latinime_BinaryDictionary_flushWithGC(JNIEnv *env, jclass clazz, jlong dict,
|
2013-09-17 09:07:16 +00:00
|
|
|
jstring filePath) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
2014-05-27 08:28:29 +00:00
|
|
|
if (!dictionary) return false;
|
2013-09-17 09:07:16 +00:00
|
|
|
const jsize filePathUtf8Length = env->GetStringUTFLength(filePath);
|
|
|
|
char filePathChars[filePathUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
|
|
|
|
filePathChars[filePathUtf8Length] = '\0';
|
2014-05-27 08:28:29 +00:00
|
|
|
return dictionary->flushWithGC(filePathChars);
|
2013-09-17 09:07:16 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-04 12:39:44 +00:00
|
|
|
static void latinime_BinaryDictionary_getHeaderInfo(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jintArray outHeaderSize, jintArray outFormatVersion, jobject outAttributeKeys,
|
|
|
|
jobject outAttributeValues) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return;
|
2014-02-06 08:55:45 +00:00
|
|
|
const DictionaryHeaderStructurePolicy *const headerPolicy =
|
|
|
|
dictionary->getDictionaryStructurePolicy()->getHeaderStructurePolicy();
|
2014-05-14 10:47:03 +00:00
|
|
|
JniDataUtils::putIntToArray(env, outHeaderSize, 0 /* index */, headerPolicy->getSize());
|
|
|
|
JniDataUtils::putIntToArray(env, outFormatVersion, 0 /* index */,
|
|
|
|
headerPolicy->getFormatVersionNumber());
|
2014-02-06 08:55:45 +00:00
|
|
|
// Output attribute map
|
|
|
|
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
|
|
|
jmethodID addMethodId = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");
|
|
|
|
const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap =
|
|
|
|
headerPolicy->getAttributeMap();
|
|
|
|
for (DictionaryHeaderStructurePolicy::AttributeMap::const_iterator it = attributeMap->begin();
|
|
|
|
it != attributeMap->end(); ++it) {
|
|
|
|
// Output key
|
|
|
|
jintArray keyCodePointArray = env->NewIntArray(it->first.size());
|
2014-05-14 08:42:47 +00:00
|
|
|
JniDataUtils::outputCodePoints(env, keyCodePointArray, 0 /* start */,
|
|
|
|
it->first.size(), it->first.data(), it->first.size(),
|
|
|
|
false /* needsNullTermination */);
|
2014-02-18 14:40:03 +00:00
|
|
|
env->CallBooleanMethod(outAttributeKeys, addMethodId, keyCodePointArray);
|
2014-02-06 08:55:45 +00:00
|
|
|
env->DeleteLocalRef(keyCodePointArray);
|
|
|
|
// Output value
|
|
|
|
jintArray valueCodePointArray = env->NewIntArray(it->second.size());
|
2014-05-14 08:42:47 +00:00
|
|
|
JniDataUtils::outputCodePoints(env, valueCodePointArray, 0 /* start */,
|
|
|
|
it->second.size(), it->second.data(), it->second.size(),
|
|
|
|
false /* needsNullTermination */);
|
2014-02-18 14:40:03 +00:00
|
|
|
env->CallBooleanMethod(outAttributeValues, addMethodId, valueCodePointArray);
|
2014-02-06 08:55:45 +00:00
|
|
|
env->DeleteLocalRef(valueCodePointArray);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(arrayListClass);
|
2014-02-04 12:39:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
static int latinime_BinaryDictionary_getFormatVersion(JNIEnv *env, jclass clazz, jlong dict) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return 0;
|
2014-02-06 08:55:45 +00:00
|
|
|
const DictionaryHeaderStructurePolicy *const headerPolicy =
|
|
|
|
dictionary->getDictionaryStructurePolicy()->getHeaderStructurePolicy();
|
|
|
|
return headerPolicy->getFormatVersionNumber();
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 10:36:19 +00:00
|
|
|
static void 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,
|
2014-03-20 09:47:45 +00:00
|
|
|
jintArray inputCodePointsArray, jint inputSize, jintArray suggestOptions,
|
2014-05-23 10:58:58 +00:00
|
|
|
jintArray prevWordCodePointsForBigrams, jboolean isBeginningOfSentence,
|
|
|
|
jintArray outSuggestionCount, jintArray outCodePointsArray, jintArray outScoresArray,
|
|
|
|
jintArray outSpaceIndicesArray, jintArray outTypesArray,
|
|
|
|
jintArray outAutoCommitFirstWordConfidenceArray, jfloatArray inOutLanguageWeight) {
|
2012-08-14 05:22:27 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
2014-03-07 10:36:19 +00:00
|
|
|
// Assign 0 to outSuggestionCount here in case of returning earlier in this method.
|
2014-05-14 10:47:03 +00:00
|
|
|
JniDataUtils::putIntToArray(env, outSuggestionCount, 0 /* index */, 0);
|
2014-03-07 10:36:19 +00:00
|
|
|
if (!dictionary) {
|
|
|
|
return;
|
|
|
|
}
|
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);
|
2014-05-15 06:36:56 +00:00
|
|
|
if (!traverseSession) {
|
|
|
|
return;
|
|
|
|
}
|
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];
|
2014-03-07 01:04:44 +00:00
|
|
|
int *prevWordCodePoints = nullptr;
|
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 */
|
2014-03-07 10:36:19 +00:00
|
|
|
const jsize outputCodePointsLength = env->GetArrayLength(outCodePointsArray);
|
2013-01-11 16:18:00 +00:00
|
|
|
if (outputCodePointsLength != (MAX_WORD_LENGTH * MAX_RESULTS)) {
|
|
|
|
AKLOGE("Invalid outputCodePointsLength: %d", outputCodePointsLength);
|
|
|
|
ASSERT(false);
|
2014-03-07 10:36:19 +00:00
|
|
|
return;
|
2013-01-11 16:18:00 +00:00
|
|
|
}
|
2014-03-07 10:36:19 +00:00
|
|
|
const jsize scoresLength = env->GetArrayLength(outScoresArray);
|
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);
|
2014-03-07 10:36:19 +00:00
|
|
|
return;
|
2013-01-11 09:59:01 +00:00
|
|
|
}
|
2013-09-30 12:39:43 +00:00
|
|
|
const jsize outputAutoCommitFirstWordConfidenceLength =
|
2014-03-07 10:36:19 +00:00
|
|
|
env->GetArrayLength(outAutoCommitFirstWordConfidenceArray);
|
2013-09-30 12:39:43 +00:00
|
|
|
ASSERT(outputAutoCommitFirstWordConfidenceLength == 1);
|
2014-03-27 11:05:33 +00:00
|
|
|
if (outputAutoCommitFirstWordConfidenceLength != 1) {
|
|
|
|
// We only use the first result, as obviously we will only ever autocommit the first one
|
|
|
|
AKLOGE("Invalid outputAutoCommitFirstWordConfidenceLength: %d",
|
|
|
|
outputAutoCommitFirstWordConfidenceLength);
|
|
|
|
ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-02 16:43:36 +00:00
|
|
|
float languageWeight;
|
|
|
|
env->GetFloatArrayRegion(inOutLanguageWeight, 0, 1 /* len */, &languageWeight);
|
2014-03-27 11:05:33 +00:00
|
|
|
SuggestionResults suggestionResults(MAX_RESULTS);
|
2014-05-15 09:39:23 +00:00
|
|
|
const PrevWordsInfo prevWordsInfo(prevWordCodePoints, prevWordCodePointsLength,
|
|
|
|
false /* isStartOfSentence */);
|
2013-05-30 00:18:03 +00:00
|
|
|
if (givenSuggestOptions.isGesture() || inputSize > 0) {
|
2014-03-07 10:36:19 +00:00
|
|
|
// TODO: Use SuggestionResults to return suggestions.
|
2014-03-27 11:05:33 +00:00
|
|
|
dictionary->getSuggestions(pInfo, traverseSession, xCoordinates, yCoordinates,
|
2014-05-15 09:39:23 +00:00
|
|
|
times, pointerIds, inputCodePoints, inputSize, &prevWordsInfo,
|
|
|
|
&givenSuggestOptions, languageWeight, &suggestionResults);
|
2012-07-11 03:07:44 +00:00
|
|
|
} else {
|
2014-05-15 09:39:23 +00:00
|
|
|
dictionary->getPredictions(&prevWordsInfo, &suggestionResults);
|
2012-07-11 03:07:44 +00:00
|
|
|
}
|
2014-03-27 11:05:33 +00:00
|
|
|
suggestionResults.outputSuggestions(env, outSuggestionCount, outCodePointsArray,
|
|
|
|
outScoresArray, outSpaceIndicesArray, outTypesArray,
|
2014-04-02 16:43:36 +00:00
|
|
|
outAutoCommitFirstWordConfidenceArray, inOutLanguageWeight);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-05 09:16:11 +00:00
|
|
|
static jint latinime_BinaryDictionary_getMaxProbabilityOfExactMatches(
|
|
|
|
JNIEnv *env, jclass clazz, jlong dict, jintArray word) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return NOT_A_PROBABILITY;
|
|
|
|
const jsize wordLength = env->GetArrayLength(word);
|
|
|
|
int codePoints[wordLength];
|
|
|
|
env->GetIntArrayRegion(word, 0, wordLength, codePoints);
|
2014-06-06 08:37:46 +00:00
|
|
|
return dictionary->getMaxProbabilityOfExactMatches(codePoints, wordLength);
|
2014-06-05 09:16:11 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 02:18:28 +00:00
|
|
|
static jint latinime_BinaryDictionary_getBigramProbability(JNIEnv *env, jclass clazz,
|
2014-05-23 10:58:58 +00:00
|
|
|
jlong dict, jintArray word0, jboolean isBeginningOfSentence, 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);
|
2014-05-23 10:58:58 +00:00
|
|
|
const PrevWordsInfo prevWordsInfo(word0CodePoints, word0Length, isBeginningOfSentence);
|
2014-05-15 09:39:23 +00:00
|
|
|
return dictionary->getBigramProbability(&prevWordsInfo, word1CodePoints, word1Length);
|
2012-04-27 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 05:51:58 +00:00
|
|
|
// Method to iterate all words in the dictionary for makedict.
|
|
|
|
// If token is 0, this method newly starts iterating the dictionary. This method returns 0 when
|
|
|
|
// the dictionary does not have a next word.
|
|
|
|
static jint latinime_BinaryDictionary_getNextWord(JNIEnv *env, jclass clazz,
|
|
|
|
jlong dict, jint token, jintArray outCodePoints) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return 0;
|
|
|
|
const jsize outCodePointsLength = env->GetArrayLength(outCodePoints);
|
|
|
|
if (outCodePointsLength != MAX_WORD_LENGTH) {
|
|
|
|
AKLOGE("Invalid outCodePointsLength: %d", outCodePointsLength);
|
|
|
|
ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int wordCodePoints[outCodePointsLength];
|
|
|
|
memset(wordCodePoints, 0, sizeof(wordCodePoints));
|
|
|
|
const int nextToken = dictionary->getNextWordAndNextToken(token, wordCodePoints);
|
2014-05-14 08:42:47 +00:00
|
|
|
JniDataUtils::outputCodePoints(env, outCodePoints, 0 /* start */,
|
|
|
|
MAX_WORD_LENGTH /* maxLength */, wordCodePoints, outCodePointsLength,
|
|
|
|
false /* needsNullTermination */);
|
2014-02-03 05:51:58 +00:00
|
|
|
return nextToken;
|
|
|
|
}
|
|
|
|
|
2014-01-31 02:06:42 +00:00
|
|
|
static void latinime_BinaryDictionary_getWordProperty(JNIEnv *env, jclass clazz,
|
2013-12-13 08:09:16 +00:00
|
|
|
jlong dict, jintArray word, jintArray outCodePoints, jbooleanArray outFlags,
|
2014-01-31 11:32:44 +00:00
|
|
|
jintArray outProbabilityInfo, jobject outBigramTargets, jobject outBigramProbabilityInfo,
|
|
|
|
jobject outShortcutTargets, jobject outShortcutProbabilities) {
|
2013-12-13 08:09:16 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) return;
|
|
|
|
const jsize wordLength = env->GetArrayLength(word);
|
|
|
|
int wordCodePoints[wordLength];
|
|
|
|
env->GetIntArrayRegion(word, 0, wordLength, wordCodePoints);
|
2014-01-31 02:06:42 +00:00
|
|
|
const WordProperty wordProperty = dictionary->getWordProperty(wordCodePoints, wordLength);
|
2014-01-31 11:32:44 +00:00
|
|
|
wordProperty.outputProperties(env, outCodePoints, outFlags, outProbabilityInfo,
|
|
|
|
outBigramTargets, outBigramProbabilityInfo, outShortcutTargets,
|
|
|
|
outShortcutProbabilities);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 08:28:29 +00:00
|
|
|
static bool latinime_BinaryDictionary_addUnigramWord(JNIEnv *env, jclass clazz, jlong dict,
|
2014-04-14 09:49:19 +00:00
|
|
|
jintArray word, jint probability, jintArray shortcutTarget, jint shortcutProbability,
|
2014-05-23 10:58:58 +00:00
|
|
|
jboolean isBeginningOfSentence, jboolean isNotAWord, jboolean isBlacklisted,
|
|
|
|
jint timestamp) {
|
2013-06-26 03:51:33 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
2014-05-27 08:28:29 +00:00
|
|
|
return false;
|
2013-06-26 03:51:33 +00:00
|
|
|
}
|
2014-04-14 09:49:19 +00:00
|
|
|
jsize codePointCount = env->GetArrayLength(word);
|
|
|
|
int codePoints[codePointCount];
|
|
|
|
env->GetIntArrayRegion(word, 0, codePointCount, codePoints);
|
|
|
|
std::vector<UnigramProperty::ShortcutProperty> shortcuts;
|
|
|
|
std::vector<int> shortcutTargetCodePoints;
|
|
|
|
JniDataUtils::jintarrayToVector(env, shortcutTarget, &shortcutTargetCodePoints);
|
|
|
|
if (!shortcutTargetCodePoints.empty()) {
|
|
|
|
shortcuts.emplace_back(&shortcutTargetCodePoints, shortcutProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
2014-05-12 16:28:30 +00:00
|
|
|
// Use 1 for count to indicate the word has inputted.
|
2014-05-23 10:58:58 +00:00
|
|
|
const UnigramProperty unigramProperty(isBeginningOfSentence, isNotAWord,
|
2014-05-22 15:07:14 +00:00
|
|
|
isBlacklisted, probability, timestamp, 0 /* level */, 1 /* count */, &shortcuts);
|
2014-05-27 08:28:29 +00:00
|
|
|
return dictionary->addUnigramEntry(codePoints, codePointCount, &unigramProperty);
|
2013-06-26 03:51:33 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 08:28:29 +00:00
|
|
|
static bool latinime_BinaryDictionary_addBigramWords(JNIEnv *env, jclass clazz, jlong dict,
|
2014-05-23 10:58:58 +00:00
|
|
|
jintArray word0, jboolean isBeginningOfSentence, jintArray word1, jint probability,
|
|
|
|
jint timestamp) {
|
2013-06-26 03:51:33 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
2014-05-27 08:28:29 +00:00
|
|
|
return false;
|
2013-06-26 03:51:33 +00:00
|
|
|
}
|
|
|
|
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);
|
2014-05-12 16:28:30 +00:00
|
|
|
const std::vector<int> bigramTargetCodePoints(
|
|
|
|
word1CodePoints, word1CodePoints + word1Length);
|
|
|
|
// Use 1 for count to indicate the bigram has inputted.
|
|
|
|
const BigramProperty bigramProperty(&bigramTargetCodePoints, probability,
|
|
|
|
timestamp, 0 /* level */, 1 /* count */);
|
2014-05-23 10:58:58 +00:00
|
|
|
const PrevWordsInfo prevWordsInfo(word0CodePoints, word0Length, isBeginningOfSentence);
|
2014-05-27 08:28:29 +00:00
|
|
|
return dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty);
|
2013-06-26 03:51:33 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 08:28:29 +00:00
|
|
|
static bool latinime_BinaryDictionary_removeBigramWords(JNIEnv *env, jclass clazz, jlong dict,
|
2014-05-23 10:58:58 +00:00
|
|
|
jintArray word0, jboolean isBeginningOfSentence, jintArray word1) {
|
2013-06-26 03:51:33 +00:00
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
2014-05-27 08:28:29 +00:00
|
|
|
return false;
|
2013-06-26 03:51:33 +00:00
|
|
|
}
|
|
|
|
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);
|
2014-05-23 10:58:58 +00:00
|
|
|
const PrevWordsInfo prevWordsInfo(word0CodePoints, word0Length, isBeginningOfSentence);
|
2014-05-27 08:28:29 +00:00
|
|
|
return dictionary->removeNgramEntry(&prevWordsInfo, word1CodePoints, word1Length);
|
2013-06-26 03:51:33 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +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");
|
|
|
|
jfieldID timestampFieldId =
|
|
|
|
env->GetFieldID(languageModelParamClass, "mTimestamp", "I");
|
|
|
|
jfieldID shortcutTargetFieldId =
|
|
|
|
env->GetFieldID(languageModelParamClass, "mShortcutTarget", "[I");
|
|
|
|
jfieldID shortcutProbabilityFieldId =
|
|
|
|
env->GetFieldID(languageModelParamClass, "mShortcutProbability", "I");
|
|
|
|
jfieldID isNotAWordFieldId =
|
|
|
|
env->GetFieldID(languageModelParamClass, "mIsNotAWord", "Z");
|
|
|
|
jfieldID isBlacklistedFieldId =
|
|
|
|
env->GetFieldID(languageModelParamClass, "mIsBlacklisted", "Z");
|
|
|
|
env->DeleteLocalRef(languageModelParamClass);
|
|
|
|
|
|
|
|
for (int i = startIndex; i < languageModelParamCount; ++i) {
|
|
|
|
jobject languageModelParam = env->GetObjectArrayElement(languageModelParams, i);
|
|
|
|
// languageModelParam is a set of params for word1; thus, word1 cannot be null. On the
|
|
|
|
// other hand, word0 can be null and then it means the set of params doesn't contain bigram
|
|
|
|
// information.
|
|
|
|
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);
|
|
|
|
jint timestamp = env->GetIntField(languageModelParam, timestampFieldId);
|
|
|
|
jboolean isNotAWord = env->GetBooleanField(languageModelParam, isNotAWordFieldId);
|
|
|
|
jboolean isBlacklisted = env->GetBooleanField(languageModelParam, isBlacklistedFieldId);
|
|
|
|
jintArray shortcutTarget = static_cast<jintArray>(
|
|
|
|
env->GetObjectField(languageModelParam, shortcutTargetFieldId));
|
2014-04-14 09:49:19 +00:00
|
|
|
std::vector<UnigramProperty::ShortcutProperty> shortcuts;
|
|
|
|
std::vector<int> shortcutTargetCodePoints;
|
|
|
|
JniDataUtils::jintarrayToVector(env, shortcutTarget, &shortcutTargetCodePoints);
|
|
|
|
if (!shortcutTargetCodePoints.empty()) {
|
|
|
|
jint shortcutProbability =
|
|
|
|
env->GetIntField(languageModelParam, shortcutProbabilityFieldId);
|
|
|
|
shortcuts.emplace_back(&shortcutTargetCodePoints, shortcutProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
2014-05-12 16:28:30 +00:00
|
|
|
// Use 1 for count to indicate the word has inputted.
|
2014-05-22 15:07:14 +00:00
|
|
|
const UnigramProperty unigramProperty(false /* isBeginningOfSentence */, isNotAWord,
|
|
|
|
isBlacklisted, unigramProbability, timestamp, 0 /* level */, 1 /* count */,
|
|
|
|
&shortcuts);
|
2014-05-21 09:30:34 +00:00
|
|
|
dictionary->addUnigramEntry(word1CodePoints, word1Length, &unigramProperty);
|
2013-12-13 08:09:16 +00:00
|
|
|
if (word0) {
|
|
|
|
jint bigramProbability = env->GetIntField(languageModelParam, bigramProbabilityFieldId);
|
2014-05-12 16:28:30 +00:00
|
|
|
const std::vector<int> bigramTargetCodePoints(
|
|
|
|
word1CodePoints, word1CodePoints + word1Length);
|
|
|
|
// Use 1 for count to indicate the bigram has inputted.
|
|
|
|
const BigramProperty bigramProperty(&bigramTargetCodePoints, bigramProbability,
|
|
|
|
timestamp, 0 /* level */, 1 /* count */);
|
2014-05-21 09:30:34 +00:00
|
|
|
const PrevWordsInfo prevWordsInfo(word0CodePoints, word0Length,
|
|
|
|
false /* isBeginningOfSentence */);
|
|
|
|
dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
if (dictionary->needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(word0);
|
|
|
|
env->DeleteLocalRef(word1);
|
|
|
|
env->DeleteLocalRef(shortcutTarget);
|
|
|
|
env->DeleteLocalRef(languageModelParam);
|
|
|
|
}
|
|
|
|
return languageModelParamCount;
|
|
|
|
}
|
|
|
|
|
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-12-13 08:09:16 +00:00
|
|
|
dictionary->getProperty(queryChars, queryUtf8Length, resultChars, GET_PROPERTY_RESULT_LENGTH);
|
2013-09-27 14:12:12 +00:00
|
|
|
return env->NewStringUTF(resultChars);
|
|
|
|
}
|
|
|
|
|
2014-02-21 06:09:37 +00:00
|
|
|
static bool latinime_BinaryDictionary_isCorruptedNative(JNIEnv *env, jclass clazz, jlong dict) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return dictionary->getDictionaryStructurePolicy()->isCorrupted();
|
|
|
|
}
|
|
|
|
|
2014-05-08 03:31:32 +00:00
|
|
|
static DictionaryStructureWithBufferPolicy::StructurePolicyPtr runGCAndGetNewStructurePolicy(
|
|
|
|
DictionaryStructureWithBufferPolicy::StructurePolicyPtr structurePolicy,
|
|
|
|
const char *const dictFilePath) {
|
|
|
|
structurePolicy->flushWithGC(dictFilePath);
|
|
|
|
structurePolicy.release();
|
|
|
|
return DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
|
|
|
|
dictFilePath, 0 /* offset */, 0 /* size */, true /* isUpdatable */);
|
|
|
|
}
|
|
|
|
|
2014-05-08 03:31:04 +00:00
|
|
|
static bool latinime_BinaryDictionary_migrateNative(JNIEnv *env, jclass clazz, jlong dict,
|
|
|
|
jstring dictFilePath, jlong newFormatVersion) {
|
|
|
|
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
|
|
|
|
if (!dictionary) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-08 03:31:32 +00:00
|
|
|
const jsize filePathUtf8Length = env->GetStringUTFLength(dictFilePath);
|
|
|
|
char dictFilePathChars[filePathUtf8Length + 1];
|
|
|
|
env->GetStringUTFRegion(dictFilePath, 0, env->GetStringLength(dictFilePath), dictFilePathChars);
|
|
|
|
dictFilePathChars[filePathUtf8Length] = '\0';
|
|
|
|
|
|
|
|
const DictionaryHeaderStructurePolicy *const headerPolicy =
|
|
|
|
dictionary->getDictionaryStructurePolicy()->getHeaderStructurePolicy();
|
|
|
|
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy =
|
|
|
|
DictionaryStructureWithBufferPolicyFactory::newPolicyForOnMemoryDict(
|
|
|
|
newFormatVersion, *headerPolicy->getLocale(), headerPolicy->getAttributeMap());
|
|
|
|
if (!dictionaryStructureWithBufferPolicy) {
|
|
|
|
LogUtils::logToJava(env, "Cannot migrate header.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Migrate historical information.
|
|
|
|
int wordCodePoints[MAX_WORD_LENGTH];
|
|
|
|
int token = 0;
|
|
|
|
// Add unigrams.
|
|
|
|
do {
|
|
|
|
token = dictionary->getNextWordAndNextToken(token, wordCodePoints);
|
|
|
|
const int wordLength = CharUtils::getCodePointCount(MAX_WORD_LENGTH, wordCodePoints);
|
|
|
|
const WordProperty wordProperty = dictionary->getWordProperty(wordCodePoints, wordLength);
|
|
|
|
if (dictionaryStructureWithBufferPolicy->needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
dictionaryStructureWithBufferPolicy = runGCAndGetNewStructurePolicy(
|
|
|
|
std::move(dictionaryStructureWithBufferPolicy), dictFilePathChars);
|
|
|
|
if (!dictionaryStructureWithBufferPolicy) {
|
|
|
|
LogUtils::logToJava(env, "Cannot open dict after GC.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 09:30:34 +00:00
|
|
|
if (!dictionaryStructureWithBufferPolicy->addUnigramEntry(wordCodePoints, wordLength,
|
2014-05-08 03:31:32 +00:00
|
|
|
wordProperty.getUnigramProperty())) {
|
|
|
|
LogUtils::logToJava(env, "Cannot add unigram to the new dict.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (token != 0);
|
|
|
|
|
|
|
|
// Add bigrams.
|
|
|
|
do {
|
|
|
|
token = dictionary->getNextWordAndNextToken(token, wordCodePoints);
|
|
|
|
const int wordLength = CharUtils::getCodePointCount(MAX_WORD_LENGTH, wordCodePoints);
|
|
|
|
const WordProperty wordProperty = dictionary->getWordProperty(wordCodePoints, wordLength);
|
|
|
|
if (dictionaryStructureWithBufferPolicy->needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
dictionaryStructureWithBufferPolicy = runGCAndGetNewStructurePolicy(
|
|
|
|
std::move(dictionaryStructureWithBufferPolicy), dictFilePathChars);
|
|
|
|
if (!dictionaryStructureWithBufferPolicy) {
|
|
|
|
LogUtils::logToJava(env, "Cannot open dict after GC.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 09:30:34 +00:00
|
|
|
const PrevWordsInfo prevWordsInfo(wordCodePoints, wordLength,
|
|
|
|
false /* isStartOfSentence */);
|
2014-05-12 16:28:30 +00:00
|
|
|
for (const BigramProperty &bigramProperty : *wordProperty.getBigramProperties()) {
|
2014-05-21 09:30:34 +00:00
|
|
|
if (!dictionaryStructureWithBufferPolicy->addNgramEntry(&prevWordsInfo,
|
2014-05-12 16:28:30 +00:00
|
|
|
&bigramProperty)) {
|
2014-05-08 03:31:32 +00:00
|
|
|
LogUtils::logToJava(env, "Cannot add bigram to the new dict.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (token != 0);
|
|
|
|
// Save to File.
|
|
|
|
dictionaryStructureWithBufferPolicy->flushWithGC(dictFilePathChars);
|
|
|
|
return true;
|
2014-05-08 03:31:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 10:16:47 +00:00
|
|
|
static const JNINativeMethod sMethods[] = {
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
},
|
2014-04-21 23:22:00 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("createOnMemoryNative"),
|
|
|
|
const_cast<char *>("(JLjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)J"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_createOnMemory)
|
|
|
|
},
|
2013-06-04 10:16:47 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("closeNative"),
|
|
|
|
const_cast<char *>("(J)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_close)
|
|
|
|
},
|
2013-12-13 08:09:16 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("getFormatVersionNative"),
|
|
|
|
const_cast<char *>("(J)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getFormatVersion)
|
|
|
|
},
|
2014-02-04 12:39:44 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("getHeaderInfoNative"),
|
|
|
|
const_cast<char *>("(J[I[ILjava/util/ArrayList;Ljava/util/ArrayList;)V"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getHeaderInfo)
|
|
|
|
},
|
2013-09-17 09:07:16 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("flushNative"),
|
2014-05-27 08:28:29 +00:00
|
|
|
const_cast<char *>("(JLjava/lang/String;)Z"),
|
2013-09-17 09:07:16 +00:00
|
|
|
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"),
|
2014-05-27 08:28:29 +00:00
|
|
|
const_cast<char *>("(JLjava/lang/String;)Z"),
|
2013-09-17 09:07:16 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_flushWithGC)
|
|
|
|
},
|
2013-06-04 10:16:47 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("getSuggestionsNative"),
|
2014-05-23 10:58:58 +00:00
|
|
|
const_cast<char *>("(JJJ[I[I[I[I[II[I[IZ[I[I[I[I[I[I[F)V"),
|
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)
|
|
|
|
},
|
2014-06-05 09:16:11 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("getMaxProbabilityOfExactMatchesNative"),
|
|
|
|
const_cast<char *>("(J[I)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getMaxProbabilityOfExactMatches)
|
|
|
|
},
|
2013-06-04 10:16:47 +00:00
|
|
|
{
|
2013-09-18 02:18:28 +00:00
|
|
|
const_cast<char *>("getBigramProbabilityNative"),
|
2014-05-23 10:58:58 +00:00
|
|
|
const_cast<char *>("(J[IZ[I)I"),
|
2013-09-18 02:18:28 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getBigramProbability)
|
2013-06-04 10:16:47 +00:00
|
|
|
},
|
2013-12-13 08:09:16 +00:00
|
|
|
{
|
2014-01-31 02:06:42 +00:00
|
|
|
const_cast<char *>("getWordPropertyNative"),
|
2014-01-31 11:32:44 +00:00
|
|
|
const_cast<char *>("(J[I[I[Z[ILjava/util/ArrayList;Ljava/util/ArrayList;"
|
|
|
|
"Ljava/util/ArrayList;Ljava/util/ArrayList;)V"),
|
2014-01-31 02:06:42 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getWordProperty)
|
2013-12-13 08:09:16 +00:00
|
|
|
},
|
2014-02-03 05:51:58 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("getNextWordNative"),
|
|
|
|
const_cast<char *>("(JI[I)I"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_getNextWord)
|
|
|
|
},
|
2013-06-26 03:51:33 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("addUnigramWordNative"),
|
2014-05-27 08:28:29 +00:00
|
|
|
const_cast<char *>("(J[II[IIZZZI)Z"),
|
2013-06-26 03:51:33 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_addUnigramWord)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("addBigramWordsNative"),
|
2014-05-27 08:28:29 +00:00
|
|
|
const_cast<char *>("(J[IZ[III)Z"),
|
2013-06-26 03:51:33 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_addBigramWords)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("removeBigramWordsNative"),
|
2014-05-27 08:28:29 +00:00
|
|
|
const_cast<char *>("(J[IZ[I)Z"),
|
2013-06-26 03:51:33 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_removeBigramWords)
|
2013-09-18 02:18:28 +00:00
|
|
|
},
|
2013-12-13 08:09:16 +00:00
|
|
|
{
|
|
|
|
const_cast<char *>("addMultipleDictionaryEntriesNative"),
|
|
|
|
const_cast<char *>(
|
2014-01-15 09:39:36 +00:00
|
|
|
"(J[Lcom/android/inputmethod/latin/utils/LanguageModelParam;I)I"),
|
2013-12-13 08:09:16 +00:00
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_addMultipleDictionaryEntries)
|
|
|
|
},
|
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)
|
2014-02-21 06:09:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("isCorruptedNative"),
|
|
|
|
const_cast<char *>("(J)Z"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_isCorruptedNative)
|
2014-05-08 03:31:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
const_cast<char *>("migrateNative"),
|
|
|
|
const_cast<char *>("(JLjava/lang/String;J)Z"),
|
|
|
|
reinterpret_cast<void *>(latinime_BinaryDictionary_migrateNative)
|
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
|