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

Commit bd0d1afd authored by Keisuke Kuroynagi's avatar Keisuke Kuroynagi
Browse files

Introduce BinaryDictionaryHeader to access binary dictionary header.

BinaryDictionaryHeader abstracts header structure and access header information via only its instance.

Bug: 6669677

Change-Id: Ib5ab0e8fef12265ddabc1d0165548b69953bba6a
parent 8ea77542
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -55,7 +55,9 @@ LATIN_IME_CORE_SRC_FILES := \
        dic_nodes_cache.cpp) \
    $(addprefix suggest/core/dictionary/, \
        bigram_dictionary.cpp \
        binary_dictionary_format.cpp \
        binary_dictionary_format_utils.cpp \
        binary_dictionary_header.cpp \
        binary_dictionary_header_reading_utils.cpp \
        byte_array_utils.cpp \
        dictionary.cpp \
        digraph_utils.cpp) \
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@
#include "jni.h"
#include "jni_common.h"
#include "obsolete/correction.h"
#include "suggest/core/dictionary/binary_dictionary_format.h"
#include "suggest/core/dictionary/binary_dictionary_format_utils.h"
#include "suggest/core/dictionary/binary_dictionary_info.h"
#include "suggest/core/dictionary/dictionary.h"
#include "suggest/core/suggest_options.h"
+1 −5
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 */

#include "suggest/core/dictionary/binary_dictionary_format.h"
#include "suggest/core/dictionary/binary_dictionary_format_utils.h"

namespace latinime {

@@ -31,7 +31,6 @@ const int BinaryDictionaryFormat::DICTIONARY_MINIMUM_SIZE = 4;
// then options that must be 0. Hence the first 32-bits of the format are always as follow
// and it's okay to consider them a magic number as a whole.
const uint32_t BinaryDictionaryFormat::FORMAT_VERSION_1_MAGIC_NUMBER = 0x78B10100;
const int BinaryDictionaryFormat::FORMAT_VERSION_1_HEADER_SIZE = 5;

// The versions of Latin IME that only handle format version 1 only test for the magic
// number, so we had to change it so that version 2 files would be rejected by older
@@ -39,9 +38,6 @@ const int BinaryDictionaryFormat::FORMAT_VERSION_1_HEADER_SIZE = 5;
const uint32_t BinaryDictionaryFormat::FORMAT_VERSION_2_MAGIC_NUMBER = 0x9BC13AFE;
// Magic number (4 bytes), version (2 bytes), options (2 bytes), header size (4 bytes) = 12
const int BinaryDictionaryFormat::FORMAT_VERSION_2_MINIMUM_SIZE = 12;
const int BinaryDictionaryFormat::VERSION_2_MAGIC_NUMBER_SIZE = 4;
const int BinaryDictionaryFormat::VERSION_2_DICTIONARY_VERSION_SIZE = 2;
const int BinaryDictionaryFormat::VERSION_2_DICTIONARY_FLAG_SIZE = 2;

/* static */ BinaryDictionaryFormat::FORMAT_VERSION BinaryDictionaryFormat::detectFormatVersion(
        const uint8_t *const dict, const int dictSize) {
+3 −20
Original line number Diff line number Diff line
@@ -14,8 +14,8 @@
 * limitations under the License.
 */

#ifndef LATINIME_BINARY_DICTIONARY_FORMAT_H
#define LATINIME_BINARY_DICTIONARY_FORMAT_H
#ifndef LATINIME_BINARY_DICTIONARY_FORMAT_UTILS_H
#define LATINIME_BINARY_DICTIONARY_FORMAT_UTILS_H

#include <stdint.h>

@@ -42,30 +42,13 @@ class BinaryDictionaryFormat {

    static FORMAT_VERSION detectFormatVersion(const uint8_t *const dict, const int dictSize);

    static AK_FORCE_INLINE int getHeaderSize(
            const uint8_t *const dict, const FORMAT_VERSION format) {
        switch (format) {
        case VERSION_1:
            return FORMAT_VERSION_1_HEADER_SIZE;
        case VERSION_2:
            // See the format of the header in the comment in detectFormat() above
            return ByteArrayUtils::readUint32(dict, 8);
        default:
            return S_INT_MAX;
        }
    }

 private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryDictionaryFormat);

    static const int DICTIONARY_MINIMUM_SIZE;
    static const uint32_t FORMAT_VERSION_1_MAGIC_NUMBER;
    static const int FORMAT_VERSION_1_HEADER_SIZE;
    static const uint32_t FORMAT_VERSION_2_MAGIC_NUMBER;
    static const int FORMAT_VERSION_2_MINIMUM_SIZE;
    static const int VERSION_2_MAGIC_NUMBER_SIZE;
    static const int VERSION_2_DICTIONARY_VERSION_SIZE ;
    static const int VERSION_2_DICTIONARY_FLAG_SIZE;
};
} // namespace latinime
#endif /* LATINIME_BINARY_DICTIONARY_FORMAT_H */
#endif /* LATINIME_BINARY_DICTIONARY_FORMAT_UTILS_H */
+49 −0
Original line number Diff line number Diff line
/*
 * 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/binary_dictionary_header.h"

#include "defines.h"
#include "suggest/core/dictionary/binary_dictionary_info.h"

namespace latinime {

const char *const BinaryDictionaryHeader::MULTIPLE_WORDS_DEMOTION_RATE_KEY =
        "MULTIPLE_WORDS_DEMOTION_RATE";
const float BinaryDictionaryHeader::DEFAULT_MULTI_WORD_COST_MULTIPLIER = 1.0f;
const float BinaryDictionaryHeader::MULTI_WORD_COST_MULTIPLIER_SCALE = 100.0f;

BinaryDictionaryHeader::BinaryDictionaryHeader(
        const BinaryDictionaryInfo *const binaryDictionaryInfo)
        : mBinaryDictionaryInfo(binaryDictionaryInfo),
          mDictionaryFlags(BinaryDictionaryHeaderReader::getFlags(binaryDictionaryInfo)),
          mSize(BinaryDictionaryHeaderReader::getHeaderSize(binaryDictionaryInfo)),
          mMultiWordCostMultiplier(readMultiWordCostMultiplier()) {}

float BinaryDictionaryHeader::readMultiWordCostMultiplier() const {
    const int headerValue = BinaryDictionaryHeaderReader::readHeaderValueInt(
            mBinaryDictionaryInfo, MULTIPLE_WORDS_DEMOTION_RATE_KEY);
    if (headerValue == S_INT_MIN) {
        // not found
        return DEFAULT_MULTI_WORD_COST_MULTIPLIER;
    }
    if (headerValue <= 0) {
        return static_cast<float>(MAX_VALUE_FOR_WEIGHTING);
    }
    return MULTI_WORD_COST_MULTIPLIER_SCALE / static_cast<float>(headerValue);
}

} // namespace latinime
Loading