// file: bw_phone_2.cc
//

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

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

// method: get_phn_index_cc
//
// arguments:
//  char_1* phone : (input) the input phone
//  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* phone_map : (input) the phone map used to compute the index
//
// return: the direct index for the input phone
//
// this method loops over all the monophone in the input phone and return
// the index of the whole phone
//
int_4 get_phn_index_cc(char_1* phone_a, char_1** monophones_a,
		       int_4 num_mono_a, int_4 ph_size_a,
		       int_4 context_mode_a, int_4* phone_map_a) {
  
  // local variables
  //
  int_4 ind = (int_4)0;
  int_4 len1 = (int_4)0;
  int_4 len2 = (int_4)0;
  int_4* phn = new int_4[ph_size_a];
  char_1* phn_str = (char_1*)NULL;
  char_1* tmp_str = (char_1*)NULL;

  // start checking the seperate phone
  //
  tmp_str = (char_1*)strstr((char*)phone_a, "-");

  if (tmp_str == (char_1*)NULL) {                  // no "-" in this phone

    // set the first phone
    //
    phn[0] = (int_4)0;
    
    // check the middle phone
    //
    tmp_str = (char_1*)strstr((char*)phone_a, "+");
    
    // check if this phone is a monophone
    //
    if (tmp_str == (char_1*)NULL) {                // no "+" in this phone
      
      // this is a monophone, set the third phone
      //
      phn[2] = (int_4)0;

      // check if the monophone is a valid phone
      //
      check_phone_cc(phone_a, monophones_a, num_mono_a);
      
      // set the middle phone
      //
      for (int_4 k = 0; k < num_mono_a; k++) {
	len1 = strlen((char*)phone_a);
        len2 = strlen((char*)monophones_a[k]);
	if (len1 == len2) {
	  if (strcmp((char*)phone_a, (char*)monophones_a[k]) == 0) {
	    phn[1] = k;
	    break;
	  }
	}
      }

      ind = phone_map_a[get_nphone_ind_cc(ph_size_a,
					  num_mono_a, phn,
					  context_mode_a)];
    }

    // only "+" in this phone
    //
    else {                                      
      
      // set the middle phone
      //
      tmp_str = (char_1*)strtok((char*)phone_a, "+");

      // check if the monophone is a valid phone
      //
      check_phone_cc(tmp_str, monophones_a, num_mono_a);
      
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)tmp_str, (char*)monophones_a[k]) == 0) {
	  phn[1] = k;
	  break;
	}
      }
      
      // set the third phone
      //
      tmp_str = (char_1*)strtok(NULL, " ");

      // check if the monophone is a valid phone
      //
      check_phone_cc(tmp_str, monophones_a, num_mono_a);
      
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)tmp_str, (char*)monophones_a[k]) == 0) {
	  phn[2] = k;
	  break;
	}
      }
      ind = phone_map_a[get_nphone_ind_cc(ph_size_a,
					  num_mono_a, phn,
					  context_mode_a)];
    }
  }
  
  // "-" in this phone
  //
  else {                                         
    
    // check the "+" symbol
    //
    tmp_str = (char_1*)strstr((char*)phone_a, "+");
    if (tmp_str == (char_1*)NULL) {              // only "-" in this phone
      
      // set the third phone
      //
      phn[2] = (int_4)0;
      
      // set the first phone
      //
      tmp_str = (char_1*)strtok((char*)phone_a, "-");
      
      // check if the monophone is a valid phone
      //
      check_phone_cc(tmp_str, monophones_a, num_mono_a);
      
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)tmp_str, (char*)monophones_a[k]) == 0) {
	  phn[0] = k;
	  break;
	}
      }
      
      // set the second phone
      //
      tmp_str = (char_1*)strtok(NULL, " ");

      // check if the monophone is a valid phone
      //
      check_phone_cc(tmp_str, monophones_a, num_mono_a);
      
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)tmp_str, (char*)monophones_a[k]) == 0) {
	  phn[1] = k;
	  break;
	}
      }
      ind = phone_map_a[get_nphone_ind_cc(ph_size_a,
					  num_mono_a, phn,
					  context_mode_a)];
    }

    // both "-" & "+" in this phone
    //
    else {                                   
      
      // set the first phone
      //
      tmp_str = (char_1*)strtok((char*)phone_a, "-");
      
      // check if the monophone is a valid phone
      //
      check_phone_cc(tmp_str, monophones_a, num_mono_a);
      
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)tmp_str, (char*)monophones_a[k]) == 0) {
	  phn[0] = k;
	  break;
	}
      }

      // set the second phone
      //
      tmp_str = (char_1*)strtok(NULL, "+");

      // check if the monophone is a valid phone
      //
      check_phone_cc(tmp_str, monophones_a, num_mono_a);
      
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)tmp_str, (char*)monophones_a[k]) == 0) {
	  phn[1] = k;
	  break;
	}
      }
      
      // set the third phone
      //
      tmp_str = (char_1*)strtok(NULL, " ");
      
      // check if the monophone is a valid phone
      //
      check_phone_cc(tmp_str, monophones_a, num_mono_a);
      
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)tmp_str, (char*)monophones_a[k]) == 0) {
	  phn[2] = k;
	  break;
	}
      }
      ind = phone_map_a[get_nphone_ind_cc(ph_size_a,
					  num_mono_a, phn,
					  context_mode_a)];
    }
  } // end of "-" in this phone

  // free memory
  //
  delete [] phn;
  tmp_str = (char_1*)NULL;
  phn_str = (char_1*)NULL;
 
  // exit gracefully
  //
  return ind;
}
