ConfPass.sh 6.46 KB
#!/bin/bash

#####################################################
# File :    ConfPass.sh                             #
# Brief :   Process the ASR Confidence pass         #
# Author :  Jean-François Rey                       #
#	        (base on Emmanuel Ferreira              #
#	        and hugo Mauchrétien works)             #
# Version : 1.0                                     #
# Date :    17/06/13                                #
#####################################################

#Check OTMEDIA_HOME env var
if [ -z ${OTMEDIA_HOME} ]
then
    OTMEDIA_HOME=$(dirname $(dirname $(readlink -e $0)))
    export OTMEDIA_HOME=$OTMEDIA_HOME
fi


# where is ConfPath.sh
MAIN_SCRIPT_PATH=$(dirname $(readlink -e $0))

# Scripts Path
SCRIPT_PATH=$OTMEDIA_HOME/tools/scripts

# Include scripts
. $SCRIPT_PATH"/Tools.sh"
. $SCRIPT_PATH"/CheckConfPass.sh"

# where is FirstPass.cfg
CONFPASS_CONFIG_FILE="$OTMEDIA_HOME/cfg/ConfPass.cfg"
if [ -e $CONFPASS_CONFIG_FILE ]
then
	. $CONFPASS_CONFIG_FILE
else
	echo "ERROR : Can't find configuration file $CONFPASS_CONFIG_FILE" >&2
	exit 1
fi

#---------------#
# Parse Options #
#---------------#
while getopts ":hDv:cr" opt
do
	case $opt in
		h)
			echo -e "$0 [OPTIONS] <INPUT_DIRECTORY> <TREIL_DIRECTORY_NAME>\n"
            echo -e "\t Options:"
            echo -e "\t\t-h :\tprint this message"
            echo -e "\t\t-D :\tDEBUG mode on"
            echo -e "\t\t-v l :\tVerbose mode, l=(1|2|3) level mode"
            echo -e "\t\t-c :\t Check process, stop if error detected"
            echo -e "\t\t-r :\tForce to rerun confpas without deleting existing files"
			exit 1
			;;
		D)
			DEBUG=1
			;;
        v)
            VERBOSE=$OPTARG
            ;;
        c)
            CHECK=1
            ;;
        r)
            RERUN=1
            ;;
		:)
			echo "Option -$OPTARG requires an argument." >&2
			exit 1
			;;
		\?)
			echo "BAD USAGE : unknow opton -$OPTARG"
			#exit 1
			;;
	esac
done

# mode debug enable
if [ $DEBUG -eq 1 ]
then
       set -x
       echo -e "## Mode DEBUG ON ##"
       REDIRECTION_OUTPUT=""
   else
       REDIRECTION_OUTPUT=" 2> /dev/null"
fi

# mode verbose enable
if [ $VERBOSE -gt 0 ]; then echo -e "## Verbose level : $VERBOSE ##" ;fi
 
# Check USAGE by arguments number
if [ $(($#-($OPTIND-1))) -ne 2 ]
then
    echo "BAD USAGE : ConfPass.sh [OPTIONS] <INPUT_DIR> <TREIL_DIRECTORY_NAME>"
    echo "$0 -h for more info"
    exit 1
fi

shift $((OPTIND-1))
# check input directory - first argument
if [ ! -e $1 ]
then
    print_error "can't open $1"
    exit 1
fi
# check treil input directory - second argument
if [ ! -e $1/$2 ]
then
    print_error "can't open $1/$2"
    exit 1
fi

#-------------#
# GLOBAL VARS #
#-------------#
INPUT_DIR=$(readlink -e $1)
OUTPUT_DIR=$INPUT_DIR 
BASENAME=$(basename $OUTPUT_DIR)
RES_NAME=$2
RES_P="${INPUT_DIR}/${RES_NAME}"
USF_FILE=${INPUT_DIR}/${BASENAME}.${RES_NAME}.usf
CONF_DIR="$OUTPUT_DIR/conf/$RES_NAME"
RES_CONF_DIR="$OUTPUT_DIR/conf/$RES_NAME/scored_ctm"
LOGFILE="$(dirname $OUTPUT_DIR)/info_conf.log"
ERRORFILE="$(dirname $OUTPUT_DIR)/error_conf.log"


#------------------#
# Create Workspace #
#------------------#
# Lock directory
if [ -e "$OUTPUT_DIR/CONFPASS.lock" ] && [ $RERUN -eq 0 ]
then
    print_info "Confpass lock $INPUT_DIR -> exit" 1
    exit 1
fi
rm "$OUTPUT_DIR/CONFPASS.unlock" > /dev/null 2>&1
touch "$OUTPUT_DIR/CONFPASS.lock" > /dev/null 2>&1
if [ $RERUN -eq 0 ]; then rm -r $CONF_DIR > /dev/null 2>&1; fi
if [ $RERUN -eq 1 ]; then rm $USF_FILE > /dev/null 2>&1; fi
mkdir -p $CONF_DIR > /dev/null 2>&1
mkdir -p $RES_CONF_DIR > /dev/null 2>&1

#---------------#
# Check Pass    #
#---------------#

# if usf contains more than 40% of 0.600 confidence -> usf error
if [ -s $USF_FILE ]
then
    conftozerosix=$(grep -c -E 'confidence="0.600"' "${USF_FILE}")
    conftoother=$(grep -c -v -E 'confidence="0.600"' "${USF_FILE}")
    if [ $conftoother -gt 0 ]
    then
        pourcentageofzerosix=$((($conftozerosix*100)/$conftoother))
        if [ $pourcentageofzerosix -gt 40 ]
        then
            print_warn "${USF_FILE} got $pourcentageofzerosix% of 0.6 confidence" 1
            mv "${USF_FILE}" "${USF_FILE}.back"
            rm -r $CONF_DIR > /dev/null 2>&1
        else
            print_warn "${USF_FILE} already done, skipping it" 1
            exit 0
        fi
    fi
fi

#------#
# Save #
#------#
cp $CONFPASS_CONFIG_FILE $OUTPUT_DIR/ConfPass.cfg
echo "RES_CONF_DIR=$RES_CONF_DIR" >> $OUTPUT_DIR/ConfPass.cfg
echo "CONF_DIR=$CONF_DIR" >> $OUTPUT_DIR/ConfPass.cfg

#--------------------#
# CONFIDENCE MEASURE #
#--------------------#

# Check pourcentage of scored_ctm already done, if < 85% done confidence measure
nbres_p=$(ls ${RES_P}/*.treil | wc -l)
nbconf=$(ls ${RES_CONF_DIR}/*.res | wc -l)
if [ $nbres_p -gt 0 ]
then
    pourcentageres=$((($nbconf*100)/$nbres_p))
    if [ $pourcentageres -lt 85 ]
    then
        print_info "Calcul Confidence $INPUT_DIR $RES_NAME" 1
        $MAIN_SCRIPT_PATH/ConfidenceMeasure.sh $INPUT_DIR $RES_NAME $REDIRECTION_OUTPUT

    else
        print_info "skipping Confidence Calcul $INPUT_DIR/$RES_NAME" 1
    fi
fi

### Check scored_ctm number res files !
if [ $CHECK -eq 1 ]
then
    nbconf=$(ls ${RES_CONF_DIR}/*.res | wc -l)
    if [ $nbres_p -ne $nbconf ];then echo "WARN : ConfPass $INPUT_DIR/$RES_NAME number of res files differ" >> $LOGFILE;fi
fi

#---------------------------#
# FROM RES WITH CONF => USF #
#---------------------------#
for f in `ls ${RES_CONF_DIR}`; do $SCRIPT_PATH/formatRES.pl $RES_CONF_DIR/$f; done
# create USF configuration file
echo -e "name $AUTHOR\nfileName $BASENAME\nfileExt wav\nsegFile $OUTPUT_DIR/$BASENAME.seg" > $OUTPUT_DIR/$BASENAME.usf_cfg
# create USF file
$SCRIPT_PATH/res2out.pl --dir $RES_CONF_DIR --format USF --ignore $RULES/asupp --out $USF_FILE.tmp --usf_config $OUTPUT_DIR/$BASENAME.usf_cfg $REDIRECTION_OUTPUT
rm $OUTPUT_DIR/$BASENAME.usf_cfg > /dev/null 2>&1
cat $USF_FILE.tmp | $SCRIPT_PATH/BdlexUC.pl $RULES/basic -f > $USF_FILE
cp $USF_FILE ${OUTPUT_DIR}/${BASENAME}.usf
rm $USF_FILE.tmp > /dev/null 2>&1

#----------------#
# Check USF file #
#----------------#
if [ $CHECK -eq 1 ]
then
    check_conf_pass_usf "$OUTPUT_DIR/$BASENAME.usf"
    if [ $? -eq 1 ]
    then
        echo -e "ERROR : Wrong confidence measures in USF file : $OUTPUT_DIR/$BASENAME.usf" >> $ERRORFILE
        exit 1
    fi
fi

#-------#
# CLOSE #
#-------#
# Seem OK 
print_info "<= End $BASENAME ConfPass | $(date +'%d/%m/%y %H:%M:%S')" 1
echo -e "ConfPass $BASENAME OK" >> $LOGFILE
 
# unlock directory
mv "$OUTPUT_DIR/CONFPASS.lock" "$OUTPUT_DIR/CONFPASS.unlock"