2013-06-11 03:01:49 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-08-13 11:35:16 +00:00
|
|
|
#include "suggest/policyimpl/dictionary/header/header_policy.h"
|
2013-06-11 03:01:49 +00:00
|
|
|
|
2013-09-04 10:00:53 +00:00
|
|
|
#include <cstddef>
|
2013-09-18 09:08:33 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <ctime>
|
2013-09-04 10:00:53 +00:00
|
|
|
|
2013-06-11 03:01:49 +00:00
|
|
|
namespace latinime {
|
|
|
|
|
2013-09-04 10:00:53 +00:00
|
|
|
const char *const HeaderPolicy::MULTIPLE_WORDS_DEMOTION_RATE_KEY = "MULTIPLE_WORDS_DEMOTION_RATE";
|
2013-09-05 03:28:38 +00:00
|
|
|
const char *const HeaderPolicy::USES_FORGETTING_CURVE_KEY = "USES_FORGETTING_CURVE";
|
|
|
|
const char *const HeaderPolicy::LAST_UPDATED_TIME_KEY = "date";
|
2013-09-04 10:00:53 +00:00
|
|
|
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);
|
2013-09-18 09:08:33 +00:00
|
|
|
HeaderReadWriteUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyCodePointVector);
|
2013-09-04 10:00:53 +00:00
|
|
|
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';
|
|
|
|
}
|
2013-06-11 03:01:49 +00:00
|
|
|
|
2013-09-04 10:00:53 +00:00
|
|
|
float HeaderPolicy::readMultipleWordCostMultiplier() const {
|
2013-09-05 03:28:38 +00:00
|
|
|
int attributeValue = 0;
|
|
|
|
if (getAttributeValueAsInt(MULTIPLE_WORDS_DEMOTION_RATE_KEY, &attributeValue)) {
|
|
|
|
if (attributeValue <= 0) {
|
|
|
|
return static_cast<float>(MAX_VALUE_FOR_WEIGHTING);
|
|
|
|
}
|
|
|
|
return MULTIPLE_WORD_COST_MULTIPLIER_SCALE / static_cast<float>(attributeValue);
|
|
|
|
} else {
|
2013-09-04 10:00:53 +00:00
|
|
|
return DEFAULT_MULTIPLE_WORD_COST_MULTIPLIER;
|
|
|
|
}
|
2013-09-05 03:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HeaderPolicy::readUsesForgettingCurveFlag() const {
|
|
|
|
int attributeValue = 0;
|
|
|
|
if (getAttributeValueAsInt(USES_FORGETTING_CURVE_KEY, &attributeValue)) {
|
|
|
|
return attributeValue != 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns S_INT_MIN when the key is not found or the value is invalid.
|
|
|
|
int HeaderPolicy::readLastUpdatedTime() const {
|
|
|
|
int attributeValue = 0;
|
|
|
|
if (getAttributeValueAsInt(LAST_UPDATED_TIME_KEY, &attributeValue)) {
|
|
|
|
return attributeValue;
|
|
|
|
} else {
|
|
|
|
return S_INT_MIN;
|
2013-06-11 03:01:49 +00:00
|
|
|
}
|
2013-09-05 03:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether the key is found or not and stores the found value into outValue.
|
|
|
|
bool HeaderPolicy::getAttributeValueAsInt(const char *const key, int *const outValue) const {
|
|
|
|
std::vector<int> keyVector;
|
|
|
|
insertCharactersIntoVector(key, &keyVector);
|
2013-09-18 09:08:33 +00:00
|
|
|
HeaderReadWriteUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyVector);
|
2013-09-05 03:28:38 +00:00
|
|
|
if (it == mAttributeMap.end()) {
|
|
|
|
// The key was not found.
|
|
|
|
return false;
|
2013-06-11 03:01:49 +00:00
|
|
|
}
|
2013-09-05 03:28:38 +00:00
|
|
|
*outValue = parseIntAttributeValue(&(it->second));
|
|
|
|
return true;
|
2013-09-04 10:00:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 09:08:33 +00:00
|
|
|
bool HeaderPolicy::writeHeaderToBuffer(BufferWithExtendableBuffer *const bufferToWrite,
|
|
|
|
const bool updatesLastUpdatedTime) const {
|
|
|
|
int writingPos = 0;
|
|
|
|
if (!HeaderReadWriteUtils::writeDictionaryVersion(bufferToWrite, mDictFormatVersion,
|
|
|
|
&writingPos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!HeaderReadWriteUtils::writeDictionaryFlags(bufferToWrite, mDictionaryFlags,
|
|
|
|
&writingPos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Temporarily writes a dummy header size.
|
|
|
|
int headerSizeFieldPos = writingPos;
|
|
|
|
if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, 0 /* size */,
|
|
|
|
&writingPos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (updatesLastUpdatedTime) {
|
|
|
|
// Set current time as a last updated time.
|
|
|
|
HeaderReadWriteUtils::AttributeMap attributeMapTowrite(mAttributeMap);
|
|
|
|
std::vector<int> updatedTimekey;
|
|
|
|
insertCharactersIntoVector(LAST_UPDATED_TIME_KEY, &updatedTimekey);
|
|
|
|
const time_t currentTime = time(NULL);
|
|
|
|
std::vector<int> updatedTimeValue;
|
|
|
|
char charBuf[LARGEST_INT_DIGIT_COUNT + 1];
|
|
|
|
snprintf(charBuf, LARGEST_INT_DIGIT_COUNT + 1, "%ld", currentTime);
|
|
|
|
insertCharactersIntoVector(charBuf, &updatedTimeValue);
|
|
|
|
attributeMapTowrite[updatedTimekey] = updatedTimeValue;
|
|
|
|
if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &attributeMapTowrite,
|
|
|
|
&writingPos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &mAttributeMap,
|
|
|
|
&writingPos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Writes an actual header size.
|
|
|
|
if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, writingPos,
|
|
|
|
&headerSizeFieldPos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ HeaderReadWriteUtils::AttributeMap
|
|
|
|
HeaderPolicy::createAttributeMapAndReadAllAttributes(const uint8_t *const dictBuf) {
|
|
|
|
HeaderReadWriteUtils::AttributeMap attributeMap;
|
|
|
|
HeaderReadWriteUtils::fetchAllHeaderAttributes(dictBuf, &attributeMap);
|
2013-09-04 10:00:53 +00:00
|
|
|
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]);
|
|
|
|
}
|
2013-06-11 03:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace latinime
|