📄 slev_06.cc
字号:
// file: $isip/class/search/SearchLevel/slev_06.cc// version: $Id: slev_06.cc,v 1.43 2003/04/24 18:32:28 alphonso Exp $//// isip include files//#include "SearchLevel.h"#ifndef ISIP_SOF#include <Sof.h>#endif// method: generateContextMapping//// arguments:// long left_order: (input) left context order// long right_order: (input) right context order// Filename& context_list: (input) context list file name// Vector<SearchLevel>& search_levels: (input) vector of search levels//// return: a boolean indicating status//// this method generates the comtext map for the current search level//boolean SearchLevel::generateContextMapping(long left_order_a, long right_order_a, Filename& context_list_a, Vector<SearchLevel>& search_levels_a, boolean wint_flag_a) { // declare local variables // Filename list_item; ContextMap context_map; Vector<SearchSymbol> context; Sof input_sof; SearchSymbol item; Sdb symbol_list_sdb; SearchSymbol substring; SearchSymbol central_symbol; // context map cannot be generate for the terminal (acoustic) level // if ((long)level_index_d == search_levels_a.length() - 1) { return Error::handle(name(), L"generateContextMapping", Error::ARG, __FILE__, __LINE__); } // open the context list // if (!input_sof.open(context_list_a)) { return Error::handle(name(), L"generateContextMapping", Error::ARG, __FILE__, __LINE__); } // read the sdb list // symbol_list_sdb.read(input_sof, (long)level_index_d); input_sof.close(); // if the list is empty do nothing // if (symbol_list_sdb.length() == 0) { context_list_a.debug(L"context list"); level_index_d.debug(L"search level"); Error::handle(name(), L"generateContextMapping - cannot find the context list corresponding to the specified search level", Error::ARG, __FILE__, __LINE__); return true; } // loop over each element in the sdb list // long index1 = 0; Boolean tmp_value; HashTable<SearchSymbol, Boolean> tmp_hash; // set the capacity of the context map // long num_symbols = symbol_table_d.length(); long capacity = (long)Integral::pow((double)num_symbols, (double)(left_order_a + right_order_a + 1)); context_map_d.setCapacity(capacity); for (symbol_list_sdb.gotoFirst(); symbol_list_sdb.getName(list_item); symbol_list_sdb.gotoNext()) { // get the current context form the list // list_item.getName(item); item.trim(); // get the number of left and right symbols in the context // long index2 = 0; long left = item.countTokens(PARAM_DELIM1) - 1; long right = item.countTokens(PARAM_DELIM2) - 1; // set the context length // context.setLength(left_order_a + right_order_a + 1); // when the left context is less that the order prepend a no left context // if (left < left_order_a) { for (int i=left; i < left_order_a; i++) { if (index2 >= context.length()) { item.debug(L"context"); Long lorder(left_order_a); lorder.debug(L"left context length"); return Error::handle(name(), L"generateContextMapping - symbol context length is greater that the one specified at the search level", Error::ARG, __FILE__, __LINE__); } context(index2++).assign(SearchSymbol::NO_LEFT_CONTEXT); } } // retrieve each symbol of the context and set up the map // long pos = 0; long curr_symbol = 0; boolean contextless_symbol = false; while (item.tokenize(substring, pos, PARAM_DELIM3)) { // check if the symbol is present in the symbol table // substring.trim(); if (!isValidSymbol(getSymbolIndex(substring))) { substring.debug(L"symbol"); return Error::handle(name(), L"generateContextMapping", Error::ARG, __FILE__, __LINE__); } // check if the symbol is the central context // if (curr_symbol == left_order_a) { // is this symbol without context // if (isContextLessSymbol(getSymbolIndex(substring))) { contextless_symbol = true; } } // assign the symbol to the context // curr_symbol++; if (index2 >= context.length()) { item.debug(L"context"); Long lorder(left_order_a); lorder.debug(L"left context length"); Long rorder(right_order_a); rorder.debug(L"right context length"); return Error::handle(name(), L"generateContextMapping - symbol context length is greater that the one specified at the search level", Error::ARG, __FILE__, __LINE__); } context(index2++).assign(substring); } // when the right context is less that the order append a no right context // if (right < right_order_a) { for (int i=right; i < right_order_a; i++) { if (index2 >= context.length()) { item.debug(L"context"); Long rorder(right_order_a); rorder.debug(L"right context length"); return Error::handle(name(), L"generateContextMapping - symbol context length is greater that the one specified at the search level", Error::ARG, __FILE__, __LINE__); } context(index2++).assign(SearchSymbol::NO_RIGHT_CONTEXT); } } // assign the current context to the context map (optional) // if (!contextless_symbol || wint_flag_a) { // retrieve the central context symbol // central_symbol.assign(context(left_order_a)); context_map.setContext(context); if (tmp_hash.containsKey(central_symbol)) { createCopy(central_symbol, context_map, search_levels_a); } else { tmp_hash.insert(central_symbol, &tmp_value); context_map.setContextIndex((ulong)getSymbolIndex(central_symbol)); } context_map_d.setLength(index1 + 1); context_map_d(index1++).assign(context_map); } } // map all context less symbols to their central context only // context.setLength(left_order_a + right_order_a + 1); for (long i=0; i < contextless_symbol_table_d.length(); i++) { // set the left context // long index2 = 0; for (int j=0; j < left_order_a; j++) { context(index2++).assign(SearchSymbol::NO_LEFT_CONTEXT); } // set the central context // central_symbol.assign(contextless_symbol_table_d(i)); context(index2++).assign(central_symbol); // set the right context // for (int j=0; j < right_order_a; j++) { context(index2++).assign(SearchSymbol::NO_RIGHT_CONTEXT); } if (!tmp_hash.containsKey(central_symbol)) { tmp_hash.insert(central_symbol, &tmp_value); context_map.setContext(context); context_map.setContextIndex((ulong)getSymbolIndex(central_symbol)); context_map_d.setLength(index1 + 1); context_map_d(index1++).assign(context_map); } } // exit gracefully // return true;}// method: createCopy//// arguments:// SearchSymbol& central_symbol: (input) symbol of central context// ContextMap& context_map: (input) contex map of the central symbol// Vector<SearchLevel>& search_levels: (input) all the search levels//// return: a boolean indicating status//// this method creates a copy of underlying models for every context that// shares the same central search symbol//boolean SearchLevel::createCopy(SearchSymbol& central_symbol_a, ContextMap& context_map_a, Vector<SearchLevel>& search_levels_a) { // declare local variables // long tmp_index = 0; long curr_index = 0; long symbol_index = 0; long central_index = 0; long stat_model_index = 0; Long val; StatisticalModel model_copy; DiGraph<SearchNode> graph_copy; // retrieve the symbol index of the central context symbol // central_index = getSymbolIndex(central_symbol_a); // retrieve the symbol table of the next level in the hierarchy // Vector<SearchSymbol>& symbol_table = search_levels_a((long)level_index_d + 1).getSymbolTable(); // retrieve the subgraphs of the next level in the hierarchy // Vector<DiGraph<SearchNode> >& sub_graphs = search_levels_a((long)level_index_d + 1).getSubGraphs(); // retrieve the stat models of the next level in the hierarchy // Vector<StatisticalModel>& stat_models = search_levels_a((long)level_index_d + 1).getStatisticalModels(); // retrieve the symbol hash table of the next level in the hierarchy // HashTable<SearchSymbol, Long>& symbol_hash = search_levels_a((long)level_index_d + 1).getSymbolHashTable(); // create a copy of the subgraph corresponding to the central contex symbol // and append the copy to the vector of subgraphs // sub_graphs(central_index).setAllocationMode(DstrBase::SYSTEM); graph_copy.assign(sub_graphs(central_index)); graph_copy.setAllocationMode(DstrBase::USER); sub_graphs(central_index).setAllocationMode(DstrBase::USER); sub_graphs.concat(graph_copy); // set the context map index to the position of the appended copy // curr_index = sub_graphs.length() - 1; context_map_a.setContextIndex((ulong)curr_index); // make copies of the states and models (only at the acoustic level) // if ((long)level_index_d == search_levels_a.length() - 2) { // loop over each vertex in the subgraph of the appended copy // SearchSymbol ss; for (boolean more = sub_graphs(curr_index).gotoFirst(); more; more = sub_graphs(curr_index).gotoNext()) { // retrieve the symbol index corresponding to the current vertex // symbol_index = sub_graphs(curr_index).getCurr()->getItem()->getSymbolId(); // create a new search symbol and append it to the symbol table // ss.assign(L"S_"); tmp_index = (long)symbol_table.length() + 1; ss.concat(tmp_index); while (symbol_table.contains(&ss)) { ss.assign(L"S_"); ss.concat(++tmp_index); } symbol_table.concat(ss); // set the symbol id for the search node // val.assign(symbol_table.length() - 1); sub_graphs(curr_index).getCurr()->getItem()->setSymbolId((long)val); // make a copy of the statistical model if applicable // if (stat_models.length() > 0) { // make a copy of the statistical model // stat_model_index = (long)(*symbol_hash.get(symbol_table(symbol_index))); model_copy.assign(stat_models(stat_model_index)); stat_models.concat(model_copy); // create a new entry in the symbol hash table // val.assign(stat_models.length() - 1); symbol_hash.insert(ss, &val); } else { // create a new entry in the symbol hash table // symbol_hash.insert(ss, &val); } } } // exit gracefully // return true;}// method: loadTransitionAccumulators//// arguments:// Sof& sof: (input) sof file object// long tag: (input) sof object instance tag//// return: a boolean indicating status//// this method reads the transition accumulators from file//boolean SearchLevel::loadTransitionAccumulators(Sof& sof_a, long tag_a) { // if no tag is specified, the tag should be this level number // if (tag_a == DEF_TAG) { tag_a = (long)level_index_d; } // make sure we skip the top level // if (tag_a == 0) { return true; } // make sure there are valid sub graphs at this level // if (sub_graphs_d.length() == 0) { return true; } // declare a temporary holder for the Graph<Ulong> and read them from file // Vector< DiGraph<Ulong> > tmp_graphs; if (!tmp_graphs.read(sof_a, tag_a, PARAM_MODEL)) { return false; } long num_graphs = tmp_graphs.length(); if (num_graphs != sub_graphs_d.length()) { return Error::handle(name(), L"loadTransitionAccumulators - number of sub graphs loaded from the accumulator file does not match the number of sub graphs at this level", Error::ARG, __FILE__, __LINE__); } // loop over each sub graph in tmp_graphs // for (long k = 0; k < num_graphs; k++) { long num_vertices = tmp_graphs(k).length(); if (num_vertices != sub_graphs_d(k).length()) { return Error::handle(name(), L"loadTransitionAccumulators - number of vertices loaded from the accumulator file does not match the number of vertices at this level", Error::ARG, __FILE__, __LINE__); } // loop over each vertex adjacent to the start vertex // GraphVertex<Ulong>* tmp_graph_vertex = tmp_graphs(k).getStart(); GraphVertex<SearchNode>* sub_graph_vertex = sub_graphs_d(k).getStart(); long num_arcs = tmp_graph_vertex->length(); if (num_arcs != sub_graph_vertex->length()) { return Error::handle(name(), L"loadTransitionAccumulators - number of arcs loaded from the accumulator file does not match the number of arcs at this level", Error::ARG, __FILE__, __LINE__); } // loop over each arc corresponding to the current vertex // boolean arc_more = false; for (arc_more = (tmp_graph_vertex->gotoFirst() & sub_graph_vertex->gotoFirst()); arc_more; arc_more = (tmp_graph_vertex->gotoNext() & sub_graph_vertex->gotoNext())) { // read the accumulator value // sub_graph_vertex->getCurr()->setAccumulator(sub_graph_vertex->getCurr()->getAccumulator() + tmp_graph_vertex->getCurr()->getWeight()); } // loop over each vertex in the current subgraph // boolean vertex_more = false; for (vertex_more = (sub_graphs_d(k).gotoFirst() & tmp_graphs(k).gotoFirst());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -