00001 #ifndef LM_VALUE_H
00002 #define LM_VALUE_H
00003
00004 #include "lm/config.hh"
00005 #include "lm/model_type.hh"
00006 #include "lm/value_build.hh"
00007 #include "lm/weights.hh"
00008 #include "util/bit_packing.hh"
00009
00010 #include <stdint.h>
00011
00012 namespace lm {
00013 namespace ngram {
00014
00015
00016 template <class Weights> class GenericProbingProxy {
00017 public:
00018 explicit GenericProbingProxy(const Weights &to) : to_(&to) {}
00019
00020 GenericProbingProxy() : to_(0) {}
00021
00022 bool Found() const { return to_ != 0; }
00023
00024 float Prob() const {
00025 util::FloatEnc enc;
00026 enc.f = to_->prob;
00027 enc.i |= util::kSignBit;
00028 return enc.f;
00029 }
00030
00031 float Backoff() const { return to_->backoff; }
00032
00033 bool IndependentLeft() const {
00034 util::FloatEnc enc;
00035 enc.f = to_->prob;
00036 return enc.i & util::kSignBit;
00037 }
00038
00039 protected:
00040 const Weights *to_;
00041 };
00042
00043
00044 template <class Weights> class GenericTrieUnigramProxy {
00045 public:
00046 explicit GenericTrieUnigramProxy(const Weights &to) : to_(&to) {}
00047
00048 GenericTrieUnigramProxy() : to_(0) {}
00049
00050 bool Found() const { return to_ != 0; }
00051 float Prob() const { return to_->prob; }
00052 float Backoff() const { return to_->backoff; }
00053 float Rest() const { return Prob(); }
00054
00055 protected:
00056 const Weights *to_;
00057 };
00058
00059 struct BackoffValue {
00060 typedef ProbBackoff Weights;
00061 static const ModelType kProbingModelType = PROBING;
00062
00063 class ProbingProxy : public GenericProbingProxy<Weights> {
00064 public:
00065 explicit ProbingProxy(const Weights &to) : GenericProbingProxy<Weights>(to) {}
00066 ProbingProxy() {}
00067 float Rest() const { return Prob(); }
00068 };
00069
00070 class TrieUnigramProxy : public GenericTrieUnigramProxy<Weights> {
00071 public:
00072 explicit TrieUnigramProxy(const Weights &to) : GenericTrieUnigramProxy<Weights>(to) {}
00073 TrieUnigramProxy() {}
00074 float Rest() const { return Prob(); }
00075 };
00076
00077 struct ProbingEntry {
00078 typedef uint64_t Key;
00079 typedef Weights Value;
00080 uint64_t key;
00081 ProbBackoff value;
00082 uint64_t GetKey() const { return key; }
00083 };
00084
00085 struct TrieUnigramValue {
00086 Weights weights;
00087 uint64_t next;
00088 uint64_t Next() const { return next; }
00089 };
00090
00091 const static bool kDifferentRest = false;
00092
00093 template <class Model, class C> void Callback(const Config &, unsigned int, typename Model::Vocabulary &, C &callback) {
00094 NoRestBuild build;
00095 callback(build);
00096 }
00097 };
00098
00099 struct RestValue {
00100 typedef RestWeights Weights;
00101 static const ModelType kProbingModelType = REST_PROBING;
00102
00103 class ProbingProxy : public GenericProbingProxy<RestWeights> {
00104 public:
00105 explicit ProbingProxy(const Weights &to) : GenericProbingProxy<RestWeights>(to) {}
00106 ProbingProxy() {}
00107 float Rest() const { return to_->rest; }
00108 };
00109
00110 class TrieUnigramProxy : public GenericTrieUnigramProxy<Weights> {
00111 public:
00112 explicit TrieUnigramProxy(const Weights &to) : GenericTrieUnigramProxy<Weights>(to) {}
00113 TrieUnigramProxy() {}
00114 float Rest() const { return to_->rest; }
00115 };
00116
00117
00118 #pragma pack(push)
00119 #pragma pack(4)
00120 struct ProbingEntry {
00121 typedef uint64_t Key;
00122 typedef Weights Value;
00123 Key key;
00124 Value value;
00125 Key GetKey() const { return key; }
00126 };
00127
00128 struct TrieUnigramValue {
00129 Weights weights;
00130 uint64_t next;
00131 uint64_t Next() const { return next; }
00132 };
00133 #pragma pack(pop)
00134
00135 const static bool kDifferentRest = true;
00136
00137 template <class Model, class C> void Callback(const Config &config, unsigned int order, typename Model::Vocabulary &vocab, C &callback) {
00138 switch (config.rest_function) {
00139 case Config::REST_MAX:
00140 {
00141 MaxRestBuild build;
00142 callback(build);
00143 }
00144 break;
00145 case Config::REST_LOWER:
00146 {
00147 LowerRestBuild<Model> build(config, order, vocab);
00148 callback(build);
00149 }
00150 break;
00151 }
00152 }
00153 };
00154
00155 }
00156 }
00157
00158 #endif // LM_VALUE_H