📄 hsrch_05.cc
字号:
// file: $isip/class/search/HierarchicalSearch/hsrch_05.cc// version: $Id: hsrch_05.cc,v 1.28 2003/01/23 20:00:26 alphonso Exp $//// isip include files//#include "HierarchicalSearch.h"// method: forcedAlignment//// arguments:// String& alignment: (output) forced allignment// Long level: (input) the level to print hypotheses// DoubleLinkedList<Trace>& trace_path: (input) best hypothesis trace path// boolean cumulative: (input) whether to output a cumulative score//// return: logical error status//// method to output the forced allignment of the current decoded transcription//boolean HierarchicalSearch::forcedAlignment(String& alignment_a, Long level_a, DoubleLinkedList<Trace>& trace_path_a, boolean cumulative_a) { // declare local variables // Trace* tmp_trace = (Trace*)NULL; SearchNode* prev_node = (SearchNode*)NULL; SearchNode* tmp_node = (SearchNode*)NULL; long counter = 0; long frame_ind = 0; long prev_frame_ind = -1; float score = 0.0; float prev_score = 0.0; long curr_level = 0; String out_str; SearchSymbol sym; // clear the output // alignment_a.clear(); // check the level specification // if ((long)level_a < 0) { return Error::handle(name(), L"forcedAlignment - invalid level specified", Error::ARG, __FILE__, __LINE__); } // use the best hypothesis trace path to generate the forced allignment // for (boolean more_traces = trace_path_a.gotoFirst(); more_traces; more_traces = trace_path_a.gotoNext()) { counter++; tmp_trace = trace_path_a.getCurr(); frame_ind = tmp_trace->getFrame(); // get the central vertex from the top of the history stack // GraphVertex<SearchNode>* tmp_vertex = (GraphVertex<SearchNode>*)NULL; tmp_vertex = tmp_trace->getSymbol()->getCentralVertex(); // check for a NULL vertex // if (tmp_vertex == (GraphVertex<SearchNode>*)NULL) { return Error::handle(name(), L"forcedAlignment", Error::ARG, __FILE__, __LINE__); } // make sure the vertex is neither the dummy // start nor terminating node // if ((!tmp_vertex->isStart()) && (!tmp_vertex->isTerm())) { tmp_node = tmp_vertex->getItem(); curr_level = tmp_node->getSearchLevel()->getLevelIndex(); if (curr_level == level_a) { tmp_node->getSymbol(sym); score = tmp_trace->getScore(); // generate the output hypothesis // if (counter > 1) { out_str.assign(L"\n"); } else { out_str.assign(L""); } // append previous frame index // if (level_a != (getNumLevels() - 1)) { out_str.concat(prev_frame_ind); } else { out_str.concat(prev_frame_ind + 1); } out_str.concat(L"\t"); // append current frame index // if (level_a != (getNumLevels() - 1)) { out_str.concat(frame_ind); } else { out_str.concat(frame_ind + 1); } out_str.concat(L"\t"); // append search symbol // out_str.concat(sym); out_str.concat(L"\t"); // append score // out_str.concat(score - prev_score); // append cumulative score if required // if (cumulative_a) { out_str.concat(L"\t"); out_str.concat(score); } // append partial string to the complete hypothesis report // if (level_a == (getNumLevels() - 1)) { alignment_a.concat(out_str); } else { if ((frame_ind != prev_frame_ind) && (frame_ind != 0)) { alignment_a.concat(out_str); } } prev_node = tmp_node; prev_score = score; prev_frame_ind = frame_ind; } } } // exit gracefully // return true;}// method: getHypotheses//// arguments:// String& output_hyp: (output) the current search hypotheses// long level: (input) the level to print hypotheses from// double& total_score: (output) the hypothesis total score// long& num_frames: (output) frame index of the last trace// DoubleLinkedList<Trace>& trace_path: (output) best hypothesis trace path//// return: logical error status//// build a graph representing the hypotheses and return it//boolean HierarchicalSearch::getHypotheses(String& output_hyp_a, long level_a, double& total_score_a, long& num_frames_a, DoubleLinkedList<Trace>& trace_path_a) { // declare local variables // Trace* tmp_trace = (Trace*)NULL; SearchNode* prev_node = (SearchNode*)NULL; SearchNode* tmp_node = (SearchNode*)NULL; BiGraphVertex<TrainNode>* vertex = (BiGraphVertex<TrainNode>*)NULL; long counter = 0; long curr_level = 0; long frame_ind = 0; long prev_frame_ind = -1; long back_count = 0; float score = 0.0; float prev_score = 0.0; long symbol_id = 0; String out_str; SearchSymbol sym; // clear the output and set the allocation mode for the trace path // output_hyp_a.clear(); trace_path_a.clear(); trace_path_a.setAllocationMode(DstrBase::USER); // move all traces forward in the search space // if (!propagateTraces()) { return false; } // make sure we have at least one valid hypothesis // if (trace_valid_hyps_d.length() < 1) { return false; } if (search_mode_d == TRAIN) { // loop over all valid hypothesis // for (boolean more = trace_valid_hyps_d.gotoFirst(); more; more = trace_valid_hyps_d.gotoNext()) { // retrieve the vertex corresponding to the trace // vertex = trace_valid_hyps_d.getCurr()->getReference(); if (vertex == (BiGraphVertex<TrainNode>*)NULL) { return Error::handle(name(), L"getHypotheses - cannot find the valid hypothesis in the trellis", Error::ARG, __FILE__, __LINE__); } // connect the current vertex to the terminal node // trellis_d.insertArc(vertex, trellis_d.getTerm(), false, 0); } } // loop over all valid traces in the hypothesis list // trace_valid_hyps_d.gotoFirst(); Trace* best_end_hyp = trace_valid_hyps_d.getCurr(); // find the trace with the best score, i.e., best hypothesis trace // while (true) { if (!trace_valid_hyps_d.gotoNext()) { break; } tmp_trace = trace_valid_hyps_d.getCurr(); if (tmp_trace->getScore() > best_end_hyp->getScore()) { best_end_hyp = tmp_trace; } } // backtrack from the best hypothesis trace and generate the trace path // for (tmp_trace = best_end_hyp; tmp_trace != (Trace*)NULL; tmp_trace = tmp_trace->getBackPointer()) { trace_path_a.insertFirst(tmp_trace); back_count++; } // use the best hypothesis trace path to generate the hypothesis string // for (boolean more_traces = trace_path_a.gotoFirst(); more_traces; more_traces = trace_path_a.gotoNext()) { counter++; tmp_trace = trace_path_a.getCurr(); frame_ind = tmp_trace->getFrame(); // get the central vertex from the top of the history stack // GraphVertex<SearchNode>* tmp_vertex = (GraphVertex<SearchNode>*)NULL; tmp_vertex = tmp_trace->getSymbol()->getCentralVertex(); // check for a NULL vertex // if (tmp_vertex == (GraphVertex<SearchNode>*)NULL) { return Error::handle(name(), L"getHypotheses - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } // make sure the vertex is neither the dummy // start nor terminating node // if ((!tmp_vertex->isStart()) && (!tmp_vertex->isTerm())) { tmp_node = tmp_vertex->getItem(); curr_level = tmp_node->getSearchLevel()->getLevelIndex(); if ((level_a < 0) && (curr_level == (long)initial_level_d)) { tmp_node->getSymbol(sym); score = tmp_trace->getScore(); symbol_id = tmp_node->getSymbolId(); // generate the output hypothesis containing only search symbols // if (!getSearchLevel(curr_level).isExcludeSymbol(symbol_id)) { out_str.assign(sym); out_str.concat(L" "); } else { out_str.assign(L""); } // append partial string to the complete hypothesis report // if ((frame_ind != prev_frame_ind) && (frame_ind != 0)) { output_hyp_a.concat(out_str); } prev_node = tmp_node; prev_score = score; prev_frame_ind = frame_ind; } if ((level_a >= 0) && (curr_level == level_a)) { String context; tmp_trace->getSymbol()->print(context); score = tmp_trace->getScore(); sym.assign(context); // generate the output hypothesis // if (counter > 1) { out_str.assign(L"\n"); } else { out_str.assign(L""); } // append previous frame index // if (level_a != (getNumLevels() - 1)) { out_str.concat(prev_frame_ind); } else { out_str.concat(prev_frame_ind + 1); } out_str.concat(L"\t"); // append current frame index // if (level_a != (getNumLevels() - 1)) { out_str.concat(frame_ind); } else { out_str.concat(frame_ind + 1); } out_str.concat(L"\t"); // append search symbol // out_str.concat(sym); out_str.concat(L"\t\t"); // append score // out_str.concat(score - prev_score); // append partial string to the complete hypothesis report // if (level_a == (getNumLevels() - 1)) { output_hyp_a.concat(out_str); } else { if ((frame_ind != prev_frame_ind) && (frame_ind != 0)) { output_hyp_a.concat(out_str); } } prev_node = tmp_node; prev_score = score; prev_frame_ind = frame_ind; } } } total_score_a = score; num_frames_a = frame_ind; // exit gracefully // return true;}// method: printNewPath//// arguments:// Trace* new_trace: (input) the endpoint of the new arc// Trace* old_trace: (input) the start point of the new arc//// return: logical error status//// print an arc created in the hypothesis space//boolean HierarchicalSearch::printNewPath(Trace* new_trace_a, Trace* old_trace_a) { SearchSymbol start_sym; SearchSymbol end_sym; float old_score = 0.0; float new_score = 0.0; long old_frame = -1; long new_frame = -1; if (old_trace_a != (Trace*)NULL) { GraphVertex<SearchNode>* old_vertex = old_trace_a->getSymbol()->getCentralVertex(); old_frame = old_trace_a->getFrame(); if (old_vertex->isStart()) { start_sym.assign(L"_START_"); } else if (old_vertex->isTerm()) { start_sym.assign(L"_TERM_"); } else { old_trace_a->getSymbol()->print(start_sym); } old_score = old_trace_a->getScore(); } if (new_trace_a != (Trace*)NULL) { GraphVertex<SearchNode>* new_vertex = new_trace_a->getSymbol()->getCentralVertex(); new_frame = new_trace_a->getFrame(); if (new_vertex->isStart()) { end_sym.assign(L"_START_"); } else if (new_vertex->isTerm()) { end_sym.assign(L"_TERM_"); } else { new_trace_a->getSymbol()->print(end_sym); } new_score = new_trace_a->getScore(); } String val; String out(L"\n-> trace: "); out.concat(new_trace_a); out.concat(L", backpointer: "); out.concat(old_trace_a); out.concat(L"\n ["); out.concat(start_sym); out.concat(L"], frame: "); val.assign((long)old_frame); out.concat(val); out.concat(L", score: "); out.concat(old_score); out.concat(L" -> ["); out.concat(end_sym); out.concat(L"], frame: "); val.assign((long)new_frame); out.concat(val); out.concat(L", score: "); out.concat(new_score);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -