diff options
Diffstat (limited to 'native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp')
-rw-r--r-- | native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp | 52 |
1 files changed, 39 insertions, 13 deletions
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index e420f8056..365217a60 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -373,7 +373,8 @@ static bool latinime_BinaryDictionary_addUnigramEntry(JNIEnv *env, jclass clazz, } // Use 1 for count to indicate the word has inputted. const UnigramProperty unigramProperty(isBeginningOfSentence, isNotAWord, - isBlacklisted, probability, timestamp, 0 /* level */, 1 /* count */, &shortcuts); + isBlacklisted, probability, HistoricalInfo(timestamp, 0 /* level */, 1 /* count */), + std::move(shortcuts)); return dictionary->addUnigramEntry(CodePointArrayView(codePoints, codePointCount), &unigramProperty); } @@ -403,10 +404,10 @@ static bool latinime_BinaryDictionary_addNgramEntry(JNIEnv *env, jclass clazz, j jsize wordLength = env->GetArrayLength(word); int wordCodePoints[wordLength]; env->GetIntArrayRegion(word, 0, wordLength, wordCodePoints); - // Use 1 for count to indicate the bigram has inputted. - const BigramProperty bigramProperty(CodePointArrayView(wordCodePoints, wordLength).toVector(), - probability, timestamp, 0 /* level */, 1 /* count */); - return dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty); + // Use 1 for count to indicate the ngram has inputted. + const NgramProperty ngramProperty(CodePointArrayView(wordCodePoints, wordLength).toVector(), + probability, HistoricalInfo(timestamp, 0 /* level */, 1 /* count */)); + return dictionary->addNgramEntry(&prevWordsInfo, &ngramProperty); } static bool latinime_BinaryDictionary_removeNgramEntry(JNIEnv *env, jclass clazz, jlong dict, @@ -426,6 +427,25 @@ static bool latinime_BinaryDictionary_removeNgramEntry(JNIEnv *env, jclass clazz CodePointArrayView(wordCodePoints, codePointCount)); } +static bool latinime_BinaryDictionary_updateCounter(JNIEnv *env, jclass clazz, jlong dict, + jobjectArray prevWordCodePointArrays, jbooleanArray isBeginningOfSentenceArray, + jintArray word, jboolean isValidWord, jint count, jint timestamp) { + Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict); + if (!dictionary) { + return false; + } + const PrevWordsInfo prevWordsInfo = JniDataUtils::constructPrevWordsInfo(env, + prevWordCodePointArrays, isBeginningOfSentenceArray, + env->GetArrayLength(prevWordCodePointArrays)); + jsize codePointCount = env->GetArrayLength(word); + int wordCodePoints[codePointCount]; + env->GetIntArrayRegion(word, 0, codePointCount, wordCodePoints); + const HistoricalInfo historicalInfo(timestamp, 0 /* level */, count); + return dictionary->updateCounter(&prevWordsInfo, + CodePointArrayView(wordCodePoints, codePointCount), isValidWord == JNI_TRUE, + historicalInfo); +} + // Returns how many language model params are processed. static int latinime_BinaryDictionary_addMultipleDictionaryEntries(JNIEnv *env, jclass clazz, jlong dict, jobjectArray languageModelParams, jint startIndex) { @@ -494,19 +514,19 @@ static int latinime_BinaryDictionary_addMultipleDictionaryEntries(JNIEnv *env, j } // Use 1 for count to indicate the word has inputted. const UnigramProperty unigramProperty(false /* isBeginningOfSentence */, isNotAWord, - isBlacklisted, unigramProbability, timestamp, 0 /* level */, 1 /* count */, - &shortcuts); + isBlacklisted, unigramProbability, + HistoricalInfo(timestamp, 0 /* level */, 1 /* count */), std::move(shortcuts)); dictionary->addUnigramEntry(CodePointArrayView(word1CodePoints, word1Length), &unigramProperty); if (word0) { jint bigramProbability = env->GetIntField(languageModelParam, bigramProbabilityFieldId); // Use 1 for count to indicate the bigram has inputted. - const BigramProperty bigramProperty( + const NgramProperty ngramProperty( CodePointArrayView(word1CodePoints, word1Length).toVector(), - bigramProbability, timestamp, 0 /* level */, 1 /* count */); + bigramProbability, HistoricalInfo(timestamp, 0 /* level */, 1 /* count */)); const PrevWordsInfo prevWordsInfo(word0CodePoints, word0Length, false /* isBeginningOfSentence */); - dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty); + dictionary->addNgramEntry(&prevWordsInfo, &ngramProperty); } if (dictionary->needsToRunGC(true /* mindsBlockByGC */)) { return i + 1; @@ -603,6 +623,7 @@ static bool latinime_BinaryDictionary_migrateNative(JNIEnv *env, jclass clazz, j } while (token != 0); // Add bigrams. + // TODO: Support ngrams. do { token = dictionary->getNextWordAndNextToken(token, wordCodePoints, &wordCodePointCount); const WordProperty wordProperty = dictionary->getWordProperty( @@ -617,10 +638,10 @@ static bool latinime_BinaryDictionary_migrateNative(JNIEnv *env, jclass clazz, j } const PrevWordsInfo prevWordsInfo(wordCodePoints, wordCodePointCount, wordProperty.getUnigramProperty()->representsBeginningOfSentence()); - for (const BigramProperty &bigramProperty : *wordProperty.getBigramProperties()) { + for (const NgramProperty &ngramProperty : *wordProperty.getNgramProperties()) { if (!dictionaryStructureWithBufferPolicy->addNgramEntry(&prevWordsInfo, - &bigramProperty)) { - LogUtils::logToJava(env, "Cannot add bigram to the new dict."); + &ngramProperty)) { + LogUtils::logToJava(env, "Cannot add ngram to the new dict."); return false; } } @@ -723,6 +744,11 @@ static const JNINativeMethod sMethods[] = { reinterpret_cast<void *>(latinime_BinaryDictionary_removeNgramEntry) }, { + const_cast<char *>("updateCounterNative"), + const_cast<char *>("(J[[I[Z[IZII)Z"), + reinterpret_cast<void *>(latinime_BinaryDictionary_updateCounter) + }, + { const_cast<char *>("addMultipleDictionaryEntriesNative"), const_cast<char *>( "(J[Lcom/android/inputmethod/latin/utils/LanguageModelParam;I)I"), |