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 24 namespace latinime { 25 26 class DictionaryHeaderStructurePolicy; 27 28 // TODO: Check the elapsed time and decrease the probability depending on the time. Time field is 29 // required to introduced to each terminal PtNode and bigram entry. 30 // TODO: Quit using bigram probability to indicate the delta. 31 class ForgettingCurveUtils { 32 public: 33 class TimeKeeper { 34 public: 35 TimeKeeper() : mCurrentTime(0) {} 36 void setCurrentTime(); 37 int peekCurrentTime() const { return mCurrentTime; }; 38 39 private: 40 DISALLOW_COPY_AND_ASSIGN(TimeKeeper); 41 42 int mCurrentTime; 43 }; 44 45 static const int MAX_UNIGRAM_COUNT; 46 static const int MAX_UNIGRAM_COUNT_AFTER_GC; 47 static const int MAX_BIGRAM_COUNT; 48 static const int MAX_BIGRAM_COUNT_AFTER_GC; 49 50 static TimeKeeper sTimeKeeper; 51 52 static int getProbability(const int encodedUnigramProbability, 53 const int encodedBigramProbability); 54 55 static int getUpdatedEncodedProbability(const int originalEncodedProbability, 56 const int newProbability); 57 58 static int isValidEncodedProbability(const int encodedProbability); 59 60 static int getEncodedProbabilityToSave(const int encodedProbability, 61 const DictionaryHeaderStructurePolicy *const headerPolicy); 62 63 static bool needsToDecay(const bool mindsBlockByDecay, const int unigramCount, 64 const int bigramCount, const DictionaryHeaderStructurePolicy *const headerPolicy); 65 66 private: 67 DISALLOW_IMPLICIT_CONSTRUCTORS(ForgettingCurveUtils); 68 69 class ProbabilityTable { 70 public: 71 ProbabilityTable(); 72 73 int getProbability(const int encodedProbability) const { 74 if (encodedProbability < 0 || encodedProbability > static_cast<int>(mTable.size())) { 75 return NOT_A_PROBABILITY; 76 } 77 return mTable[encodedProbability]; 78 } 79 80 private: 81 DISALLOW_COPY_AND_ASSIGN(ProbabilityTable); 82 83 std::vector<int> mTable; 84 }; 85 86 static const int MAX_COMPUTED_PROBABILITY; 87 static const int MAX_ENCODED_PROBABILITY; 88 static const int MIN_VALID_ENCODED_PROBABILITY; 89 static const int ENCODED_PROBABILITY_STEP; 90 static const float MIN_PROBABILITY_TO_DECAY; 91 static const int DECAY_INTERVAL_SECONDS; 92 93 static const ProbabilityTable sProbabilityTable; 94 95 static int decodeProbability(const int encodedProbability); 96 97 static int backoff(const int unigramProbability); 98 }; 99 } // namespace latinime 100 #endif /* LATINIME_FORGETTING_CURVE_UTILS_H */ 101