18 #ifndef NGRAM_NGRAM_REPLACE_MERGE_H_ 19 #define NGRAM_NGRAM_REPLACE_MERGE_H_ 24 #include <fst/connect.h> 34 typedef fst::StdArc::Label
Label;
41 Label backoff_label = 0,
43 bool check_consistency =
false)
44 :
NGramMerge(infst1, backoff_label, norm_eps, check_consistency) {}
52 int max_replace_order = -1,
bool norm =
false) {
56 infst2,
false, max_replace_order)) {
57 NGRAMERROR() <<
"NGramReplaceMerge: Model merging failed";
65 NGRAMERROR() <<
"NGramReplaceMerge: Merged model not fully normalized";
75 bool in_fst1,
bool in_fst2)
const override {
77 NGRAMERROR() <<
"n-grams in original model must be a subset of n-grams " 78 "in the updating model.";
85 double NormWeight(StateId st,
bool in_fst1,
bool in_fst2)
const override {
96 std::vector<StateId> dest_states(
NumStates());
97 for (StateId st = 0; st <
NumStates(); ++st) {
100 dest_states[st] =
GetFst().NumArcs(st) == 0 &&
106 for (StateId st = 0; st <
NumStates(); ++st) {
108 if (dest_states[st] != st)
continue;
110 for (fst::MutableArcIterator<fst::MutableFst<fst::StdArc>>
112 !aiter.Done(); aiter.Next()) {
113 fst::StdArc arc = aiter.Value();
114 if (dest_states[arc.nextstate] != arc.nextstate) {
116 if (dest_states[arc.nextstate] < 0) {
118 UpdateDestStates(bo, arc, &dest_states);
120 if (dest_states[arc.nextstate] < 0) {
125 arc.nextstate = dest_states[arc.nextstate];
137 void UpdateDestStates(StateId st,
const fst::StdArc &in_arc,
138 std::vector<StateId> *dest_states) {
139 if ((*dest_states)[in_arc.nextstate] >= 0) {
140 NGRAMERROR() <<
"Destination state already set.";
149 fst::Matcher<fst::StdFst> matcher(
GetFst(), fst::MATCH_INPUT);
150 matcher.SetState(st);
151 if (!matcher.Find(in_arc.ilabel)) {
152 NGRAMERROR() <<
"Could not find n-gram arc at backoff state.";
156 fst::StdArc arc = matcher.Value();
157 if ((*dest_states)[arc.nextstate] < 0) {
159 UpdateDestStates(bo, arc, dest_states);
161 if ((*dest_states)[arc.nextstate] < 0) {
166 if (arc.nextstate != in_arc.nextstate) {
168 (*dest_states)[in_arc.nextstate] = (*dest_states)[arc.nextstate];
176 #endif // NGRAM_NGRAM_REPLACE_MERGE_H_ fst::MutableFst< fst::StdArc > * GetMutableFst()
StateId GetBackoff(StateId st, Weight *bocost) const
NGramReplaceMerge(fst::StdMutableFst *infst1, Label backoff_label=0, double norm_eps=kNormEps, bool check_consistency=false)
bool CheckNormalization() const
fst::StdArc::Weight Weight
fst::StdArc::StateId StateId
StateId NumStates() const
bool MaxOrderOkay(int order) const override
static double ScalarValue(Weight w)
Weight MergeWeights(StateId s1, StateId s2, Label label, Weight w1, Weight w2, bool in_fst1, bool in_fst2) const override
StateId UnigramState() const
double NormWeight(StateId st, bool in_fst1, bool in_fst2) const override
const fst::Fst< fst::StdArc > & GetFst() const
void MergeNGramModels(const fst::StdFst &infst2, int max_replace_order=-1, bool norm=false)