📄 hsrch_14.cc
字号:
// file: $isip/class/search/HierarchicalSearch/hsrch_14.cc// version: $Id: hsrch_14.cc,v 1.23 2003/04/14 16:12:53 huang Exp $//// isip include files//#include "HierarchicalSearch.h"// method: printInstance//// arguments:// Instance* instance_a: (input) the instance to be printed// long level_num_a: (input) the search level// boolean recursive_a: (input) recursive or not//// return: logical error status//// print instances//boolean HierarchicalSearch::printInstance(Instance* new_instance_a, long level_num_a, boolean recursive_a) { // lcoal variable // String val; String out; SearchSymbol start_sym; float old_score = 0.0; long old_frame = -1; SearchNode* old_node = (SearchNode*)NULL; Instance* old_instance = (Instance*)NULL; GraphVertex<SearchNode>* old_vert = (GraphVertex<SearchNode>*)NULL; Context* old_context = (Context*)NULL; boolean recursive = true; long center = 0; // print level information // if ( level_num_a >= 0 ){ center = getSearchLevel(level_num_a).getRightContext() + 1; } old_instance = new_instance_a; while ( recursive ){ val.assign(L""); // old instance not NULL // if (old_instance != (Instance*)NULL) { // get current instance info // old_context = old_instance->getSymbol(); // get the current context length // long total_length = old_context->length(); // loop over all context symbols in current context, starting from // the oldest one // for (long i = total_length; i > 0; i--) { // get the i-th previous context symbol // old_vert = (GraphVertex<SearchNode>*)(ulong)(*old_context)(-i); // central vertex is NULL // if ( old_vert == (GraphVertex<SearchNode>*)NULL ){ start_sym.assign(L"_NULL_"); } // central vertex is not NULL // else { old_node = old_vert->getItem(); old_frame = old_instance->getFrame(); old_score = old_instance->getScore(); 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); } } val.concat(start_sym); // put in a form of b-oy+r // if ( i > 1 && i > center ){ val.concat(L"-"); } else if( i > 1 ){ val.concat(L"+"); } } // end of for loop } // old instance is not NULL // else old instance is a NULL // else{ start_sym.assign(L"NULL_INSTANCE"); val.concat(start_sym); } start_sym.assign(val); // print out the trace // out.concat(L"["); val.assign((long)old_frame); out.concat(val); out.concat(L" "); out.concat(start_sym); out.concat(L" "); val.assign(old_score, L"%4.7f"); out.concat(val); out.concat(L" "); out.concat(L"]"); // change the flag for recursive // recursive = ( recursive_a && (old_instance != (Instance*)NULL) ) || (old_instance == new_instance_a); if ( recursive ){ if ( old_instance == new_instance_a ){ out.concat(L"<--"); } } else { // print level information // if ( level_num_a >= 0 ){ out.concat(L" ==> L"); val.assign(level_num_a); out.concat(val); } } if ( !(old_instance == new_instance_a) ){ Console::put(out); out.assign(L""); } if ( old_instance != (Instance*)NULL ){ old_instance = old_instance->getBackPointer(); } } //debug_level_d = Integral::NONE; // exit gracefully // return true;}// method: changeHistory//// arguments:// Instance*& curr_instance_a: (input) the instance that we want to change// boolean ascend_a: (input) ascend or descend// long level_num_a: (input) current level// GraphVertex<SearchNode>* start_vert_a : (input) lower level start vertex//// return: logical error status//// change the history of current instance//boolean HierarchicalSearch::changeHistory(Instance*& curr_instance_a, boolean ascend_a, long level_num_a, GraphVertex<SearchNode>* start_vert_a){ // if going up - ascend // if ( ascend_a ){ curr_instance_a->setSymbol(context_pool_d.setLastAndAllocate(curr_instance_a->getSymbol(), (GraphVertex<SearchNode>*)NULL)); ascend(curr_instance_a); } // if going down - descend // else { // if no preHistory, generate new context // if ((curr_instance_a->getSymbolStack() != (History*)NULL) && curr_instance_a->getSymbolStack()->isEmpty()) { // set the context length // long central_pos = getSearchLevel(level_num_a + 1).getRightContext() + 1; long left_context_length = getSearchLevel(level_num_a + 1).getLeftContext(); long total_context_length = left_context_length + central_pos; // error checking // if (getSearchLevel(level_num_a + 1).getLeftContext() != getSearchLevel(level_num_a + 1).getRightContext()) { return Error::handle(name(), L"changeHistory - left and right context lengths must be the same", Error::ARG, __FILE__, __LINE__); } // initialize left vertices of the context // Context new_context(total_context_length, central_pos); for (long i = 0; i < total_context_length; i++) { new_context.assignAndAdvance((ulong)start_vert_a); } // set current history // Context* symbol = context_pool_d.get(new_context); if (curr_instance_a->getSymbol() != (Context*)NULL) { curr_instance_a->setHistory(history_pool_d.pushAndAllocate(curr_instance_a->getHistory(), curr_instance_a->getSymbol())); } curr_instance_a->setSymbol(symbol); } // use preHistory // else{ descend(curr_instance_a); } } // exit // return true; }// method: addInstance//// arguments:// long level_num_a: (input) the level for which we will add instance// Instance*& curr_instance_a: (input) the current instance // Instance*& next_instance_a: (input) the next instance that we added// boolean pruning_a: (input) do a pruning or not//// return: logical error status//// add the instance to a certain level //boolean HierarchicalSearch::addInstance(long level_num_a, Instance*& curr_instance_a, Instance*& next_instance_a, boolean pruning_a) { // local variable // float curr_score; //debug_level_d = Integral::DETAILED; if ( next_instance_a->getSymbol()->getCentralVertex() == (GraphVertex<SearchNode>*)NULL ) { // can't pruning in this case // pruning_a = false; } else if ( next_instance_a->getSymbol()-> getCentralVertex()->isStart() ) { // can't pruning in this case // pruning_a = false; } // apply n-symbol score // applyNSymbolScore(next_instance_a, level_num_a); // if not pruning // if ( !pruning_a ){ // set the backpointer // next_instance_a->setBackPointer(curr_instance_a); curr_score = next_instance_a->getScore(); // update the maximal instance score for the beam pruning // if(getSearchLevel(level_num_a).useBeam() && (curr_score > max_instance_scores_d(level_num_a)) ) { max_instance_scores_d(level_num_a) = curr_score; } // add the instance to this level's instance list // instance_lists_d(level_num_a).insertLast(next_instance_a); // print debugging information // if (debug_level_d >= Integral::DETAILED) { // print the best instance // printInstance(next_instance_a, level_num_a); } // exit gracefully // return true; } // add the instance to the search node's instance list // Instance* existing_instance = (Instance*)NULL; SearchNode* next_node = next_instance_a->getSymbol()-> getCentralVertex()->getItem(); if (next_node->insertInstance(next_instance_a, current_frame_d, existing_instance)) { // set the backpointer // next_instance_a->setBackPointer(curr_instance_a); // add the instance to this level's instance list // instance_lists_d(level_num_a).insertLast(next_instance_a); // print debugging information // if (debug_level_d >= Integral::DETAILED) { // print the best instance // printInstance(next_instance_a, level_num_a); } } else { // delete the instance since there is an existing instance // - same history // // print debugging information // if (debug_level_d >= Integral::DETAILED) { // print the best instance // printInstance(next_instance_a, level_num_a); } delete next_instance_a; return false; } curr_score = next_instance_a->getScore(); // update the maximal instance score for the beam pruning // if(getSearchLevel(level_num_a).useBeam() && (curr_score > max_instance_scores_d(level_num_a)) ) { max_instance_scores_d(level_num_a) = curr_score; } // print debugging information // if (debug_level_d >= Integral::DETAILED) { // print the best instance // printInstance(next_instance_a, level_num_a); } // exit gracefully // return true; }// method: ascendInstance//// arguments:// long level_num: (input) the level for which we will propagate instances// Instance*& curr_instance_a: (input) the instance that we want to ascend//// return: logical error status//// propagate current instance up in the hierarchy.// only instances at the end of each subgraph are considered//boolean HierarchicalSearch::ascendInstance(long level_num_a, Instance*& curr_instance_a) { // local variable // Instance* new_instance = (Instance*) NULL; Instance* tmp_instance = (Instance*) NULL; Instance* middle_instance = (Instance*) NULL; GraphVertex<SearchNode>* tmp_vert = (GraphVertex<SearchNode>*)NULL; GraphArc<SearchNode>* tmp_arc = (GraphArc<SearchNode>*)NULL; float tmp_score = 0; boolean pruning = true; boolean same_level = false; boolean dummy_node = false; // must have a history // Context* curr_context = curr_instance_a->getSymbol(); // keep the instance for propagate down // if (curr_context->getLastVertex() == (GraphVertex<SearchNode>*)NULL) { // keep the old instance, now exit // return false; } // last vertext is an terminal, ready for propagate up // else if (curr_context->getLastVertex()->isTerm()) { // new instance // //new_instance = curr_instance_a; new_instance = new Instance(*curr_instance_a); new_instance->setFrame((ulong)current_frame_d); // if we are at the top level, generate a NULL right context // if ( level_num_a == 0 ){ //new_instance->setBackPointer(curr_instance_a); new_instance->setSymbol(context_pool_d.setLastAndAllocate(new_instance->getSymbol(), (GraphVertex<SearchNode>*)NULL)); // add the instance to current level's instance list // addInstance( level_num_a, curr_instance_a, new_instance, pruning); // old instance was replaced, now exit // return true; } // decrement the history of the new instance // else { // set back pointer // new_instance->setBackPointer(curr_instance_a); // change history, go up one level // changeHistory(new_instance, true); // this part is added for skip-symbols // check with the instance with non-inactive state // if current level is one level above the skip level // the central vertex needs to be swapped // if ( new_instance->getInstanceState() == Instance::PSEUDO_ACTIVE && new_instance->getSkipLevel() == (ulong) level_num_a - 1 ){ Instance* tmp_instance = new Instance(*new_instance); tmp_instance->setFrame((ulong)current_frame_d); //tmp_instance->setBackPointer(new_instance); // set to normal condition // tmp_instance->setInstanceState(Instance::ACTIVE); // swap the central vertex // tmp_vert = (GraphVertex<SearchNode>*)tmp_instance->getSkipSymbol();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -