Introduce HistoricalInfo.

Bug: 11073222
Change-Id: I5220656a333c93d10163bd3dff71d011c8f6d965
main
Keisuke Kuroyanagi 2013-12-09 12:08:06 +09:00
parent c980b1bfa7
commit 291db3d4b0
6 changed files with 104 additions and 67 deletions

View File

@ -48,7 +48,8 @@ const BigramEntry BigramDictContent::getBigramEntryAndAdvancePosition(
(encodedTargetTerminalId == Ver4DictConstants::INVALID_BIGRAM_TARGET_TERMINAL_ID) ? (encodedTargetTerminalId == Ver4DictConstants::INVALID_BIGRAM_TARGET_TERMINAL_ID) ?
Ver4DictConstants::NOT_A_TERMINAL_ID : encodedTargetTerminalId; Ver4DictConstants::NOT_A_TERMINAL_ID : encodedTargetTerminalId;
if (mHasHistoricalInfo) { if (mHasHistoricalInfo) {
return BigramEntry(hasNext, probability, timestamp, level, count, targetTerminalId); const HistoricalInfo historicalInfo(timestamp, level, count);
return BigramEntry(hasNext, probability, &historicalInfo, targetTerminalId);
} else { } else {
return BigramEntry(hasNext, probability, targetTerminalId); return BigramEntry(hasNext, probability, targetTerminalId);
} }
@ -72,22 +73,23 @@ bool BigramDictContent::writeBigramEntryAndAdvancePosition(
bigramEntryToWrite->getProbability()); bigramEntryToWrite->getProbability());
return false; return false;
} }
if (!bigramListBuffer->writeUintAndAdvancePosition(bigramEntryToWrite->getTimeStamp(), const HistoricalInfo *const historicalInfo = bigramEntryToWrite->getHistoricalInfo();
if (!bigramListBuffer->writeUintAndAdvancePosition(historicalInfo->getTimeStamp(),
Ver4DictConstants::TIME_STAMP_FIELD_SIZE, entryWritingPos)) { Ver4DictConstants::TIME_STAMP_FIELD_SIZE, entryWritingPos)) {
AKLOGE("Cannot write bigram timestamps. pos: %d, timestamp: %d", *entryWritingPos, AKLOGE("Cannot write bigram timestamps. pos: %d, timestamp: %d", *entryWritingPos,
bigramEntryToWrite->getTimeStamp()); historicalInfo->getTimeStamp());
return false; return false;
} }
if (!bigramListBuffer->writeUintAndAdvancePosition(bigramEntryToWrite->getLevel(), if (!bigramListBuffer->writeUintAndAdvancePosition(historicalInfo->getLevel(),
Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, entryWritingPos)) { Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, entryWritingPos)) {
AKLOGE("Cannot write bigram level. pos: %d, level: %d", *entryWritingPos, AKLOGE("Cannot write bigram level. pos: %d, level: %d", *entryWritingPos,
bigramEntryToWrite->getLevel()); historicalInfo->getLevel());
return false; return false;
} }
if (!bigramListBuffer->writeUintAndAdvancePosition(bigramEntryToWrite->getCount(), if (!bigramListBuffer->writeUintAndAdvancePosition(historicalInfo->getCount(),
Ver4DictConstants::WORD_COUNT_FIELD_SIZE, entryWritingPos)) { Ver4DictConstants::WORD_COUNT_FIELD_SIZE, entryWritingPos)) {
AKLOGE("Cannot write bigram count. pos: %d, count: %d", *entryWritingPos, AKLOGE("Cannot write bigram count. pos: %d, count: %d", *entryWritingPos,
bigramEntryToWrite->getCount()); historicalInfo->getCount());
return false; return false;
} }
} }

View File

@ -19,6 +19,7 @@
#include "defines.h" #include "defines.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
#include "suggest/policyimpl/dictionary/utils/historical_info.h"
namespace latinime { namespace latinime {
@ -26,38 +27,38 @@ class BigramEntry {
public: public:
BigramEntry(const BigramEntry& bigramEntry) BigramEntry(const BigramEntry& bigramEntry)
: mHasNext(bigramEntry.mHasNext), mProbability(bigramEntry.mProbability), : mHasNext(bigramEntry.mHasNext), mProbability(bigramEntry.mProbability),
mTimestamp(bigramEntry.mTimestamp), mLevel(bigramEntry.mLevel), mHistoricalInfo(), mTargetTerminalId(bigramEntry.mTargetTerminalId) {}
mCount(bigramEntry.mCount), mTargetTerminalId(bigramEntry.mTargetTerminalId) {}
// Entry with historical information. // Entry with historical information.
BigramEntry(const bool hasNext, const int probability, const int targetTerminalId) BigramEntry(const bool hasNext, const int probability, const int targetTerminalId)
: mHasNext(hasNext), mProbability(probability), : mHasNext(hasNext), mProbability(probability), mHistoricalInfo(),
mTimestamp(NOT_A_TIMESTAMP), mLevel(0), mCount(0),
mTargetTerminalId(targetTerminalId) {} mTargetTerminalId(targetTerminalId) {}
// Entry with historical information. // Entry with historical information.
BigramEntry(const bool hasNext, const int probability, const int timestamp, const int level, BigramEntry(const bool hasNext, const int probability,
const int count, const int targetTerminalId) const HistoricalInfo *const historicalInfo, const int targetTerminalId)
: mHasNext(hasNext), mProbability(probability), mTimestamp(timestamp), : mHasNext(hasNext), mProbability(probability), mHistoricalInfo(*historicalInfo),
mLevel(level), mCount(count), mTargetTerminalId(targetTerminalId) {} mTargetTerminalId(targetTerminalId) {}
const BigramEntry getInvalidatedEntry() const { const BigramEntry getInvalidatedEntry() const {
return updateTargetTerminalIdAndGetEntry(Ver4DictConstants::NOT_A_TERMINAL_ID); return updateTargetTerminalIdAndGetEntry(Ver4DictConstants::NOT_A_TERMINAL_ID);
} }
const BigramEntry updateHasNextAndGetEntry(const bool hasNext) const { const BigramEntry updateHasNextAndGetEntry(const bool hasNext) const {
return BigramEntry(hasNext, mProbability, mTimestamp, mLevel, mCount, return BigramEntry(hasNext, mProbability, &mHistoricalInfo, mTargetTerminalId);
mTargetTerminalId);
} }
const BigramEntry updateTargetTerminalIdAndGetEntry(const int newTargetTerminalId) const { const BigramEntry updateTargetTerminalIdAndGetEntry(const int newTargetTerminalId) const {
return BigramEntry(mHasNext, mProbability, mTimestamp, mLevel, mCount, return BigramEntry(mHasNext, mProbability, &mHistoricalInfo, newTargetTerminalId);
newTargetTerminalId);
} }
const BigramEntry updateProbabilityAndGetEntry(const int probability) const { const BigramEntry updateProbabilityAndGetEntry(const int probability) const {
return BigramEntry(mHasNext, probability, mTimestamp, mLevel, mCount, return BigramEntry(mHasNext, probability, &mHistoricalInfo, mTargetTerminalId);
mTargetTerminalId); }
const BigramEntry updateHistoricalInfoAndGetEntry(
const HistoricalInfo *const historicalInfo) const {
return BigramEntry(mHasNext, mProbability, historicalInfo, mTargetTerminalId);
} }
bool isValid() const { bool isValid() const {
@ -72,16 +73,8 @@ class BigramEntry {
return mProbability; return mProbability;
} }
int getTimeStamp() const { const HistoricalInfo *getHistoricalInfo() const {
return mTimestamp; return &mHistoricalInfo;
}
int getLevel() const {
return mLevel;
}
int getCount() const {
return mCount;
} }
int getTargetTerminalId() const { int getTargetTerminalId() const {
@ -95,9 +88,7 @@ class BigramEntry {
const bool mHasNext; const bool mHasNext;
const int mProbability; const int mProbability;
const int mTimestamp; const HistoricalInfo mHistoricalInfo;
const int mLevel;
const int mCount;
const int mTargetTerminalId; const int mTargetTerminalId;
}; };
} // namespace latinime } // namespace latinime

View File

@ -41,7 +41,8 @@ const ProbabilityEntry ProbabilityDictContent::getProbabilityEntry(const int ter
Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &entryPos); Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &entryPos);
const int count = buffer->readUintAndAdvancePosition( const int count = buffer->readUintAndAdvancePosition(
Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &entryPos); Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &entryPos);
return ProbabilityEntry(flags, probability, timestamp, level, count); const HistoricalInfo historicalInfo(timestamp, level, count);
return ProbabilityEntry(flags, probability, &historicalInfo);
} else { } else {
return ProbabilityEntry(flags, probability); return ProbabilityEntry(flags, probability);
} }
@ -136,17 +137,18 @@ bool ProbabilityDictContent::writeEntry(const ProbabilityEntry *const probabilit
return false; return false;
} }
if (mHasHistoricalInfo) { if (mHasHistoricalInfo) {
if (!bufferToWrite->writeUintAndAdvancePosition(probabilityEntry->getTimeStamp(), const HistoricalInfo *const historicalInfo = probabilityEntry->getHistoricalInfo();
if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getTimeStamp(),
Ver4DictConstants::TIME_STAMP_FIELD_SIZE, &writingPos)) { Ver4DictConstants::TIME_STAMP_FIELD_SIZE, &writingPos)) {
AKLOGE("Cannot write timestamp in probability dict content. pos: %d", writingPos); AKLOGE("Cannot write timestamp in probability dict content. pos: %d", writingPos);
return false; return false;
} }
if (!bufferToWrite->writeUintAndAdvancePosition(probabilityEntry->getLevel(), if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getLevel(),
Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &writingPos)) { Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &writingPos)) {
AKLOGE("Cannot write level in probability dict content. pos: %d", writingPos); AKLOGE("Cannot write level in probability dict content. pos: %d", writingPos);
return false; return false;
} }
if (!bufferToWrite->writeUintAndAdvancePosition(probabilityEntry->getCount(), if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getCount(),
Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &writingPos)) { Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &writingPos)) {
AKLOGE("Cannot write count in probability dict content. pos: %d", writingPos); AKLOGE("Cannot write count in probability dict content. pos: %d", writingPos);
return false; return false;

View File

@ -19,6 +19,7 @@
#include "defines.h" #include "defines.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
#include "suggest/policyimpl/dictionary/utils/historical_info.h"
namespace latinime { namespace latinime {
@ -26,32 +27,28 @@ class ProbabilityEntry {
public: public:
ProbabilityEntry(const ProbabilityEntry &probabilityEntry) ProbabilityEntry(const ProbabilityEntry &probabilityEntry)
: mFlags(probabilityEntry.mFlags), mProbability(probabilityEntry.mProbability), : mFlags(probabilityEntry.mFlags), mProbability(probabilityEntry.mProbability),
mTimestamp(probabilityEntry.mTimestamp), mLevel(probabilityEntry.mLevel), mHistoricalInfo(probabilityEntry.mHistoricalInfo) {}
mCount(probabilityEntry.mCount) {}
// Dummy entry // Dummy entry
ProbabilityEntry() ProbabilityEntry()
: mFlags(0), mProbability(NOT_A_PROBABILITY), : mFlags(0), mProbability(NOT_A_PROBABILITY), mHistoricalInfo() {}
mTimestamp(NOT_A_TIMESTAMP), mLevel(0), mCount(0) {}
// Entry without historical information // Entry without historical information
ProbabilityEntry(const int flags, const int probability) ProbabilityEntry(const int flags, const int probability)
: mFlags(flags), mProbability(probability), : mFlags(flags), mProbability(probability), mHistoricalInfo() {}
mTimestamp(NOT_A_TIMESTAMP), mLevel(0), mCount(0) {}
// Entry with historical information. // Entry with historical information.
ProbabilityEntry(const int flags, const int probability, const int timestamp, ProbabilityEntry(const int flags, const int probability,
const int level, const int count) const HistoricalInfo *const historicalInfo)
: mFlags(flags), mProbability(probability), mTimestamp(timestamp), mLevel(level), : mFlags(flags), mProbability(probability), mHistoricalInfo(*historicalInfo) {}
mCount(count) {}
const ProbabilityEntry createEntryWithUpdatedProbability(const int probability) const { const ProbabilityEntry createEntryWithUpdatedProbability(const int probability) const {
return ProbabilityEntry(mFlags, probability, mTimestamp, mLevel, mCount); return ProbabilityEntry(mFlags, probability, &mHistoricalInfo);
} }
const ProbabilityEntry createEntryWithUpdatedHistoricalInfo(const int timestamp, const ProbabilityEntry createEntryWithUpdatedHistoricalInfo(
const int level, const int count) const { const HistoricalInfo *const historicalInfo) const {
return ProbabilityEntry(mFlags, mProbability, timestamp, level, count); return ProbabilityEntry(mFlags, mProbability, historicalInfo);
} }
int getFlags() const { int getFlags() const {
@ -62,16 +59,8 @@ class ProbabilityEntry {
return mProbability; return mProbability;
} }
int getTimeStamp() const { const HistoricalInfo *getHistoricalInfo() const {
return mTimestamp; return &mHistoricalInfo;
}
int getLevel() const {
return mLevel;
}
int getCount() const {
return mCount;
} }
private: private:
@ -80,9 +69,7 @@ class ProbabilityEntry {
const int mFlags; const int mFlags;
const int mProbability; const int mProbability;
const int mTimestamp; const HistoricalInfo mHistoricalInfo;
const int mLevel;
const int mCount;
}; };
} // namespace latinime } // namespace latinime
#endif /* LATINIME_PROBABILITY_ENTRY_H */ #endif /* LATINIME_PROBABILITY_ENTRY_H */

View File

@ -305,6 +305,7 @@ const UnigramProperty Ver4PatriciaTriePolicy::getUnigramProperty(const int *cons
const ProbabilityEntry probabilityEntry = const ProbabilityEntry probabilityEntry =
mBuffers.get()->getProbabilityDictContent()->getProbabilityEntry( mBuffers.get()->getProbabilityDictContent()->getProbabilityEntry(
ptNodeParams.getTerminalId()); ptNodeParams.getTerminalId());
const HistoricalInfo *const historicalInfo = probabilityEntry.getHistoricalInfo();
// Fetch shortcut information. // Fetch shortcut information.
std::vector<std::vector<int> > shortcutTargets; std::vector<std::vector<int> > shortcutTargets;
std::vector<int> shortcutProbabilities; std::vector<int> shortcutProbabilities;
@ -327,8 +328,8 @@ const UnigramProperty Ver4PatriciaTriePolicy::getUnigramProperty(const int *cons
return UnigramProperty(ptNodeParams.getCodePoints(), ptNodeParams.getCodePointCount(), return UnigramProperty(ptNodeParams.getCodePoints(), ptNodeParams.getCodePointCount(),
ptNodeParams.isNotAWord(), ptNodeParams.isBlacklisted(), ptNodeParams.hasBigrams(), ptNodeParams.isNotAWord(), ptNodeParams.isBlacklisted(), ptNodeParams.hasBigrams(),
ptNodeParams.hasShortcutTargets(), ptNodeParams.getProbability(), ptNodeParams.hasShortcutTargets(), ptNodeParams.getProbability(),
probabilityEntry.getTimeStamp(), probabilityEntry.getLevel(), historicalInfo->getTimeStamp(), historicalInfo->getLevel(),
probabilityEntry.getCount(), &shortcutTargets, &shortcutProbabilities); historicalInfo->getCount(), &shortcutTargets, &shortcutProbabilities);
} }
} // namespace latinime } // namespace latinime

View File

@ -0,0 +1,54 @@
/*
* 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_HISTORICAL_INFO_H
#define LATINIME_HISTORICAL_INFO_H
#include "defines.h"
namespace latinime {
class HistoricalInfo {
public:
// Invalid historical info.
HistoricalInfo()
: mTimestamp(NOT_A_TIMESTAMP), mLevel(0), mCount(0) {}
HistoricalInfo(const int timestamp, const int level, const int count)
: mTimestamp(timestamp), mLevel(level), mCount(count) {}
int getTimeStamp() const {
return mTimestamp;
}
int getLevel() const {
return mLevel;
}
int getCount() const {
return mCount;
}
private:
// Copy constructor is public to use this class as a type of return value.
DISALLOW_ASSIGNMENT_OPERATOR(HistoricalInfo);
const int mTimestamp;
const int mLevel;
const int mCount;
};
} // namespace latinime
#endif /* LATINIME_HISTORICAL_INFO_H */