// file: tp_load_0.cc
//
// function to load the acustic model related data into the decoder
//

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

// method: load_ac_models_cc
//
// arguments: none
//
// return: a logical flag to indicate status
//
// this method loads the acoustic models and LM
// Note: only for Communicator
//
logical_1 Trace_projector::load_models_cc() {

  // This is a method only for communicator to load the models and LM
  // before decoding
  //
  if (isCommunicator_d) {
  
    // load acoustic model related data into the decoder and clean
    // related memory
    //
    fprintf(stdout, "Loading acoustic model data...\n");
    fflush(stdout);
    load_ac_models_cc();
    
    // if ngram required in the specified function mode, load the ngram
    // language model
    //
    if ((function_mode_d == TP_NGRAM_DECODE_FUNCTION) ||
	(function_mode_d == TP_LATTICE_GENERATE_FUNCTION) ||
	(function_mode_d == TP_LATTICE_VERIFY_FUNCTION)) {

      FILE* fp;
      // check if the ngram lm file exists
      //
      if (lm_file_d == (char_1*)NULL) {
	fprintf(stdout, "Error: language model file not specified\n");
	fflush(stdout);
	exit(ISIP_PROTO_ERROR);
      }
      open_file_cc(fp, lm_file_d, (char_1*)"r");
      
      // load the ngram
      //
      fprintf(stdout, "Reading language model data...\n");
      fflush(stdout);
      decoder_d->read_ngram_cc(fp);
      fclose(fp);


      // check if the list of grammars is specified
      //
      if (gram_list_file_d != (char_1*)NULL) {

	// open file
	//
	open_file_cc(fp, gram_list_file_d, (char_1*)"r");

	// load the grammars for hierarchical grammar processing
	//
	decoder_d->read_gram_list_cc(fp);
	fclose(fp);
      }
    }

    // set the user specified lm scaling and word insertion values  
    //
    decoder_d->set_lmscale_cc(lm_scale_d);
    decoder_d->set_wdpenalty_cc(word_penalty_d);
    
    // set the acoustic context mode
    //
    decoder_d->set_context_mode_cc(context_mode_d);
    
    // set the pruning related parameters and clean related memory 
    //
    decoder_d->set_beams_cc(beam_width_d);
    decoder_d->set_mapmi_limit_cc(mapmi_limit_d);
    decoder_d->set_max_words_cc(num_wdcut_d);
    
    // set the nbest pruning related parameters
    //
    decoder_d->set_nbest_maxpaths_cc(nbest_max_paths_d);
    decoder_d->set_nbest_beam_cc(nbest_beam_d);

    // initialize the decoder for hypothesizing utterances
    //
    decoder_d->init_cc();

    // file pointers for processing lists of files
    //
    FILE* fpli = (FILE*)NULL;

    // open the file containing the list of lattice files
    //
    if ((function_mode_d == TP_LATTICE_RESCORE_FUNCTION) ||
	(function_mode_d == TP_LATTICE_LATTICE_FUNCTION) ||
	(function_mode_d == TP_LATTICE_VERIFY_FUNCTION)) {
      
      // check if file exists
      // ???
      if (lattice_file_d == (char_1*)NULL) {
	fprintf(stdout, "Error: input lattice list file not specified\n");
	fflush(stdout);
	exit(ISIP_PROTO_ERROR);
      }
	
      // make sure there is a corresponding lattice file for each file
      //
      open_file_cc(fpli, lattice_file_d, (char_1*)"r");
      
      // read lattice into the decoder and close file
      //
      decoder_d->read_lattice_cc(fpli);
      fclose(fpli);
    }
  }
  
  // return gracefully
  //
  return ISIP_TRUE;
}

logical_1 Trace_projector::init_cc() {

  // file pointers for processing lists of files
  //
  FILE* fpli = (FILE*)NULL;
  
  // open the file containing the list of lattice files
  //
  if ((function_mode_d == TP_LATTICE_RESCORE_FUNCTION) ||
      (function_mode_d == TP_LATTICE_LATTICE_FUNCTION) ||
      (function_mode_d == TP_LATTICE_VERIFY_FUNCTION)) {
    
    // check if file exists
    // ???
    if (lattice_file_d == (char_1*)NULL) {
	fprintf(stdout, "Error: input lattice list file not specified\n");
	fflush(stdout);
	exit(ISIP_PROTO_ERROR);
    }
    
    // make sure there is a corresponding lattice file for each file
    //
    open_file_cc(fpli, lattice_file_d, (char_1*)"r");
    
    // read lattice into the decoder and close file
    //
    decoder_d->read_lattice_cc(fpli);
    fclose(fpli);
  }
  
  // return gracefully
  //
  return ISIP_TRUE;
}
