📄 hsrch_09.cc
字号:
// return: logical error status//// initialize the lists, etc. needed to begin searching//boolean HierarchicalSearch::initializeLinearDecoder() { // declare local variables // DoubleLinkedList<Float> score_list(DstrBase::USER); DoubleLinkedList<Context> right_context_list(DstrBase::USER); // initialize the hypothesis path containers and forward-backward structures // initializeLinearPartial(); // error checking // if (getSearchLevel(initial_level_d).getRightContext() != getSearchLevel(initial_level_d).getLeftContext()) { return Error::handle(name(), L"initializeLinearDecoder - left and right context lengths must be the same", Error::ARG, __FILE__, __LINE__); } // get the position of the central item in the context // long central_pos = getSearchLevel(initial_level_d).getRightContext() + 1; long left_context_length = getSearchLevel(initial_level_d).getLeftContext(); long total_context_length = left_context_length + central_pos; // initialize the context length // Context left_context(total_context_length, central_pos); for (long i = 0; i < central_pos; i++) { left_context.assignAndAdvance((ulong)search_levels_d((long)initial_level_d).getSubGraph(0).getStart()); } // output the debugging information // if (debug_level_d >= Integral::ALL) { left_context.print(); } // generate all possible right contexts by extending left context to // the depth of the right context // Context* symbol = context_pool_d.get(left_context); generateRightContexts(right_context_list, score_list, symbol, search_levels_d((long)initial_level_d).getRightContext(), (long)initial_level_d); // generate trace for each right conext // for (boolean more_items = right_context_list.gotoFirst() && score_list.gotoFirst(); more_items; more_items = right_context_list.gotoNext() && score_list.gotoNext()) { // seed the top trace list with the start node of the search graph // Trace* tmp_trace = new Trace(); tmp_trace->setFrame((ulong)current_frame_d); if (search_mode_d == TRAIN) { tmp_trace->setTraceMode(Trace::TRAIN); } tmp_trace->setBackPointer((Trace*)NULL); tmp_trace->setSymbol(right_context_list.getCurr()); // initialize the structure that holds the n-symbols // initializeNsymbolTrace(tmp_trace); // update the score // tmp_trace->setScore(tmp_trace->getScore() + (float)(*score_list.getCurr())); // has the history been previously generated? if so reuse the history, // if not generate a new history and add it to the history pool // tmp_trace->setHistory(history_pool_d.initAndAllocate()); // add the start trace to the list // trace_lists_d((long)initial_level_d).insertLast(tmp_trace); // add the start trace to the trellis // if (search_mode_d == TRAIN) { String output; Long val; BiGraphVertex<TrainNode>* src = trellis_d.getStart(); BiGraphVertex<TrainNode>* dst = insertTrace(tmp_trace); trellis_d.insertArc(src, dst, false, 0); } // output the debugging information // if (debug_level_d >= Integral::DETAILED) { printNewPath(tmp_trace, (Trace*)NULL); } } // clear up memory // right_context_list.clear(); score_list.setAllocationMode(DstrBase::SYSTEM); score_list.clear(); // exit gracefully // return true;}// method: initializeNsymbolTrace//// arguments:// Trace* trace_a: (input) current trace//// return: logical error status//// initialze the first n-symbols for a trace//boolean HierarchicalSearch::initializeNsymbolTrace(Trace*& curr_trace_a){ // initialize the structure that holds the n-symbol contexts // long num_levels = getNumLevels(); nsymbol_indices_d.setLength(num_levels); // error checking // if (num_levels == 0) { return Error::handle(name(), L"initializeNsymbolTrace", Error::ARG, __FILE__, __LINE__); } // loop over each search level in the hierarchy // long num_nsymbol_levels = 0; for (long i = 0; i < num_levels; i++) { nsymbol_indices_d(i) = -1; if (getSearchLevel(i).useNSymbol()) { nsymbol_indices_d(i) = num_nsymbol_levels++; } } // set the actual n-symbol levels // Context*& nsymbol = curr_trace_a->getNSymbol(); if (num_nsymbol_levels > 0) { nsymbol = new Context[num_nsymbol_levels]; curr_trace_a->setNSymbolLength(num_nsymbol_levels); } // loop over each search level in the hierarchy // for (long i = 0; i < num_levels; i++) { if (getSearchLevel(i).useNSymbol()) { // get the n-symbol order for this search level // long nsymbol_order = getSearchLevel(i).getNSymbolOrder(); // initialize the n-symbol context for this level // Context tmp_context(nsymbol_order); for (long j = 0; j < nsymbol_order; j++) { tmp_context.assignAndAdvance((ulong)search_levels_d(i).getSubGraph(0).getStart()); } // error checking // if (((long)nsymbol_indices_d(i) < 0) || ((long)nsymbol_indices_d(i) >= num_levels)) { return Error::handle(name(), L"initializeNsymbolTrace", Error::ARG, __FILE__, __LINE__); } nsymbol[(long)nsymbol_indices_d(i)].assign(tmp_context); } } // exit gracefully // return true; }// method: initializeNsymbolInstance//// arguments:// Instance* instance_a: (input) current instance//// return: logical error status//// initialze the first n-symbols for a instance//boolean HierarchicalSearch::initializeNsymbolInstance(Instance*& curr_instance_a){ // initialize the structure that holds the n-symbol contexts // long num_levels = getNumLevels(); nsymbol_indices_d.setLength(num_levels); // loop over each search level in the hierarchy // long num_nsymbol_levels = 0; for (long i = 0; i < num_levels; i++) { // determine is n-symbol probabilities are used at this level // nsymbol_indices_d(i) = -1; if (getSearchLevel(i).useNSymbol()) { nsymbol_indices_d(i) = num_nsymbol_levels++; } } // set the actual n-symbol levels // Context*& nsymbol = curr_instance_a->getNSymbol(); if (num_nsymbol_levels > 0) { nsymbol = new Context[num_nsymbol_levels]; curr_instance_a->setNSymbolLength(num_nsymbol_levels); } // loop over each search level in the hierarchy // for (long i = 0; i < num_levels; i++) { // determine is n-symbol probabilities are used at this level // if (getSearchLevel(i).useNSymbol()) { // get the n-symbol order for this search level // long nsymbol_order = getSearchLevel(i).getNSymbolOrder(); // initialize the n-symbol context for this level // Context tmp_context(nsymbol_order); for (long j = 0; j < nsymbol_order; j++) { tmp_context.assignAndAdvance((ulong)search_levels_d(i).getSubGraph(0).getStart()); } // error checking // if (((long)nsymbol_indices_d(i) < 0) || ((long)nsymbol_indices_d(i) >= num_levels)) { return Error::handle(name(), L"initializeNsymbolInstance", Error::ARG, __FILE__, __LINE__); } nsymbol[(long)nsymbol_indices_d(i)].assign(tmp_context); } } // exit gracefully // return true; }// method: shiftNSymbol//// arguments:// Trace*& trace: (input) input hypothesis path// long level: (input) search level we are at// GraphVertex<SearchNode>* vertex: (input) graph vertex wer are at//// return: n-symbol probability//// method computes the n-symbol probability corresponding to the instance//boolean HierarchicalSearch::shiftNSymbol(Trace*& trace_a, long level_a, GraphVertex<SearchNode>* vertex_a) { // error checking // if (trace_a == (Trace*)NULL) { return Error::handle(name(), L"shiftNSymbol", Error::ARG, __FILE__, __LINE__); } if ((level_a < 0) || (level_a >= nsymbol_indices_d.length())) { return Error::handle(name(), L"shiftNSymbol", Error::ARG, __FILE__, __LINE__); } // shift the vertex into the n-symbol list at this level // Context*& nsymbol = trace_a->getNSymbol(); if (nsymbol == (Context*)NULL) { return Error::handle(name(), L"shiftNSymbol", Error::ARG, __FILE__, __LINE__); } nsymbol[(long)nsymbol_indices_d(level_a)].assignAndAdvance((ulong)vertex_a); // exit gracefully // return true; }// method: shiftNSymbol//// arguments:// Instance*& instance: (input) input hypothesis path// long level: (input) search level we are at// GraphVertex<SearchNode>* vertex: (input) graph vertex wer are at//// return: n-symbol probability//// method computes the n-symbol probability corresponding to the instance//boolean HierarchicalSearch::shiftNSymbol(Instance*& instance_a, long level_a, GraphVertex<SearchNode>* vertex_a) { // error checking // if (instance_a == (Instance*)NULL) { return Error::handle(name(), L"shiftNSymbol", Error::ARG, __FILE__, __LINE__); } if ((level_a < 0) || (level_a >= nsymbol_indices_d.length())) { return Error::handle(name(), L"shiftNSymbol", Error::ARG, __FILE__, __LINE__); } // shift the vertex into the n-symbol list at this level // Context*& nsymbol = instance_a->getNSymbol(); if (nsymbol == (Context*)NULL) { return Error::handle(name(), L"shiftNSymbol", Error::ARG, __FILE__, __LINE__); } nsymbol[(long)nsymbol_indices_d(level_a)].assignAndAdvance((ulong)vertex_a); // exit gracefully // return true; }// method: getPosteriorScore//// arguments:// Context* context: (input) n-symbol history// long level: (input) level of the context //// return: n-symbol probability//// method computes the n-symbol probability corresponding to the instance//float HierarchicalSearch::getPosteriorScore(Context* context_a, long level_a) { // declare local variables // long lm_order = 0; long nsymbol_index = (long)nsymbol_indices_d(level_a); float lm_scale = 1.0; float lm_score = 0.0; // error checking // if (context_a == (Context*)NULL) { return Error::handle(name(), L"getPosteriorScore", Error::ARG, __FILE__, __LINE__); } // print debugging information // if (debug_level_d >= Integral::ALL) { context_a[nsymbol_index].print(); } // get the language model scale factor // lm_scale = getSearchLevel(level_a).getLmScale(); // get the order of the language model // lm_order = getSearchLevel(level_a).getNSymbolOrder(); // get the n-gram language model // NGramModel& ngram_model = getSearchLevel(level_a).getNSymbolModel(); // lookup the ngram score for the context // lm_score = nsymbol_cache_d.getScore(ngram_model, context_a[nsymbol_index]); // return the scaled n-symbol probability // return (lm_scale * lm_score);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -