32 template <
typename FST>
34 bool use_final_probs)
const {
38 this->GetRawLattice(&raw_lat, use_final_probs);
39 ShortestPath(raw_lat, &lat1);
42 GetBestPath(&lat2, use_final_probs);
45 if (!fst::RandEquivalent(lat1, lat2, num_paths, delta, rand())) {
55 template <
typename FST>
57 bool use_final_probs)
const {
60 BestPathIterator iter = BestPathEnd(use_final_probs, &final_graph_cost);
63 StateId state = olat->AddState();
65 while (!iter.Done()) {
67 iter = TraceBackBestPath(iter, &arc);
68 arc.nextstate = state;
69 StateId new_state = olat->AddState();
70 olat->AddArc(new_state, arc);
73 olat->SetStart(state);
77 template <
typename FST>
81 if (this->decoding_finalized_ && !use_final_probs)
82 KALDI_ERR <<
"You cannot call FinalizeDecoding() and then call " 83 <<
"BestPathEnd() with use_final_probs == false";
85 "You cannot call BestPathEnd if no frames were decoded.");
87 unordered_map<Token*, BaseFloat> final_costs_local;
89 const unordered_map<Token*, BaseFloat> &final_costs =
90 (this->decoding_finalized_ ? this->final_costs_ :final_costs_local);
91 if (!this->decoding_finalized_ && use_final_probs)
92 this->ComputeFinalCosts(&final_costs_local, NULL, NULL);
96 BaseFloat best_cost = std::numeric_limits<BaseFloat>::infinity();
98 Token *best_tok = NULL;
99 for (
Token *tok = this->active_toks_.back().toks;
100 tok != NULL; tok = tok->
next) {
101 BaseFloat cost = tok->tot_cost, final_cost = 0.0;
102 if (use_final_probs && !final_costs.empty()) {
105 typename unordered_map<Token*, BaseFloat>::const_iterator
106 iter = final_costs.find(tok);
107 if (iter != final_costs.end()) {
108 final_cost = iter->second;
111 cost = std::numeric_limits<BaseFloat>::infinity();
114 if (cost < best_cost) {
117 best_final_cost = final_cost;
120 if (best_tok == NULL) {
126 *final_cost_out = best_final_cost;
127 return BestPathIterator(best_tok, this->NumFramesDecoded() - 1);
131 template <
typename FST>
133 BestPathIterator iter,
LatticeArc *oarc)
const {
136 int32 cur_t = iter.frame, ret_t = cur_t;
137 if (tok->backpointer != NULL) {
139 for (link = tok->backpointer->links;
140 link != NULL; link = link->
next) {
143 oarc->olabel = link->
olabel;
147 KALDI_ASSERT(static_cast<size_t>(cur_t) < this->cost_offsets_.size());
148 acoustic_cost -= this->cost_offsets_[cur_t];
156 KALDI_ERR <<
"Error tracing best-path back (likely " 157 <<
"bug in token-pruning algorithm)";
164 return BestPathIterator(tok->backpointer, ret_t);
167 template <
typename FST>
170 bool use_final_probs,
180 if (this->decoding_finalized_ && !use_final_probs)
181 KALDI_ERR <<
"You cannot call FinalizeDecoding() and then call " 182 <<
"GetRawLattice() with use_final_probs == false";
184 unordered_map<Token*, BaseFloat> final_costs_local;
186 const unordered_map<Token*, BaseFloat> &final_costs =
187 (this->decoding_finalized_ ? this->final_costs_ : final_costs_local);
188 if (!this->decoding_finalized_ && use_final_probs)
189 this->ComputeFinalCosts(&final_costs_local, NULL, NULL);
191 ofst->DeleteStates();
194 int32 num_frames = this->active_toks_.size() - 1;
196 for (
int32 f = 0; f <= num_frames; f++) {
197 if (this->active_toks_[f].toks == NULL) {
198 KALDI_WARN <<
"No tokens active on frame " << f
199 <<
": not producing lattice.\n";
203 unordered_map<Token*, StateId> tok_map;
204 std::queue<std::pair<Token*, int32> > tok_queue;
207 for (
Token *tok = this->active_toks_[0].toks;
208 tok != NULL; tok = tok->
next) {
209 if (tok->next == NULL) {
210 tok_map[tok] = ofst->AddState();
211 ofst->SetStart(tok_map[tok]);
212 std::pair<Token*, int32> tok_pair(tok, 0);
213 tok_queue.push(tok_pair);
218 while (!tok_queue.empty()) {
219 std::pair<Token*, int32> cur_tok_pair = tok_queue.front();
221 Token *cur_tok = cur_tok_pair.first;
222 int32 cur_frame = cur_tok_pair.second;
224 cur_frame <= this->cost_offsets_.size());
226 typename unordered_map<Token*, StateId>::const_iterator iter =
227 tok_map.find(cur_tok);
229 StateId cur_state = iter->second;
234 Token *next_tok = l->next_tok;
237 int32 next_frame = l->ilabel == 0 ? cur_frame : cur_frame + 1;
239 if (tok_map.find(next_tok) == tok_map.end()) {
240 nextstate = tok_map[next_tok] = ofst->AddState();
241 tok_queue.push(std::pair<Token*, int32>(next_tok, next_frame));
243 nextstate = tok_map[next_tok];
245 BaseFloat cost_offset = (l->ilabel != 0 ?
246 this->cost_offsets_[cur_frame] : 0);
247 Arc arc(l->ilabel, l->olabel,
248 Weight(l->graph_cost, l->acoustic_cost - cost_offset),
250 ofst->AddArc(cur_state, arc);
253 if (cur_frame == num_frames) {
254 if (use_final_probs && !final_costs.empty()) {
255 typename unordered_map<Token*, BaseFloat>::const_iterator iter =
256 final_costs.find(cur_tok);
257 if (iter != final_costs.end())
264 return (ofst->NumStates() != 0);
fst::StdArc::StateId StateId
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
fst::ArcTpl< LatticeWeight > LatticeArc
static const LatticeWeightTpl One()
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
fst::VectorFst< LatticeArc > Lattice
LatticeFasterOnlineDecoderTpl is as LatticeFasterDecoderTpl but also supports an efficient way to get...
BestPathIterator TraceBackBestPath(BestPathIterator iter, LatticeArc *arc) const
This function can be used in conjunction with BestPathEnd() to trace back the best path one link at a...
bool TestGetBestPath(bool use_final_probs=true) const
This function does a self-test of GetBestPath().
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
BestPathIterator BestPathEnd(bool use_final_probs, BaseFloat *final_cost=NULL) const
This function returns an iterator that can be used to trace back the best path.
bool GetBestPath(Lattice *ofst, bool use_final_probs=true) const
Outputs an FST corresponding to the single best path through the lattice.
bool GetRawLatticePruned(Lattice *ofst, bool use_final_probs, BaseFloat beam) const
Behaves the same as GetRawLattice but only processes tokens whose extra_cost is smaller than the best...
typename Arc::StateId StateId