#!/bin/sh
 
# file: $ISIP_PROTO/util/isip_eval.sh
#
# this script takes three arguments:
#
#       isip_eval.sh file_format input_file_list ref_filename.score out.score
#
# the arguments are:
#
#	file_format:    input hypothesis file formats like "isip_word", 
#			"isip_model" etc.
#	input_file_list:  reference file list.
#       filename.score:  Reference  file in workshops standard transcription
#                       format with cut ids and time segmentations
#
# This is the wrapper around sclite. It converts the ISIP recognizer's
# output to the "score" format and scores it against a reference "score"
# file.
#
# Note that the environment variable ISIP_PROTO must to be set appropriately
# for the script to work.
#
# You need to have the NIST package "sclite" installed for this utility to 
# work properly
# 

# define constants
#
NUM_ARGS="4";
ISIP_TO_SCORE="${ISIP_PROTO}/bin/scripts/isip_to_score";
INPUT_FILE_FORMAT="$1";
INPUT_FILE_LIST="$2";
UTTERANCE_ID_FORMAT="swb";
OUTPUT_OPTIONS="dtl all";
FILE_EXT_DTL="dtl";
FILE_EXT_RAW="raw";
FILE_EXT_SYS="sys"
FILE_EXT_PRA="pra";
FILE_EXT_REPORT="report";
ISIP_WORD_MODE=0;
ISIP_MODEL_MODE=1;
ISIP_STATE_MODE=2;

# specify location of sclite and other postprocessing utilities
#
SCLITE=@SCLITE@

# define input fileformats
#
MLF="mlf";
ISIP_WORD_FORMAT="isip_word";
ISIP_MODEL_FORMAT="isip_model";
ISIP_STATE_FORMAT="isip_state";
SCORE_FORMAT="score";

# check arguments
#
if (test "$#" -ne "$NUM_ARGS") then
    echo "  $0> incorrect number of arguments ($# $NUM_ARGS)";
    echo "  $0> usage: isip_eval file_format input_file_list ref_file\
 hyp_file";
    exit 0;
fi

# if input file is an MLF word alignment file
#
if (test "$ISIP_WORD_FORMAT" = "$INPUT_FILE_FORMAT") then

    # convert the input files into "score" format using the PERL script 
    # mlf_to_score.pl
    #
    echo "\t$0> converting from $1 format to "score" format .....";
    
    output_file="$4";
    $ISIP_TO_SCORE $INPUT_FILE_LIST $ISIP_WORD_MODE \
	"$output_file.${SCORE_FORMAT}";

    # set the input files required by sclite
    #
    input_score_ref_file="$3";
    input_score_hyp_file="$output_file.${SCORE_FORMAT}";
    output_file_suffix=`echo $4 | cut -d"." -f1`;

# if input file is an MLF model alignment file
#
elif (test "$ISIP_MODEL_FORMAT" = "$INPUT_FILE_FORMAT") then

    # convert the input files into "score" format using the PERL script 
    # mlf_to_score.pl
    #
    echo "\t$0> converting from $1 format to "score" format .....";
    
    output_file="$4";
    $ISIP_TO_SCORE $INPUT_FILE_LIST $ISIP_MODEL_MODE \
	"$output_file.${SCORE_FORMAT}";

    # set the input files required by sclite
    #
    input_score_ref_file="$3";
    input_score_hyp_file="$output_file.${SCORE_FORMAT}";
    output_file_suffix=`echo $4 | cut -d"." -f1`;
    
# if input file is an MLF model alignment file
#
elif (test "$ISIP_STATE_FORMAT" = "$INPUT_FILE_FORMAT") then

    # convert the input files into "score" format using the PERL script 
    # mlf_to_score.pl
    #
    echo "\t$0> converting from $1 format to "score" format .....";
    
    output_file="$4";
    $ISIP_TO_SCORE $INPUT_FILE_LIST $ISIP_STATE_MODE \
	"$output_file.${SCORE_FORMAT}";
    # set the input files required by sclite
    #
    input_score_ref_file="$3";
    input_score_hyp_file="$output_file.${SCORE_FORMAT}";
    output_file_suffix=`echo $4 | cut -d"." -f1`;
    
# if input file is a "score" file
#
elif (test "$SCORE_FORMAT" = "$INPUT_FILE_FORMAT") then

    input_score_ref_file="$3";
    output_file="$4";

    # set the input files required by sclite
    #
    input_score_ref_file="$3";
    input_score_hyp_file="$INPUT_FILE_LIST";
    output_file_suffix=`echo $4 | cut -d"." -f1`;
    
else 
    echo "\t$0> $1 not a recognized input format";
    exit 0;
fi

echo "\t$0> evaluating using sclite .....";

echo "$SCLITE -F -i $UTTERANCE_ID_FORMAT -r $input_score_ref_file \
 	   -h $input_score_hyp_file -o $OUTPUT_OPTIONS ";
$SCLITE -F -i $UTTERANCE_ID_FORMAT -r $input_score_ref_file \
 	   -h $input_score_hyp_file -o $OUTPUT_OPTIONS;

# Output important evaluation information from sclite's  output
#
echo "\n===============================================================================";
head -21 ${input_score_hyp_file}.${FILE_EXT_DTL} | grep -v "DETAILED OVERALL REPORT";

# merge all outut into one file
#
cat  ${input_score_hyp_file}.${FILE_EXT_DTL}   ${input_score_hyp_file}.${FILE_EXT_PRA} ${input_score_hyp_file}.${FILE_EXT_SYS}  ${input_score_hyp_file}.${FILE_EXT_RAW} >  ${output_file}.${FILE_EXT_REPORT} ;

rm ${input_score_hyp_file}.${FILE_EXT_PRA} ${input_score_hyp_file}.${FILE_EXT_SYS}  ${input_score_hyp_file}.${FILE_EXT_RAW} ${input_score_hyp_file}.${FILE_EXT_DTL}

echo "\n";

# end of file
#
