📄 isip_lm_tester.cc
字号:
// file: $isip/util/speech/isip_lm_tester/isip_lm_tester.cc// version: $Id: isip_lm_tester.cc,v 1.13 2003/05/14 15:36:01 zheng Exp $//// isip include files//#include "isip_lm_tester.h"// isip_lm_tester: language model tester utility//// this is the general language model tester utility. it can be used for// randomly generated sentences based on the language model file//int main(int argc, const char** argv) { // ccommand line parser and an Sdb object. // Sdb sdb; CommandLine cmdl(sdb); cmdl.setUsage(#include "usage_message.text" ); cmdl.setHelp(#include "help_message.text" ); cmdl.setIdent("$Revision: 1.13 $", "$Name: isip_r00_n11 $", "$Date: 2003/05/14 15:36:01 $"); // add a command line option for the grammar file // Filename grammar_file; Filename def_file(L"my_grammar.sof"); cmdl.addOptionParam(grammar_file, PARAM_GRAMMAR_NAME, def_file); // add a command line option for the output database file // Filename output_file; def_file.assign(L""); cmdl.addOptionParam(output_file, PARAM_OUTPUT_NAME, def_file); // add a command line option for the output type // String output_type_str; def_file.assign(L"text"); cmdl.addOptionParam(output_type_str, PARAM_OUTPUT_TYPE, def_file); // add a command line option for the output database name // String db_name; def_file.assign(L""); cmdl.addOptionParam(db_name, PARAM_DB_NAME, def_file); // add a command line option for the debug level // DebugLevel debug_level; cmdl.addOptionParam(debug_level); // add a command line option for random seed // Long random_seed; cmdl.addOptionParam(random_seed, PARAM_RANDOM_SEED, DEF_RANDOM_SEED); // add a command line option for number of sentences // Long sent_number; cmdl.addOptionParam(sent_number, PARAM_SENTENCE_NUMBER, DEF_SENTENCE_NUMBER); // add a command line option for the sentence length // Long sent_length; cmdl.addOptionParam(sent_length, PARAM_SENTENCE_LENGTH, DEF_SENTENCE_LENGTH); // add a command line option for the output level // String output_level_tag; cmdl.addOptionParam(output_level_tag, PARAM_OUTPUT_LEVEL, EMPTY); // parse the command line // if (!cmdl.parse(argc, argv)) { cmdl.printUsage(); }; // check the grammar file argument // if(grammar_file.eq(NULL)) { cmdl.printUsage(); }; // verify that the user has specified the output file type // File::TYPE output_type = DEF_OUTPUT_TYPE; if (output_type_str.eq(File::TYPE_TEXT)) { output_type = File::TEXT; } else if (output_type_str.eq(File::TYPE_BINARY)) { output_type = File::BINARY; } // open the output database file name // Sof out_fp; if(!output_file.eq(NULL)) { out_fp.open(output_file, File::WRITE_ONLY, output_type); // check if the DB name is told // if(db_name.eq(NULL)) { String msg(L"Error: Transcription DB name is specified! "); Console::put(msg); Error::handle(PROG_NAME, L"main", Error::NO_PARAM_FILE, __FILE__, __LINE__); } }; // initialize random generator // Random random_gen; random_gen.setAlgorithm(Random::UNIFORM); random_gen.setImplementation(Random::SUBTRACTIVE); // check if the random seed is input, otherwise generate it from time // if (random_seed == DEF_RANDOM_SEED) { random_seed = (Long)Integral::time(); random_gen.seed((long)random_seed); random_seed.rand(random_gen); } // seed the random variable // random_gen.seed((long)random_seed); // load grammar file // String model_file_a; model_file_a.assign(grammar_file); LanguageModel language_model; Vector<SearchLevel> search_levels; language_model.load(model_file_a, search_levels); // get the output level // Long output_level; if (output_level_tag.eq(NULL)) { output_level = search_levels.length() - 1; output_level_tag.assign(search_levels(output_level).getLevelTag()); } else { for (output_level = 0; output_level < search_levels.length(); output_level++) { if (output_level_tag.eq(search_levels(output_level).getLevelTag())) { break; } } // verify if the output level is valid // if (output_level.eq(search_levels.length())) { String msg(L"Error: the specified output level is invalid! "); Console::put(msg); Error::handle(PROG_NAME, L"main", Error::NO_PARAM_FILE, __FILE__, __LINE__); } } // local variables // Vector<String> symbol_table; TranscriptionDatabase transdb; String id; String sentence; boolean is_id_file; // set database name // transdb.setDataBaseName(db_name); // if id list file exists // if (sdb.gotoFirst()) { is_id_file = true; sent_number = sdb.length(); sdb.gotoFirst(); } else { is_id_file = false; } // create a vector table for identifiers // symbol_table.setLength((long)sent_number); // display debug information // if (debug_level >= Integral::BRIEF) { random_seed.debug(L"random_seed"); sent_number.debug(L"sentence_count"); sent_length.debug(L"max_length"); output_level.debug(L"output_level"); output_level_tag.debug(L"output_level_tag"); db_name.debug(L"db_name"); String value; value.assign(output_type); value.debug(L"output_type"); } // generate sentence specified by the grammar and the probabilty // assigned to each corresponding arc // for (long i = 0; i < (long)sent_number; i++) { GenRandomSentences((long)sent_length, random_gen, output_level, search_levels, sentence); // save the identifier in the symbol-table // if (out_fp.isOpen()) { if (is_id_file) { sdb.getName((Filename&)id); sdb.gotoNext(); } else { id.assign(i); } symbol_table(i).assign(id); transdb.storePartial(sentence, db_name, output_level_tag, i, out_fp, 0); } if (!out_fp.isOpen() || debug_level >= Integral::BRIEF) { Console::put(sentence); } } // write the symbol table and close output file // if (out_fp.isOpen()) { transdb.storePartial(out_fp, (long)0, symbol_table); out_fp.close(); } // exit gracefully // return Integral::exit();}// generates sentence specified by grammar and probabilty assigned for each arc//void GenRandomSentences(long sent_len_a, Random& random_gen_a, long output_level_a, Vector<SearchLevel>& search_levels_a, String& sentence_a) { // declare local variables // Context tmp_context(1, 1); ContextPool context_pool; GraphVertex<SearchNode>* v = (GraphVertex<SearchNode>*)NULL; GraphVertex<SearchNode>* nv = (GraphVertex<SearchNode>*)NULL; // sanity check // if (search_levels_a.length() < 1) { return; } // initialize the trace // v = search_levels_a(0).getSubGraph(0).getStart(); tmp_context.assignAndAdvance((ulong)v); // create a new trace // Trace* curr_trace = new Trace(); curr_trace->setBackPointer((Trace*)NULL); // set the symbol // curr_trace->setSymbol(context_pool.get(tmp_context)); // set the history // History* history = new History(); curr_trace->setHistory(history); // loop untill we are done // boolean level = 0; boolean sent_len = 0; boolean ascend = false; boolean status = true; while (status) { status = false; // have we reached the max sentence length // if (sent_len >= sent_len_a) { break; } //curr_trace->getSymbol()->print(); v = curr_trace->getSymbol()->getCentralVertex(); // can we descend a level? // // if ((level != output_level_a) && !status && !ascend) { if ((level != output_level_a ) && !status && !ascend) { // ignore start and term vertices // if (!v->isStart() && !v->isTerm()) { // does this vertex have a subgraph? // Ulong* subgr_ind = (Ulong*)NULL; Context* tmp_symbol = (Context*)NULL; curr_trace->getSymbol()->convert(tmp_symbol); subgr_ind = search_levels_a(level).getSubGraphIndex(*tmp_symbol); if (subgr_ind != (Ulong*)NULL) { // tag the context // curr_trace->getSymbol()->setExtendedLength(1); status = true; ascend = false; v = (search_levels_a(level + 1).getSubGraph((long)*subgr_ind)).getStart(); // create a new trace // Trace* next_trace = new Trace(*curr_trace); next_trace->setBackPointer(curr_trace); // set the symbol // tmp_context.assignAndAdvance((ulong)v); //tmp_context.print(); next_trace->setSymbol(context_pool.get(tmp_context)); // set the history // next_trace->getHistory()->push(curr_trace->getSymbol()); curr_trace = next_trace; level++; } // free memory // if (tmp_symbol != (Context*)NULL) { delete tmp_symbol; } } } // can we ascend a level? // if ((level != 0) && !status) { // consider only terminals // if (v->isTerm()) { status = true; ascend = true; v = curr_trace->getHistory()->peek()->getCentralVertex(); // create a new trace // Trace* next_trace = new Trace(*curr_trace); next_trace->setBackPointer(curr_trace); // set the symbol // tmp_context.assignAndAdvance((ulong)v); //tmp_context.print(); next_trace->setSymbol(context_pool.get(tmp_context)); // set the history // next_trace->getHistory()->pop(); curr_trace = next_trace; level--; } } // can we move forward // if ((v->length() > 0) && !status) { // create a new trace // Trace* next_trace = new Trace(*curr_trace); next_trace->setBackPointer(curr_trace); double score = 0.0; double prob = random_gen_a.get(); for (boolean more = v->gotoFirst(); more; more = v->gotoNext()) { double weight = v->getCurr()->getWeight(); nv = v->getCurr()->getVertex(); if ((prob >= score) && (prob < (score + weight))) { // set the symbol // sent_len++; tmp_context.assignAndAdvance((ulong)nv); //tmp_context.print(); next_trace->setSymbol(context_pool.get(tmp_context)); ascend = false; status = true; break; } score = score + weight; } curr_trace = next_trace; } } Trace* tmp_trace = curr_trace; DoubleLinkedList<Trace> trace_list(DstrBase::USER); while (curr_trace != (Trace*)NULL) { trace_list.insertFirst(curr_trace); curr_trace = curr_trace->getBackPointer(); } sentence_a.clear(); for (boolean more = trace_list.gotoFirst(); more; more = trace_list.gotoNext()) { curr_trace = trace_list.getCurr(); v = curr_trace->getSymbol()->getCentralVertex(); if (curr_trace->getSymbol()->getExtendedLength() < 1) { if (!v->isStart() && !v->isTerm()) { String symbol; curr_trace->getSymbol()->print(symbol); sentence_a.concat(symbol); sentence_a.concat(L" "); } } } // free memory // Trace::deleteTrace(tmp_trace, true); delete history; history = (History*)NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -