Merge "Separate TimeKeeper from ForgettingCurveUtils."
This commit is contained in:
commit
8ca8807b96
10 changed files with 112 additions and 36 deletions
|
@ -117,7 +117,8 @@ LATIN_IME_CORE_SRC_FILES := \
|
|||
$(addprefix utils/, \
|
||||
autocorrection_threshold_utils.cpp \
|
||||
char_utils.cpp \
|
||||
log_utils.cpp)
|
||||
log_utils.cpp \
|
||||
time_keeper.cpp)
|
||||
|
||||
LOCAL_SRC_FILES := \
|
||||
$(LATIN_IME_JNI_SRC_FILES) \
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h"
|
||||
#include "suggest/policyimpl/typing/typing_suggest_policy_factory.h"
|
||||
#include "utils/log_utils.h"
|
||||
#include "utils/time_keeper.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
|
@ -47,6 +48,7 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession
|
|||
int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint,
|
||||
const SuggestOptions *const suggestOptions, int *outWords, int *frequencies,
|
||||
int *spaceIndices, int *outputTypes, int *outputAutoCommitFirstWordConfidence) const {
|
||||
TimeKeeper::setCurrentTime();
|
||||
int result = 0;
|
||||
if (suggestOptions->isGesture()) {
|
||||
DicTraverseSession::initSessionInstance(
|
||||
|
@ -74,12 +76,14 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession
|
|||
|
||||
int Dictionary::getBigrams(const int *word, int length, int *outWords, int *frequencies,
|
||||
int *outputTypes) const {
|
||||
TimeKeeper::setCurrentTime();
|
||||
if (length <= 0) return 0;
|
||||
return mBigramDictionary.get()->getPredictions(word, length, outWords, frequencies,
|
||||
outputTypes);
|
||||
}
|
||||
|
||||
int Dictionary::getProbability(const int *word, int length) const {
|
||||
TimeKeeper::setCurrentTime();
|
||||
int pos = getDictionaryStructurePolicy()->getTerminalPtNodePositionOfWord(word, length,
|
||||
false /* forceLowerCaseSearch */);
|
||||
if (NOT_A_DICT_POS == pos) {
|
||||
|
@ -90,40 +94,48 @@ int Dictionary::getProbability(const int *word, int length) const {
|
|||
|
||||
int Dictionary::getBigramProbability(const int *word0, int length0, const int *word1,
|
||||
int length1) const {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mBigramDictionary.get()->getBigramProbability(word0, length0, word1, length1);
|
||||
}
|
||||
|
||||
void Dictionary::addUnigramWord(const int *const word, const int length, const int probability,
|
||||
const int timestamp) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->addUnigramWord(word, length, probability,
|
||||
timestamp);
|
||||
}
|
||||
|
||||
void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
|
||||
const int length1, const int probability, const int timestamp) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->addBigramWords(word0, length0, word1, length1,
|
||||
probability, timestamp);
|
||||
}
|
||||
|
||||
void Dictionary::removeBigramWords(const int *const word0, const int length0,
|
||||
const int *const word1, const int length1) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->removeBigramWords(word0, length0, word1, length1);
|
||||
}
|
||||
|
||||
void Dictionary::flush(const char *const filePath) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->flush(filePath);
|
||||
}
|
||||
|
||||
void Dictionary::flushWithGC(const char *const filePath) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->flushWithGC(filePath);
|
||||
}
|
||||
|
||||
bool Dictionary::needsToRunGC(const bool mindsBlockByGC) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mDictionaryStructureWithBufferPolicy.get()->needsToRunGC(mindsBlockByGC);
|
||||
}
|
||||
|
||||
void Dictionary::getProperty(const char *const query, const int queryLength, char *const outResult,
|
||||
const int maxResultLength) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mDictionaryStructureWithBufferPolicy.get()->getProperty(query, queryLength, outResult,
|
||||
maxResultLength);
|
||||
}
|
||||
|
|
|
@ -92,12 +92,12 @@ bool HeaderPolicy::writeHeaderToBuffer(BufferWithExtendableBuffer *const bufferT
|
|||
if (updatesLastUpdatedTime) {
|
||||
// Set current time as a last updated time.
|
||||
HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, LAST_UPDATED_TIME_KEY,
|
||||
time(0));
|
||||
TimeKeeper::peekCurrentTime());
|
||||
}
|
||||
if (updatesLastDecayedTime) {
|
||||
// Set current time as a last updated time.
|
||||
HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, LAST_DECAYED_TIME_KEY,
|
||||
time(0));
|
||||
TimeKeeper::peekCurrentTime());
|
||||
}
|
||||
if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &attributeMapTowrite,
|
||||
&writingPos)) {
|
||||
|
|
|
@ -17,13 +17,13 @@
|
|||
#ifndef LATINIME_HEADER_POLICY_H
|
||||
#define LATINIME_HEADER_POLICY_H
|
||||
|
||||
#include <ctime>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "defines.h"
|
||||
#include "suggest/core/policy/dictionary_header_structure_policy.h"
|
||||
#include "suggest/policyimpl/dictionary/header/header_read_write_utils.h"
|
||||
#include "suggest/policyimpl/dictionary/utils/format_utils.h"
|
||||
#include "utils/time_keeper.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
|
@ -39,9 +39,9 @@ class HeaderPolicy : public DictionaryHeaderStructurePolicy {
|
|||
mIsDecayingDict(HeaderReadWriteUtils::readBoolAttributeValue(&mAttributeMap,
|
||||
IS_DECAYING_DICT_KEY, false /* defaultValue */)),
|
||||
mLastUpdatedTime(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
|
||||
LAST_UPDATED_TIME_KEY, time(0) /* defaultValue */)),
|
||||
LAST_UPDATED_TIME_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
|
||||
mLastDecayedTime(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
|
||||
LAST_DECAYED_TIME_KEY, time(0) /* defaultValue */)),
|
||||
LAST_DECAYED_TIME_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
|
||||
mUnigramCount(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
|
||||
UNIGRAM_COUNT_KEY, 0 /* defaultValue */)),
|
||||
mBigramCount(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
|
||||
|
@ -61,9 +61,9 @@ class HeaderPolicy : public DictionaryHeaderStructurePolicy {
|
|||
mIsDecayingDict(HeaderReadWriteUtils::readBoolAttributeValue(&mAttributeMap,
|
||||
IS_DECAYING_DICT_KEY, false /* defaultValue */)),
|
||||
mLastUpdatedTime(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
|
||||
LAST_UPDATED_TIME_KEY, time(0) /* defaultValue */)),
|
||||
LAST_UPDATED_TIME_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
|
||||
mLastDecayedTime(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
|
||||
LAST_UPDATED_TIME_KEY, time(0) /* defaultValue */)),
|
||||
LAST_UPDATED_TIME_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
|
||||
mUnigramCount(0), mBigramCount(0), mExtendedRegionSize(0),
|
||||
mHasHistoricalInfoOfWords(HeaderReadWriteUtils::readBoolAttributeValue(
|
||||
&mAttributeMap, HAS_HISTORICAL_INFO_KEY, false /* defaultValue */)) {}
|
||||
|
|
|
@ -59,9 +59,6 @@ void Ver4PatriciaTrieWritingHelper::writeToDictFileWithGC(const int rootPtNodeAr
|
|||
Ver4DictBuffers::createVer4DictBuffers(headerPolicy));
|
||||
int unigramCount = 0;
|
||||
int bigramCount = 0;
|
||||
if (needsToDecay) {
|
||||
ForgettingCurveUtils::sTimeKeeper.setCurrentTime();
|
||||
}
|
||||
if (!runGC(rootPtNodeArrayPos, headerPolicy, dictBuffers.get(), &unigramCount, &bigramCount,
|
||||
needsToDecay)) {
|
||||
return;
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
|
||||
#include "suggest/policyimpl/dictionary/utils/file_utils.h"
|
||||
#include "suggest/policyimpl/dictionary/utils/format_utils.h"
|
||||
#include "utils/time_keeper.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
|
@ -31,6 +32,7 @@ const char *const DictFileWritingUtils::TEMP_FILE_SUFFIX_FOR_WRITING_DICT_FILE =
|
|||
|
||||
/* static */ bool DictFileWritingUtils::createEmptyDictFile(const char *const filePath,
|
||||
const int dictVersion, const HeaderReadWriteUtils::AttributeMap *const attributeMap) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
switch (dictVersion) {
|
||||
case 4:
|
||||
return createEmptyV4DictFile(filePath, attributeMap);
|
||||
|
|
|
@ -14,14 +14,14 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "suggest/policyimpl/dictionary/utils/forgetting_curve_utils.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "suggest/core/policy/dictionary_header_structure_policy.h"
|
||||
#include "suggest/policyimpl/dictionary/utils/probability_utils.h"
|
||||
#include "utils/time_keeper.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
|
@ -40,11 +40,6 @@ const float ForgettingCurveUtils::MIN_PROBABILITY_TO_DECAY = 0.03f;
|
|||
const int ForgettingCurveUtils::DECAY_INTERVAL_SECONDS = 2 * 60 * 60;
|
||||
|
||||
const ForgettingCurveUtils::ProbabilityTable ForgettingCurveUtils::sProbabilityTable;
|
||||
ForgettingCurveUtils::TimeKeeper ForgettingCurveUtils::sTimeKeeper;
|
||||
|
||||
void ForgettingCurveUtils::TimeKeeper::setCurrentTime() {
|
||||
mCurrentTime = time(0);
|
||||
}
|
||||
|
||||
/* static */ int ForgettingCurveUtils::getProbability(const int encodedUnigramProbability,
|
||||
const int encodedBigramProbability) {
|
||||
|
@ -86,7 +81,7 @@ void ForgettingCurveUtils::TimeKeeper::setCurrentTime() {
|
|||
|
||||
/* static */ int ForgettingCurveUtils::getEncodedProbabilityToSave(const int encodedProbability,
|
||||
const DictionaryHeaderStructurePolicy *const headerPolicy) {
|
||||
const int elapsedTime = sTimeKeeper.peekCurrentTime() - headerPolicy->getLastDecayedTime();
|
||||
const int elapsedTime = TimeKeeper::peekCurrentTime() - headerPolicy->getLastDecayedTime();
|
||||
const int decayIterationCount = max(elapsedTime / DECAY_INTERVAL_SECONDS, 1);
|
||||
int currentEncodedProbability = max(min(encodedProbability, MAX_ENCODED_PROBABILITY), 0);
|
||||
// TODO: Implement the decay in more proper way.
|
||||
|
@ -116,7 +111,8 @@ void ForgettingCurveUtils::TimeKeeper::setCurrentTime() {
|
|||
if (mindsBlockByDecay) {
|
||||
return false;
|
||||
}
|
||||
if (headerPolicy->getLastDecayedTime() + DECAY_INTERVAL_SECONDS < time(0)) {
|
||||
if (headerPolicy->getLastDecayedTime() + DECAY_INTERVAL_SECONDS
|
||||
< TimeKeeper::peekCurrentTime()) {
|
||||
// Time to decay.
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -30,25 +30,11 @@ class DictionaryHeaderStructurePolicy;
|
|||
// TODO: Quit using bigram probability to indicate the delta.
|
||||
class ForgettingCurveUtils {
|
||||
public:
|
||||
class TimeKeeper {
|
||||
public:
|
||||
TimeKeeper() : mCurrentTime(0) {}
|
||||
void setCurrentTime();
|
||||
int peekCurrentTime() const { return mCurrentTime; };
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(TimeKeeper);
|
||||
|
||||
int mCurrentTime;
|
||||
};
|
||||
|
||||
static const int MAX_UNIGRAM_COUNT;
|
||||
static const int MAX_UNIGRAM_COUNT_AFTER_GC;
|
||||
static const int MAX_BIGRAM_COUNT;
|
||||
static const int MAX_BIGRAM_COUNT_AFTER_GC;
|
||||
|
||||
static TimeKeeper sTimeKeeper;
|
||||
|
||||
static int getProbability(const int encodedUnigramProbability,
|
||||
const int encodedBigramProbability);
|
||||
|
||||
|
|
41
native/jni/src/utils/time_keeper.cpp
Normal file
41
native/jni/src/utils/time_keeper.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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 "utils/time_keeper.h"
|
||||
|
||||
#include <ctime>
|
||||
|
||||
namespace latinime {
|
||||
|
||||
int TimeKeeper::sCurrentTime;
|
||||
bool TimeKeeper::sSetForTesting;
|
||||
|
||||
/* static */ void TimeKeeper::setCurrentTime() {
|
||||
if (!sSetForTesting) {
|
||||
sCurrentTime = time(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* static */ void TimeKeeper::startTestModeWithForceCurrentTime(const int currentTime) {
|
||||
sCurrentTime = currentTime;
|
||||
sSetForTesting = true;
|
||||
}
|
||||
|
||||
/* static */ void TimeKeeper::stopTestMode() {
|
||||
sSetForTesting = false;
|
||||
}
|
||||
|
||||
} // namespace latinime
|
41
native/jni/src/utils/time_keeper.h
Normal file
41
native/jni/src/utils/time_keeper.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef LATINIME_TIME_KEEPER_H
|
||||
#define LATINIME_TIME_KEEPER_H
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
class TimeKeeper {
|
||||
public:
|
||||
static void setCurrentTime();
|
||||
|
||||
static void startTestModeWithForceCurrentTime(const int currentTime);
|
||||
|
||||
static void stopTestMode();
|
||||
|
||||
static int peekCurrentTime() { return sCurrentTime; };
|
||||
|
||||
private:
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(TimeKeeper);
|
||||
|
||||
static int sCurrentTime;
|
||||
static bool sSetForTesting;
|
||||
};
|
||||
} // namespace latinime
|
||||
#endif /* LATINIME_TIME_KEEPER_H */
|
Loading…
Reference in a new issue