18 #ifndef NGRAM_NGRAM_BAYES_MODEL_MERGE_H_ 19 #define NGRAM_NGRAM_BAYES_MODEL_MERGE_H_ 26 #include <fst/mutable-fst.h> 35 typedef fst::StdArc::Label
Label;
41 Label backoff_label = 0,
43 :
NGramMerge<
fst::StdArc>(infst1, backoff_label, norm_eps, true) {
45 NGRAMERROR() <<
"NGramBayesModelMerge: Model 1 must be normalized to" 46 <<
" use smoothing in merging";
57 NGRAMERROR() <<
"NGramBayesModelMerge: Model 2 must be normalized to" 58 <<
" use smoothing in merging";
66 NGRAMERROR() <<
"NGramBayesModelMerge: Model merging failed";
73 fst::StdArc::Weight
MergeWeights(StateId s1, StateId s2, Label label,
74 fst::StdArc::Weight w1,
75 fst::StdArc::Weight w2,
bool in_fst1,
76 bool in_fst2)
const override {
78 return in_fst1 ? w1.Value() : w2.Value();
81 double alpha = StateAlpha(st);
83 return NegLogSum(w1.Value() + alpha, w2.Value() + beta);
89 double StateAlpha(StateId st)
const {
90 while (st >= state_alpha_.size()) state_alpha_.push_back(-1.0);
91 if (state_alpha_[st] < 0.0) {
99 state_alpha_[st] = w1 + alpha_;
103 if (state_alpha_[st] < fst::StdArc::Weight::Zero().Value())
104 state_alpha_[st] -= NegLogSum(w1 + alpha_, w2 + beta_);
106 return state_alpha_[st];
113 mutable std::vector<double> state_alpha_;
118 #endif // NGRAM_NGRAM_BAYES_MODEL_MERGE_H_ NGramBayesModelMerge(fst::StdMutableFst *infst1, Label backoff_label=0, double norm_eps=kNormEps)
bool CheckNormalization() const
const NGramModel< fst::StdArc > & NGram2() const
fst::StdArc::Weight MergeWeights(StateId s1, StateId s2, Label label, fst::StdArc::Weight w1, fst::StdArc::Weight w2, bool in_fst1, bool in_fst2) const override
StateId ExactMap2To1(StateId s2) const
void MergeNGramModels(const fst::StdFst &infst2, double alpha, double beta)
static double ScalarValue(Weight w)
double NegLogDiff(double a, double b) const
fst::StdArc::StateId StateId
Weight GetNGramCost(const std::vector< Label > &ngram) const
Label BackoffLabel() const
const std::vector< Label > & StateNGram(StateId state) const