// file: tp_conf_0.cc
//

// isip include files
//
#include "trace_projector.h"
#include "trace_projector_constants.h"

// method: config_decoder_cc
//
// arguments:
//  logical_1 demo_flag: (input) if running in the demo mode
//  logical_1 isCommunicator: (input) if it is for communicator application
//
// return: a logical flag to indicate success
//
logical_1 Trace_projector::config_decoder_cc(logical_1 demo_flag_a,
					     logical_1 isCommunicator_a) {

  // update the context size if this is monophone mode
  //
  if (context_mode_d == TP_MONO_PHONE_MODE) {
    context_size_d = TP_MONOPHONE_SIZE;
  }
  
  // create a memory manager
  //
  Memory_manager* manager = new Memory_manager(TP_DEFAULT_BLOCK_SIZE,
					       TP_NODE_BLOCK_SIZE);

  // set it in the decoder
  //
  decoder_d->set_manager_cc(manager);

  // set the number of levels and context size, as well as the lm
  // order
  //
  decoder_d->set_num_levels_cc(num_levels_d);
  decoder_d->set_context_size_cc(context_size_d);
  decoder_d->set_ngram_order_cc(ngram_size_d);
  
  // set the decoder's function mode and alignment mode
  //
  decoder_d->set_context_mode_cc(context_mode_d);
  decoder_d->set_function_mode_cc(function_mode_d);
  decoder_d->set_align_mode_cc(align_mode_d);
  decoder_d->set_segment_mode_cc(segment_mode_d);
  decoder_d->set_input_format_cc(input_format_d);
  decoder_d->set_input_feature_format_cc(input_feature_format_d);
  decoder_d->set_delta_win_cc(delta_win_d);
  decoder_d->set_delta_cc(delta_d);
  decoder_d->set_acc_cc(acc_d);
  decoder_d->set_demo_mode_cc(demo_flag_a);
  
  // set the number of partial hypothesis needed in demo mode
  //
  decoder_d->set_num_nbest_cc(num_nbest_d);

  // set the number of back pointers to be used for lattice generation
  //
  decoder_d->set_num_hist_cc(num_hist_d);

  // set the length of each history, this has to be done after the
  // the function mode and ngram order being set
  //
  decoder_d->set_hist_length_cc();  
  
  // set the flag of compact_lattice
  //
  decoder_d->set_compact_lattice_cc(compact_lattice_d);

  // read the phonetic symbol set
  //
  if (monophones_file_d == (char_1*)NULL) {
    fprintf(stdout, "Error: monophones file not specified\n");
    exit(ISIP_PROTO_ERROR);
  }
  FILE* fp = (FILE*)NULL;
  open_file_cc(fp, monophones_file_d, (char_1*)"r");

  fprintf(stdout, "Reading phonetic symbol set...\n");
  decoder_d->read_ci_models_cc(fp);
  fclose(fp);

  // read the lexicon
  //
  if (lexicon_file_d == (char_1*)NULL) {
    fprintf(stdout, "Error: lexicon file not specified\n");
    exit(ISIP_PROTO_ERROR);
  }
  fp = (FILE*)NULL;
  open_file_cc(fp, lexicon_file_d, (char_1*)"r");
  
  fprintf(stdout, "Reading lexicon...\n");
  decoder_d->read_lexicon_cc(fp);
  fclose(fp);

  if (isCommunicator_a) {

    // set flag for communicator
    //
    isCommunicator_d = ISIP_TRUE;

    // load AM and LM before decoding
    //
    load_models_cc();
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
