📄 lm_11.cc
字号:
// file: $isip/class/asr/LanguageModel/lm_04.cc// version: $Id: lm_11.cc,v 1.19 2002/12/15 23:00:08 parihar Exp $//// isip include files//#include "LanguageModel.h"// method: store//// arguments:// const String& model_file_a: (output) file name for a SOF file containing// language models// const String& stat_file_a: (output) file name for a SOF file containing// statistical models associated with the last// level// const Vector<SearchLevel>& search_levels_a: (input) a set of search levels// OUTPUT_FORMAT format_a: (input) user-defined file format for the output// model file// OUTPUT_TYPE type_a: (input) user-defined file type (text/binary)//// return: a boolean value indicating status//// this method outputs a set of input search levels to two SOF files// with user-defined filenames according to a user-chosen file format// that is either NATIVE format or JSGF format//boolean LanguageModel::store(const String& model_file_a, const String& stat_file_a, Vector<SearchLevel>& search_levels_a, OUTPUT_FORMAT format_a, OUTPUT_TYPE type_a) { // get the last level index // long last_level = search_levels_a.length() - 1; boolean context_map = false; // branch on type: BINARY // all format types are included in this case - NATIVE, JSGF // if (type_a == BINARY) { // open the input sof file // Sof model_sof; if(!model_sof.open(model_file_a, File::WRITE_ONLY, File::BINARY)) { return Error::handle(model_file_a, L"open", Error::TEST, __FILE__, __LINE__); } // store the input search levels into the model file // for (long i = 0; i < search_levels_a.length(); i++) { search_levels_a(i).store(model_sof, i); } // close the file // model_sof.close(); } // end of type = BINARY // else branch on type: TEXT // else if (type_a == TEXT) { // branch on format: NATIVE // this case is same as type: BINARY because the NATIVE format // deals with binary format automatically through the Sof class // interface // if (format_a == NATIVE) { // open the input sof file // Sof model_sof; if(!model_sof.open(model_file_a, File::WRITE_ONLY)) { return Error::handle(model_file_a, L"open", Error::TEST, __FILE__, __LINE__); } // store the input search levels into the model file // for (long i = 0; i < search_levels_a.length(); i++) { search_levels_a(i).store(model_sof, i); } // close the file // model_sof.close(); } // end of format: NATIVE // branch on the format: JSGF // else if (format_a == JSGF) { // declare a vector of strings to store each level of JSGF grammars // Vector<String> JSGF_models; // loop through the search levels // for (long i = 0; i < search_levels_a.length(); i++) { // declare a string to store the JSGF grammars in this level // String JSGF_level; context_map = false; // get the symbol table of the previous search level if available // long level_index = 0; if (i > 0) { level_index = i - 1; } Vector<SearchSymbol>& symbol_table = search_levels_a(level_index).getSymbolTable(); Vector<SearchSymbol> graph_symbols; // get the number of subgraphs in the level // long num_graphs = search_levels_a(i).getNumSubGraphs(); // do we have context mapping ? // if (search_levels_a(level_index).getContextMap().length() > 0){ context_map = true; // set the symbol table // for (long j = 0; j < num_graphs; j++) { String symbol; symbol.assign(L"G_"); Ulong index; index.assign(j); symbol.concat(index); //symbol.debug(L"symbol"); graph_symbols.concat(symbol); } } // loop through each subgraph in this level // for (long j = 0; j < num_graphs; j++) { // added for debug // //Long index(j); //index.debug(L"the index j"); // declare a JSGF grammar string // String JSGF_grammar(L"grammar = {\n #JSGF V1.0;\n"); // get the graph // DiGraph<SearchNode>& graph = search_levels_a(i).getSubGraph(j); // get the grammar name for the graph // SearchSymbol grammar_name; // the top level // if (i == 0) { grammar_name.assign(L"sentence"); } // for all other level, the graph's grammar name is the corresponding // symbol in the symbol table of the previous level // else { if ( context_map ){ grammar_name.assign(graph_symbols(j)); } else { grammar_name.assign(symbol_table(j)); } } // convert the graph to a JSGF grammar string // //JSGF_grammar.concat(convertToJSGF(graph, grammar_name)); JSGF_grammar.concat(digraphToJSGF(graph, grammar_name)); JSGF_grammar.concat(L"};\n\n"); // add this grammar to the level in JSGF format // JSGF_level.concat(JSGF_grammar); } // end: for (long j = 0; j < num_graphs; j++) // add this level of JSGF to the vector of JSGF models // JSGF_models.concat(JSGF_level); } // end: for (long i = 0; i < search_levels_a.length(); i++) // output the JSGF models to the output SOF file // // declare the line of algorithm tag // String algo_tag(L"algorithm = \"JSGF\";\n"); // set the size of the object to be written // long algo_size = algo_tag.length(); // first store in the output SOF file the the grammars of definitions // for graph starting and stoping points // String start_def(L"grammar = {\n #JSGF V1.0;\n // Define the grammar name\n grammar network.grammar.ISIP_JSGF_START;\n\n // Define the ISIP graph start symbol\n public <ISIP_JSGF_1_0_START> = S;\n};\n\n"); String term_def(L"grammar = {\n #JSGF V1.0;\n // Define the grammar name\n grammar network.grammar.ISIP_JSGF_TERM;\n\n // Define the ISIP graph terminal symbol\n public <ISIP_JSGF_1_0_TERM> = T;\n};\n\n"); // open the input sof file // Sof model_sof; if(!model_sof.open(model_file_a, File::WRITE_ONLY)) { return Error::handle(model_file_a, L"open", Error::TEST, __FILE__, __LINE__); } // write the definitions of graph starting and stoping points // into the sof file // model_sof.put(L"JSGF", 100, algo_size + start_def.length() + term_def.length()); model_sof.puts(algo_tag); model_sof.puts(start_def); model_sof.puts(term_def); // loop through each level and write its JSGF grammars into the SOF file // for (long i = 0; i < JSGF_models.length(); i++) { model_sof.put(L"JSGF", i, algo_size + JSGF_models(i).length()); model_sof.puts(algo_tag); model_sof.puts(JSGF_models(i)); // write other stuff at each level that are not grammar // storeNonGrammars(model_sof, search_levels_a, i); } // close the file // model_sof.close(); } // end of format: JSGF // else error // else { return Error::handle(name(), L"invalid format", Error::TEST, __FILE__, __LINE__); } } // end of type: TEXT // else error // else { return Error::handle(name(), L"invalid type", Error::TEST, __FILE__, __LINE__); } // get the statistical model hashtable and the models // HashTable<SearchSymbol, Long>& hash_table = search_levels_a(last_level).getSymbolHashTable(); Vector<StatisticalModel>& stat_models = search_levels_a(last_level).getStatisticalModels(); // if no statistical model is found, output a warning message // if(stat_models.length() == 0) { Console::put(L"Warning: no statistical models available in the last search level."); } // if statistical models are available // store the statistical models into a separate SOF file // else { // open the SOF file // Sof stat_sof; // open the model file for i/o (TEXT) // if (type_a == TEXT) { if(!stat_sof.open(stat_file_a, File::WRITE_ONLY)) { return Error::handle(stat_file_a, L"open", Error::TEST, __FILE__, __LINE__); } } // open the model file for i/o (BINATY) // if (type_a == BINARY) { if(!stat_sof.open(stat_file_a, File::WRITE_ONLY, File::BINARY)) { return Error::handle(stat_file_a, L"open", Error::TEST, __FILE__, __LINE__); } } // store the hashtable and statistical models into the statistical SOF file // hash_table.write(stat_sof, last_level, PARAM_STAT_HASH); stat_models.write(stat_sof, last_level, PARAM_STAT); // close the statistical model SOF file // stat_sof.close(); } // exit gracefully // return true;}// method: store//// arguments:// const String& model_file_a: (output) file name for a SOF file containing// language models// const Vector<SearchLevel>& search_levels_a: (input) a set of search levels// OUTPUT_FORMAT format_a: (input) user-defined file format for the output model file// OUTPUT_TYPE type_a: (input) user-defined file type (text/binary)////// return: a boolean value indicating status//// this method outputs a set of input search levels to two SOF files// with user-defined filenames according to a user-chosen file format// that is either NATIVE format or JSGF format//boolean LanguageModel::store(const String& model_file_a, Vector<SearchLevel>& search_levels_a, OUTPUT_FORMAT format_a, OUTPUT_TYPE type_a) { // set an empty acoustic model file // String ac_model; return store (model_file_a, ac_model, search_levels_a, format_a, type_a);}// method: convertToJSGF//// arguments:// DiGraph<SearchNode> graph_a: (input) a DiGraph object to be converted// SearchSymbol grammar_name_a: (input) the JSGF grammar name for the graph//// return: (output) a JSGF grammar string converted from DiGraph//// This method converts the input grammar from DiGraph of SearchNode format// to JSGF format. Each SearchSymbol is picked up from the input symbol table// according to the SearchNode index in the graph.// String LanguageModel::convertToJSGF(DiGraph<SearchNode>& graph_a, SearchSymbol grammar_name_a) { // declare and initialize a JSGF grammar string // String JSGF_output(L" // Define the grammar name\n"); JSGF_output.concat(L" grammar network.grammar."); JSGF_output.concat(grammar_name_a); JSGF_output.concat(L";\n\n"); JSGF_output.concat(L" // Define the rules\n"); JSGF_output.concat(L" public"); String debug_string; // debug message // debug_string.assign(L"debug 1"); debug_string.debug(L"point"); // get all vertices in the input graph // long num_vertices = graph_a.length() + 2; GraphVertex<SearchNode>* verts[num_vertices]; verts[0] = graph_a.getStart(); graph_a.gotoFirst(); for(long i = 1; i < num_vertices - 1; i++) { verts[i] = (GraphVertex<SearchNode>*)graph_a.getCurr(); graph_a.gotoNext(); } verts[num_vertices - 1] = graph_a.getTerm(); String rule; SingleLinkedList<String> open_list; String start(L"ISIP_JSGF_1_0_START"); open_list.insert(&start); Vector<Triple<String, SingleLinkedList<Long>, Float> > close_list; SingleLinkedList<Long> rule_vertex_list; Long start_index(0); rule_vertex_list.insert(&start_index); Triple<String, SingleLinkedList<Long>, Float > triple; Float rule_weight;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -