#!/bin/bash -e

####################################################################################### 
#              REVERB  CHALLENGE -  automatic speech recognition                      # 
#                                                                                     # 
# scripts and tools written by:                                                       # 
# - Volker Leutnant,                                                                  # 
# - Marc Puels,                                                                       # 
# - Reinhold Haeb-Umbach                                                              # 
#                                                                                     # 
# Department of Communications Engineering, University of Paderborn, Germany          # 
#                                                                                     # 
# support: reverb-asr@lab.ntt.co.jp                                                   #
#######################################################################################

. printlib


resDir=$1
shift

testSet=$1
shift

nbTasks=$1
shift

nbSets=$1
shift

for (( count=0; count<$nbTasks; count++ ))
do
  taskSets="$taskSets $1"
  shift
done

for (( count=0; count<$nbSets; count++ ))
do
  setIds=("$setIds $1")
  shift
done

# Create result table header
spacing=12
adj_spacing=$((spacing * nbTasks))

for setId in ${setIds[@]};
do 
  printf "%-${adj_spacing}s" " |   Room $setId"
done
printf " ||\n"

for setId in ${setIds[@]};
do
  for task in ${taskSets[@]};
  do
    printf "%-${spacing}s" " | $task" 
  done
done
printf "%-${spacing}s |\n" " || Avg." 
#printf " |\n"

for setId in ${setIds[@]};
  do
  for task in ${taskSets[@]};
  do
    printf '%0.1s' "-"{1..12}
  done
done
printf '%0.1s' "-"{1..14}
printf "\n"

# Print results and calculate average
mean_wer="0"
tot_sent="0"
for setId in ${setIds[@]};
do
  for task in ${taskSets[@]};
  do
    taskName=${testSet}_for_${task}_room${setId}.log
    resLog=($(find "$resDir" -maxdepth 1 -type f -name '*'${taskName}))
    if [ -e $resLog ]; then
      wer=$(perl ${WSJTOOLS}/perl/getwer $resLog)
      sent=$(perl ${WSJTOOLS}/perl/get_sentnb $resLog)
      if (("$sent" -n)); then
	tot_sent=$(echo $tot_sent + $sent|bc)
	mean_wer=$(echo $mean_wer + $wer '*' $sent |bc)
      fi
        printf "%-${spacing}s" " | $wer"
      fi
  done
done

if (("$tot_sent" != "0"));then
  mean_wer=$(echo "scale=2;" $mean_wer / $tot_sent |bc)
  printf "%-${spacing}s |\n" " || $mean_wer"
else
  printf "\n"
  echo " | !!! ERROR in the results !!!\n"
fi
printf "\n"

