#! /usr/local/bin/perl

# file: $ISIP_DEVEL/util/speech/isip_to_score.sh
#

# define constants
#
$mode = "$ARGV[1]";
$STATE_MODE = 2;
$MODEL_MODE = 1;
$WORD_MODE = 0;
$line_count = 0;
$flag = 0;

# define which field contains the word information
#
if ($mode == $MODEL_MODE) {
    $field = 4;
}
elsif ($mode == $STATE_MODE) {
    $field = 7;
}
else {
    $field = 2;
}

# open files
#
open(fp_in, "$ARGV[0]");
open(fp_out, ">$ARGV[2]");

while (<fp_in>) {

    chop;
    
    # get file name
    #
    $file_name = $_;
    
    # open file
    #
    open(fin, "$file_name");

    $line_count = 0;
    $flag = 0;

    # string the utterance
    #
    while (<fin>) {
	
	if ($line_count < 2) {
	    $line_count++;
	    $flag = 0;
	}
	else {
	    $flag = 1;
	}
	
	if ($flag == 1) {
	    chop;
	    @a = split(/\s+/, $_);

            # don't include special words like [SILENCE], !SENT_START and
	    # !SENT_END
	    #
	    if ($a[$field] !~ /\!|\[/) {
		if ($a[$field] ne "") {
		    $utterance .= "$a[$field] ";
		}
	    }
	}
    }

    # get the utterance id
    #
    if ($file_name =~ /(\S+)\.\S+/) {	
	@a = split (/\//, $file_name);
	$file_name = pop(@a);
	@a = split(/\./, $file_name);
	$file_name = "";
	for ($i = 0; $i < $#a - 1; $i++) {	    
	    $file_name .= $a[$i] . ".";
	}
	$file_name .= $a[$#a-1];
	print fp_out "$utterance ($file_name)\n";
	$utterance = "";
    }
}

# close files
#
close(fp_in);
close(fp_out);
