ScoringRes.sh 5.06 KB
#!/bin/bash

#####################################################
# File :    ScoringRes.sh                           #
# Brief :   Score result using SRT file             #
# Author :  Jean-François Rey                       #
# Version : 1.0                                     #
# Date :    11/07/13                                #
#####################################################

echo "### ScoringRes.sh ###"

# 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 ScoringRes.sh
MAIN_SCRIPT_PATH=$OTMEDIA_HOME/main_tools

# Scripts Path
SCRIPT_PATH=$OTMEDIA_HOME/tools/scripts

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

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

#---------------#
# Parse Options #
#---------------#
while getopts ":hDv:" opt
do
	case $opt in
		h)
			echo -e "$0 [OPTIONS] <PASS_DIRECTORY>\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"
			exit 1
			;;
		D)
			DEBUG=1
			;;
        v)
            VERBOSE=$OPTARG
            ;;
		:)
			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 ##"
fi

# mode verbose enable
if [ $VERBOSE -gt 0 ]; then echo -e "## Verbose level : $VERBOSE ##" ; fi

# Check USAGE by arguments number
if [ $(($#-($OPTIND-1))) -ne 1 ]
then
	echo "BAD USAGE : SecondPass.sh [OPTIONS] <PASS_DIR>"
	echo "$0 -h for more info"
    exit 1
fi

shift $((OPTIND-1))
# check Pass directory - First argument
if [ -e $1 ] && [ -d $1 ]
then
    PASS_DIR=$(readlink -e $1)
else
	print_error "can't find $1 directory"
	exit 1
fi

#-------------#
# GLOBAL VARS #
#-------------#
BASENAME=$(basename $PASS_DIR)
SCORING_DIR=$PASS_DIR/scoring
LOGFILE="$PASS_DIR/info_scoring.log"
ERRORFILE="$PASS_DIR/error_scoring.log"

print_info "[${BASENAME}] => ScoringRes start | $(date +'%d/%m/%y %H:%M:%S')" 1

#------------------#
# Create WORKSPACE #
#------------------#

# Lock directory
if [ -e $PASS_DIR/*.lock ]
then
    print_warn "[${BASENAME}] Directory is locked -> exit" 2;
    exit 1
fi

rm -r $SCORING_DIR > /dev/null 2>&1
mkdir -p $SCORING_DIR
print_info "Make directory $SCORING_DIR" 1

if [ ! -e $PASS_DIR/$BASENAME.SRT ]
then
    print_warn "[${BASENAME}] No SRT file -> exit" 2
    exit 0;
else
    SRT_FILE=$PASS_DIR/$BASENAME.SRT
    print_info "[${BASENAME}] SRT file : $BASENAME.SRT" 1
fi

if [ ! -e $PASS_DIR/$BASENAME.1pass.ctm ] && [ ! -e $PASS_DIR/$BASENAME.2pass.ctm ] && [ ! -e $PASS_DIR/$BASENAME.3pass.ctm ] && [ ! -e $PASS_DIR/$BASENAME.allpass.ctm ]
then
    print_error "[${BASENAME}] No CTM files -> exit\n";
    exit 1;
fi

print_info "[${BASENAME}] Create stm file from SRT" 1
i=$((-3))
while read line  
do
    i=$(($i + 1));   
    if [ $i -eq 4 ]
    then
        echo " $line"
        i=$((0));
    fi
done < $SRT_FILE > $SCORING_DIR/$BASENAME.tmp.txt

cat $SCORING_DIR/$BASENAME.tmp.txt | sed -e "s|\n| |g" | $SCRIPT_PATH/CleanFilter.sh | $SCRIPT_PATH/ApplyCorrectionRules.pl ${LEXICON}.regex | $SCRIPT_PATH/BdlexUC.pl $RULES/basic -t | sed -e "s|<s>||g" | sed -e "s|</s>||g" | sed -e "s|\n+| |g" > $SCORING_DIR/$BASENAME.tmp2.txt
#cat $SCORING_DIR/$BASENAME.tmp.txt | sed -e "s|\n| |g" | $SCRIPT_PATH/BdlexUC.pl $RULES/basic -t | sed -f $RULES/preprocess.regex | sed -f $RULES/lastprocess.regex | sed -e "s|<s>||g" | sed -e "s|</s>||g" | sed -e "s|\n+| |g" > $SCORING_DIR/$BASENAME.tmp2.txt

$SCRIPT_PATH/srt2stm.pl $SCORING_DIR/$BASENAME.tmp2.txt > "$SCORING_DIR/$BASENAME.stm"

## TODO
# lia_ne bug...
export LIA_TAGG_LANG="french"
export LIA_TAGG="$OTMEDIA_HOME/tools/lia_ltbox/lia_tagg/"
export LIA_NE="$OTMEDIA_HOME/tools/lia_ltbox/lia_ne_v2.2/"
#cat $SCORING_DIR/$BASENAME.tmp2.txt | $SCRIPT_PATH/BdlexUC.pl $RULES/basic -f | iconv -t ISO-8859-1 -f UTF-8 | $OTMEDIA_HOME/tools/lia_ltbox/lia_ne_v2.2/script/lia_ne_tagg_txt > $SCORING_DIR/$BASENAME.tmp2.tag
cat $SCORING_DIR/$BASENAME.stm | $SCRIPT_PATH/BdlexUC.pl $RULES/basic -f | iconv -t ISO-8859-1 -f UTF-8 > $SCORING_DIR/$BASENAME.tmp3.txt
$OTMEDIA_HOME/tools/lia_ltbox/lia_ne_v2.2/script/lia_ne_tagg_stm $SCORING_DIR/$BASENAME.tmp3.txt $SCORING_DIR/$BASENAME.tmp3.tag

rm $SCORING_DIR/$BASENAME.tmp.txt $SCORING_DIR/$BASENAME.tmp2.txt $SCORING_DIR/$BASENAME.tmp3.txt

print_info "Scoring using sclite" 1
for ctmfile in $(ls $PASS_DIR/*.ctm)
do
    cp $ctmfile $SCORING_DIR/
    print_info "$BIN_PATH/sclite -D -F -r $SCORING_DIR/$BASENAME.stm stm -h $SCORING_DIR/$(basename $ctmfile) ctm -o all" 3
    $BIN_PATH/sclite -D -F -r $SCORING_DIR/$BASENAME.stm stm -h $SCORING_DIR/$(basename $ctmfile) ctm -o all
done

print_info "[${BASENAME}] <= ScoringRes End | $(date +'%d/%m/%y %H:%M:%S')" 1