aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
diff options
context:
space:
mode:
authorKeisuke Kuroyanagi <ksk@google.com>2013-09-04 19:00:53 +0900
committerKeisuke Kuroyanagi <ksk@google.com>2013-09-04 19:00:53 +0900
commit5754817a5e2e804f5ced54e601d4c08087d75b44 (patch)
tree5327a0d1cedcaf97bad861a093045a3b11150349 /native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
parent64621cf49b609ec5c8081cce202ae2f016be20bd (diff)
downloadlatinime-5754817a5e2e804f5ced54e601d4c08087d75b44.tar.gz
latinime-5754817a5e2e804f5ced54e601d4c08087d75b44.tar.xz
latinime-5754817a5e2e804f5ced54e601d4c08087d75b44.zip
Employ a header attribute map in headerPolicy.
Bug: 6669677 Change-Id: I4a084cdd7fcd7a8ed3a70fb7e365031eedd981e5
Diffstat (limited to 'native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp')
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp86
1 files changed, 76 insertions, 10 deletions
diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp b/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
index eb828b58c..439c3de1d 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
@@ -16,24 +16,90 @@
#include "suggest/policyimpl/dictionary/header/header_policy.h"
+#include <cstddef>
+
namespace latinime {
-const char *const HeaderPolicy::MULTIPLE_WORDS_DEMOTION_RATE_KEY =
- "MULTIPLE_WORDS_DEMOTION_RATE";
-const float HeaderPolicy::DEFAULT_MULTI_WORD_COST_MULTIPLIER = 1.0f;
-const float HeaderPolicy::MULTI_WORD_COST_MULTIPLIER_SCALE = 100.0f;
+const char *const HeaderPolicy::MULTIPLE_WORDS_DEMOTION_RATE_KEY = "MULTIPLE_WORDS_DEMOTION_RATE";
+const float HeaderPolicy::DEFAULT_MULTIPLE_WORD_COST_MULTIPLIER = 1.0f;
+const float HeaderPolicy::MULTIPLE_WORD_COST_MULTIPLIER_SCALE = 100.0f;
+
+// Used for logging. Question mark is used to indicate that the key is not found.
+void HeaderPolicy::readHeaderValueOrQuestionMark(const char *const key, int *outValue,
+ int outValueSize) const {
+ if (outValueSize <= 0) return;
+ if (outValueSize == 1) {
+ outValue[0] = '\0';
+ return;
+ }
+ std::vector<int> keyCodePointVector;
+ insertCharactersIntoVector(key, &keyCodePointVector);
+ HeaderReadingUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyCodePointVector);
+ if (it == mAttributeMap.end()) {
+ // The key was not found.
+ outValue[0] = '?';
+ outValue[1] = '\0';
+ return;
+ }
+ const int terminalIndex = min(static_cast<int>(it->second.size()), outValueSize - 1);
+ for (int i = 0; i < terminalIndex; ++i) {
+ outValue[i] = it->second[i];
+ }
+ outValue[terminalIndex] = '\0';
+}
-float HeaderPolicy::readMultiWordCostMultiplier() const {
- const int headerValue = HeaderReadingUtils::readHeaderValueInt(
- mDictBuf, MULTIPLE_WORDS_DEMOTION_RATE_KEY);
+float HeaderPolicy::readMultipleWordCostMultiplier() const {
+ std::vector<int> multipleWordsDemotionRateKeyVector;
+ insertCharactersIntoVector(MULTIPLE_WORDS_DEMOTION_RATE_KEY,
+ &multipleWordsDemotionRateKeyVector);
+ HeaderReadingUtils::AttributeMap::const_iterator it =
+ mAttributeMap.find(multipleWordsDemotionRateKeyVector);
+ if (it == mAttributeMap.end()) {
+ // The key was not found.
+ return DEFAULT_MULTIPLE_WORD_COST_MULTIPLIER;
+ }
+ const int headerValue = parseIntAttributeValue(&(it->second));
if (headerValue == S_INT_MIN) {
- // not found
- return DEFAULT_MULTI_WORD_COST_MULTIPLIER;
+ // Invalid value
+ return DEFAULT_MULTIPLE_WORD_COST_MULTIPLIER;
}
if (headerValue <= 0) {
return static_cast<float>(MAX_VALUE_FOR_WEIGHTING);
}
- return MULTI_WORD_COST_MULTIPLIER_SCALE / static_cast<float>(headerValue);
+ return MULTIPLE_WORD_COST_MULTIPLIER_SCALE / static_cast<float>(headerValue);
+}
+
+/* static */ HeaderReadingUtils::AttributeMap HeaderPolicy::createAttributeMapAndReadAllAttributes(
+ const uint8_t *const dictBuf) {
+ HeaderReadingUtils::AttributeMap attributeMap;
+ HeaderReadingUtils::fetchAllHeaderAttributes(dictBuf, &attributeMap);
+ return attributeMap;
+}
+
+/* static */ int HeaderPolicy::parseIntAttributeValue(
+ const std::vector<int> *const attributeValue) {
+ int value = 0;
+ bool isNegative = false;
+ for (size_t i = 0; i < attributeValue->size(); ++i) {
+ if (i == 0 && attributeValue->at(i) == '-') {
+ isNegative = true;
+ } else {
+ if (!isdigit(attributeValue->at(i))) {
+ // If not a number, return S_INT_MIN
+ return S_INT_MIN;
+ }
+ value *= 10;
+ value += attributeValue->at(i) - '0';
+ }
+ }
+ return isNegative ? -value : value;
+}
+
+/* static */ void HeaderPolicy::insertCharactersIntoVector(const char *const characters,
+ std::vector<int> *const vector) {
+ for (int i = 0; characters[i]; ++i) {
+ vector->push_back(characters[i]);
+ }
}
} // namespace latinime