// file: bw_model_1.cc
//

// system include files
//
#include <string.h>

// isip include files
//
#include "bw_train.h"
#include "bw_train_constants.h"

// method: get_model_list_cc
//
// arguments:
//  int_4& num_mod : (output) the number of phones(models) in the current label
//  int_4*& model_list : (output) the array containing the model index
//  int_4& num_state : (output) the number of total emitting states in the
//                              model list
//  char_1* lab_file : (input) the word string
//  char_1** monophones : (input) the monophones
//  int_4 num_mono : (input) the number of monophones
//  int_4 ph_size : (input) the context size
//  int_4 context_mode : (input) the current context mode
//  int_4* phones_map : (input) the mapping from phone index to line index
//  int_4* mod_map : (input) the mapping from the line index to model index
//  Model** models : (input) all models
//  int_4* model_access_counts : (input) the number of times a model is used
//
// return: a logical flag to indicate success
//
// this method loops over all the words in the input string and return the
// model index according to the context mode
//
logical_1 get_model_list_cc(int_4& num_mod_a, int_4*& model_list_a,
			    int_4& num_state_a, char_1* lab_file_a,
			    char_1** monophones_a, int_4 num_mono_a,
			    int_4 ph_size_a, int_4 context_mode_a,
			    int_4* phones_map_a, int_4* mod_map_a,
			    Train_Model** models_a, int_4* model_access_counts_a) {

  // string to hold temporary data
  //
  char_1 *tmp, *tmp_str, *tmp_str1, **phns_list;
  int_4 ph_index = 0;
  int_4 num_st = 0;
  num_mod_a = (int_4)0;
  num_state_a = (int_4)0;

  // allocate memory for model list
  //
  tmp_str = new char_1[strlen((char*)lab_file_a)+1];
  strcpy((char*)tmp_str, (char*)lab_file_a);
  tmp_str1 = (char_1*)strtok((char*)tmp_str, ISIP_STRING_SPACE);
  while((tmp_str1 != (char_1*)NULL) && (tmp_str1[0] != ISIP_NEWLINE)) {
    num_mod_a++;
    tmp_str1 = (char_1*)strtok(NULL, ISIP_STRING_SPACE);
  }
  model_list_a = new int_4[num_mod_a];
  memset(model_list_a, (int_4)0, num_mod_a * sizeof(int_4));

  // allocate memory for phones list
  //
  strcpy((char*)tmp_str, (char*)lab_file_a);
  phns_list = new char_1*[num_mod_a];
  tmp_str1 = (char_1*)strtok((char*)tmp_str, ISIP_STRING_SPACE);
  for (int_4 i = 0; i < num_mod_a; i++) {
    phns_list[i] = new char_1[ISIP_MAX_STRING_LENGTH];
    strcpy((char*)phns_list[i], (char*)tmp_str1);
    tmp_str1 = (char_1*)strtok(NULL, ISIP_STRING_SPACE);
  }
  
  // read data from lab file
  //
  for (int_4 i = 0; i < num_mod_a; i++) {
    
    // get the phone
    //
    tmp = phns_list[i];
    
    // remove line symbol
    //
    if (tmp[strlen((char*)tmp)-1] == '\n') {
      tmp[strlen((char*)tmp)-1] = '\0';
    }

    ph_index = get_phn_index_cc(tmp, monophones_a, num_mono_a, ph_size_a,
				context_mode_a, phones_map_a);
    model_list_a[i] = mod_map_a[ph_index];
    model_access_counts_a[model_list_a[i]]++;
    num_st = models_a[model_list_a[i]]->get_num_states_cc();
    num_st -= 2;
    num_state_a += num_st;
  }
  
  // clean up
  //
  tmp = (char_1*)NULL;
  delete [] tmp_str;
  for (int_4 i = 0; i < num_mod_a; i++) {
    delete [] phns_list[i];
  }
  delete [] phns_list;
  
  // return gracefully
  //
  return(ISIP_TRUE);
}
