#!/pkg/gnu/bin/gawk -f
#!/usr/bin/awk -f 
# usage:
#     s4BatchSummary s4_res.1.out [s4_res.2.out ....]
#
# This script will go though all the given s4 output
# data report a summary word error rate for all files.
#
# This is useful when tests have been divided into separate
# batches and summary info is desired.


# ------------- Summary statistics ----------- 

BEGIN {
    done = 0;
    hasData = 0;
}

$1 == "#" && $3 == "Summary" && $4 == "statistics" {
    done = 1;
}

done == 1 && $1 == "Used:" {
    done = 0;
}


done == 0 && $1 == "Words:" && $3 == "Matches:" {
   hasData = 1;
   words[FILENAME] = $2;
   lines[FILENAME]++;
}

done == 0 && $1== "Accuracy:" && $3 == "Errors:" {
   errors[FILENAME] = $4;
}

# Total Time Audio: 5.37s  Proc: 51.47s  Speed: 9.59 X real time

done == 0 && $1== "Total" && $3 == "Audio:" {
   aTime=$4;
   pTime=$6;
   gsub(/s/, "", aTime);
   gsub(/s/, "", pTime);
   audioTime[FILENAME] =aTime;
   procTime[FILENAME] =pTime;
}

done == 1 && $1 == "Words:" && $3 == "Matches:" {
    done = 0;
}



END {
    if (hasData == 1) {
        printf("%6.6s %6.6s %6.6s %7.7s %6.6s %s\n", \
            "Lines", "Words", "Errors", "WER%", "xRT", "File");
        printf("=====================================================================\n");
        for (i in words) {
            sumWords += words[i];
            sumErrors += errors[i];
            sumLines += lines[i];
            sumAudio += audioTime[i];
            sumProc += procTime[i];
            if (words[i] > 0) {
                wer = (errors[i]/words[i]) * 100.0;
                speed = procTime[i]/audioTime[i];
                printf("%6d %6d %6d %6.2f%% %6.2f %s\n",  \
                    lines[i], words[i], errors[i], wer, speed, i);
            }
        }
        printf("=====================================================================\n");
        if (sumWords != 0 && sumAudio != 0) {
            wer = (sumErrors/sumWords) * 100.0;
            sumSpeed = sumProc/sumAudio;
            printf("%6d %6d %6d %6.2f%% %6.2f %s\n",  \
                sumLines, sumWords, sumErrors, wer, sumSpeed, "total");
        }
    }
}

