📄 hsrch_12.cc
字号:
next_instance->setScore(next_instance->getScore() + (float)(*next_score)); // add any posterior score such as n-symbol probabilities // if (getSearchLevel(level_num_a).useNSymbol()) { GraphVertex<SearchNode>* central_vertex = next_instance->getSymbol()->getCentralVertex(); long symbol_id = central_vertex->getItem()->getSymbolId(); if (!getSearchLevel(level_num_a).isDummySymbol(symbol_id) && !getSearchLevel(level_num_a).isSkipSymbol(symbol_id) && !central_vertex->isStart() && !central_vertex->isTerm()) { shiftNSymbol(next_instance, level_num_a, central_vertex); float posterior_score = getPosteriorScore(next_instance->getNSymbol(), level_num_a); next_instance->setScore(next_instance->getScore() + posterior_score); } } // add the instance to the search node's instance list // if (!addHypothesisPath(next_instance, curr_instance_a, level_num_a, *next_score)) { return Error::handle(name(), L"initializeRightContexts", Error::ARG, __FILE__, __LINE__); } } // free allocated memory // score_list.setAllocationMode(DstrBase::SYSTEM); score_list.clear(); // exit gracefully // return true;}// method: isTerminal//// arguments:// Instance* curr_instance: (input) instance in hypotheses space// long level_num: (input) current level number//// return: logical error status//// method determines if the instance has reached the end of the subgraph//boolean HierarchicalSearch::isTerminal(Instance* curr_instance_a, long level_num_a) { // declare local variables // GraphVertex<SearchNode>* next_vertex = (GraphVertex<SearchNode>*)NULL; GraphVertex<SearchNode>* central_vertex = (GraphVertex<SearchNode>*)NULL; GraphVertex<SearchNode>* skip_vertex = (GraphVertex<SearchNode>*)NULL; // when the internal state of the instance is pseudo-active // if (curr_instance_a->getInstanceState() == Instance::PSEUDO_ACTIVE) { // when the skip vertex is not adjacent from the central vertex then the // current instance is at the end of the subgraph // if ((long)curr_instance_a->getSkipLevel() == level_num_a) { skip_vertex = (GraphVertex<SearchNode>*)curr_instance_a->getSkipSymbol(); central_vertex = curr_instance_a->getSymbol()->getCentralVertex(); if (!central_vertex->isAdjacent(skip_vertex)) { return true; } } // return the status // return false; } // retrieve the central vertex // central_vertex = curr_instance_a->getSymbol()->getCentralVertex(); if (central_vertex == (GraphVertex<SearchNode>*)NULL) { return Error::handle(name(), L"isTerminal - central vertex is null", Error::ARG, __FILE__, __LINE__); } // retrieve the vertex that will be shifted into central vertex // next_vertex = curr_instance_a->getSymbol()->getAfterCentralVertex(); if (next_vertex == (GraphVertex<SearchNode>*)NULL) { return Error::handle(name(), L"isTerminal - next vertex is null", Error::ARG, __FILE__, __LINE__); } // when the next vertex is not adjacent from the central vertex then the // current instance is at the end of the subgraph // if (!central_vertex->isAdjacent(next_vertex)) { return true; } // return the status // return false;}// method: extendRightContexts//// arguments:// Instance* curr_instance: (input) initial instance to start from// long level_num: (input) level number in the hierarchy//// return: logical error status//// generate a list of contexts expanded from the starting context to right// into the context depth//boolean HierarchicalSearch::extendRightContexts(Instance* curr_instance_a, long level_num_a, Context* prev_symbol_a) { // declare local variables // Float* next_score = (Float*)NULL; Instance* next_instance = (Instance*)NULL; DoubleLinkedList<Instance> inst_list(DstrBase::USER); DoubleLinkedList<Float> score_list(DstrBase::USER); // create a new instance // next_instance = new Instance(*curr_instance_a); next_instance->setFrame((ulong)current_frame_d); // set the current symbol if it is valid // if (prev_symbol_a != (Context*)NULL) { next_instance->setSymbol(prev_symbol_a); } // generate right contexts for the instance // inst_list.insertLast(next_instance); lookAhead(level_num_a, 1, inst_list, score_list); // generate a new instance for each right context // for (boolean more_items = (inst_list.gotoFirst() && score_list.gotoFirst()); more_items; more_items = (inst_list.gotoNext() && score_list.gotoNext())) { // retrieve the current instance // next_score = score_list.getCurr(); next_instance = inst_list.getCurr(); // update the score // next_instance->setScore(next_instance->getScore() + (float)(*next_score)); // add any posterior score such as n-symbol probabilities // if (getSearchLevel(level_num_a).useNSymbol()) { GraphVertex<SearchNode>* central_vertex = next_instance->getSymbol()->getCentralVertex(); long symbol_id = central_vertex->getItem()->getSymbolId(); if (!getSearchLevel(level_num_a).isDummySymbol(symbol_id) && !getSearchLevel(level_num_a).isSkipSymbol(symbol_id) && !central_vertex->isStart() && !central_vertex->isTerm()) { shiftNSymbol(next_instance, level_num_a, central_vertex); float posterior_score = getPosteriorScore(next_instance->getNSymbol(), level_num_a); next_instance->setScore(next_instance->getScore() + posterior_score); } } // add the instance to the search node's instance list // if (!addHypothesisPath(next_instance, curr_instance_a, level_num_a, *next_score)) { return Error::handle(name(), L"extendRightContexts", Error::ARG, __FILE__, __LINE__); } } // free allocated memory // score_list.setAllocationMode(DstrBase::SYSTEM); score_list.clear(); // exit gracefully // return true;}// method: printHistory//// arguments:// const History* history: (input) history of the instance//// return: logical error status//boolean HierarchicalSearch::printHistory(const History* history_a) { // declare local variables // boolean more = false; // save the current state // const_cast<History* >(history_a)->setMark(); // print each level of the history // for (more = const_cast<History* >(history_a)->gotoFirst(); more; more = const_cast<History* >(history_a)->gotoNext()) { const_cast<History* >(history_a)->getCurr()->print(); } // restore the saved state // const_cast<History* >(history_a)->gotoMark(); // exit gracefully // return true; }// method: propagateUp//// arguments:// 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 the search hoerarchy in the process of generating// right context in cross-word decoding//boolean HierarchicalSearch::propagateUp( 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) { // when we are at the top most level we cannot go up the hiererchy // if (curr_level_a == (long)initial_level_d) { return false; } // ascend to the previous level in the hierarchy and look across symbols // else { // pop the current stack context and push it into the previous stack // Context* symbol = curr_hist_a.getCurr()->pop(); prev_hist_a.getCurr()->push(symbol); ascend(inst_list_a.getCurr()); // output the debugging information // if (debug_level_d >= Integral::ALL) { inst_list_a.getCurr()->getSymbol()->print(); } // recursively call the lookahead helper - decrement the current level // if (!lookAheadHelper((GraphVertex<SearchNode>*)NULL, curr_level_a-1, level_a, curr_depth_a, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a)) { return false; } } // return the status // return true; }// method: propagateDown//// arguments:// Context* symbol: (input) initial context to start with// Context* symbol1: (input) working initial context// 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 down the search hoerarchy in the process of generating// right context in cross-word decoding//boolean HierarchicalSearch::propagateDown( Context* symbol_a, Context* symbol1_a, 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>* start_vertex = (GraphVertex<SearchNode>*)NULL; if ((symbol_a == (Context*)NULL) || (symbol1_a == (Context*)NULL)) { return Error::handle(name(), L"propagateDown", Error::ARG, __FILE__, __LINE__); } // we cannot propagate down if the current level is equal to the level // if (curr_level_a == level_a) { return false; } // generate the context to index the subgraph at the lower level // SearchLevel& sl_curr = getSearchLevel(curr_level_a); SearchLevel& sl_lower = getSearchLevel(curr_level_a+1); Context* symbol = symbol_a; if (!symbol->getLastVertex()->isTerm()) { symbol = context_pool_d.extendAndAllocate(symbol, curr_vertex_a); } // output the debugging information // if (debug_level_d >= Integral::ALL) { symbol->print(); } Context* symbol1 = symbol1_a; symbol1 = context_pool_d.shiftAndAllocate(symbol1, curr_vertex_a); // shift the history at the current level // Context* tmp_symbol = inst_list_a.getCurr()->getSymbol(); inst_list_a.getCurr()->setSymbol(symbol); Context* tmp_symbol1 = curr_hist_a.getCurr()->pop(); curr_hist_a.getCurr()->push(symbol1); // retrieve the subgraph at the lower level // Context* conv_context = (Context*)NULL; symbol1->convert(conv_context); Ulong* subgr_ind = sl_curr.getSubGraphIndex(*conv_context); if (conv_context != (Context*)NULL) { delete conv_context; } // make sure we have a valid subgraph index // if (subgr_ind == (Ulong*)NULL) { // restore the history stach to its previous state // inst_list_a.getCurr()->setSymbol(tmp_symbol); symbol1 = curr_hist_a.getCurr()->pop(); curr_hist_a.getCurr()->push(tmp_symbol1); return false; } // get the start vertex of the subgraph // start_vertex = (sl_lower.getSubGraph((long)*subgr_ind)).getStart(); // insert the history at the next level // tmp_symbol1 = prev_hist_a.getCurr()->pop(); curr_hist_a.getCurr()->push(tmp_symbol1); descend(inst_list_a.getCurr()); // recursively call the lookahead helper - incerment the current level // if (!lookAheadHelper(start_vertex, curr_level_a+1, level_a, curr_depth_a, depth_a, inst_list_a, curr_hist_a, prev_hist_a, score_a)) { return false; } // return the status // return true; }// method: lookAhead//// arguments:// long level: (input) level in the hierarchy we started at// long depth: (input) desired right context length// DoubleLinkedList<Instance>& inst_list: (output) instances generated// DoubleLinkedList<Float>& 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::lookAhead(long level_a, long depth_a, DoubleLinkedList<Instance>& inst_list_a, DoubleLinkedList<Float>& score_a) { // declare local variables // History* init_history = (History*)NULL; Instance* init_instance = (Instance*)NULL; GraphVertex<SearchNode>* init_vertex = (GraphVertex<SearchNode>*)NULL; DoubleLinkedList<History> curr_hist(DstrBase::USER); DoubleLinkedList<History> prev_hist(DstrBase::USER); // make sure the instance list is not empty // if (inst_list_a.isEmpty()) { return false; } init_instance = inst_list_a.getFirst(); if (init_instance == (Instance*)NULL) { return Error::handle(name(), L"lookAhead", Error::ARG, __FILE__, __LINE__); } // make sure the symbol associated with the instance is not null // if (init_instance->getSymbol() == (Context*)NULL) { return Error::handle(name(), L"lookAhead", Error::ARG, __FILE__, __LINE__); } // when the internal state of the instance is pseudo-active // if ((init_instance->getInstanceState() == Instance::PSEUDO_ACTIVE) && (init_instance->getSkipLevel() == (ulong)level_a)) { // swap the contexts // ulong skip_symbol = init_instance->getSkipSymbol(); GraphVertex<SearchNode>* tmp_vertex = init_instance->getSymbol()->getCentralVertex();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -