00001 #ifndef LM_VIRTUAL_INTERFACE_H
00002 #define LM_VIRTUAL_INTERFACE_H
00003
00004 #include "lm/return.hh"
00005 #include "lm/word_index.hh"
00006 #include "util/string_piece.hh"
00007
00008 #include <string>
00009 #include <cstring>
00010
00011 namespace lm {
00012 namespace base {
00013
00014 template <class T, class U, class V> class ModelFacade;
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class Vocabulary {
00030 public:
00031 virtual ~Vocabulary();
00032
00033 WordIndex BeginSentence() const { return begin_sentence_; }
00034 WordIndex EndSentence() const { return end_sentence_; }
00035 WordIndex NotFound() const { return not_found_; }
00036
00037
00038
00039
00040
00041 virtual WordIndex Index(const StringPiece &str) const = 0;
00042 virtual WordIndex Index(const std::string &str) const {
00043 return Index(StringPiece(str));
00044 }
00045 virtual WordIndex Index(const char *str) const {
00046 return Index(StringPiece(str));
00047 }
00048
00049 protected:
00050
00051 Vocabulary() {}
00052
00053 Vocabulary(WordIndex begin_sentence, WordIndex end_sentence, WordIndex not_found) {
00054 SetSpecial(begin_sentence, end_sentence, not_found);
00055 }
00056
00057 void SetSpecial(WordIndex begin_sentence, WordIndex end_sentence, WordIndex not_found);
00058
00059 WordIndex begin_sentence_, end_sentence_, not_found_;
00060
00061 private:
00062
00063
00064 Vocabulary(const Vocabulary &);
00065 Vocabulary &operator=(const Vocabulary &);
00066 };
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 class Model {
00118 public:
00119 virtual ~Model();
00120
00121 size_t StateSize() const { return state_size_; }
00122 const void *BeginSentenceMemory() const { return begin_sentence_memory_; }
00123 void BeginSentenceWrite(void *to) const { memcpy(to, begin_sentence_memory_, StateSize()); }
00124 const void *NullContextMemory() const { return null_context_memory_; }
00125 void NullContextWrite(void *to) const { memcpy(to, null_context_memory_, StateSize()); }
00126
00127
00128 virtual float BaseScore(const void *in_state, const WordIndex new_word, void *out_state) const = 0;
00129
00130
00131 virtual FullScoreReturn BaseFullScore(const void *in_state, const WordIndex new_word, void *out_state) const = 0;
00132
00133
00134 virtual FullScoreReturn BaseFullScoreForgotState(const WordIndex *context_rbegin, const WordIndex *context_rend, const WordIndex new_word, void *out_state) const = 0;
00135
00136 unsigned char Order() const { return order_; }
00137
00138 const Vocabulary &BaseVocabulary() const { return *base_vocab_; }
00139
00140 private:
00141 template <class T, class U, class V> friend class ModelFacade;
00142 explicit Model(size_t state_size) : state_size_(state_size) {}
00143
00144 const size_t state_size_;
00145 const void *begin_sentence_memory_, *null_context_memory_;
00146
00147 const Vocabulary *base_vocab_;
00148
00149 unsigned char order_;
00150
00151
00152
00153 Model(const Model &);
00154 Model &operator=(const Model &);
00155 };
00156
00157 }
00158 }
00159
00160 #endif // LM_VIRTUAL_INTERFACE_H