// file: itri_model_0.cc
//
// isip include files
//
#include "init_triphones.h"
#include "init_triphones_constants.h"
#include <string.h>

// method: get_cimodel_cc
//
// arguments:
//  char_1* phy_model : (input) name of the input physical model
//  Hash_table* ci_table : (input) hash_table of CI model indices
//  int_4 num_ci : (input) number of context independent models
//  char_1* ci_model : (output) name of the corresp. ci model
//
// return: an int_4 index of the physical model
//
int_4 get_cimodel_cc(char_1* phy_model, Train_Hash_table* ci_table, int_4 num_ci,
		     char_1* ci_model) {


  // local variables
  //
  int_4 model_index = (int_4)0;
  logical_1 right_context = ISIP_FALSE;
  logical_1 left_context = ISIP_FALSE;
  Train_Hash_cell* hcell;
  
  // make a local copy of the physical model's name
  //
  char_1* temp_buf;
  char_1* tmp_str = new char_1[strlen((char*)phy_model) + 1];
  strcpy((char*)tmp_str, (char*)phy_model);

  // buffer to hold phone sequence
  //
  int_4* phn = new int_4[3];

  // determine if the physical model has left context
  //
  if (strchr((char*)tmp_str, '-') != (char*)NULL) {
    *strchr((char*)tmp_str, '-') = '\0';
    hcell = ci_table->hash_lookup_cc(tmp_str);
    phn[0] = (int_4)hcell->get_item_cc();
    left_context = ISIP_TRUE;
  }  
  else {
    phn[0] = 0;
  }
  
  // reset the temp string
  //
  strcpy((char*)tmp_str, (char*)phy_model);

  // determine if physical model has right context specified
  //
  if (strchr((char*)tmp_str, '+') != (char*)NULL) {
    temp_buf = (char_1*)(strchr((char*)tmp_str, '+') + 1);
    temp_buf[strlen((char*)temp_buf)] = '\0';
    hcell = ci_table->hash_lookup_cc(temp_buf);
    phn[2] = (int_4)hcell->get_item_cc();
    right_context = ISIP_TRUE;
  }  
  else {
    phn[2] = 0;
  }

  // reset the temp string
  //
  strcpy((char*)tmp_str, (char*)phy_model);

  // handle biphones by setting the central phone correctly
  //
  if (left_context == ISIP_TRUE && right_context == ISIP_FALSE) {
    temp_buf = (char_1*)(strchr((char*)tmp_str, '-') + 1);
    temp_buf[strlen((char*)temp_buf)] = '\0';
    strcpy((char*)ci_model, (char*)temp_buf);
    hcell = ci_table->hash_lookup_cc(temp_buf);
    phn[1] = (int_4)hcell->get_item_cc();
  }
  else if (left_context == ISIP_FALSE && right_context == ISIP_TRUE) {
    temp_buf = tmp_str;
    *strchr((char*)temp_buf, '+') = '\0';
    strcpy((char*)ci_model, (char*)temp_buf);
    hcell = ci_table->hash_lookup_cc(temp_buf);
    phn[1] = (int_4)hcell->get_item_cc();
  }
  else if (left_context == ISIP_TRUE && right_context == ISIP_TRUE) {
    temp_buf = (char_1*)(strchr((char*)tmp_str, '-') + 1);
    *strchr((char*)temp_buf, '+') = '\0';
    strcpy((char*)ci_model, (char*)temp_buf);
    hcell = ci_table->hash_lookup_cc(temp_buf);
    phn[1] = (int_4)hcell->get_item_cc();
  }    
  else {
    strcpy((char*)ci_model, (char*)tmp_str);
    hcell = ci_table->hash_lookup_cc(phy_model);
    phn[1] = (int_4)hcell->get_item_cc();
  }
  
  // get the phone index
  //
  for (int_4 i = 0; i < 3; i++) {
    model_index += phn[i] * (int_4)pow(num_ci, i);    
  }
  
  // clean up memory
  //
  delete [] phn;
  delete [] tmp_str;
  
  // exit gracefully
  //
  return model_index;
}
