📄 lm_08.cc
字号:
// acoustic_model_sof.close(); } // exit gracefully // return true;}// method: setNumLevels//// arguments: const String& file_a: (input) model file //// return: a boolean value indicating status//// This method checks out the number of search levels in the input file// and set the number to the num_level_d//boolean LanguageModel::setNumLevels(const String& file_a) { // boolean variables indicating input file format // boolean sof_format = true; boolean jsgf_format = true; // open the input sof file // Sof input; if(!input.open(file_a, File::READ_ONLY)) { return Error::handle(file_a, L"open", Error::TEST, __FILE__, __LINE__); } // check if the input lm_model_file_a is in ISIP Sof NATIVE format by // searching the number of levels by checking the search_symbols tag // long sof_tag_counter = 0; for(long tag = input.first(L"search_symbols"); tag != Sof::NO_TAG; tag = input.next(L"search_symbols", tag)) { // count the number of JSGF tags // sof_tag_counter++; } // if no search_symbols tag has been found, the file must not be // in JSGF format // if(sof_tag_counter == 0) { sof_format = false; } // Then, check if the input lm_model_file_a is in JSGF format by // searching from the input file the number of JSGF tags // // declare a counter to count the number of JSGF tags // long jsgf_tag_counter = 0; for(long tag = input.first(L"JSGF"); tag != Sof::NO_TAG; tag = input.next(L"JSGF", tag)) { // count the number of JSGF tags // jsgf_tag_counter++; } // if no JSGF tag has been found, the file must not be in JSGF format // if(jsgf_tag_counter == 0) { jsgf_format = false; } // if the file format is neither Sof nor JSGF, return error // if (((!sof_format) && (!jsgf_format)) || ((sof_format) && (jsgf_format))) { return Error::handle(name(), L"the format of input model file is not acceptable", Error::TEST, __FILE__, __LINE__); } // set number of search levels // if(sof_format) { // set num of levels // if(num_level_d == 0) { num_level_d = sof_tag_counter; } } if(jsgf_format) { // set num of levels // if(num_level_d == 0) { num_level_d = jsgf_tag_counter - 1; } // read in the user settings for the ISIP graph // starting and terminal symbols // setGraphEndings(input); } // close the input file // input.close(); // exit gracefully // return true;}// method: loadContextMapping//// arguments:// Sof& sof: (input) sof file object// const long tag: (input) sof object instance tag// SearchLevel& level: (input/output) search level// long level_index: (input) search level index//// return: a boolean value indicating status//// this method has the object read itself from an Sof file according// to the specified name and tag//boolean LanguageModel::loadContextMapping(Sof& sof_a, const String& tag_a, SearchLevel& level_a, long level_index_a) { // make sure the symbol table has been loaded // long num_symbols = (level_a.getSymbolTable()).length(); if (num_symbols < 1) { return Error::handle(name(), L"loadContextMapping", Error::ARG, __FILE__, __LINE__); } // local variable // Vector<String> context_maps; Vector<ContextMap> contexts; // read the all the contextmaps as vector of string from the given // level-index // context_maps.read(sof_a, level_index_a, SearchLevel::PARAM_CONTEXT_MAPPING); // set the capacity and length // long len = context_maps.length(); contexts.setCapacity(len); contexts.setLength(len); // loop over all the context_maps and parse string in each loop // for (long k = 0; k < context_maps.length(); k++) { // local variables // String context_map; Vector<String> symbol_list; Vector<String> rule_names; String symbol; Vector<SearchSymbol> context; // set the JSGF parser // JSGFParser jsgf_parser; jsgf_parser.setExpression(context_maps(k)); jsgf_parser.parseJSGF(); // get the data // symbol_list = jsgf_parser.getSymbolList(); jsgf_parser.getPublicRuleNames(rule_names); if (symbol_list.length() > 1 || rule_names.length() > 1) { return Error::handle(name(), L"loadContextMapping: the number of rule names or symbols great than one", Error::READ, __FILE__, __LINE__, Error::WARNING); } // get the data string // long context_length = rule_names(0).countTokens(L"-"); long rule_length = symbol_list(0).countTokens(L"_"); if ( rule_length != 2 ){ return Error::handle(name(), L"loadContextMapping: wrong graph name", Error::READ, __FILE__, __LINE__, Error::WARNING); } // get the context // long j=0; for (long i = 0; i < context_length; i++) { rule_names(0).tokenize(symbol, j, L"-"); context.concat(symbol); } // set this context // if (!contexts(k).setContext(context)) { return Error::handle(name(), L"loadContextMapping", Error::READ, __FILE__, __LINE__, Error::WARNING); } // get the context index // j = 0; for (long i = 0; i < rule_length; i++) { symbol_list(0).tokenize(symbol, j, L"_"); } // set the context index // Ulong index; index.assign(symbol); if (!contexts(k).setContextIndex(index)) { return Error::handle(name(), L"loadContextMapping", Error::READ, __FILE__, __LINE__, Error::WARNING); } } // set the contextmapping in the search level // if (!level_a.setContextMap(contexts)) { return Error::handle(name(), L"loadContextMapping", Error::READ, __FILE__, __LINE__, Error::WARNING); } // when there is a context mapping table at this level // if (contexts.length() > 0) { // add start and terminal search symbol to the symbol table // Vector<SearchSymbol>& symbol_table = level_a.getSymbolTable(); symbol_table.concat(SearchSymbol::NO_LEFT_CONTEXT); symbol_table.concat(SearchSymbol::NO_RIGHT_CONTEXT); // set up start and terminal search node for each subgraph // Vector<DiGraph<SearchNode > >& sub_graphs = level_a.getSubGraphs(); for (long i = 0; i < sub_graphs.length(); i++) { // start vertex // SearchNode* snode_p = new SearchNode(); snode_p->setSearchLevel(&level_a); snode_p->setSymbol(SearchSymbol::NO_LEFT_CONTEXT); sub_graphs(i).getStart()->setItem(snode_p); // terminal vertex // snode_p = new SearchNode(); snode_p->setSearchLevel(&level_a); snode_p->setSymbol(SearchSymbol::NO_RIGHT_CONTEXT); sub_graphs(i).getTerm()->setItem(snode_p); } // insert all context pairs into the context mapping hash table // long total_context_length = level_a.getLeftContext() + level_a.getRightContext() + 1; Context context(total_context_length); // loop over all context pairs // for (long i = 0; i < contexts.length(); i++) { // check the context length // if (contexts(i).getContext().length() != total_context_length) { contexts(i).getContext().debug(L"invalid context"); return Error::handle(name(), L"loadContextMapping", Error::ARG, __FILE__, __LINE__); } // loop over the symbols in the context // for (long j = 0; j < total_context_length; j++) { SearchSymbol ss(contexts(i).getContext()(j)); long symbol_id = level_a.getSymbolIndex(ss); // check whether the symbol is valid // if (symbol_id == -1) { ss.debug(L"symbol"); return Error::handle(name(), L"loadContextMapping", Error::ARG, __FILE__, __LINE__); } else { context.assignAndAdvance(symbol_id); } } // insert a context specification into the hash table // Ulong index = contexts(i).getContextIndex(); // check if the context is already in the table // HashTable<Context, Ulong>& context_hash = level_a.getContextHash(); Ulong* existing_index = context_hash.get(context); // if this context is not in the table yet, insert it // if (existing_index == NULL) { context_hash.insert(context, &index); } // if the context is already in the table, then check whether // the index of the model is the same as the one from table // else { // if indices are different, explain conflict and return error // if (!existing_index->eq(index)) { contexts(i).debug(L"Context:"); String out; out.concat(L" is already in context mapping table with the index: "); out.concat(*existing_index); out.concat(L"\n while new index is: "); out.concat(index); Console::put(out); return Error::handle(name(), L"loadContextMapping", Error::ARG, __FILE__, __LINE__); } // otherwise indices are not conflicting, just print a warning // else { contexts(i).debug(L"Warning: This context is already in the table:"); String out; out.concat(L" is already in context mapping table with the index: "); out.concat(*existing_index); Console::put(out); return Error::handle(name(), L"loadContextMapping", Error::ARG, __FILE__, __LINE__); } } } // output the debugging information // if (debug_level_d >= Integral::ALL) { HashTable<Context, Ulong>& context_hash = level_a.getContextHash(); context_hash.debug(L"context hash table:"); } } // when there is NO context mapping table at this level // else { // symbols will be mapped to the model with the same index at lower level // Context dummy(1); HashTable<Context, Ulong>& context_hash = level_a.getContextHash(); for (Ulong i = 0; i < (ulong)num_symbols; i++) { dummy.assignAndAdvance(i); context_hash.insert(dummy, &i); } if (debug_level_d >= Integral::ALL) { context_hash.debug(L"dummy context hash table:"); } } // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -