46 StdArc dummy_arc(0, 0, StdWeight::One(), start_state);
53 int32 max_num_frames) {
55 "You must call InitDecoding() before AdvanceDecoding()");
62 int32 target_frames_decoded = num_frames_ready;
63 if (max_num_frames >= 0)
64 target_frames_decoded = std::min(target_frames_decoded,
77 for (unordered_map<StateId, Token*>::const_iterator iter =
cur_toks_.begin();
80 if (iter->second->cost_ != std::numeric_limits<BaseFloat>::infinity() &&
81 fst_.Final(iter->first) != StdWeight::Zero())
90 double infinity = std::numeric_limits<double>::infinity();
93 double best_cost = infinity,
94 best_cost_with_final = infinity;
95 for (unordered_map<StateId, Token*>::const_iterator iter =
cur_toks_.begin();
100 best_cost = std::min(best_cost, iter->second->cost_);
101 best_cost_with_final = std::min(best_cost_with_final,
102 iter->second->cost_ +
103 fst_.Final(iter->first).Value());
105 BaseFloat extra_cost = best_cost_with_final - best_cost;
106 if (extra_cost != extra_cost) {
108 KALDI_WARN <<
"Found NaN (likely search failure in decoding)";
118 fst_out->DeleteStates();
119 Token *best_tok = NULL;
122 for (unordered_map<StateId, Token*>::const_iterator iter =
cur_toks_.begin();
125 if (best_tok == NULL || *best_tok < *(iter->second) )
126 best_tok = iter->second;
128 double infinity =std::numeric_limits<double>::infinity(),
129 best_cost = infinity;
130 for (unordered_map<StateId, Token*>::const_iterator iter =
cur_toks_.begin();
133 double this_cost = iter->second->
cost_ +
fst_.Final(iter->first).Value();
134 if (this_cost != infinity && this_cost < best_cost) {
135 best_cost = this_cost;
136 best_tok = iter->second;
140 if (best_tok == NULL)
return false;
142 std::vector<LatticeArc> arcs_reverse;
143 for (
Token *tok = best_tok; tok != NULL; tok = tok->
prev_)
144 arcs_reverse.push_back(tok->arc_);
146 arcs_reverse.pop_back();
148 StateId cur_state = fst_out->AddState();
149 fst_out->SetStart(cur_state);
150 for (ssize_t
i = static_cast<ssize_t>(arcs_reverse.size())-1;
i >= 0;
i--) {
152 arc.nextstate = fst_out->AddState();
153 fst_out->AddArc(cur_state, arc);
154 cur_state = arc.nextstate;
156 if (is_final && use_final_probs)
157 fst_out->SetFinal(cur_state,
171 double cutoff = std::numeric_limits<BaseFloat>::infinity();
172 for (unordered_map<StateId, Token*>::iterator iter =
prev_toks_.begin();
176 Token *tok = iter->second;
178 for (fst::ArcIterator<fst::Fst<StdArc> > aiter(
fst_, state);
181 const StdArc &arc = aiter.Value();
182 if (arc.ilabel != 0) {
184 double total_cost = tok->
cost_ + arc.weight.Value() + acoustic_cost;
186 if (total_cost >= cutoff)
continue;
187 if (total_cost +
beam_ < cutoff)
188 cutoff = total_cost +
beam_;
189 Token *new_tok =
new Token(arc, acoustic_cost, tok);
190 unordered_map<StateId, Token*>::iterator find_iter
195 if ( *(find_iter->second) < *new_tok ) {
197 find_iter->second = new_tok;
211 std::vector<StateId> queue;
212 double infinity = std::numeric_limits<double>::infinity();
213 double best_cost = infinity;
214 for (unordered_map<StateId, Token*>::iterator iter =
cur_toks_.begin();
217 queue.push_back(iter->first);
218 best_cost = std::min(best_cost, iter->second->cost_);
220 double cutoff = best_cost +
beam_;
222 while (!queue.empty()) {
227 for (fst::ArcIterator<fst::Fst<StdArc> > aiter(
fst_, state);
230 const StdArc &arc = aiter.Value();
231 if (arc.ilabel == 0) {
233 Token *new_tok =
new Token(arc, acoustic_cost, tok);
234 if (new_tok->
cost_ > cutoff) {
237 unordered_map<StateId, Token*>::iterator find_iter
241 queue.push_back(arc.nextstate);
243 if ( *(find_iter->second) < *new_tok ) {
245 find_iter->second = new_tok;
246 queue.push_back(arc.nextstate);
259 for (unordered_map<StateId, Token*>::iterator iter = toks.begin();
260 iter != toks.end(); ++iter) {
272 double best_cost = std::numeric_limits<double>::infinity();
273 for (unordered_map<StateId, Token*>::iterator iter = toks->begin();
274 iter != toks->end(); ++iter)
275 best_cost = std::min(best_cost, iter->second->cost_);
276 std::vector<StateId> retained;
277 double cutoff = best_cost + beam;
278 for (unordered_map<StateId, Token*>::iterator iter = toks->begin();
279 iter != toks->end(); ++iter) {
280 if (iter->second->cost_ < cutoff)
281 retained.push_back(iter->first);
285 unordered_map<StateId, Token*> tmp;
286 for (
size_t i = 0;
i < retained.size();
i++) {
287 tmp[retained[
i]] = (*toks)[retained[
i]];
289 KALDI_VLOG(2) <<
"Pruned to " << (retained.size()) <<
" toks.\n";
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
fst::ArcTpl< LatticeWeight > LatticeArc
void ProcessEmitting(DecodableInterface *decodable)
int32 num_frames_decoded_
virtual int32 NumFramesReady() const
The call NumFramesReady() will return the number of frames currently available for this decodable obj...
unordered_map< StateId, Token * > cur_toks_
DecodableInterface provides a link between the (acoustic-modeling and feature-processing) code and th...
static const LatticeWeightTpl One()
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
unordered_map< StateId, Token * > prev_toks_
static void ClearToks(unordered_map< StateId, Token *> &toks)
bool ReachedFinal() const
void ProcessNonemitting()
BaseFloat FinalRelativeCost() const
*** The next functions are from the "new interface". ***
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
const fst::Fst< fst::StdArc > & fst_
fst::VectorFst< LatticeArc > Lattice
static void PruneToks(BaseFloat beam, unordered_map< StateId, Token *> *toks)
#define KALDI_ASSERT(cond)
bool Decode(DecodableInterface *decodable)
Decode this utterance.
static void TokenDelete(Token *tok)
void AdvanceDecoding(DecodableInterface *decodable, int32 max_num_frames=-1)
This will decode until there are no more frames ready in the decodable object, but if max_num_frames ...
virtual BaseFloat LogLikelihood(int32 frame, int32 index)=0
Returns the log likelihood, which will be negated in the decoder.
bool GetBestPath(Lattice *fst_out, bool use_final_probs=true) const
void InitDecoding()
InitDecoding initializes the decoding, and should only be used if you intend to call AdvanceDecoding(...