// file: ht_phone_2.cc
//
 
// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"

// method: get_nphone_ind_cc
//
// arguments:
//  int_4 ph_size : (input) context size
//  int_4 num_mono : (input) number of monophones
//  int_4* ph_map : (input) indices of the context-dependent phone
//  int_4 context_mode : (input) the context mode
//  
// return: an int_4 which is the index to nphone
//
// this method returns the phone index of the given n-phone formed by
// the given constituent phones
//
int_4 get_nphone_ind_cc(int_4 ph_size_a, int_4 num_mono_a, int_4* ph_map_a,
			int_4 context_mode_a, int_4 num_sph_a,
			int_4* sph_index_a) {
  
  // declare local variables
  //
  int_4 ph_map_index = (int_4)0;
  int_4 tmp = (int_4)0;

  // this is just for monophone debugging
  // we can put the context_mode_a for judgement later
  // 
  if(context_mode_a == HT_MONOPHONE){

    tmp = ph_map_a[1];

    if ((tmp == HT_SILENCE_PHONE) || (tmp == HT_SP_PHONE) ||
	check_sph_cc(tmp, num_sph_a, sph_index_a)) {
      ph_map_index = tmp * (int_4)pow(num_mono_a, 1);
    }
    
    else {
      if (tmp < HT_SP_PHONE) {
	tmp = (int_4)0;
      }
      ph_map_index += tmp * (int_4)pow(num_mono_a, 1);
    }
  }

  // this code are for tri-phone and word internal
  //
  else {
    
    // check the middle phone
    //
    tmp = ph_map_a[ph_size_a/2];
    if ((tmp == HT_SILENCE_PHONE) || (tmp == HT_SP_PHONE)) {
      ph_map_index = tmp * (int_4)pow(num_mono_a, ph_size_a/2);
    }
    
    // otherwise loop over the constituent phone indices to determine
    // the location of the nphone
    //
    else {
      for (int_4 i = 0; i < ph_size_a; i++) {
	tmp = ph_map_a[i];
	if (tmp <= HT_SP_PHONE) {
	  tmp = (int_4)0;
	}
	ph_map_index += tmp * (int_4)pow(num_mono_a, i);
      }
    }
  }

  // exit gracefully
  //
  return ph_map_index;
}




