// file: $isip/util/speech/isip_model_converter/isip_model_converter.h
// version: $Id: isip_model_converter.h 8977 2003-01-12 06:37:52Z jelinek $
//
// this is the header file for the isip_recognize utility
//
// make sure the file is included only once
//
#ifndef ISIP_UTIL_MODEL_CONVERTER
#define ISIP_UTIL_MODEL_CONVERTER

// isip include files
//
#ifndef ISIP_COMMAND_LINE
#include <CommandLine.h>
#endif

#ifndef ISIP_MIXTURE_MODEL
#include <MixtureModel.h>
#endif

#ifndef ISIP_TRIPLE
#include <Triple.h>
#endif

#ifndef ISIP_CONTEXT_MAP
#include <ContextMap.h>
#endif

#ifndef ISIP_GAUSSIAN_MODEL
#include <GaussianModel.h>
#endif

#ifndef ISIP_DI_GRAPH
#include <DiGraph.h>
#endif

#ifndef ISIP_HASH_TABLE
#include <HashTable.h>
#endif

#ifndef ISIP_MATRIX_FLOAT
#include <MatrixFloat.h>
#endif

#ifndef ISIP_GAUSSIAN_MODEL
#include <GaussianModel.h>
#endif

#ifndef ISIP_SEARCH_SYMBOL
#include <SearchSymbol.h>
#endif

#ifndef ISIP_HASHTABLE
#include <HashTable.h>
#endif

#ifndef ISIP_LONG
#include <Long.h>
#endif

#ifndef __ISIP_HASH_TABLE
#include <hash_table.h>
#endif

#ifndef __ISIP_MODEL
#include <model.h>
#endif

#ifndef __ISIP_PHONE
#include <phone.h>
#endif

#ifndef __ISIP_STATE
#include <state.h>
#endif

static const String IMC_PROG_NAME(L"isip_model_converter");
static const String IMC_PARAM_PARAM_FILE(L"param_file");
static const String IMC_PARAM_DEBUG(L"debug");

// context size related parameters
//
#define IMC_WRD_INTERNAL (int_4)2
#define IMC_MONOPHONE (int_4)1
#define IMC_CROSS_WORD (int_4)3
#define IMC_DEF_CONTEXT_MODE IMC_CROSS_WORD

// define some special phones
//
#define IMC_SP_PHONE (int_4)0
#define IMC_SILENCE_PHONE (int_4)1
#define IMC_NULL_PHONE (int_4)0

// phonetic context units
//
#define IMC_TRIPHONE_SIZE (int_4)3
#define IMC_DEF_PHONE_SIZE IMC_TRIPHONE_SIZE

// parameter file related constants (user choices)
//
#define IMC_INPUT_MODE_NAME (char*)"input_mode"
#define IMC_OUTPUT_MODE_NAME (char*)"output_mode"
#define IMC_ENV_DELIMITERS (char*)"$~(){}[]?"

// define defaults
//
#define IMC_DEF_INPUT_MODE IMC_ASCII_MODE
#define IMC_DEF_OUTPUT_MODE IMC_ASCII_MODE

// input feature modes and transcription modes
//
#define IMC_ASCII_MODE 0
#define IMC_BINARY_MODE 1

// constants string for the format of parameter files
//
#define IMC_STR_NUM_MODELS "num_models"
#define IMC_STR_NUM_MONOPHONES "num_monophones"
#define IMC_STR_NUM_PHONES "num_phones"
#define IMC_STR_NUM_TRANSITIONS "num_transitions"

// define the sp string
//
#define IMC_SP_STRING "sp"
#define IMC_SIL_STRING "sil"
#define IMC_INIT_MOD_INDEX (int_4)-1

// define the default transition weight
//
#define IMC_NOTRANS_SCORE (float_4)-1.0e10

// parameter file parsing related constants
//
static const unichar IMC_COMMENT_CHAR = L'#';
static const unichar IMC_START_ARC_CHAR = L'J';
static const unichar IMC_DELIMITER_CHAR = L' ';
static const unichar IMC_EQ_DELIMITER_CHAR = L'=';
static const String IMC_MONOPHONE_FILE(L"monophones_file");
static const String IMC_LEXICON_FILE(L"lexicon_file");
static const String IMC_EXCLUDE_FILE(L"exclude_file");
static const String IMC_LATTICE_FILE(L"lattice_file");
static const String IMC_STATES_FILE(L"states_file");
static const String IMC_TRANSITIONS_FILE(L"transitions_file");
static const String IMC_MODELS_FILE(L"models_file");
static const String IMC_PHONES_FILE(L"phones_file");
static const String IMC_OUTPUT_LM_FILE(L"output_lm_file");
static const String IMC_OUTPUT_AM_FILE(L"output_am_file");
static const String IMC_OUTPUT_CONFIG_FILE(L"output_config_file");
static const String IMC_OUTPUT_MODE(L"output_mode");
static const String IMC_ASCII(L"ascii");
static const String IMC_BINARY(L"binary");
static const String IMC_CONTEXT_MODE(L"context_mode");
static const String IMC_CONTEXT_MODE_PROD(L"context_mode");
static const String IMC_MONOPHONE_MODE(L"monophone");
static const String IMC_CROSS_WORD_MODE(L"cross_word");
static const String IMC_WRD_INTERNAL_MODE(L"word_internal");
static const String IMC_CROSS_WORD_MODE_PROD(L"CROSS_SYMBOL");
static const String IMC_WRD_INTERNAL_MODE_PROD(L"SYMBOL_INTERNAL");


// function to read and decipher the parameter file
//
boolean read_params_cc(String& param_filename, File& param_file,
                       String& mono_filename, String& lexicon_filename,
		       String& exclude_filename, String& lat_filename,
		       String& state_filename,
		       String& transitions_filename, String& models_filename,
		       String& phones_filename, long& context_mode,
		       String& output_lm_filename, String& output_am_filename,
		       String& output_config_filename, long& output_mode);

boolean read_mono_cc(String& mono_filename, File& mono_file,
		     Vector<SearchSymbol>& phone_syms,
		     HashTable<String, Ulong>& phone_hash);

boolean read_lexicon_cc(String& lexicon_filename, File& lexicon_file,
			Vector<SearchSymbol>& word_syms,
			Vector<DiGraph<Ulong> >& pron_graph,
			HashTable<String, Ulong>& lex_hash,
			HashTable<String, Ulong>& phone_hash);

boolean read_lat_cc(String& lat_filename, File& lat_file,
		    HashTable<String, Ulong>& lex_hash,
		    Vector<DiGraph<Ulong> >& wrd_graph);

boolean create_loop_grammar_cc(String& exclude_filename,
			       HashTable<String, Ulong>& lex_hash,
			       Vector<DiGraph<Ulong> >& wrd_graph);

State** read_states_cc(int_4& num_states, int_4& num_features,
		       char_1* states_filename);

// functions to read transition data from parameter file
//
boolean read_trans_cc(char_1* transitions_filename,
		      Vector<MatrixFloat>& trans_matrices);

// functions to read HMM models data from parameter file
//
boolean read_models_cc(char_1* models_filename,
		       Vector<VectorLong>& model_state_map,
		       VectorLong& model_trans_map,
		       Vector<String>& model_names);

logical_1 check_phone_cc(char_1* phone, char_1** monophones, int_4 num_mono);

// function to read phone mapping
//
boolean read_phones_cc(char_1* file,
		       Vector<VectorLong>& phone_mono_map,
		       VectorLong& phone_model_map,
		       Vector<String>& phone_names,
		       long total_num_mono);

#endif
