📄 hsrch_12.cc
字号:
// file: $isip/class/search/HierarchicalSearch/hsrch_12.cc// version: $Id: hsrch_12.cc,v 1.20 2003/03/27 05:03:17 alphonso Exp $//// isip include files//#include "HierarchicalSearch.h"// method: ascend//// arguments:// Instance* instance: (input) input instance//// return: logical error status//// this method saves the current symbol in the symbol stack before// replacing it with the symbol on the history stack//boolean HierarchicalSearch::ascend(Instance* instance_a) { // declare local variables // Context* symbol = (Context*)NULL; History* history = (History*)NULL; History* symbol_stack = (History*)NULL; // make sure the instance is valid // if (instance_a == (Instance*)NULL) { return Error::handle(name(), L"ascend - instance is null", Error::ARG, __FILE__, __LINE__); } // if the symbol is null then we cannot ascend // symbol = instance_a->getSymbol(); if (symbol == (Context*)NULL) { return Error::handle(name(), L"ascend - symbol is null", Error::ARG, __FILE__, __LINE__); } // output the debugging information // if (debug_level_d >= Integral::ALL) { symbol->print(); } // if the history stack is empty or null then we cannot ascend any further // history = instance_a->getHistory(); if (history == (History*)NULL) { return Error::handle(name(), L"ascend - history is null", Error::ARG, __FILE__, __LINE__); } if (history->isEmpty()) { return true; } // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!history->isEmpty()) { history->peek()->print(); } } // if the symbol stack is null then we cannot ascend // symbol_stack = instance_a->getSymbolStack(); if (symbol_stack == (History*)NULL) { return Error::handle(name(), L"ascend - symbol stack is empty", Error::ARG, __FILE__, __LINE__); } // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!symbol_stack->isEmpty()) { symbol_stack->peek()->print(); } } // push the current symbol on the symbol stack // symbol_stack = history_pool_d.pushAndAllocate(symbol_stack, symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!symbol_stack->isEmpty()) { symbol_stack->peek()->print(); } } // has the history been previously generated? // if so reuse the history, // if not generate a new history and add it to the history pool // instance_a->setSymbolStack(symbol_stack); // move the symbol on top of the history to the current symbol // history = history_pool_d.popAndAllocate(history, symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!history->isEmpty()) { history->peek()->print(); } } // the context must exist in the context pool in this case // instance_a->setSymbol(symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { instance_a->getSymbol()->print(); } // has the history been previously generated? // if so reuse the history, // if not generate a new history and add it to the history pool // instance_a->setHistory(history); // exit gracefully // return true;}// method: descend//// arguments:// Instance* instance: (input) input instance//// return: logical error status//// this method saves the current symbol in the symbol stack before// replacing it with the symbol on the history stack//boolean HierarchicalSearch::descend(Instance* instance_a) { // declare local variables // Context* symbol = (Context*)NULL; History* history = (History*)NULL; History* symbol_stack = (History*)NULL; // make sure the instance is valid // if (instance_a == (Instance*)NULL) { return Error::handle(name(), L"descend - instance is null", Error::ARG, __FILE__, __LINE__); } // if the symbol is null then we cannot descend // symbol = instance_a->getSymbol(); if (symbol == (Context*)NULL) { return Error::handle(name(), L"descend - symbol is null", Error::ARG, __FILE__, __LINE__); } // output the debugging information // if (debug_level_d >= Integral::ALL) { symbol->print(); } // if the history stack null then we cannot descend any further // history = instance_a->getHistory(); if (history == (History*)NULL) { return Error::handle(name(), L"descend - history is null", Error::ARG, __FILE__, __LINE__); } // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!history->isEmpty()) { history->peek()->print(); } } // if the symbol stack is null or empty then we cannot descend // symbol_stack = instance_a->getSymbolStack(); if (symbol_stack == (History*)NULL) { return Error::handle(name(), L"descend - symbol stack is empty", Error::ARG, __FILE__, __LINE__); } if (symbol_stack->isEmpty()) { return true; } // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!symbol_stack->isEmpty()) { symbol_stack->peek()->print(); } } // push the current symbol on the history stack // history = history_pool_d.pushAndAllocate(history, symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!history->isEmpty()) { history->peek()->print(); } } // has the history been previously generated? // if so reuse the history, // if not generate a new history and add it to the history pool // instance_a->setHistory(history); // move the symbol on top of the symbol stack to the current symbol // symbol_stack = history_pool_d.popAndAllocate(symbol_stack, symbol); // output the debugging information // if (debug_level_d >= Integral::ALL) { if (!symbol_stack->isEmpty()) { symbol_stack->peek()->print(); } } // the context must exist in the context pool in this case // instance_a->setSymbol(symbol); // has the history been previously generated? // if so reuse the history, // if not generate a new history and add it to the history pool // instance_a->setSymbolStack(symbol_stack); // exit gracefully // return true;}// method: generateRightContexts//// arguments:// DoubleLinkedList<Context>& context_list: (output) list of next contexts// DoubleLinkedList<Float>& score: (output) transition probabilities// const Context* initial_context: (input) initial context to start from// long depth: (input) right context length// long level: (input) current search level//// return: logical error status//// generate a list of contexts expanded from the starting context to right// into the context depth//boolean HierarchicalSearch::generateRightContexts( DoubleLinkedList<Context>& context_list_a, DoubleLinkedList<Float>& score_list_a, Context* initial_context_a, long depth_a, long level_a) { // declare local variables // Float* new_score = (Float*)NULL; Float* curr_score = (Float*)NULL; Context* new_context = (Context*)NULL; Context* curr_context = (Context*)NULL; context_list_a.insertLast(initial_context_a); score_list_a.insertLast(new Float(0.0)); // genrate new context lists into the depht of depth_a // for (long i = 0; i < depth_a; i++) { // all new contexts will be added at the end of the list so we set // the mark to tell us when to stop propagating // context_list_a.gotoLast(); context_list_a.setMark(); context_list_a.gotoFirst(); score_list_a.gotoLast(); score_list_a.setMark(); score_list_a.gotoFirst(); // propagate all contexts in the list // boolean end_loop = context_list_a.length() < 1; while (!end_loop) { end_loop = (context_list_a.isMarkedElement() & score_list_a.isMarkedElement()); // get the current context from the list // context_list_a.removeFirst(curr_context); score_list_a.removeFirst(curr_score); // get the last vertex of the current context // GraphVertex<SearchNode>* curr_vertex = curr_context->getLastVertex(); // if it is NULL vertex, throw an error and exit // if (curr_vertex == (GraphVertex<SearchNode>*)NULL) { return Error::handle(name(), L"generateRightContexts - from NULL!", Error::ARG, __FILE__, __LINE__); } if (!curr_vertex->isTerm()) { // loop over arcs of the last vertex if it is not terminal vertex // for (boolean more_arcs = curr_vertex->gotoFirst(); more_arcs; more_arcs = curr_vertex->gotoNext()) { // determine the symbol insertion penalty and transition // scale factor // float tr_scale = getSearchLevel(level_a).getTrScale(); float symbol_penalty = getSearchLevel(level_a).getSymbolPenalty(); // shift next context // new_score = new Float(*curr_score); new_context = context_pool_d.shiftAndAllocate(curr_context, curr_vertex->getCurr()->getVertex()); // update the score with the arc score // long symbol_id = curr_vertex->getItem()->getSymbolId(); if (!getSearchLevel(level_a).isDummySymbol(symbol_id)) { new_score->add((curr_vertex->getCurr()->getWeight() * tr_scale) + symbol_penalty); } else { new_score->add(curr_vertex->getCurr()->getWeight()); } // add new context to the list // context_list_a.insertLast(new_context); score_list_a.insertLast(new_score); } // end of loop over all arcs of the last vertex // delete the current context from the list, since we have // propagated from it // delete curr_score; } // end of if not terminal // if the last vertex of the current context is terminal, simply // shift context // else { curr_context = context_pool_d.shiftAndAllocate(curr_context, curr_vertex); context_list_a.insertLast(curr_context); score_list_a.insertLast(curr_score); } // end of else terminal vertex context_list_a.gotoFirst(); score_list_a.gotoFirst(); } // end of the loop over contexts list } // end of for depths loop //exit gracefully // return true;}// method: initializeRightContexts//// arguments:// Instance* curr_instance: (input) initial instance to start from// Context& init_context: (input) initial context to start from// float curr_weight: (input) transition probability// long depth: (input) right context length//// return: logical error status//// generate a list of contexts expanded from the starting context to right// into the context depth//boolean HierarchicalSearch::initializeRightContexts(Instance* curr_instance_a, Context& init_context_a, long level_num_a, float curr_weight_a, long depth_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); // set up the initial instance // if (curr_instance_a == (Instance*)NULL) { // create a new instance // next_instance = new Instance(); next_instance->setFrame((ulong)current_frame_d); if (search_mode_d == TRAIN) { next_instance->setInstanceMode(Instance::TRAIN); } // has the history been previously generated? if so reuse the history, // if not generate a new history and add it to the history pool // next_instance->setHistory(history_pool_d.initAndAllocate()); next_instance->setSymbolStack(history_pool_d.initAndAllocate()); // initialize the structure that holds the n-symbols // initializeNsymbolInstance(next_instance); } else { // create a new instance // next_instance = new Instance(*curr_instance_a); next_instance->setFrame((ulong)current_frame_d); } // output the debugging information // if (debug_level_d >= Integral::ALL) { init_context_a.print(); } // has the context been previously generated? if so reuse the context, // if not generate a new context and add it to the context pool // next_instance->setSymbol(context_pool_d.get(init_context_a)); // generate right contexts for the instance // inst_list.insertLast(next_instance); lookAhead(level_num_a, depth_a, 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 //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -