diff options
Diffstat (limited to 'native/jni/src')
11 files changed, 134 insertions, 125 deletions
diff --git a/native/jni/src/suggest/core/dictionary/dictionary.h b/native/jni/src/suggest/core/dictionary/dictionary.h index a5e986d15..b1774371b 100644 --- a/native/jni/src/suggest/core/dictionary/dictionary.h +++ b/native/jni/src/suggest/core/dictionary/dictionary.h @@ -60,6 +60,7 @@ class Dictionary { static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; static const int KIND_FLAG_EXACT_MATCH = 0x40000000; static const int KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION = 0x20000000; + static const int KIND_FLAG_APPROPRIATE_FOR_AUTOCORRECTION = 0x10000000; Dictionary(JNIEnv *env, DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy); diff --git a/native/jni/src/suggest/core/dictionary/error_type_utils.cpp b/native/jni/src/suggest/core/dictionary/error_type_utils.cpp index 8f07ce275..61093e174 100644 --- a/native/jni/src/suggest/core/dictionary/error_type_utils.cpp +++ b/native/jni/src/suggest/core/dictionary/error_type_utils.cpp @@ -21,13 +21,14 @@ namespace latinime { const ErrorTypeUtils::ErrorType ErrorTypeUtils::NOT_AN_ERROR = 0x0; const ErrorTypeUtils::ErrorType ErrorTypeUtils::MATCH_WITH_WRONG_CASE = 0x1; const ErrorTypeUtils::ErrorType ErrorTypeUtils::MATCH_WITH_MISSING_ACCENT = 0x2; -const ErrorTypeUtils::ErrorType ErrorTypeUtils::MATCH_WITH_WRONG_ACCENT = 0x4; -const ErrorTypeUtils::ErrorType ErrorTypeUtils::MATCH_WITH_DIGRAPH = 0x8; -const ErrorTypeUtils::ErrorType ErrorTypeUtils::INTENTIONAL_OMISSION = 0x10; -const ErrorTypeUtils::ErrorType ErrorTypeUtils::EDIT_CORRECTION = 0x20; -const ErrorTypeUtils::ErrorType ErrorTypeUtils::PROXIMITY_CORRECTION = 0x40; -const ErrorTypeUtils::ErrorType ErrorTypeUtils::COMPLETION = 0x80; -const ErrorTypeUtils::ErrorType ErrorTypeUtils::NEW_WORD = 0x100; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::MATCH_WITH_MISSING_EXPLICIT_ACCENT = 0x4; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::MATCH_WITH_WRONG_ACCENT = 0x8; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::MATCH_WITH_DIGRAPH = 0x10; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::INTENTIONAL_OMISSION = 0x20; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::EDIT_CORRECTION = 0x40; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::PROXIMITY_CORRECTION = 0x80; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::COMPLETION = 0x100; +const ErrorTypeUtils::ErrorType ErrorTypeUtils::NEW_WORD = 0x200; const ErrorTypeUtils::ErrorType ErrorTypeUtils::ERRORS_TREATED_AS_AN_EXACT_MATCH = NOT_AN_ERROR | MATCH_WITH_WRONG_CASE | MATCH_WITH_MISSING_ACCENT | MATCH_WITH_DIGRAPH; diff --git a/native/jni/src/suggest/core/dictionary/error_type_utils.h b/native/jni/src/suggest/core/dictionary/error_type_utils.h index e92c509fa..75111ba75 100644 --- a/native/jni/src/suggest/core/dictionary/error_type_utils.h +++ b/native/jni/src/suggest/core/dictionary/error_type_utils.h @@ -32,6 +32,7 @@ class ErrorTypeUtils { static const ErrorType NOT_AN_ERROR; static const ErrorType MATCH_WITH_WRONG_CASE; static const ErrorType MATCH_WITH_MISSING_ACCENT; + static const ErrorType MATCH_WITH_MISSING_EXPLICIT_ACCENT; static const ErrorType MATCH_WITH_WRONG_ACCENT; static const ErrorType MATCH_WITH_DIGRAPH; // Treat error as an intentional omission when the CorrectionType is omission and the node can @@ -61,6 +62,10 @@ class ErrorTypeUtils { & ~ERRORS_TREATED_AS_AN_EXACT_MATCH_WITH_INTENTIONAL_OMISSION) == 0; } + static bool isMissingExplicitAccent(const ErrorType errorType) { + return (errorType & MATCH_WITH_MISSING_EXPLICIT_ACCENT) != 0; + } + static bool isEditCorrectionError(const ErrorType errorType) { return (errorType & EDIT_CORRECTION) != 0; } diff --git a/native/jni/src/suggest/core/dictionary/property/word_property.cpp b/native/jni/src/suggest/core/dictionary/property/word_property.cpp deleted file mode 100644 index 019f0880f..000000000 --- a/native/jni/src/suggest/core/dictionary/property/word_property.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2013 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. - */ - -#include "suggest/core/dictionary/property/word_property.h" - -#include "utils/jni_data_utils.h" -#include "suggest/core/dictionary/property/historical_info.h" - -namespace latinime { - -void WordProperty::outputProperties(JNIEnv *const env, jintArray outCodePoints, - jbooleanArray outFlags, jintArray outProbabilityInfo, - jobject outNgramPrevWordsArray, jobject outNgramPrevWordIsBeginningOfSentenceArray, - jobject outNgramTargets, jobject outNgramProbabilities, jobject outShortcutTargets, - jobject outShortcutProbabilities) const { - JniDataUtils::outputCodePoints(env, outCodePoints, 0 /* start */, - MAX_WORD_LENGTH /* maxLength */, mCodePoints.data(), mCodePoints.size(), - false /* needsNullTermination */); - jboolean flags[] = {mUnigramProperty.isNotAWord(), mUnigramProperty.isPossiblyOffensive(), - !mNgrams.empty(), mUnigramProperty.hasShortcuts(), - mUnigramProperty.representsBeginningOfSentence()}; - env->SetBooleanArrayRegion(outFlags, 0 /* start */, NELEMS(flags), flags); - const HistoricalInfo &historicalInfo = mUnigramProperty.getHistoricalInfo(); - int probabilityInfo[] = {mUnigramProperty.getProbability(), historicalInfo.getTimestamp(), - historicalInfo.getLevel(), historicalInfo.getCount()}; - env->SetIntArrayRegion(outProbabilityInfo, 0 /* start */, NELEMS(probabilityInfo), - probabilityInfo); - - jclass integerClass = env->FindClass("java/lang/Integer"); - jmethodID intToIntegerConstructorId = env->GetMethodID(integerClass, "<init>", "(I)V"); - jclass arrayListClass = env->FindClass("java/util/ArrayList"); - jmethodID addMethodId = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z"); - - // Output ngrams. - jclass intArrayClass = env->FindClass("[I"); - for (const auto &ngramProperty : mNgrams) { - const NgramContext *const ngramContext = ngramProperty.getNgramContext(); - jobjectArray prevWordWordCodePointsArray = env->NewObjectArray( - ngramContext->getPrevWordCount(), intArrayClass, nullptr); - jbooleanArray prevWordIsBeginningOfSentenceArray = - env->NewBooleanArray(ngramContext->getPrevWordCount()); - for (size_t i = 0; i < ngramContext->getPrevWordCount(); ++i) { - const CodePointArrayView codePoints = ngramContext->getNthPrevWordCodePoints(i + 1); - jintArray prevWordCodePoints = env->NewIntArray(codePoints.size()); - JniDataUtils::outputCodePoints(env, prevWordCodePoints, 0 /* start */, - codePoints.size(), codePoints.data(), codePoints.size(), - false /* needsNullTermination */); - env->SetObjectArrayElement(prevWordWordCodePointsArray, i, prevWordCodePoints); - env->DeleteLocalRef(prevWordCodePoints); - JniDataUtils::putBooleanToArray(env, prevWordIsBeginningOfSentenceArray, i, - ngramContext->isNthPrevWordBeginningOfSentence(i + 1)); - } - env->CallBooleanMethod(outNgramPrevWordsArray, addMethodId, prevWordWordCodePointsArray); - env->CallBooleanMethod(outNgramPrevWordIsBeginningOfSentenceArray, addMethodId, - prevWordIsBeginningOfSentenceArray); - env->DeleteLocalRef(prevWordWordCodePointsArray); - env->DeleteLocalRef(prevWordIsBeginningOfSentenceArray); - - const std::vector<int> *const targetWordCodePoints = ngramProperty.getTargetCodePoints(); - jintArray targetWordCodePointArray = env->NewIntArray(targetWordCodePoints->size()); - JniDataUtils::outputCodePoints(env, targetWordCodePointArray, 0 /* start */, - targetWordCodePoints->size(), targetWordCodePoints->data(), - targetWordCodePoints->size(), false /* needsNullTermination */); - env->CallBooleanMethod(outNgramTargets, addMethodId, targetWordCodePointArray); - env->DeleteLocalRef(targetWordCodePointArray); - - const HistoricalInfo &ngramHistoricalInfo = ngramProperty.getHistoricalInfo(); - int bigramProbabilityInfo[] = {ngramProperty.getProbability(), - ngramHistoricalInfo.getTimestamp(), ngramHistoricalInfo.getLevel(), - ngramHistoricalInfo.getCount()}; - jintArray bigramProbabilityInfoArray = env->NewIntArray(NELEMS(bigramProbabilityInfo)); - env->SetIntArrayRegion(bigramProbabilityInfoArray, 0 /* start */, - NELEMS(bigramProbabilityInfo), bigramProbabilityInfo); - env->CallBooleanMethod(outNgramProbabilities, addMethodId, bigramProbabilityInfoArray); - env->DeleteLocalRef(bigramProbabilityInfoArray); - } - - // Output shortcuts. - for (const auto &shortcut : mUnigramProperty.getShortcuts()) { - const std::vector<int> *const targetCodePoints = shortcut.getTargetCodePoints(); - jintArray shortcutTargetCodePointArray = env->NewIntArray(targetCodePoints->size()); - JniDataUtils::outputCodePoints(env, shortcutTargetCodePointArray, 0 /* start */, - targetCodePoints->size(), targetCodePoints->data(), targetCodePoints->size(), - false /* needsNullTermination */); - env->CallBooleanMethod(outShortcutTargets, addMethodId, shortcutTargetCodePointArray); - env->DeleteLocalRef(shortcutTargetCodePointArray); - jobject integerProbability = env->NewObject(integerClass, intToIntegerConstructorId, - shortcut.getProbability()); - env->CallBooleanMethod(outShortcutProbabilities, addMethodId, integerProbability); - env->DeleteLocalRef(integerProbability); - } - env->DeleteLocalRef(integerClass); - env->DeleteLocalRef(arrayListClass); -} - -} // namespace latinime diff --git a/native/jni/src/suggest/core/dictionary/property/word_property.h b/native/jni/src/suggest/core/dictionary/property/word_property.h index d4db3f09f..9efc7f304 100644 --- a/native/jni/src/suggest/core/dictionary/property/word_property.h +++ b/native/jni/src/suggest/core/dictionary/property/word_property.h @@ -20,7 +20,6 @@ #include <vector> #include "defines.h" -#include "jni.h" #include "suggest/core/dictionary/property/ngram_property.h" #include "suggest/core/dictionary/property/unigram_property.h" #include "utils/int_array_view.h" @@ -39,12 +38,6 @@ class WordProperty { : mCodePoints(std::move(codePoints)), mUnigramProperty(unigramProperty), mNgrams(ngrams) {} - void outputProperties(JNIEnv *const env, jintArray outCodePoints, jbooleanArray outFlags, - jintArray outProbabilityInfo, jobject outNgramPrevWordsArray, - jobject outNgramPrevWordIsBeginningOfSentenceArray, jobject outNgramTargets, - jobject outNgramProbabilities, jobject outShortcutTargets, - jobject outShortcutProbabilities) const; - const CodePointArrayView getCodePoints() const { return CodePointArrayView(mCodePoints); } diff --git a/native/jni/src/suggest/core/result/suggestions_output_utils.cpp b/native/jni/src/suggest/core/result/suggestions_output_utils.cpp index 74db95953..1aff72952 100644 --- a/native/jni/src/suggest/core/result/suggestions_output_utils.cpp +++ b/native/jni/src/suggest/core/result/suggestions_output_utils.cpp @@ -144,11 +144,16 @@ const int SuggestionsOutputUtils::MIN_LEN_FOR_MULTI_WORD_AUTOCORRECT = 16; const bool isExactMatchWithIntentionalOmission = ErrorTypeUtils::isExactMatchWithIntentionalOmission( terminalDicNode->getContainedErrorTypes()); + // TODO: Decide whether the word should be auto-corrected or not here. + const bool isAppropriateForAutoCorrection = !ErrorTypeUtils::isMissingExplicitAccent( + terminalDicNode->getContainedErrorTypes()); const int outputTypeFlags = (wordAttributes.isPossiblyOffensive() ? Dictionary::KIND_FLAG_POSSIBLY_OFFENSIVE : 0) | ((isExactMatch && boostExactMatches) ? Dictionary::KIND_FLAG_EXACT_MATCH : 0) | (isExactMatchWithIntentionalOmission ? - Dictionary::KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION : 0); + Dictionary::KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION : 0) + | (isAppropriateForAutoCorrection ? + Dictionary::KIND_FLAG_APPROPRIATE_FOR_AUTOCORRECTION : 0); // Entries that are blacklisted or do not represent a word should not be output. const bool isValidWord = !(wordAttributes.isBlacklisted() || wordAttributes.isNotAWord()); diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp index 2bdf07752..7bda3dc95 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp @@ -16,7 +16,6 @@ #include "suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.h" -#include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_reading_utils.h" #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" namespace latinime { diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/trie_map.cpp b/native/jni/src/suggest/policyimpl/dictionary/utils/trie_map.cpp index 39f417ebb..b7ef2b9bd 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/utils/trie_map.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/utils/trie_map.cpp @@ -79,6 +79,9 @@ int TrieMap::getNextLevelBitmapEntryIndex(const int key, const int bitmapEntryIn } // Create a value entry and a bitmap entry. const int valueEntryIndex = allocateTable(TERMINAL_LINKED_ENTRY_COUNT); + if (valueEntryIndex == INVALID_INDEX) { + return INVALID_INDEX; + } if (!writeEntry(Entry(0, terminalEntry.getValue()), valueEntryIndex)) { return INVALID_INDEX; } @@ -195,6 +198,9 @@ bool TrieMap::writeValue(const uint64_t value, const int terminalEntryIndex) { } // Create value entry and write value. const int valueEntryIndex = allocateTable(TERMINAL_LINKED_ENTRY_COUNT); + if (valueEntryIndex == INVALID_INDEX) { + return false; + } if (!writeEntry(Entry(value >> (FIELD1_SIZE * CHAR_BIT), value), valueEntryIndex)) { return false; } diff --git a/native/jni/src/suggest/policyimpl/typing/typing_weighting.cpp b/native/jni/src/suggest/policyimpl/typing/typing_weighting.cpp index db7a39efb..a0e54115d 100644 --- a/native/jni/src/suggest/policyimpl/typing/typing_weighting.cpp +++ b/native/jni/src/suggest/policyimpl/typing/typing_weighting.cpp @@ -17,6 +17,7 @@ #include "suggest/policyimpl/typing/typing_weighting.h" #include "suggest/core/dicnode/dic_node.h" +#include "suggest/core/layout/proximity_info.h" #include "suggest/policyimpl/typing/scoring_params.h" namespace latinime { @@ -39,6 +40,8 @@ ErrorTypeUtils::ErrorType TypingWeighting::getErrorType(const CorrectionType cor const int primaryCodePoint = pInfoState->getPrimaryCodePointAt( dicNode->getInputIndex(0)); const int nodeCodePoint = dicNode->getNodeCodePoint(); + const int keyIndex = traverseSession->getProximityInfo()->getKeyIndexOf( + primaryCodePoint); // TODO: Check whether the input code point is on the keyboard. if (primaryCodePoint == nodeCodePoint) { // Node code point is same as original code point on the keyboard. @@ -53,6 +56,9 @@ ErrorTypeUtils::ErrorType TypingWeighting::getErrorType(const CorrectionType cor } else if (CharUtils::toBaseCodePoint(primaryCodePoint) == CharUtils::toBaseCodePoint(nodeCodePoint)) { // Base code points are the same but the code point is intentionally input. + if (keyIndex == NOT_AN_INDEX) { + return ErrorTypeUtils::MATCH_WITH_MISSING_EXPLICIT_ACCENT; + } return ErrorTypeUtils::MATCH_WITH_WRONG_ACCENT; } else if (CharUtils::toLowerCase(primaryCodePoint) == CharUtils::toBaseLowerCase(nodeCodePoint)) { @@ -61,6 +67,10 @@ ErrorTypeUtils::ErrorType TypingWeighting::getErrorType(const CorrectionType cor return ErrorTypeUtils::MATCH_WITH_MISSING_ACCENT | ErrorTypeUtils::MATCH_WITH_WRONG_CASE; } else { + if (keyIndex == NOT_AN_INDEX) { + return ErrorTypeUtils::MATCH_WITH_MISSING_EXPLICIT_ACCENT + | ErrorTypeUtils::MATCH_WITH_WRONG_CASE; + } // Base code points are the same and the cases are different. return ErrorTypeUtils::MATCH_WITH_WRONG_ACCENT | ErrorTypeUtils::MATCH_WITH_WRONG_CASE; diff --git a/native/jni/src/utils/jni_data_utils.cpp b/native/jni/src/utils/jni_data_utils.cpp index 5555293d5..41f0623d8 100644 --- a/native/jni/src/utils/jni_data_utils.cpp +++ b/native/jni/src/utils/jni_data_utils.cpp @@ -16,9 +16,100 @@ #include "utils/jni_data_utils.h" +#include "utils/int_array_view.h" + namespace latinime { const int JniDataUtils::CODE_POINT_REPLACEMENT_CHARACTER = 0xFFFD; const int JniDataUtils::CODE_POINT_NULL = 0; +/* static */ void JniDataUtils::outputWordProperty(JNIEnv *const env, + const WordProperty &wordProperty, jintArray outCodePoints, jbooleanArray outFlags, + jintArray outProbabilityInfo, jobject outNgramPrevWordsArray, + jobject outNgramPrevWordIsBeginningOfSentenceArray, jobject outNgramTargets, + jobject outNgramProbabilities, jobject outShortcutTargets, + jobject outShortcutProbabilities) { + const CodePointArrayView codePoints = wordProperty.getCodePoints(); + JniDataUtils::outputCodePoints(env, outCodePoints, 0 /* start */, + MAX_WORD_LENGTH /* maxLength */, codePoints.data(), codePoints.size(), + false /* needsNullTermination */); + const UnigramProperty &unigramProperty = wordProperty.getUnigramProperty(); + const std::vector<NgramProperty> &ngrams = wordProperty.getNgramProperties(); + jboolean flags[] = {unigramProperty.isNotAWord(), unigramProperty.isPossiblyOffensive(), + !ngrams.empty(), unigramProperty.hasShortcuts(), + unigramProperty.representsBeginningOfSentence()}; + env->SetBooleanArrayRegion(outFlags, 0 /* start */, NELEMS(flags), flags); + const HistoricalInfo &historicalInfo = unigramProperty.getHistoricalInfo(); + int probabilityInfo[] = {unigramProperty.getProbability(), historicalInfo.getTimestamp(), + historicalInfo.getLevel(), historicalInfo.getCount()}; + env->SetIntArrayRegion(outProbabilityInfo, 0 /* start */, NELEMS(probabilityInfo), + probabilityInfo); + + jclass integerClass = env->FindClass("java/lang/Integer"); + jmethodID intToIntegerConstructorId = env->GetMethodID(integerClass, "<init>", "(I)V"); + jclass arrayListClass = env->FindClass("java/util/ArrayList"); + jmethodID addMethodId = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z"); + + // Output ngrams. + jclass intArrayClass = env->FindClass("[I"); + for (const auto &ngramProperty : ngrams) { + const NgramContext *const ngramContext = ngramProperty.getNgramContext(); + jobjectArray prevWordWordCodePointsArray = env->NewObjectArray( + ngramContext->getPrevWordCount(), intArrayClass, nullptr); + jbooleanArray prevWordIsBeginningOfSentenceArray = + env->NewBooleanArray(ngramContext->getPrevWordCount()); + for (size_t i = 0; i < ngramContext->getPrevWordCount(); ++i) { + const CodePointArrayView codePoints = ngramContext->getNthPrevWordCodePoints(i + 1); + jintArray prevWordCodePoints = env->NewIntArray(codePoints.size()); + JniDataUtils::outputCodePoints(env, prevWordCodePoints, 0 /* start */, + codePoints.size(), codePoints.data(), codePoints.size(), + false /* needsNullTermination */); + env->SetObjectArrayElement(prevWordWordCodePointsArray, i, prevWordCodePoints); + env->DeleteLocalRef(prevWordCodePoints); + JniDataUtils::putBooleanToArray(env, prevWordIsBeginningOfSentenceArray, i, + ngramContext->isNthPrevWordBeginningOfSentence(i + 1)); + } + env->CallBooleanMethod(outNgramPrevWordsArray, addMethodId, prevWordWordCodePointsArray); + env->CallBooleanMethod(outNgramPrevWordIsBeginningOfSentenceArray, addMethodId, + prevWordIsBeginningOfSentenceArray); + env->DeleteLocalRef(prevWordWordCodePointsArray); + env->DeleteLocalRef(prevWordIsBeginningOfSentenceArray); + + const std::vector<int> *const targetWordCodePoints = ngramProperty.getTargetCodePoints(); + jintArray targetWordCodePointArray = env->NewIntArray(targetWordCodePoints->size()); + JniDataUtils::outputCodePoints(env, targetWordCodePointArray, 0 /* start */, + targetWordCodePoints->size(), targetWordCodePoints->data(), + targetWordCodePoints->size(), false /* needsNullTermination */); + env->CallBooleanMethod(outNgramTargets, addMethodId, targetWordCodePointArray); + env->DeleteLocalRef(targetWordCodePointArray); + + const HistoricalInfo &ngramHistoricalInfo = ngramProperty.getHistoricalInfo(); + int bigramProbabilityInfo[] = {ngramProperty.getProbability(), + ngramHistoricalInfo.getTimestamp(), ngramHistoricalInfo.getLevel(), + ngramHistoricalInfo.getCount()}; + jintArray bigramProbabilityInfoArray = env->NewIntArray(NELEMS(bigramProbabilityInfo)); + env->SetIntArrayRegion(bigramProbabilityInfoArray, 0 /* start */, + NELEMS(bigramProbabilityInfo), bigramProbabilityInfo); + env->CallBooleanMethod(outNgramProbabilities, addMethodId, bigramProbabilityInfoArray); + env->DeleteLocalRef(bigramProbabilityInfoArray); + } + + // Output shortcuts. + for (const auto &shortcut : unigramProperty.getShortcuts()) { + const std::vector<int> *const targetCodePoints = shortcut.getTargetCodePoints(); + jintArray shortcutTargetCodePointArray = env->NewIntArray(targetCodePoints->size()); + JniDataUtils::outputCodePoints(env, shortcutTargetCodePointArray, 0 /* start */, + targetCodePoints->size(), targetCodePoints->data(), targetCodePoints->size(), + false /* needsNullTermination */); + env->CallBooleanMethod(outShortcutTargets, addMethodId, shortcutTargetCodePointArray); + env->DeleteLocalRef(shortcutTargetCodePointArray); + jobject integerProbability = env->NewObject(integerClass, intToIntegerConstructorId, + shortcut.getProbability()); + env->CallBooleanMethod(outShortcutProbabilities, addMethodId, integerProbability); + env->DeleteLocalRef(integerProbability); + } + env->DeleteLocalRef(integerClass); + env->DeleteLocalRef(arrayListClass); +} + } // namespace latinime diff --git a/native/jni/src/utils/jni_data_utils.h b/native/jni/src/utils/jni_data_utils.h index a259e1cd0..01295859c 100644 --- a/native/jni/src/utils/jni_data_utils.h +++ b/native/jni/src/utils/jni_data_utils.h @@ -21,6 +21,7 @@ #include "defines.h" #include "jni.h" +#include "suggest/core/dictionary/property/word_property.h" #include "suggest/core/session/ngram_context.h" #include "suggest/core/policy/dictionary_header_structure_policy.h" #include "suggest/policyimpl/dictionary/header/header_read_write_utils.h" @@ -140,6 +141,12 @@ class JniDataUtils { env->SetFloatArrayRegion(array, index, 1 /* len */, &value); } + static void outputWordProperty(JNIEnv *const env, const WordProperty &wordProperty, + jintArray outCodePoints, jbooleanArray outFlags, jintArray outProbabilityInfo, + jobject outNgramPrevWordsArray, jobject outNgramPrevWordIsBeginningOfSentenceArray, + jobject outNgramTargets, jobject outNgramProbabilities, jobject outShortcutTargets, + jobject outShortcutProbabilities); + private: DISALLOW_IMPLICIT_CONSTRUCTORS(JniDataUtils); |