1 /* 2 * Copyright (C) 2013, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LATINIME_FORGETTING_CURVE_UTILS_H 18 #define LATINIME_FORGETTING_CURVE_UTILS_H 19 20 #include <vector> 21 22 #include "defines.h" 23 #include "suggest/policyimpl/dictionary/utils/historical_info.h" 24 25 namespace latinime { 26 27 class HeaderPolicy; 28 29 class ForgettingCurveUtils { 30 public: 31 static const HistoricalInfo createUpdatedHistoricalInfo( 32 const HistoricalInfo *const originalHistoricalInfo, const int newProbability, 33 const HistoricalInfo *const newHistoricalInfo, const HeaderPolicy *const headerPolicy); 34 35 static const HistoricalInfo createHistoricalInfoToSave( 36 const HistoricalInfo *const originalHistoricalInfo, 37 const HeaderPolicy *const headerPolicy); 38 39 static int decodeProbability(const HistoricalInfo *const historicalInfo, 40 const HeaderPolicy *const headerPolicy); 41 42 static int getProbability(const int encodedUnigramProbability, 43 const int encodedBigramProbability); 44 45 static bool needsToKeep(const HistoricalInfo *const historicalInfo, 46 const HeaderPolicy *const headerPolicy); 47 48 static bool needsToDecay(const bool mindsBlockByDecay, const int unigramCount, 49 const int bigramCount, const HeaderPolicy *const headerPolicy); 50 51 AK_FORCE_INLINE static int getUnigramCountHardLimit(const int maxUnigramCount) { 52 return static_cast<int>(static_cast<float>(maxUnigramCount) 53 * UNIGRAM_COUNT_HARD_LIMIT_WEIGHT); 54 } 55 56 AK_FORCE_INLINE static int getBigramCountHardLimit(const int maxBigramCount) { 57 return static_cast<int>(static_cast<float>(maxBigramCount) 58 * BIGRAM_COUNT_HARD_LIMIT_WEIGHT); 59 } 60 61 private: 62 DISALLOW_IMPLICIT_CONSTRUCTORS(ForgettingCurveUtils); 63 64 class ProbabilityTable { 65 public: 66 ProbabilityTable(); 67 68 int getProbability(const int tableId, const int level, 69 const int elapsedTimeStepCount) const { 70 return mTables[tableId][level][elapsedTimeStepCount]; 71 } 72 73 private: 74 DISALLOW_COPY_AND_ASSIGN(ProbabilityTable); 75 76 static const int PROBABILITY_TABLE_COUNT; 77 static const int WEAK_PROBABILITY_TABLE_ID; 78 static const int MODEST_PROBABILITY_TABLE_ID; 79 static const int STRONG_PROBABILITY_TABLE_ID; 80 static const int AGGRESSIVE_PROBABILITY_TABLE_ID; 81 82 static const int WEAK_MAX_PROBABILITY; 83 static const int MODEST_BASE_PROBABILITY; 84 static const int STRONG_BASE_PROBABILITY; 85 static const int AGGRESSIVE_BASE_PROBABILITY; 86 87 std::vector<std::vector<std::vector<int>>> mTables; 88 89 static int getBaseProbabilityForLevel(const int tableId, const int level); 90 }; 91 92 static const int MULTIPLIER_TWO_IN_PROBABILITY_SCALE; 93 static const int DECAY_INTERVAL_SECONDS; 94 95 static const int MAX_LEVEL; 96 static const int MIN_VISIBLE_LEVEL; 97 static const int MAX_ELAPSED_TIME_STEP_COUNT; 98 static const int DISCARD_LEVEL_ZERO_ENTRY_TIME_STEP_COUNT_THRESHOLD; 99 100 static const float UNIGRAM_COUNT_HARD_LIMIT_WEIGHT; 101 static const float BIGRAM_COUNT_HARD_LIMIT_WEIGHT; 102 103 static const ProbabilityTable sProbabilityTable; 104 105 static int backoff(const int unigramProbability); 106 static int getElapsedTimeStepCount(const int timestamp, const int durationToLevelDown); 107 static int clampToVisibleEntryLevelRange(const int level); 108 static int clampToValidLevelRange(const int level); 109 static int clampToValidCountRange(const int count, const HeaderPolicy *const headerPolicy); 110 static int clampToValidTimeStepCountRange(const int timeStepCount); 111 }; 112 } // namespace latinime 113 #endif /* LATINIME_FORGETTING_CURVE_UTILS_H */ 114