Joshua
open source statistical hierarchical phrase-based machine translation system
|
00001 #ifndef LM_FACADE_H 00002 #define LM_FACADE_H 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 /* Translate from void* to State */ 00020 FullScoreReturn BaseFullScore(const void *in_state, const WordIndex new_word, void *out_state) const { 00021 return static_cast<const Child*>(this)->FullScore( 00022 *reinterpret_cast<const State*>(in_state), 00023 new_word, 00024 *reinterpret_cast<State*>(out_state)); 00025 } 00026 00027 FullScoreReturn BaseFullScoreForgotState(const WordIndex *context_rbegin, const WordIndex *context_rend, const WordIndex new_word, void *out_state) const { 00028 return static_cast<const Child*>(this)->FullScoreForgotState( 00029 context_rbegin, 00030 context_rend, 00031 new_word, 00032 *reinterpret_cast<State*>(out_state)); 00033 } 00034 00035 // Default Score function calls FullScore. Model can override this. 00036 float Score(const State &in_state, const WordIndex new_word, State &out_state) const { 00037 return static_cast<const Child*>(this)->FullScore(in_state, new_word, out_state).prob; 00038 } 00039 00040 float BaseScore(const void *in_state, const WordIndex new_word, void *out_state) const { 00041 return static_cast<const Child*>(this)->Score( 00042 *reinterpret_cast<const State*>(in_state), 00043 new_word, 00044 *reinterpret_cast<State*>(out_state)); 00045 } 00046 00047 const State &BeginSentenceState() const { return begin_sentence_; } 00048 const State &NullContextState() const { return null_context_; } 00049 const Vocabulary &GetVocabulary() const { return *static_cast<const Vocabulary*>(&BaseVocabulary()); } 00050 00051 protected: 00052 ModelFacade() : Model(sizeof(State)) {} 00053 00054 virtual ~ModelFacade() {} 00055 00056 // begin_sentence and null_context can disappear after. vocab should stay. 00057 void Init(const State &begin_sentence, const State &null_context, const Vocabulary &vocab, unsigned char order) { 00058 begin_sentence_ = begin_sentence; 00059 null_context_ = null_context; 00060 begin_sentence_memory_ = &begin_sentence_; 00061 null_context_memory_ = &null_context_; 00062 base_vocab_ = &vocab; 00063 order_ = order; 00064 } 00065 00066 private: 00067 State begin_sentence_, null_context_; 00068 }; 00069 00070 } // mamespace base 00071 } // namespace lm 00072 00073 #endif // LM_FACADE_H