📄 ssrch_05.cc
字号:
// file: $isip/class/search/StackSearch/ssrch_05.cc// version: $Id: ssrch_05.cc,v 1.4 2002/10/18 21:52:59 alphonso Exp $//// isip include files//#include "StackSearch.h"// 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 StackSearch::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; 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; 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()) { // cleanup after the utterance // // clear the stacks // for (long i = 0; i < stacks_d.length(); i++) { stacks_d(i).clear(); } // clean state traces // DoubleLinkedList<Trace> state_traces(DstrBase::USER); getTraces(state_traces, getNumLevels() - 1); Trace* curr_trace = NULL; while (state_traces.removeFirst(curr_trace)) { Trace::deleteTrace(curr_trace, true); } // clear valid hypotheses // clearValidHypsTrace(); // exit ungracefully // return false; } // make sure we have at least one valid hypothesis // if (valid_hyps_d.length() < 1) { // cleanup after the utterance // // clear the stacks // for (long i = 0; i < stacks_d.length(); i++) { stacks_d(i).clear(); } // clean state traces // DoubleLinkedList<Trace> state_traces(DstrBase::USER); getTraces(state_traces, getNumLevels() - 1); Trace* curr_trace = NULL; while (state_traces.removeFirst(curr_trace)) { Trace::deleteTrace(curr_trace, true); } // clear valid hypotheses // clearValidHypsTrace(); // exit ungracefully // return false; } // loop over all valid traces in the hypothesis list // valid_hyps_d.gotoFirst(); Trace* best_end_hyp = valid_hyps_d.getCurr(); // find the trace with the best score, i.e., best hypothesis trace // while (true) { if (!valid_hyps_d.gotoNext()) { break; } tmp_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(); // make sure there is a valid history // History* tmp_hist = tmp_trace->getHistory(); if (tmp_hist->isEmpty()) { return Error::handle(name(), L"getHypotheses - hypothesis has empty history", Error::ARG, __FILE__, __LINE__); } // get the central vertex from the top of the history stack // GraphVertex<SearchNode>* tmp_vertex = (GraphVertex<SearchNode>*)NULL; tmp_vertex = tmp_hist->peek()->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 == 0)) { tmp_node->getSymbol(sym); score = tmp_trace->getScore(); // generate the output hypothesis containing only search symbols // if (sym.ne(L"!SENT_START") && sym.ne(L"!SENT_END")) { 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_hist->peek()->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; // cleanup after the utterance // // clear the stacks // for (long i = 0; i < stacks_d.length(); i++) { stacks_d(i).clear(); } // clean state traces // DoubleLinkedList<Trace> state_traces(DstrBase::USER); getTraces(state_traces, getNumLevels() - 1); Trace* curr_trace = NULL; while (state_traces.removeFirst(curr_trace)) { Trace::deleteTrace(curr_trace, true); } // clear valid hypotheses // clearValidHypsTrace(); // 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 StackSearch::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) { SearchNode* old_node = old_trace_a->getHistory()->peek()->getCentralVertex()->getItem(); old_frame = old_trace_a->getFrame(); if ( old_node == &DiGraph<SearchNode>::START_OBJ) { start_sym.assign(L"_START_"); } else if (old_node == &DiGraph<SearchNode>::TERM_OBJ) { start_sym.assign(L"_TERM_"); } else { old_node->getSymbol(start_sym); } old_score = old_trace_a->getScore(); } if (new_trace_a != (Trace*)NULL) { SearchNode* new_node = new_trace_a->getHistory()->peek()->getCentralVertex()->getItem(); new_frame = new_trace_a->getFrame(); if (new_node == &DiGraph<SearchNode>::START_OBJ) { end_sym.assign(L"_START_"); } else if (new_node == &DiGraph<SearchNode>::TERM_OBJ) { end_sym.assign(L"_TERM_"); } else { new_node->getSymbol(end_sym); } new_score = new_trace_a->getScore(); } String out(L"oTR "); out.concat(old_trace_a); out.concat(L" from ["); out.concat(start_sym); out.concat(L"] to ["); out.concat(end_sym); out.concat(L"] nTR "); out.concat(new_trace_a); out.concat(L" FR "); out.concat(new_frame); out.concat(L"\nold score: "); out.concat(old_score); out.concat(L" -> new score: "); out.concat(new_score); Console::put(out); return true;}// method: printDeletedPath//// 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 StackSearch::printDeletedPath(Trace* new_trace_a, Trace* old_trace_a) { SearchSymbol start_sym; SearchSymbol end_sym; long old_frame = -1; long new_frame = -1; if (old_trace_a != (Trace*)NULL) { SearchNode* old_node = old_trace_a->getHistory()->peek()->getCentralVertex()->getItem(); old_frame = old_trace_a->getFrame(); if ( old_node == &DiGraph<SearchNode>::START_OBJ) { start_sym.assign(L"_START_"); } else if (old_node == &DiGraph<SearchNode>::TERM_OBJ) { start_sym.assign(L"_TERM_"); } else { old_node->getSymbol(start_sym); } } if (new_trace_a != (Trace*)NULL) { SearchNode* new_node = new_trace_a->getHistory()->peek()->getCentralVertex()->getItem(); new_frame = new_trace_a->getFrame(); if (new_node == &DiGraph<SearchNode>::START_OBJ) { end_sym.assign(L"_START_"); } else if (new_node == &DiGraph<SearchNode>::TERM_OBJ) { end_sym.assign(L"_TERM_"); } else { new_node->getSymbol(end_sym); } } String out(L"deleted trace "); out.concat(new_trace_a); out.concat(L" generated from ["); out.concat(start_sym); out.concat(L"] in frame "); String val; val.assign((long)old_frame); out.concat(val); out.concat(L" to ["); out.concat(end_sym); out.concat(L"] in frame "); val.assign((long)new_frame); out.concat(val); Console::put(out); return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -