// file: bw_train.h
//
// definitions file for the baum-welch
//

// make sure definitions are made only once
//
#ifndef __ISIP_BW_TRAIN
#define __ISIP_BW_TRAIN

// isip include files
//
#ifndef __ISIP_INTEGRAL
#include <integral.h>
#endif

// forward declaration of classes
//
#ifndef __ISIP_TRAIN_HASH_CELL
#include <train_hash_cell.h>
#endif

#ifndef __ISIP_TRAIN_HASH_TABLE
#include <train_hash_table.h>
#endif

#ifndef __ISIP_TRAIN_LEX_TREE
#include <train_lex_tree.h>
#endif

#ifndef __ISIP_TRAIN_LEX_NODE
#include <train_lex_node.h>
#endif

#ifndef __ISIP_TRAIN_LINK_LIST
#include <train_link_list.h>
#endif

#ifndef __ISIP_TRAIN_LINK_NODE
#include <train_link_node.h>
#endif

#ifndef __ISIP_TRAIN_MEMORY_MANAGER
#include <train_memory_manager.h>
#endif

#ifndef __ISIP_TRAIN_MODEL
#include <train_model.h>
#endif

#ifndef __ISIP_TRAIN_PHONE
#include <train_phone.h>
#endif

#ifndef __ISIP_TRAIN_STATE
#include <train_state.h>
#endif

#ifndef __ISIP_TRAIN_TRACE
#include <train_trace.h>
#endif

#ifndef __ISIP_TRAIN_WORD
#include <train_word.h>
#endif

#ifndef __ISIP_EXTRACT_FEATURE
#include <extract_feature.h>
#endif

// functions to decipher commandline
//
logical_1 expand_filename_cc(char_1*& fname);
logical_1 read_cmdline_cc(int_4 argc, char_1** argv, char_1* prm_file);

// functions to read the parameters file
//
logical_1 read_params_cc(FILE* fp, char_1* trans_file, char_1* state_file,
			 char_1* new_trans_file, char_1* new_state_file,
			 char_1* model_file, char_1* mfcclist_file,
			 char_1* monophones_file, char_1* phones_file,
			 char_1* lablist_file, char_1* acc_file,
			 char_1* acclist_file, char_1* state_occ_file,
			 char_1* var_floor_file, int_4& input_mode,
			 int_4& output_mode, int_4& context_mode,
			 int_4& train_mode, int_4& occ_mode,
			 float_4& width, float_4& min_mpd, float_4& min_occp,
			 int_4& min_mod_cnt, int_4& input_feature_format,
			 int_4& delta_win, logical_1& delta, logical_1& acc);

// functions to read the monophones
//
char_1** read_monophones_cc(int_4& num_mono, char_1* mono_file);

// function to check the monophones in the transcriptions
//
logical_1 check_phone_cc(char_1* phone, char_1** monophones,
			 int_4 num_mono);

// functions to read transition data from parameter file
//
float_4*** read_trans_cc(int_4& num_trans, int_4*& trans_size, char_1* file);

// functions to read states data from parameter file
//
Train_State** read_states_cc(int_4& num_st, int_4& num_feat, char_1* file);

// functions to read HMM models data from parameter file
//
Train_Model** read_models_cc(int_4& num_mod, char_1* file,
			     Train_State** states, float_4*** trans,
			     int_4**& st_map, int_4*& trans_map);

// functions to read triphones
//
Train_Phone** read_phones_cc(int_4 num_mono, int_4 ph_size,
			     Train_Model** models, int_4*& ph_map,
			     int_4& num_ph, char_1* file,
			     int_4*& mod_map, int_4 context_mode);

// function to read input feature vectors
//
logical_1 read_input_cc(float_8**& data, int_4& num_vect, int_4 num_feat,
			int_4 input_mode, char_1* m_file,
			int_4 input_feature_format, int_4 delta_win,
			logical_1 delta, logical_1 acc);

// function to get model index
//
logical_1 get_model_list_cc(int_4& num_mod, int_4*& model_list,
			    int_4& num_state, char_1* lab_file,
			    char_1** monophones, int_4 num_mono,
			    int_4 ph_size, int_4 context_mode,
			    int_4* phones_map, int_4* mod_map,
			    Train_Model** models, int_4* model_access_counts);

// function to get the phone index
//
int_4 get_phn_index_cc(char_1* phone, char_1** monophones,
		       int_4 num_mono, int_4 ph_size,
		       int_4 context_mode, int_4* phone_map);

// function to compute the phone index
//
int_4 get_nphone_ind_cc(int_4 ph_size, int_4 num_mono, int_4* phn,
			int_4 context_mode);

// function to compute the bound for each time frame
//
logical_1 get_bound_cc(int_2* upper, int_2* lower, int_4 num_vect,
		       Train_Model** models, int_4 num_mod, int_4* model_list);

// output functions
//
logical_1 print_trans_cc(char_1* file, int_4 num_trans, int_4* trans_size,
			 float_8*** new_trans, float_4*** old_trans,
			 Train_Model** models, int_4* model_access_counts,
			 int_4 num_models, int_4 min_model_count);

logical_1 print_occ_cc(char_1* file, Train_State** states,
		       Train_Phone** phones, int_4 num_phones,
		       int_4 num_states, float_8* state_occ);

// functions to compute the FB prob
//
logical_1 comp_for_prob_cc(float_8** for_prob, float_8** for_prob1,
			   float_8*** back, int_2* upper, int_2* lower,
			   int_4 frame, float_4*** transitions,
			   Train_State** states, int_4 num_mod,
			   int_4* model_list, Train_Model** models,
			   int_4* trans_map, float_8** vectors, int_4** st_map,
			   float_8* max_back, float_8** max_mback,
			   float_4 width, float_8 utt_prob,
			   float_8*** st_score, float_4 min_mpd);

logical_1 comp_back_prob_cc(float_8*** back_prob, float_8& prob,
			    float_8* max_back, float_8** max_mback,
			    int_2* upper,
			    int_2* lower, float_4 beam_width,
			    int_4 num_vect,
			    float_4*** transitions, Train_State** states,
			    int_4 num_mod, int_4* model_list,
			    Train_Model** models, int_4* trans_map,
			    float_8** vectors, int_4** st_map,
			    int_4 num_phy_st, float_8*** st_score);

logical_1 print_prob_cc(FILE* output_file, float_8** for_prob,
			float_8** back_prob, int_4 num_vect,
			int_4 num_mod, int_4* model_list,
			Train_Model** models, float_8 utt_prob,
			float_8* max_mback);

float_8 log_add_cc(float_8 x, float_8 y);
float_8 log_sub_cc(float_8 x, float_8 y);

logical_1 acc_trans_cc(float_8*** new_trans, float_8** for_prob,
		       float_8*** back_prob,
		       float_4*** transitions, Train_State** states,
		       Train_Model** models, int_4 num_mod, int_4* model_list,
		       int_4* trans_map, int_4** st_map, int_4 num_vect,
		       float_8** vectors, float_8 utt_prob,
		       float_8*** st_score, int_4 frame,
		       int_2* upper, int_2* lower);

logical_1 update_trans_cc(float_8***& new_trans, float_8*** den_prob,
			  int_4 num_trans, int_4* trans_size);

logical_1 comp_occ_cc(float_8& occ_prob, int_4 mod_ind, int_4 loc_st,
		      int_4 frame, Train_State* state, int_4 cur_st,
		      int_4 mix_ind, float_8 utt_prob,
		      float_4** trans, float_8** for_probt,
		      float_8** for_probt1,
		      float_8*** back_prob, float_8* data);

logical_1 comp_focc_cc(float_8& for_occ, float_4** trans, float_8** for_probt,
		       float_8** for_probt1, int_4 mod_ind, int_4 loc_st,
		       int_4 cur_st, int_4 frame);

logical_1 print_occs_cc(Train_State** states, Train_Model** models,
			float_8* states_occ);

logical_1 acc_pdf_cc(float_8*** means, float_8*** covars,
		     float_8** mix_weights, float_8* states_occ,
		     float_8** vectors, int_4 num_feat, int_4 num_vect,
		     int_4 num_mod, int_4* model_list, int_4** st_map,
		     Train_State** states, Train_Model** models,
		     float_8 utt_prob, float_8** for_probt,
		     float_8** for_probt1, float_8*** back_prob,
		     int_4 frame, int_2* upper, int_2* lower,
		     float_4 min_mpd, float_4 min_occp);

logical_1 update_pdf_cc(Train_State** states, float_8*** means,
			float_8*** covars, float_8** mix_weights,
			float_8* states_occ, int_4 num_states,
			int_4 num_features, float_4* var_floor,
			Train_Model** models, int_4* model_access_counts,
			int_4 num_models, int_4 min_model_cnt);

logical_1 print_states_cc(char_1* file, Train_State** states, int_4 num_st,
			 int_4 num_feat, logical_1 output_mode);

logical_1 print_ascii_state_cc(char_1* file, Train_State** states,
			       int_4 num_st, int_4 num_feat);

logical_1 print_bin_state_cc(char_1* file, Train_State** states,
			     int_4 num_st, int_4 num_feat);

Train_Hash_table* read_lexicon_cc(int_4& num_words, int_4 num_monophone,
			    char_1** monophones, char_1* lexicon_file);

logical_1 get_minlen_cc(int_2& min_len, Train_Model* model);

logical_1 print_acc_cc(FILE* fp_acc_file, float_8*** new_trans,
		       int_4 num_trans,
		       int_4* trans_size, float_8*** train_mean,
		       float_8*** train_covar, float_8** mix_weights,
		       float_8* state_occ, int_4 num_states,
		       int_4 num_mix, int_4 num_features,
		       int_4* model_access_counts, int_4 num_models);

logical_1 combine_acc_cc(float_8*** new_trans, float_8*** train_mean,
			 float_8*** train_covar,
			 float_8** mix_weights, float_8* state_occ,
			 int_4* model_access_counts, int_4 num_trans,
			 int_4* trans_size, int_4 num_states, int_4 num_mix,
			 int_4 num_features, int_4 num_models,
			 FILE* fp_acc_list);

logical_1 comp_maxmp_cc(float_8& max_mprob, int_4 frame, int_4 cur_mod,
			int_4 end_mod, int_4* model_list,
			Train_Model** models, float_8*** back_prob,
			float_8** for_probt);

logical_1 read_varf_cc(float_4* var_floor, int_4 num_features,
		       char_1* var_floor_file);

// end of file
//
#endif

