📄 hmm_05.cc
字号:
// file: $isip/class/pr/HiddenMarkovModel/hmm_05.cc// version: $Id: hmm_05.cc,v 1.122 2003/05/12 22:41:53 huang Exp $//// isip include files//#include "HiddenMarkovModel.h"#include <CommandLine.h>#include <LanguageModel.h>// method: store//// arguments: none//// return: a boolean value indicating status//// this method stores the HMM models//boolean HiddenMarkovModel::store() { // write the models to file // LanguageModel lm_model; // when the output format is NATIVE // if (output_format_d == NATIVE) { // open the model file for i\o (TEXT) // if (output_type_d == TEXT) { if (!lm_model.store(update_lm_model_file_d, update_ac_model_file_d, search_engine_d.getSearchLevels(), LanguageModel::NATIVE, LanguageModel::TEXT)) { return Error::handle(name(), L"store - error writing models to file", Error::ARG, __FILE__, __LINE__); } } // open the model file for i\o (BINARY) // if (output_type_d == BINARY) { if (!lm_model.store(update_lm_model_file_d, update_ac_model_file_d, search_engine_d.getSearchLevels(), LanguageModel::NATIVE, LanguageModel::BINARY)) { return Error::handle(name(), L"store - error writing models to file", Error::ARG, __FILE__, __LINE__); } } } // when the output format is JSGF // else if (output_format_d == JSGF) { // open the model file for i\o (TEXT) // if (output_type_d == TEXT) { if (!lm_model.store(update_lm_model_file_d, update_ac_model_file_d, search_engine_d.getSearchLevels(), LanguageModel::JSGF, LanguageModel::TEXT)) { return Error::handle(name(), L"store - error writing models to file", Error::ARG, __FILE__, __LINE__); } } // open the model file for i\o (BINARY) // if (output_type_d == BINARY) { if (!lm_model.store(update_lm_model_file_d, update_ac_model_file_d, search_engine_d.getSearchLevels(), LanguageModel::JSGF, LanguageModel::BINARY)) { return Error::handle(name(), L"store - error writing models to file", Error::ARG, __FILE__, __LINE__); } } } // when the output format is not supported // else { return Error::handle(name(), L"store - invalid output format", Error::ARG, __FILE__, __LINE__); } // write the symbol occupancy for each SearchLevel (optional) // for (long curr_level = 0; curr_level < num_levels_d; curr_level++) { // have the search levels write the symbol occupancies // SearchLevel& search_level = search_engine_d.getSearchLevel(curr_level); search_level.storeOccupancies(); } // close the audio database (optional) // if (audio_db_file_d.length() > 0) { if (!audio_db_d.close()) { return Error::handle(name(), L"store", Error::ARG, __FILE__, __LINE__); } } // close the transcription database (optional) // if (transcription_db_file_d.length() > 0) { if (!transcription_db_d.close()) { return Error::handle(name(), L"store", Error::ARG, __FILE__, __LINE__); } } // exit gracefully // return true;}// method: parseLevels//// const String& source: (input) levels string// VectorByte& vector: (output) string converted to a vector//// return: a boolean value indicating status//// this method converts the string to a vector of level// indices after configuring SearchLevels//boolean HiddenMarkovModel::parseLevels(const String& source_a, VectorByte& vector_a) { // allocate space to the number of levels in the search space // vector_a.setLength(num_levels_d); // pad with all OFF's // vector_a.assign((byte)OFF); // count the number of sub-strings separated by ',' // long tokens = source_a.countTokens(L','); // get all the sub-strings separated by ',' // String vals[tokens]; for (long i = 0, pos = 0; i < tokens; i++) { source_a.tokenize(vals[i], pos, L","); } // compute the number of all the sub-strings (levels) in input // string seperated by the delimiter ':' // long num = 0; for (long i = 0; i < tokens; i++) { num += vals[i].countTokens(L':'); } // local variables // Vector<String> levels; // loop through all the sub-strings // for (long i = 0; i < tokens; i++) { // local variables // String level; long pos = 0; // clear the levels from the previous loop // levels.clear(Integral::RELEASE); // parse the input in the format based on the endlimiter ":" // while (vals[i].tokenize(level, pos, L":")) { // get rid of white spaces if any // level.trim(); // get the levels in numericals // long len = levels.length(); levels.setLength(len + (long)1); levels(len).assign(level); } // local variables // VectorLong indices; long len = levels.length(); long count = 0; indices.setLength(len); // get the level values corresponding to the SearchLevel tags // for (long curr_level = 0; curr_level < num_levels_d; curr_level++) { if (algorithm_d == DECODE && implementation_d == STACK) { SearchLevel& search_level = stack_engine_d.getSearchLevel(curr_level); for (long i = 0; i < len; i++) { if ( levels(i).eq(search_level.getLevelTag()) ) { indices(count) = curr_level; count++; continue; } } } else { SearchLevel& search_level = search_engine_d.getSearchLevel(curr_level); for (long i = 0; i < len; i++) { if ( levels(i).eq(search_level.getLevelTag()) ) { indices(count) = curr_level; count++; continue; } } } } if (count != len) { return Error::handle(name(), L"parseLevels - invalid string", Error::ARG, __FILE__, __LINE__); } // find the local min and maximium index // long min_index = indices.min(); long max_index = indices.max(); // get the missing levels between the min and max indices, and set // them to ON // for (long j = min_index; j <= max_index; j++) { vector_a(j) = ON; } } // exit gracefully // return true;}// method: load//// arguments: none//// return: a boolean value indicating status//// this method loads the HMM models//boolean HiddenMarkovModel::load() { // declare local variables // String output; // load the audio database (optional) // if (audio_db_file_d.length() > 0) { if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\nloading audio database: "); output.concat(audio_db_file_d); Console::put(output); Console::decreaseIndention(); } // load the audio database // if (!audio_db_d.open(audio_db_file_d)) { audio_db_file_d.debug(L"audio_db_file_d"); return Error::handle(name(), L"run: open audio database file", Error::ARG, __FILE__, __LINE__); } } else { if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\n*** no audio database file was specified ***"); Console::put(output); Console::decreaseIndention(); } } // load the transcription database (optional) // if (transcription_db_file_d.length() > 0) { if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\nloading transcription database: "); output.concat(transcription_db_file_d); Console::put(output); Console::decreaseIndention(); } // load the transcriptions // if (!transcription_db_d.open(transcription_db_file_d)) { transcription_db_file_d.debug(L"transcription_db_file_d"); return Error::handle(name(), L"error opening transcription database file", Error::ARG, __FILE__, __LINE__); } } else { if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\n*** no transcription database file was specified ***"); Console::put(output); Console::decreaseIndention(); } } // load the front-end parameters (optional) // if (fend_file_d.length() > 0) { if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\nloading front-end: "); output.concat(fend_file_d); Console::put(output); Console::decreaseIndention(); } Sof fend_file_sof; if (!fend_file_sof.open(fend_file_d)) { return Error::handle(name(), L"load - unable to open front-end file", Error::ARG, __FILE__, __LINE__); } // read the FrontEnd parameters // if (!fe_d.read(fend_file_sof, fend_file_sof.first(fe_d.name()))) { return Error::handle(name(), L"load - error reading front-end file", Error::ARG, __FILE__, __LINE__); } // close the fornt-end file // fend_file_sof.close(); } else { if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\n*** no front-end file was specified ***"); Console::put(output); Console::decreaseIndention(); } } // set up the number of levels for the search engine // search_engine_d.setNumLevels(num_levels_d); // load the configuration parameters (optional) // if (cnfg_file_d.length() > 0) { // open the configuration file // Sof cnfg_file_sof; if (!cnfg_file_sof.open(cnfg_file_d)) { return Error::handle(name(), L"load - unable to open configuration file", Error::ARG, __FILE__, __LINE__); } if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\nloading configuration file: "); output.concat(cnfg_file_d); Console::put(output); Console::decreaseIndention(); } // have the search levels read in its own configuration information // for (long curr_level = 0; curr_level < num_levels_d; curr_level++) { if (algorithm_d == DECODE && implementation_d == STACK) { // configure stack search engine // SearchLevel& search_level = stack_engine_d.getSearchLevel(curr_level); // read the configuration parameters corresponding to this level // search_level.read(cnfg_file_sof, curr_level); } else { // configure Viterbi search engine // SearchLevel& search_level = search_engine_d.getSearchLevel(curr_level); // read the configuration parameters corresponding to this level // search_level.read(cnfg_file_sof, curr_level); } } // close the configuration files // cnfg_file_sof.close(); } else { if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\n*** no configuration file was specified ***"); Console::put(output); Console::decreaseIndention(); } } // check the language model // if (lm_model_file_d.length() == 0) { return Error::handle(name(), L"load - invalid language model file", Error::ARG, __FILE__, __LINE__); } if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\nloading language model: "); output.concat(lm_model_file_d); Console::put(output); Console::decreaseIndention(); } // check the acoustic model // if (ac_model_file_d.length() == 0) { return Error::handle(name(), L"load - invalid acoustic model file", Error::ARG, __FILE__, __LINE__); } if (verbosity_d >= Integral::BRIEF) { Console::increaseIndention(); output.assign(L"\nloading acoustic model: "); output.concat(ac_model_file_d); Console::put(output); Console::decreaseIndention(); } // load the models and set them to the search engine // LanguageModel lm_model; if (!(algorithm_d == DECODE && implementation_d == STACK)) { if (!lm_model.load(lm_model_file_d, ac_model_file_d, search_engine_d.getSearchLevels())) { return Error::handle(name(), L"load: loading model file", Error::ARG, __FILE__, __LINE__); } // make sure the correct number of levels were specified // if ((long)num_levels_d != search_engine_d.getSearchLevels().length()) { return Error::handle(name(), L"load - invalid number of levels specified", Error::ARG, __FILE__, __LINE__); } } else { if (!lm_model.load(lm_model_file_d, ac_model_file_d, stack_engine_d.getSearchLevels())) { return Error::handle(name(), L"load: loading model file", Error::ARG, __FILE__, __LINE__); } // make sure the correct number of levels were specified // if ((long)num_levels_d != stack_engine_d.getSearchLevels().length()) { return Error::handle(name(), L"load - invalid number of levels specified", Error::ARG, __FILE__, __LINE__); } } // loop over all search levels and set the initial level // for (long level = 0; level < (long)num_levels_d; level++) { SearchLevel& search_level = search_engine_d.getSearchLevel(level); if (search_level.getLevelTag().eq(transcription_level_d)) { initial_level_d = level; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -