Joshua
open source statistical hierarchical phrase-based machine translation system
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/joshua/decoder/ff/lm/kenlm/lm/facade.hh
00001 #ifndef LM_FACADE__
00002 #define LM_FACADE__
00003 
00004 #include "lm/virtual_interface.hh"
00005 #include "util/string_piece.hh"
00006 
00007 #include <string>
00008 
00009 namespace lm {
00010 namespace base {
00011 
00012 // Common model interface that depends on knowing the specific classes. 
00013 // Curiously recurring template pattern.  
00014 template <class Child, class StateT, class VocabularyT> class ModelFacade : public Model {
00015   public:
00016     typedef StateT State;
00017     typedef VocabularyT Vocabulary;
00018 
00019     // Default Score function calls FullScore.  Model can override this.  
00020     float Score(const State &in_state, const WordIndex new_word, State &out_state) const {
00021       return static_cast<const Child*>(this)->FullScore(in_state, new_word, out_state).prob;
00022     }
00023 
00024     /* Translate from void* to State */
00025     FullScoreReturn FullScore(const void *in_state, const WordIndex new_word, void *out_state) const {
00026       return static_cast<const Child*>(this)->FullScore(
00027           *reinterpret_cast<const State*>(in_state),
00028           new_word,
00029           *reinterpret_cast<State*>(out_state));
00030     }
00031     float Score(const void *in_state, const WordIndex new_word, void *out_state) const {
00032       return static_cast<const Child*>(this)->Score(
00033           *reinterpret_cast<const State*>(in_state),
00034           new_word,
00035           *reinterpret_cast<State*>(out_state));
00036     }
00037 
00038     const State &BeginSentenceState() const { return begin_sentence_; }
00039     const State &NullContextState() const { return null_context_; }
00040     const Vocabulary &GetVocabulary() const { return *static_cast<const Vocabulary*>(&BaseVocabulary()); }
00041 
00042   protected:
00043     ModelFacade() : Model(sizeof(State)) {}
00044 
00045     virtual ~ModelFacade() {}
00046 
00047     // begin_sentence and null_context can disappear after.  vocab should stay.  
00048     void Init(const State &begin_sentence, const State &null_context, const Vocabulary &vocab, unsigned char order) {
00049       begin_sentence_ = begin_sentence;
00050       null_context_ = null_context;
00051       begin_sentence_memory_ = &begin_sentence_;
00052       null_context_memory_ = &null_context_;
00053       base_vocab_ = &vocab;
00054       order_ = order;
00055     }
00056 
00057   private:
00058     State begin_sentence_, null_context_;
00059 };
00060 
00061 } // mamespace base
00062 } // namespace lm
00063 
00064 #endif // LM_FACADE__