📄 lm_07.cc
字号:
// file: $isip/class/asr/LanguageModel/lm_07.cc// version: $Id: lm_07.cc,v 1.7 2002/09/25 20:26:41 huang Exp $//// isip include files//#include "LanguageModel.h"// method: convertGraph//// arguments:// DiGraph<String>& graph_a: (input) a graph of seach symbols// DiGraph<SearchNode>& node_graph_a: (output) a graph of search nodes// long& num_symbols_a: (input) the number of symbols in the symbol table// SearchLevel& level_a: (input) search level to hold the graph//// return: a boolean value indicating status //// this method convert a given DiGraph<String> to a DiGraph<SearchNode>//boolean LanguageModel::convertGraph(DiGraph<String>& symbol_graph_a, DiGraph<SearchNode>& node_graph_a, long& num_symbols_a, SearchLevel& level_a) { // variable for graphs conversion // long symbol_index = -1; long num_vertices = symbol_graph_a.length() + 2; Vector<SearchNode> snode(num_vertices); GraphVertex<SearchNode>* snode_vert[num_vertices]; GraphVertex<String>* tmp_vert[num_vertices]; // index all the vertices so that they can be referenced easily // tmp_vert[0] = symbol_graph_a.getStart(); // loop through the list of vertices and index them // symbol_graph_a.gotoFirst(); for (long i = 1; i < num_vertices - 1; i++) { tmp_vert[i] = (GraphVertex<String>*)symbol_graph_a.getCurr(); symbol_graph_a.gotoNext(); } // the last vertex is the terminal node // tmp_vert[num_vertices - 1] = symbol_graph_a.getTerm(); // create a list of SearchNode Vertices // and set the items properly // snode_vert[0] = node_graph_a.getStart(); snode_vert[num_vertices - 1] = node_graph_a.getTerm(); for (long i = 1; i < num_vertices - 1; i++) { // set the node in the given level // snode(i).setSearchLevel(&level_a); // get the corresponding search symbol for the current node // String* current_symbol = tmp_vert[i]->getItem(); // check if this symbol already exists in the level symbol table // for(long j=0; j<num_symbols_a; j++) { // get each symbol in the level // SearchSymbol existing_symbol; if(level_a.isValidSymbol(j)) { level_a.getSymbol(existing_symbol, j); } else { return Error::handle(existing_symbol, L"invalid symbol in the search level symbol table", Error::TEST, __FILE__, __LINE__); } // compare two symbols // if(current_symbol->eq(existing_symbol)) { // set symbol index as the existing symbol in level // symbol_index = j; //set = true; break; } } // set the symbol id to the node // snode(i).setSymbolId(symbol_index); } // end: for (long i = 1; i < num_vertices - 1; i++) // add the vertices to the graph // for (long i = 1; i < num_vertices - 1; i++) { snode_vert[i] = node_graph_a.insertVertex(&snode(i)); } // connect the vertices with arcs to complete the graph // for (long i = 0; i < num_vertices; i++) { // get the vertex // GraphVertex<String>* tmp_start_vert = tmp_vert[i]; // get the list of arcs for this vertex // boolean arcs_remain = tmp_start_vert->gotoFirst(); while (arcs_remain) { // declare temporary arcs and vertices // long dest_index = -1; GraphArc<String>* tmp_arc = tmp_start_vert->getCurr(); GraphVertex<String>* dest_vertex = tmp_arc->getVertex(); boolean is_eps = tmp_arc->getEpsilon(); // find the index of the destination vertex // for (long j = 0; j < num_vertices; j++) { if (dest_vertex == tmp_vert[j]) { dest_index = j; break; } } // connect the vertices in the searchnode graph // node_graph_a.insertArc(snode_vert[i], snode_vert[dest_index], is_eps, (float)tmp_arc->getWeight()); // goto the next arc // arcs_remain = tmp_start_vert->gotoNext(); } } // gracefully exit // return true;}// method: alignGraphs//// arguments:// Vector< Digraph<String> >& graph_list_a: (output) vector of graphs// Vector<SearchSymbol>& symbol_table_a: (input) symbol table// Vector<String>& graph_name_list_a: (input) vector of grammar names//// return: a boolean value indicating status//// this method change the order of the graphs in given vector to// match the order of search symbols in given symbol table//boolean LanguageModel::alignGraphs( Vector< DiGraph<String> >& graph_list_a, Vector<SearchSymbol>& symbol_table_a, Vector<String>& graph_name_list_a) { // make sure the three input vectors have the same length // if( graph_list_a.length() != symbol_table_a.length() || graph_list_a.length() != graph_name_list_a.length() || graph_name_list_a.length() != symbol_table_a.length()) { symbol_table_a.debug(L"symbol_table"); graph_name_list_a.debug(L"graph_name"); return Error::handle(name(), L"cannot align graphs because the symbol table, graph list and graph name list don't have the same length", Error::TEST, __FILE__, __LINE__); } // declare a temp vector of graphs // Vector< DiGraph<String> > tmp_graph_list; for(long i=0; i<symbol_table_a.length(); i++) { // get symbol // String symbol(symbol_table_a(i)); //symbol.debug(L"symbol_table"); for(long j=0; j<graph_name_list_a.length(); j++) { //graph_name_list_a(j).debug(L"graph_name"); // search the matching graph and add it to the temp graph vector // if(symbol.eq(graph_name_list_a(j))) { tmp_graph_list.concat(graph_list_a(j)); break; } } // end: for loop with j } // end: for loop with i // make sure the graph_list_a and the tmp_graph_list have the same length // if(graph_list_a.length() != tmp_graph_list.length()) { return Error::handle(name(), L"invalid alignment of graphs", Error::TEST, __FILE__, __LINE__); } // update the graph_list_a // graph_list_a.assign(tmp_graph_list); // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -