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_PROBABILITY_UTILS_H 18 #define LATINIME_PROBABILITY_UTILS_H 19 20 #include <stdint.h> 21 22 #include "defines.h" 23 24 namespace latinime { 25 26 class ProbabilityUtils { 27 public: 28 static AK_FORCE_INLINE int backoff(const int unigramProbability) { 29 return unigramProbability; 30 // For some reason, applying the backoff weight gives bad results in tests. To apply the 31 // backoff weight, we divide the probability by 2, which in our storing format means 32 // decreasing the score by 8. 33 // TODO: figure out what's wrong with this. 34 // return unigramProbability > 8 ? 35 // unigramProbability - 8 : (0 == unigramProbability ? 0 : 8); 36 } 37 38 static AK_FORCE_INLINE int computeProbabilityForBigram( 39 const int unigramProbability, const int bigramProbability) { 40 // We divide the range [unigramProbability..255] in 16.5 steps - in other words, we want 41 // the unigram probability to be the median value of the 17th step from the top. A value of 42 // 0 for the bigram probability represents the middle of the 16th step from the top, 43 // while a value of 15 represents the middle of the top step. 44 // See makedict.BinaryDictEncoder#makeBigramFlags for details. 45 const float stepSize = static_cast<float>(MAX_PROBABILITY - unigramProbability) 46 / (1.5f + MAX_BIGRAM_ENCODED_PROBABILITY); 47 return unigramProbability 48 + static_cast<int>(static_cast<float>(bigramProbability + 1) * stepSize); 49 } 50 51 private: 52 DISALLOW_IMPLICIT_CONSTRUCTORS(ProbabilityUtils); 53 }; 54 } 55 #endif /* LATINIME_PROBABILITY_UTILS_H */ 56