📄 hsrch_12.cc
字号:
init_instance->setSymbol(context_pool_d.setCentralAndAllocate(init_instance->getSymbol(), (GraphVertex<SearchNode>*)skip_symbol)); init_instance->setSkipSymbol((ulong)tmp_vertex); // set the internal state for the instance to active // score_a.insertLast(new Float(0.0)); init_instance->setInstanceState(Instance::ACTIVE); // exit gracefully // return true; } // when the internal state of the instance is active // if ((init_instance->getInstanceState() == Instance::ACTIVE) && (init_instance->getSkipLevel() == (ulong)level_a)) { // setup the context for the instance // ulong tmp_symbol = init_instance->getSkipSymbol(); init_instance->setSymbol(context_pool_d.setCentralAndAllocate(init_instance->getSymbol(), (GraphVertex<SearchNode>*)tmp_symbol)); // set the internal state for the instance to inactive // init_instance->setInstanceState(Instance::INACTIVE); } // output the debugging information // if (debug_level_d >= Integral::ALL) { // print the symbol // init_instance->getSymbol()->print(); // print the history // if (init_instance->getHistory() != (History*)NULL) { printHistory(init_instance->getHistory()); } // print the symbol stack // if (init_instance->getSymbolStack() != (History*)NULL) { printHistory(init_instance->getSymbolStack()); } } // get the history associated with the instance // init_history = new History(*init_instance->getHistory()); init_history->push(init_instance->getSymbol()); // initialize the lists before generating the right context // score_a.insertLast(new Float(0.0)); curr_hist.insertLast(new History(*init_history)); prev_hist.insertLast(new History()); // initialize the vertices // init_vertex = init_instance->getSymbol()->getLastVertex(); // generate right context for the central vertex // lookAheadHelper(init_vertex, level_a, level_a, 0, depth_a, inst_list_a, curr_hist, prev_hist, score_a); // free allocated memory // delete init_history; init_history = (History*)NULL; curr_hist.setAllocationMode(DstrBase::SYSTEM); curr_hist.clear(); prev_hist.setAllocationMode(DstrBase::SYSTEM); prev_hist.clear(); // exit gracefully // return true;}// method: lookAheadHelper//// arguments:// GraphVertex<SearchNode>* curr_vertex: (input) initial graph vertex// long curr_level: (input) current level in the hierarchy// long level: (input) level in the hierarchy we started at// long curr_depth: (input) current right context length// long depth: (input) desired right context length// DoubleLinkedList<Instance>& inst_list: (output) new instances generated// DoubleLinkedList<History>& curr_hist: (input) working history// DoubleLinkedList<History>& prev_hist: (input) working history// DoubleLinkedList<History>& score: (output) transition probabilities//// return: logical error status//// method propagates up and down the search hoerarchy in the process of// generating right context in corss-word decoding//boolean HierarchicalSearch::lookAheadHelper( GraphVertex<SearchNode>* curr_vertex_a, long curr_level_a, long level_a, long curr_depth_a, long depth_a, DoubleLinkedList<Instance>& inst_list_a, DoubleLinkedList<History>& curr_hist_a, DoubleLinkedList<History>& prev_hist_a, DoubleLinkedList<Float>& score_a) { // declare local variables // GraphVertex<SearchNode>* next_vertex = (GraphVertex<SearchNode>*)NULL; // when the current depth is equal to the requires depth we are done // if (curr_depth_a == depth_a) { return true; } // when we are at the level needed to generate right context // if (curr_level_a == level_a) { // when the current context has a right context length of zero and it // has an extension, i.e., the context has been previously extended // if (inst_list_a.getCurr()->getSymbol()->isExtended()) { Context* symbol = inst_list_a.getCurr()->getSymbol(); symbol = context_pool_d.foldAndAllocate(symbol); inst_list_a.getCurr()->setSymbol(symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { inst_list_a.getCurr()->getSymbol()->print(); } return true; } // when the current vertex is a term - shift the context // if (curr_vertex_a->isTerm()) { Context* symbol = curr_hist_a.getCurr()->pop(); symbol = context_pool_d.shiftAndAllocate(symbol, curr_vertex_a); curr_hist_a.getCurr()->push(symbol); inst_list_a.getCurr()->setSymbol(symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { inst_list_a.getCurr()->getSymbol()->print(); } return true; } // loop over all next vertices of the current vertex // long index = 0; Float* curr_score = new Float(*score_a.getCurr()); History* prev_history = new History(*prev_hist_a.getCurr()); History* curr_history = new History(*curr_hist_a.getCurr()); Instance* curr_instance = new Instance(*inst_list_a.getCurr()); for (boolean more = curr_vertex_a->gotoFirst(); more; more = curr_vertex_a->gotoNext(), index++) { // make sure each next vertex has a unique history // if (index > 0) { score_a.insertLast(new Float(*curr_score)); curr_hist_a.insertLast(new History(*curr_history)); prev_hist_a.insertLast(new History(*prev_history)); inst_list_a.insertLast(new Instance(*curr_instance)); } // retrieve the next vertex // next_vertex = curr_vertex_a->getCurr()->getVertex(); // determine the transition scale factor for the current level // long symbol_id = next_vertex->getItem()->getSymbolId(); float tr_scale = getSearchLevel(level_a).getTrScale(); float symbol_penalty = getSearchLevel(level_a).getSymbolPenalty(); // update the transition score // float trans_score = curr_vertex_a->getCurr()->getWeight() * tr_scale; score_a.getCurr()->assign(*(score_a.getCurr()) + trans_score); // when the next vertex is NOT a term node - stay at the same level // if (!next_vertex->isTerm()) { // update the score // if (!getSearchLevel(level_a).isDummySymbol(symbol_id)) { inst_list_a.getCurr()->setScore(inst_list_a.getCurr()->getScore() + symbol_penalty); } // when the next vertex is a skip symbol // if (getSearchLevel(level_a).isSkipSymbol(symbol_id)) { // set the internal state for the instance to pseudo-active // inst_list_a.getCurr()->setInstanceState(Instance::PSEUDO_ACTIVE); // save the skip symbol for later retrieval // inst_list_a.getCurr()->setSkipLevel((ulong)level_a); inst_list_a.getCurr()->setSkipSymbol((ulong)next_vertex); // recursively call the lookahead helper - incerment the depth // lookAheadHelper(next_vertex, curr_level_a, level_a, curr_depth_a, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a); } // when the next vertex is not a skip symbol // else { // shift the right context of the symbol into the current context // Context* symbol = curr_hist_a.getCurr()->pop(); symbol = context_pool_d.shiftAndAllocate(symbol, next_vertex); curr_hist_a.getCurr()->push(symbol); inst_list_a.getCurr()->setSymbol(symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { symbol->print(); } // recursively call the lookahead helper - incerment the depth // lookAheadHelper(next_vertex, curr_level_a, level_a, curr_depth_a+1, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a); } } // when the next vertex is a terminal node - ascend one level // else { // ascend to the previous level in the hierarchy // if (!propagateUp(curr_level_a, level_a, curr_depth_a, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a)) { // restore the original contents of the current list // while (!prev_hist_a.getCurr()->isEmpty()) { descend(inst_list_a.getCurr()); Context* symbol = prev_hist_a.getCurr()->pop(); curr_hist_a.getCurr()->push(symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { symbol->print(); } } // shift !NO_RIGHT_CONTEXT for the required depth // for (int i=curr_depth_a; i<depth_a; i++) { Context* symbol = curr_hist_a.getCurr()->pop(); symbol = context_pool_d.shiftAndAllocate(symbol, next_vertex); curr_hist_a.getCurr()->push(symbol); inst_list_a.getCurr()->setSymbol(symbol); } // free allocated memory // delete curr_score; delete prev_history; delete curr_history; delete curr_instance; // exit gracefully // return true; } } } // free allocated memory // delete curr_score; delete prev_history; delete curr_history; delete curr_instance; } // when we are NOT at the level needed to generate right context // else { // make a copy of the current context // Context* symbol = inst_list_a.getCurr()->getSymbol(); Context* symbol1 = curr_hist_a.getCurr()->peek(); // output the debugging information // if (debug_level_d >= Integral::ALL) { symbol->print(); } if (curr_vertex_a == (GraphVertex<SearchNode>*)NULL) { curr_vertex_a = symbol->getLastVertex(); } // when the current vertex is a terminal we cannot proceed further // if (curr_vertex_a->isTerm()) { return false; } // loop over all next vertices of the current vertex // long index = 0; Float* curr_score = new Float(*score_a.getCurr()); History* prev_history = new History(*prev_hist_a.getCurr()); History* curr_history = new History(*curr_hist_a.getCurr()); Instance* curr_instance = new Instance(*inst_list_a.getCurr()); for (boolean more=curr_vertex_a->gotoFirst(); more; more=curr_vertex_a->gotoNext(), index++) { // make sure each next vertex has a unique history // if (index > 0) { score_a.insertLast(new Float(*curr_score)); curr_hist_a.insertLast(new History(*curr_history)); prev_hist_a.insertLast(new History(*prev_history)); inst_list_a.insertLast(new Instance(*curr_instance)); } // retrieve the next vertex // next_vertex = curr_vertex_a->getCurr()->getVertex(); // determine the transition scale factor for the current level // float tr_scale = getSearchLevel(level_a).getTrScale(); // update the transition score // float trans_score = curr_vertex_a->getCurr()->getWeight() * tr_scale; score_a.getCurr()->assign(*(score_a.getCurr()) + trans_score); // when the next vertex is NOT a term node - descend one level // if (!next_vertex->isTerm()) { // descend to the next level in the hierarchy // if (!propagateDown(symbol, symbol1, next_vertex, curr_level_a, level_a, curr_depth_a, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a)) { // free allocated memory // delete curr_score; delete prev_history; delete curr_history; delete curr_instance; // exit gracefully // return false; } } // when the next vertex is a terminal node - ascend one level // else { // ascend to the previous level in the hierarchy // if (!propagateUp(curr_level_a, level_a, curr_depth_a, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a)) { // retrieve the current context // Context* symbol = inst_list_a.getCurr()->getSymbol(); Context* symbol1 = curr_hist_a.getCurr()->peek(); // descend to the next level in the hierarchy // if (!propagateDown(symbol, symbol1, next_vertex, curr_level_a, level_a, curr_depth_a, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a)) { // pop the previous stack context and push into the current stack // descend(inst_list_a.getCurr()); Context* symbol = prev_hist_a.getCurr()->pop(); curr_hist_a.getCurr()->push(symbol); // free allocated memory // delete curr_score; delete prev_history; delete curr_history; delete curr_instance; // exit gracefully // return false; } } } } // free allocated memory // delete curr_score; delete prev_history; delete curr_history; delete curr_instance; } // exit gracefully // return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -