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