#! @PERL@
#
# sample usage: index_model.pl input_states.text input_trans.text 
#               input_model.text output_model.text
#
# this script indexes the states in the input model definition 
#

# define constants
#
$NUM_ARGS = 3;
$MODEL_FLAG = "\~h";
$TRANS_FLAG = "\~t";
$STATES_FLAG = "\~s";
$MODEL_INDEX_STR = "index\: ";
$MODEL_PHONE_STR = "phone\: ";
$MODEL_NUM_STATES_STR = "num_states\: ";
$MODEL_TRANS_STR = "transitions\: ";
$MODEL_STATES_STR = "states\: ";

$SENT_START = "\!s";
$SENT_END = "s\!";
$SENT_BOUND_LEN = 2;

# define local variables
#
# trans_ind starts with 1 because transitions index for S! and !S are set to
# 0 by default in the  decoder
# states_ind also starts with 1 for similar reasons
#
$trans_ind = 1;
$states_ind = 1;
$num_models = 0;
$model_ind = 0;

# check for correct number of arguments
#
($#ARGV == ($NUM_ARGS)) || 
    die "Usage: index_model.pl <input states> <input trans> <input models> <output models>\n";

$states = $ARGV[0];
$trans = $ARGV[1];
$in_models = $ARGV[2];
$out_models = $ARGV[3];

# open files
# 
open (states_file, "$states") || die "Cannot open file $states: $!\n";
open (trans_file, "$trans") || die "Cannot open file $trans: $!\n";
open (in_models_file, "$in_models") || die "Cannot open file $in_models: $!\n";
open (out_models_file, ">$out_models") || die "Cannot open file $out_models: $!\n";


# read the input model file and find the number of models
#
while (<in_models_file>) {
    
    chop;
    $line = $_;

    if ($line =~ $MODEL_FLAG) {
	$num_models++;
    }
}
close (in_models_file);
open (in_models_file, "$in_models") || die "Cannot open file $in_models: $!\n";

# print the number of models
#
print out_models_file "num_models = $num_models\n\n";

# read the transitions file and index the transitions
#
while (<trans_file>) {

    chop;
    $line = $_;

    if ($line =~ $TRANS_FLAG) {
	@trans_def = split (/\s+/, $line);
	if (!defined($transitions{$trans_def[1]})) {
	    $transitions{$trans_def[1]} = $trans_ind;
	    $trans_ind++;
	}
    }
}
close(trans_file);

# read the states file and index the states
#
while (<states_file>) {

    chop;
    $line = $_;

    if ($line =~ $STATES_FLAG) {
	@states_def = split (/\s+/, $line);
	if (!defined($states{$states_def[1]})) {
	    $states{$states_def[1]} = $states_ind;
	    $states_ind++;
	}
    }
}
close(states_file);

# read the input models file and output the new models file in isip_decoder's
# format
#
while (!eof(in_models_file)) {

    $line = <in_models_file>;
    chop $line;
    
    # get the hmms
    #
    if ($line =~ $MODEL_FLAG) {
	
	@hmm_def = split (/\s+/, $line);    
	$model_name = $hmm_def[1];
	$model_name =~ s/\"//g;

	$line = <in_models_file>;	
	$line = <in_models_file>;
	@num_state = split (/\s+/, $line);
	$num_states = $num_state[1];
	
	# loop over all states (remember that is 2 less than the number on
	# the line)
	#
	for ($i = 0; $i < ($num_state[1] - 2); $i++) {
	    
	    $line = <in_models_file>;
	    $line = <in_models_file>;
	    @state = split (/\s+/, $line);
	    $state_name = $state[1];
	    	    
	    push(@state_list, $states{$state_name});
	}

	$line = <in_models_file>;
	@trans = split (/\s+/, $line);
	$trans_name = $trans[1];
	$trans_ind = $transitions{$trans_name};
    }

    $line = <in_models_file>;

    print out_models_file "$MODEL_INDEX_STR $model_ind\n";
    print out_models_file "$MODEL_PHONE_STR $model_name\n";
    print out_models_file "$MODEL_NUM_STATES_STR $num_states\n";
    print out_models_file "$MODEL_TRANS_STR $trans_ind\n";
    print out_models_file "$MODEL_STATES_STR 0 @state_list 0\n\n";    
    @state_list="";
    $model_ind++;
}
close(in_models_file);
close(out_models_file);

# 
# end of script



