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

Commit 79bb37d4 authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi
Browse files

Rename BigramProperty to NgramProperty.

Remaining work is changing bigram to ngram for supporting
ngram entry counting, dumping, and migration.

Bug: 14425059
Change-Id: Ifba288a1166996d62a5e57698f63537ea0a2a8ee
parent 6c7a85cb
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -403,10 +403,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(),
    // Use 1 for count to indicate the ngram has inputted.
    const NgramProperty ngramProperty(CodePointArrayView(wordCodePoints, wordLength).toVector(),
            probability, timestamp, 0 /* level */, 1 /* count */);
    return dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty);
    return dictionary->addNgramEntry(&prevWordsInfo, &ngramProperty);
}

static bool latinime_BinaryDictionary_removeNgramEntry(JNIEnv *env, jclass clazz, jlong dict,
@@ -501,12 +501,12 @@ static int latinime_BinaryDictionary_addMultipleDictionaryEntries(JNIEnv *env, j
        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 */);
            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 +603,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 +618,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;
            }
        }
+2 −2
Original line number Diff line number Diff line
@@ -144,9 +144,9 @@ bool Dictionary::removeUnigramEntry(const CodePointArrayView codePoints) {
}

bool Dictionary::addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
        const BigramProperty *const bigramProperty) {
        const NgramProperty *const ngramProperty) {
    TimeKeeper::setCurrentTime();
    return mDictionaryStructureWithBufferPolicy->addNgramEntry(prevWordsInfo, bigramProperty);
    return mDictionaryStructureWithBufferPolicy->addNgramEntry(prevWordsInfo, ngramProperty);
}

bool Dictionary::removeNgramEntry(const PrevWordsInfo *const prevWordsInfo,
+1 −1
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ class Dictionary {
    bool removeUnigramEntry(const CodePointArrayView codePoints);

    bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
            const BigramProperty *const bigramProperty);
            const NgramProperty *const ngramProperty);

    bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo,
            const CodePointArrayView codePoints);
+6 −7
Original line number Diff line number Diff line
@@ -14,8 +14,8 @@
 * limitations under the License.
 */

#ifndef LATINIME_BIGRAM_PROPERTY_H
#define LATINIME_BIGRAM_PROPERTY_H
#ifndef LATINIME_NGRAM_PROPERTY_H
#define LATINIME_NGRAM_PROPERTY_H

#include <vector>

@@ -23,10 +23,9 @@

namespace latinime {

// TODO: Change to NgramProperty.
class BigramProperty {
class NgramProperty {
 public:
    BigramProperty(const std::vector<int> &&targetCodePoints, const int probability,
    NgramProperty(const std::vector<int> &&targetCodePoints, const int probability,
            const int timestamp, const int level, const int count)
            : mTargetCodePoints(std::move(targetCodePoints)), mProbability(probability),
              mTimestamp(timestamp), mLevel(level), mCount(count) {}
@@ -53,7 +52,7 @@ class BigramProperty {

 private:
    // Default copy constructor and assign operator are used for using in std::vector.
    DISALLOW_DEFAULT_CONSTRUCTOR(BigramProperty);
    DISALLOW_DEFAULT_CONSTRUCTOR(NgramProperty);

    // TODO: Make members const.
    std::vector<int> mTargetCodePoints;
@@ -63,4 +62,4 @@ class BigramProperty {
    int mCount;
};
} // namespace latinime
#endif // LATINIME_WORD_PROPERTY_H
#endif // LATINIME_NGRAM_PROPERTY_H
+7 −6
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ void WordProperty::outputProperties(JNIEnv *const env, jintArray outCodePoints,
            MAX_WORD_LENGTH /* maxLength */, mCodePoints.data(), mCodePoints.size(),
            false /* needsNullTermination */);
    jboolean flags[] = {mUnigramProperty.isNotAWord(), mUnigramProperty.isBlacklisted(),
            !mBigrams.empty(), mUnigramProperty.hasShortcuts(),
            !mNgrams.empty(), mUnigramProperty.hasShortcuts(),
            mUnigramProperty.representsBeginningOfSentence()};
    env->SetBooleanArrayRegion(outFlags, 0 /* start */, NELEMS(flags), flags);
    int probabilityInfo[] = {mUnigramProperty.getProbability(), mUnigramProperty.getTimestamp(),
@@ -42,8 +42,9 @@ void WordProperty::outputProperties(JNIEnv *const env, jintArray outCodePoints,
    jmethodID addMethodId = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");

    // Output bigrams.
    for (const auto &bigramProperty : mBigrams) {
        const std::vector<int> *const word1CodePoints = bigramProperty.getTargetCodePoints();
    // TODO: Support n-gram
    for (const auto &ngramProperty : mNgrams) {
        const std::vector<int> *const word1CodePoints = ngramProperty.getTargetCodePoints();
        jintArray bigramWord1CodePointArray = env->NewIntArray(word1CodePoints->size());
        JniDataUtils::outputCodePoints(env, bigramWord1CodePointArray, 0 /* start */,
                word1CodePoints->size(), word1CodePoints->data(), word1CodePoints->size(),
@@ -51,9 +52,9 @@ void WordProperty::outputProperties(JNIEnv *const env, jintArray outCodePoints,
        env->CallBooleanMethod(outBigramTargets, addMethodId, bigramWord1CodePointArray);
        env->DeleteLocalRef(bigramWord1CodePointArray);

        int bigramProbabilityInfo[] = {bigramProperty.getProbability(),
                bigramProperty.getTimestamp(), bigramProperty.getLevel(),
                bigramProperty.getCount()};
        int bigramProbabilityInfo[] = {ngramProperty.getProbability(),
                ngramProperty.getTimestamp(), ngramProperty.getLevel(),
                ngramProperty.getCount()};
        jintArray bigramProbabilityInfoArray = env->NewIntArray(NELEMS(bigramProbabilityInfo));
        env->SetIntArrayRegion(bigramProbabilityInfoArray, 0 /* start */,
                NELEMS(bigramProbabilityInfo), bigramProbabilityInfo);
Loading