Joshua
open source statistical hierarchical phrase-based machine translation system
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/kenlm/lm/blank.hh
00001 #ifndef LM_BLANK_H
00002 #define LM_BLANK_H
00003 
00004 #include <limits>
00005 #include <stdint.h>
00006 #include <cmath>
00007 
00008 namespace lm {
00009 namespace ngram {
00010 
00011 /* Suppose "foo bar" appears with zero backoff but there is no trigram
00012  * beginning with these words.  Then, when scoring "foo bar", the model could
00013  * return out_state containing "bar" or even null context if "bar" also has no
00014  * backoff and is never followed by another word.  Then the backoff is set to
00015  * kNoExtensionBackoff.  If the n-gram might be extended, then out_state must
00016  * contain the full n-gram, in which case kExtensionBackoff is set.  In any
00017  * case, if an n-gram has non-zero backoff, the full state is returned so
00018  * backoff can be properly charged.
00019  * These differ only in sign bit because the backoff is in fact zero in either
00020  * case.
00021  */
00022 const float kNoExtensionBackoff = -0.0;
00023 const float kExtensionBackoff = 0.0;
00024 const uint64_t kNoExtensionQuant = 0;
00025 const uint64_t kExtensionQuant = 1;
00026 
00027 inline void SetExtension(float &backoff) {
00028   if (backoff == kNoExtensionBackoff) backoff = kExtensionBackoff;
00029 }
00030 
00031 // This compiles down nicely.
00032 inline bool HasExtension(const float &backoff) {
00033   typedef union { float f; uint32_t i; } UnionValue;
00034   UnionValue compare, interpret;
00035   compare.f = kNoExtensionBackoff;
00036   interpret.f = backoff;
00037   return compare.i != interpret.i;
00038 }
00039 
00040 } // namespace ngram
00041 } // namespace lm
00042 #endif // LM_BLANK_H