📄 lm_12.cc
字号:
// file: $isip/class/asr/LanguageModel/lm_04.cc// version: $Id: lm_12.cc,v 1.11 2002/12/17 16:38:22 parihar Exp $//// isip include files//#include "LanguageModel.h"// method: storeNonGrammars//// arguments:// const Sof& 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// long level_index_a: (input) the level to store.////// return: a boolean value indicating status//// this method store non-grammar part of the search level to a file//boolean LanguageModel::storeNonGrammars(Sof& model_file_a, Vector<SearchLevel>& search_levels_a, long level_index_a) { // write the name of the level // Vector<String> temp_strings; String level_tag = search_levels_a(level_index_a).getLevelTag(); temp_strings.concat(level_tag); writeOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_LEVEL_TAG, temp_strings); // write the context mapping // if ( search_levels_a(level_index_a).getContextMap().length() > 0 ){ storeContextMapping(model_file_a, L"JSGF", search_levels_a(level_index_a), level_index_a); } // write the non speech symbol // if ( search_levels_a(level_index_a).getNonSpeechSymbolTable().length() > 0 ){ temp_strings.clear(); for ( int i = 0; i < search_levels_a(level_index_a). getNonSpeechSymbolTable().length(); i++){ temp_strings.concat(search_levels_a(level_index_a). getNonSpeechSymbolTable()(i)); } writeOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_NONSPEECH_SYMBOL, temp_strings); } // write the dummy symbols // if ( search_levels_a(level_index_a).getDummySymbolTable().length() > 0 ){ temp_strings.clear(); for ( int i = 0; i < search_levels_a(level_index_a). getDummySymbolTable().length(); i++){ temp_strings.concat(search_levels_a(level_index_a). getDummySymbolTable()(i)); } writeOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_DUMMY_SYMBOL, temp_strings); } // write the context-less symbols // if (search_levels_a(level_index_a).getContextLessSymbolTable().length() > 0 ){ temp_strings.clear(); for ( int i = 0; i < search_levels_a(level_index_a). getContextLessSymbolTable().length(); i++){ temp_strings.concat(search_levels_a(level_index_a). getContextLessSymbolTable()(i)); } writeOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_CONTEXTLESS_SYMBOL, temp_strings); } // write the skip symbols // if (search_levels_a(level_index_a).getSkipSymbolTable().length() > 0 ){ temp_strings.clear(); for ( int i = 0; i < search_levels_a(level_index_a). getSkipSymbolTable().length(); i++){ temp_strings.concat(search_levels_a(level_index_a). getSkipSymbolTable()(i)); } writeOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_SKIP_SYMBOL, temp_strings); } // write the skip symbols // if (search_levels_a(level_index_a).getExcludeSymbolTable().length() > 0 ){ temp_strings.clear(); for ( int i = 0; i < search_levels_a(level_index_a). getExcludeSymbolTable().length(); i++){ temp_strings.concat(search_levels_a(level_index_a). getExcludeSymbolTable()(i)); } writeOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_EXCLUDE_SYMBOL, temp_strings); } // exit gracefully // return true;}// method: loadNonGrammars//// arguments:// const Sof& 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// long level_index_a: (input) the level to load.////// return: a boolean value indicating status//// this method load non-grammar part of the search level to a file//boolean LanguageModel::loadNonGrammars(Sof& model_file_a, Vector<SearchLevel>& search_levels_a, long level_index_a) { // local variable // Vector<String> temp_strings; // load the level tag // readOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_LEVEL_TAG, temp_strings); if ( temp_strings.length() > 0 ){ String level_tag = temp_strings(0); search_levels_a(level_index_a).setLevelTag(level_tag); temp_strings.clear(); } else{ return Error::handle(name(), L"read - level tag is not specified", Error::ARG, __FILE__, __LINE__); } // load the non speech symbol // readOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_NONSPEECH_SYMBOL, temp_strings); search_levels_a(level_index_a).getNonSpeechSymbolTable().clear(); for ( int i = 0; i < temp_strings.length(); i++){ search_levels_a(level_index_a).getNonSpeechSymbolTable().concat(temp_strings(i)); } temp_strings.clear(); // load the dummy symbols // readOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_DUMMY_SYMBOL, temp_strings); search_levels_a(level_index_a).getDummySymbolTable().clear(); for ( int i = 0; i < temp_strings.length(); i++){ search_levels_a(level_index_a).getDummySymbolTable().concat(temp_strings(i)); } temp_strings.clear(); // load the context less symbols // readOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_CONTEXTLESS_SYMBOL, temp_strings); search_levels_a(level_index_a).getContextLessSymbolTable().clear(); for ( int i = 0; i < temp_strings.length(); i++){ search_levels_a(level_index_a).getContextLessSymbolTable().concat(temp_strings(i)); } temp_strings.clear(); // load the skip symbols // readOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_SKIP_SYMBOL, temp_strings); search_levels_a(level_index_a).getSkipSymbolTable().clear(); for ( int i = 0; i < temp_strings.length(); i++){ search_levels_a(level_index_a).getSkipSymbolTable().concat(temp_strings(i)); } temp_strings.clear(); // load the exclude symbols // readOneRule(model_file_a, level_index_a, L"JSGF", SearchLevel::PARAM_EXCLUDE_SYMBOL, temp_strings); search_levels_a(level_index_a).getExcludeSymbolTable().clear(); for ( int i = 0; i < temp_strings.length(); i++){ search_levels_a(level_index_a).getExcludeSymbolTable().concat(temp_strings(i)); } temp_strings.clear(); // print the debug message // if (debug_level_d >= Integral::BRIEF) { search_levels_a(level_index_a).getNonSpeechSymbolTable().debug(SearchLevel::PARAM_NONSPEECH_SYMBOL); search_levels_a(level_index_a).getDummySymbolTable().debug(SearchLevel::PARAM_DUMMY_SYMBOL); search_levels_a(level_index_a).getContextLessSymbolTable().debug(SearchLevel::PARAM_CONTEXTLESS_SYMBOL); search_levels_a(level_index_a).getSkipSymbolTable().debug(SearchLevel::PARAM_SKIP_SYMBOL); search_levels_a(level_index_a).getSkipSymbolTable().debug(SearchLevel::PARAM_EXCLUDE_SYMBOL); } // exit gracefully // return true;}// method: writeOneRule//// arguments:// Sof& sof: (input) sof file object// long tag: (input) sof object instance tag// const String& name: (input) sof object instance name//// return: boolean value indicating status//// this method has the object write itself to an Sof file//boolean LanguageModel::writeOneRule(Sof& sof_a, long tag_a, const String& format_a, const String& name_a, Vector<String>& rules_a) const { // set the rule // String algorithm; algorithm.assign(format_a); String grammar; grammar.assign(L"\n #JSGF V1.0;\n // Define the ISIP reserved grammar name\n grammar network.grammar."); grammar.concat(name_a); grammar.concat(L";\n\n // Define the symbols\n public <"); grammar.concat(name_a); grammar.concat(L"> = "); for (int i = 0 ; i < rules_a.length(); i++){ grammar.concat(rules_a(i)); grammar.concat(L" "); } grammar.concat(L";\n"); // write to the sof file // grammar.write(sof_a, tag_a, name_a); // exit gracefully // return true;}// method: readOneRule//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name// long size: (input) size in bytes of object (or full_size)// boolean param: (input) is the parameter name in the file?// boolean nested: (input) are we nested?//// return: a boolean value indicating status//// this method read one rule from the sof file//boolean LanguageModel::readOneRule(Sof& sof_a, long tag_a, const String& format_a, const String& name_a, Vector<String>& rule_list_a) { //local variable // JSGFParser jsgf_parser; SofParser parser; String grammar; String algorithm; // read the grammar // if ( !grammar.read(sof_a, tag_a, name_a)){ return false; } // set the JSGF parser // jsgf_parser.setExpression(grammar); jsgf_parser.parseJSGF(); // get the data // rule_list_a.assign(jsgf_parser.getSymbolList()); // exit gracefully // return true;}// method: storeContextMapping//// arguments:// Sof& sof: (input) sof file object// const long tag: (input) sof object instance tag// SearchLevel& level: (input) search level// long level_index: (input) search level index//// return: a boolean value indicating status//// this method has the object store itself from an Sof file according// to the specified name and tag//boolean LanguageModel::storeContextMapping(Sof& sof_a, const String& tag_a, SearchLevel& level_a, long level_index_a) { // local variable // Vector<ContextMap> contexts; Vector<String> context_maps; String jsgf_prefix; // JSGF prefix for the grammar (common to all the contetmaps in JSGF // format) // jsgf_prefix.assign(L"#JSGF V1.0;\n // Define the grammar name\n grammar network.grammar.context;\n\n // Define the context rule\n"); // get the context maps from the searchlevel // contexts = level_a.getContextMap(); // set the length of the context-maps // long len = contexts.length(); context_maps.setCapacity(len); context_maps.setLength(len); // loop over all the context_maps and parse string in each loop // for (long k = 0; k < contexts.length(); k++) { // local variable // String rule_name; String context_map; Vector<SearchSymbol> context; Ulong index; // get the context and the context-index from the current // context-map // context = contexts(k).getContext(); index = contexts(k).getContextIndex(); // set the rule name // rule_name.assign(L" public <"); for (long i = 0; i < context.length(); i++){ // read the rule name // rule_name.concat(context(i)); if ( i != (context.length() - 1)){ rule_name.concat(L"-"); } } rule_name.concat(L"> = G_"); rule_name.concat(index); rule_name.concat(L" ;"); context_map.assign(jsgf_prefix); context_map.concat(rule_name); // add this JSGF contextmap to the output vector // context_maps(k).assign(context_map); } // write the vector of contextmaps to the sof file // context_maps.write(sof_a, level_index_a, SearchLevel::PARAM_CONTEXT_MAPPING); // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -