// file: $isip/class/stat/SearchLevel/slev_04.cc // version: $Id: slev_04.cc 10311 2005-12-14 19:29:13Z may $ // // isip include files // #include "SearchLevel.h" // method: sofSize // // arguments: none // // return: size of object // // this method returns the size of the object in the Sof file and is // used for binary write // int32 SearchLevel::sofSize() const { return (level_index_d.sofSize() + level_tag_d.sofSize() + nonspeech_boundary_symbol_table_d.sofSize() + nonspeech_internal_symbol_table_d.sofSize() + dummy_symbol_table_d.sofSize() + exclude_symbol_table_d.sofSize() + nsymbol_exclude_symbol_table_d.sofSize() + spenalty_exclude_symbol_table_d.sofSize() + contextless_symbol_table_d.sofSize() + skip_symbol_table_d.sofSize() + non_adapt_symbol_table_d.sofSize() + subGraphSize() + context_map_d.sofSize() + symbol_table_d.sofSize()); } // method: write // // arguments: // Sof& sof: (input) sof file object // int32 tag: (input) sof object instance tag // const String& name: (input) sof object instance name // // return: bool8 value indicating status // // this method has the object write itself to an Sof file // bool8 SearchLevel::write(Sof& sof_a, int32 tag_a, const String& name_a) const { int32 obj_size = 0; // write the instance of the object into the Sof file // if (sof_a.isText()) { // set the size to by dynamic // obj_size = Sof::ANY_SIZE; } else { // the size index and the size of each element // obj_size = sofSize(); } // write the object into the sof file's index // if (!sof_a.put(name_a, tag_a, obj_size)) { return false; } // exit gracefully // return writeData(sof_a); } // method: writeData // // arguments: // Sof& sof: (input) sof file object // const String& pname: (input) parameter name // // return: bool8 value indicating status // // this method has the object write itself to an Sof file. it assumes // that the Sof file is already positioned correctly. // bool8 SearchLevel::writeData(Sof& sof_a, const String& pname_a) const { // write a start string if necessary // sof_a.writeLabelPrefix(pname_a); // write the level index // level_index_d.writeData(sof_a, PARAM_LEVEL_INDEX); // write the level tag // if (!writeLevelTag(sof_a)) { return false; } // must write the symbol table first // if (!writeSymbols(sof_a)) { return false; } // write the non-speech symbols (optional) // if ((nonspeech_boundary_symbol_table_d.length() > 0) || (nonspeech_internal_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeNonSpeechSymbols(sof_a); } // write the dummy symbols (optional) // if ((dummy_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeDummySymbols(sof_a); } // write the exclude symbols (optional) // if ((exclude_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeExcludeSymbols(sof_a); } // write the nsymbol exclude symbols (optional) // if ((nsymbol_exclude_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeNSymbolExcludeSymbols(sof_a); } // write the spenalty exclude symbols (optional) // if ((spenalty_exclude_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeSPenaltyExcludeSymbols(sof_a); } // write the context less symbols (optional) // if ((contextless_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeContextLessSymbols(sof_a); } // write the skip symbols (optional) // if ((skip_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeSkipSymbols(sof_a); } // write the non adaptation symbols (optional) // if ((non_adapt_symbol_table_d.length() > 0) || sof_a.isBinary()) { writeNonAdaptSymbols(sof_a); } // write the sub graphs // if (!writeSubGraphs(sof_a)) { return false; } // write the context mapping table (optional) // if ((context_map_d.length() > 0) || sof_a.isBinary()) { writeContextMapping(sof_a); } // put an end string if necessary // sof_a.writeLabelSuffix(pname_a); // exit gracefully // return true; } // method: writeContextLessSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the ContextLess symbol table from Sof file // bool8 SearchLevel::writeContextLessSymbols(Sof& sof_a) const { // write the symbol table and exit // return contextless_symbol_table_d.writeData(sof_a, PARAM_CONTEXTLESS_SYMBOL); } // method: writeSkipSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the Skip symbol table from Sof file // bool8 SearchLevel::writeSkipSymbols(Sof& sof_a) const { // write the symbol table and exit // return skip_symbol_table_d.writeData(sof_a, PARAM_SKIP_SYMBOL); } // method: writeDummySymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the dummy symbol table from Sof file // bool8 SearchLevel::writeDummySymbols(Sof& sof_a) const { // write the symbol table and exit // return dummy_symbol_table_d.writeData(sof_a, PARAM_DUMMY_SYMBOL); } // method: writeNonAdaptSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the dummy symbol table from Sof file // bool8 SearchLevel::writeNonAdaptSymbols(Sof& sof_a) const { // write the symbol table and exit // return non_adapt_symbol_table_d.writeData(sof_a, PARAM_NON_ADAPT_SYMBOL); } // method: writeExcludeSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the exclude symbol table from Sof file // bool8 SearchLevel::writeExcludeSymbols(Sof& sof_a) const { // write the symbol table and exit // return exclude_symbol_table_d.writeData(sof_a, PARAM_EXCLUDE_SYMBOL); } // method: writeNSymbolExcludeSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the nsymbol exclude symbol table from Sof file // bool8 SearchLevel::writeNSymbolExcludeSymbols(Sof& sof_a) const { // write the symbol table and exit // return nsymbol_exclude_symbol_table_d.writeData(sof_a, PARAM_NSYMBOL_EXCLUDE_SYMBOL); } // method: writeSPenaltyExcludeSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the spenalty exclude symbol table from Sof file // bool8 SearchLevel::writeSPenaltyExcludeSymbols(Sof& sof_a) const { // write the symbol table and exit // return spenalty_exclude_symbol_table_d.writeData(sof_a, PARAM_SPENALTY_EXCLUDE_SYMBOL); } // method: writeLevelTag // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the Skip symbol table from Sof file // bool8 SearchLevel::writeLevelTag(Sof& sof_a) const { // write the symbol table and exit // return level_tag_d.writeData(sof_a, PARAM_LEVEL_TAG); } // method: writeSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the symbol table from Sof file // bool8 SearchLevel::writeSymbols(Sof& sof_a) const { // remove the NO_LEFT_CONTEXT and NO_RIGHT_CONTEXT symbols in case // these symbols exist in the symbol-table // int32 j = 0; int32 len = symbol_table_d.length(); Vector symbol_table(len); for (int32 i = 0; i < len; i++) { if (!((symbol_table_d(i).eq(SearchSymbol::NO_LEFT_CONTEXT)) || (symbol_table_d(i).eq(SearchSymbol::NO_RIGHT_CONTEXT)))) { symbol_table.setLength(j + 1); symbol_table(j) = symbol_table_d(i); j++; } } // write the symbol table and exit // return symbol_table.writeData(sof_a, PARAM_SYMBOL); } // method: writeNonSpeechSymbols // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method writes the non-speech symbol table from Sof file // bool8 SearchLevel::writeNonSpeechSymbols(Sof& sof_a) const { if (nonspeech_boundary_symbol_table_d.length() > 0 || sof_a.isBinary()) { if (!nonspeech_boundary_symbol_table_d.writeData(sof_a, PARAM_NONSPEECH_BOUNDARY_SYMBOL)) { return false; } } if (nonspeech_internal_symbol_table_d.length() > 0 || sof_a.isBinary()) { if (!nonspeech_internal_symbol_table_d.writeData(sof_a, PARAM_NONSPEECH_INTERNAL_SYMBOL)) { return false; } } return true; } // method: writeContextMapping // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 value indicating status // // this method reads context mapping information from an Sof file // bool8 SearchLevel::writeContextMapping(Sof& sof_a) const { // write the context map at this level // context_map_d.writeData(sof_a, PARAM_CONTEXT_MAPPING); // gracefully exit // return true; } // method: subGraphSize // // arguments: // none // // return: an integer indicating size // // this finds the size of the subgraphs that will be written to a file. // int32 SearchLevel::subGraphSize() const { // make sure the symbol table has been loaded // int32 index = 0; int32 dst_index = 0; bool8 is_eps = false; int32 num_graphs = sub_graphs_d.length(); int32 num_symbols = symbol_table_d.length(); if (num_symbols == 0) { // please load symbol table first // return Error::handle(name(), L"subGraphSize", SearchLevel::ERR, __FILE__, __LINE__); } // declare a Vector of Graph to be written to file // Ulong tmp_item; SearchNode* snode; GraphArc* dst_arc; GraphVertex* dst_vertex; Vector< DiGraph > tmp_graphs; Vector >sub_graphs; // convert each Graph > to Graph // sub_graphs.assign(sub_graphs_d); tmp_graphs.setLength(sub_graphs.length()); for (int32 k = 0; k < num_graphs; k++) { // set the properties of graphs // int32 num_vertices = sub_graphs(k).length() + 2; tmp_graphs(k).setWeighted(sub_graphs(k).isWeighted()); // insert all vertices corresponding the current graph index // index = 0; GraphVertex* vertices[num_vertices]; GraphVertex* tmp_vertices[num_vertices]; // add the start vertices to the arrays // vertices[index] = sub_graphs(k).getStart(); tmp_vertices[index++] = tmp_graphs(k).getStart(); // add the rest of the vertices to the arrays // for (bool8 more = sub_graphs(k).gotoFirst(); more; more = sub_graphs(k).gotoNext()) { // get the current vertex // vertices[index] = const_cast* >(sub_graphs(k).getCurr()); // get the search node corresponding to the vertex // snode = vertices[index]->getItem(); // insert a vertex in the Graph for the current vertex // using the symbol ID of the vertex as the node item // tmp_item.assign(snode->getSymbolId()); tmp_vertices[index++] = tmp_graphs(k).insertVertex(&tmp_item); } // add the term vertices to the arrays // vertices[num_vertices - 1] = sub_graphs(k).getTerm(); tmp_vertices[num_vertices - 1] = tmp_graphs(k).getTerm(); // connect all vertices corresponding to the current graph index // for (int32 i = 0; i < num_vertices; i++) { bool8 arcs_remain = vertices[i]->gotoFirst(); while (arcs_remain) { // get the destination vertex // dst_arc = vertices[i]->getCurr(); dst_vertex = dst_arc->getVertex(); is_eps = dst_arc->getEpsilon(); // find the destination vertex // dst_index = -1; for (int32 j = 0; j < num_vertices; j++) { if (dst_vertex == vertices[j]) { dst_index = j; break; } } // error - when we cannot find a destination index // if (dst_index == -1) { return Error::handle(name(), L"subGraphSize", Error::ARG, __FILE__, __LINE__); } // connect the vertices in the Graph // if (!tmp_graphs(k).insertArc(tmp_vertices[i], tmp_vertices[dst_index], is_eps, (float64)dst_arc->getWeight())) { return Error::handle(name(), L"subGraphSize", Error::ARG, __FILE__, __LINE__); } arcs_remain = vertices[i]->gotoNext(); } } } return tmp_graphs.sofSize(); } // method: writeSubGraphs // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 indicating status // // this method has the object write itself from an Sof file // bool8 SearchLevel::writeSubGraphs(Sof& sof_a) const { Vector< DiGraph > tmp_graphs; tmp_graphs = convertSubgraphs(); // write the Vector of Graph to file // if (!tmp_graphs.writeData(sof_a, PARAM_MODEL)) { return false; } // exit gracefully // return true; }