Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 79312485 authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi
Browse files

Make addUnigramWord use UnigramProperty.

Bug: 13406708
Change-Id: I10c108dd7047fe230c60495f13800f32fdc46d0d
parent cb05dc1d
Loading
Loading
Loading
Loading
+25 −18
Original line number Diff line number Diff line
@@ -19,16 +19,19 @@
#include "com_android_inputmethod_latin_BinaryDictionary.h"

#include <cstring> // for memset()
#include <vector>

#include "defines.h"
#include "jni.h"
#include "jni_common.h"
#include "suggest/core/dictionary/dictionary.h"
#include "suggest/core/dictionary/property/unigram_property.h"
#include "suggest/core/dictionary/property/word_property.h"
#include "suggest/core/result/suggestion_results.h"
#include "suggest/core/suggest_options.h"
#include "suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h"
#include "utils/char_utils.h"
#include "utils/jni_data_utils.h"
#include "utils/time_keeper.h"

namespace latinime {
@@ -288,22 +291,24 @@ static void latinime_BinaryDictionary_getWordProperty(JNIEnv *env, jclass clazz,
}

static void latinime_BinaryDictionary_addUnigramWord(JNIEnv *env, jclass clazz, jlong dict,
        jintArray word, jint probability, jintArray shortcutTarget, jint shortuctProbability,
        jintArray word, jint probability, jintArray shortcutTarget, jint shortcutProbability,
        jboolean isNotAWord, jboolean isBlacklisted, jint timestamp) {
    Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
    if (!dictionary) {
        return;
    }
    jsize wordLength = env->GetArrayLength(word);
    int codePoints[wordLength];
    env->GetIntArrayRegion(word, 0, wordLength, codePoints);
    jsize shortcutLength = shortcutTarget ? env->GetArrayLength(shortcutTarget) : 0;
    int shortcutTargetCodePoints[shortcutLength];
    if (shortcutTarget) {
        env->GetIntArrayRegion(shortcutTarget, 0, shortcutLength, shortcutTargetCodePoints);
    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);
    }
    dictionary->addUnigramWord(codePoints, wordLength, probability, shortcutTargetCodePoints,
            shortcutLength, shortuctProbability, isNotAWord, isBlacklisted, timestamp);
    const UnigramProperty unigramProperty(isNotAWord, isBlacklisted,
            probability, timestamp, 0 /* level */, 0 /* count */, &shortcuts);
    dictionary->addUnigramWord(codePoints, codePointCount, &unigramProperty);
}

static void latinime_BinaryDictionary_addBigramWords(JNIEnv *env, jclass clazz, jlong dict,
@@ -394,15 +399,17 @@ static int latinime_BinaryDictionary_addMultipleDictionaryEntries(JNIEnv *env, j
        jboolean isBlacklisted = env->GetBooleanField(languageModelParam, isBlacklistedFieldId);
        jintArray shortcutTarget = static_cast<jintArray>(
                env->GetObjectField(languageModelParam, shortcutTargetFieldId));
        jsize shortcutLength = shortcutTarget ? env->GetArrayLength(shortcutTarget) : 0;
        int shortcutTargetCodePoints[shortcutLength];
        if (shortcutTarget) {
            env->GetIntArrayRegion(shortcutTarget, 0, shortcutLength, shortcutTargetCodePoints);
        }
        jint shortcutProbability = env->GetIntField(languageModelParam, shortcutProbabilityFieldId);
        dictionary->addUnigramWord(word1CodePoints, word1Length, unigramProbability,
                shortcutTargetCodePoints, shortcutLength, shortcutProbability,
                isNotAWord, isBlacklisted, timestamp);
        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);
        }
        const UnigramProperty unigramProperty(isNotAWord, isBlacklisted,
                unigramProbability, timestamp, 0 /* level */, 0 /* count */, &shortcuts);
        dictionary->addUnigramWord(word1CodePoints, word1Length, &unigramProperty);
        if (word0) {
            jint bigramProbability = env->GetIntField(languageModelParam, bigramProbabilityFieldId);
            dictionary->addBigramWords(word0CodePoints, word0Length, word1CodePoints, word1Length,
+7 −16
Original line number Diff line number Diff line
@@ -50,15 +50,10 @@ void Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession
    TimeKeeper::setCurrentTime();
    DicTraverseSession::initSessionInstance(
            traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
    if (suggestOptions->isGesture()) {
        mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
    const auto &suggest = suggestOptions->isGesture() ? mGestureSuggest : mTypingSuggest;
    suggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
            ycoordinates, times, pointerIds, inputCodePoints, inputSize,
            languageWeight, outSuggestionResults);
    } else {
        mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
                ycoordinates, times, pointerIds, inputCodePoints, inputSize,
                languageWeight, outSuggestionResults);
    }
    if (DEBUG_DICT) {
        outSuggestionResults->dumpSuggestions();
    }
@@ -87,14 +82,10 @@ int Dictionary::getBigramProbability(const int *word0, int length0, const int *w
    return mBigramDictionary.getBigramProbability(word0, length0, word1, length1);
}

void Dictionary::addUnigramWord(const int *const word, const int length, const int probability,
        const int *const shortcutTargetCodePoints, const int shortcutLength,
        const int shortcutProbability, const bool isNotAWord, const bool isBlacklisted,
        const int timestamp) {
void Dictionary::addUnigramWord(const int *const word, const int length,
        const UnigramProperty *const unigramProperty) {
    TimeKeeper::setCurrentTime();
    mDictionaryStructureWithBufferPolicy->addUnigramWord(word, length, probability,
            shortcutTargetCodePoints, shortcutLength, shortcutProbability, isNotAWord,
            isBlacklisted, timestamp);
    mDictionaryStructureWithBufferPolicy->addUnigramWord(word, length, unigramProperty);
}

void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
+2 −5
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ class DicTraverseSession;
class ProximityInfo;
class SuggestionResults;
class SuggestOptions;
class WordProperty;

class Dictionary {
 public:
@@ -74,10 +73,8 @@ class Dictionary {

    int getBigramProbability(const int *word0, int length0, const int *word1, int length1) const;

    void addUnigramWord(const int *const word, const int length, const int probability,
            const int *const shortcutTargetCodePoints, const int shortcutLength,
            const int shortcutProbability, const bool isNotAWord, const bool isBlacklisted,
            const int timestamp);
    void addUnigramWord(const int *const codePoints, const int codePointCount,
            const UnigramProperty *const unigramProperty);

    void addBigramWords(const int *const word0, const int length0, const int *const word1,
            const int length1, const int probability, const int timestamp);
+2 −3
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ class DicNodeVector;
class DictionaryBigramsStructurePolicy;
class DictionaryHeaderStructurePolicy;
class DictionaryShortcutsStructurePolicy;
class UnigramProperty;

/*
 * This class abstracts the structure of dictionaries.
@@ -69,9 +70,7 @@ class DictionaryStructureWithBufferPolicy {

    // Returns whether the update was success or not.
    virtual bool addUnigramWord(const int *const word, const int length,
            const int probability, const int *const shortcutTargetCodePoints,
            const int shortcutLength, const int shortcutProbability, const bool isNotAWord,
            const bool isBlacklisted,const int timestamp) = 0;
            const UnigramProperty *const unigramProperty) = 0;

    // Returns whether the update was success or not.
    virtual bool addBigramWords(const int *const word0, const int length0, const int *const word1,
+2 −4
Original line number Diff line number Diff line
@@ -81,10 +81,8 @@ class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
        return &mShortcutListPolicy;
    }

    bool addUnigramWord(const int *const word, const int length, const int probability,
            const int *const shortcutTargetCodePoints, const int shortcutLength,
            const int shortcutProbability, const bool isNotAWord, const bool isBlacklisted,
            const int timestamp) {
    bool addUnigramWord(const int *const word, const int length,
            const UnigramProperty *const unigramProperty) {
        // This method should not be called for non-updatable dictionary.
        AKLOGI("Warning: addUnigramWord() is called for non-updatable dictionary.");
        return false;
Loading