# Author: Daniel Ortiz Mart\'inez
# *- bash -*

# Obtain information from the verbose output generated by the
# perplexity() function

# \textbf{Categ}:

if [ $# -ne 1 ]; then
    echo "Use: obtain_info_from_verbose_ppl [verbose_ppl_file]"
else
    # Get parameters
    file=$1
    if [ ! -f $file ]; then
        echo "Error: file $file does not exist"
        exit 1
    fi
    # Obtain information
    sents=`"$GREP" "\*\*\* Sentence" "$file" | wc -l | "$AWK" '{printf"%s",$1}'`
    wordsplussents=`"$GREP" "P( " "$file" | wc -l | "$AWK" '{printf"%s",$1}'`
    words=`expr $wordsplussents - $sents`
    log10prob=`"$GREP" "P( " "$file" | "$AWK" 'BEGIN{sum=0}{sum+=(log($(NF-1))*(1/log(10)))}END{printf"%.1f",sum}'`
    oovw=`"$GREP" "P( <unk>" "$file" | wc -l | "$AWK" '{printf"%d",$1}'`
    oovwlog10p=`"$GREP" "P( <unk>" "$file" | "$AWK" 'BEGIN{sum=0}{sum+=(log($(NF-1))*(1/log(10)))}END{printf"%.1f",sum}'`
    perplexity=`echo "" | "$AWK" -v sents=$sents -v words=$words -v l10p=$log10prob '{printf"%.1f",exp(-(l10p/(words+sents))*log(10))}'`
    perpwithoutoov=`echo "" | "$AWK" -v sents=$sents -v words=$words -v l10p=$log10prob -v oovw=$oovw -v oovwlog10p=$oovwlog10p \
                               '{printf"%.1f",exp(-((l10p-oovwlog10p)/(words-oovw+sents))*log(10))}'`

    # Print information
    echo "Sentences: $sents"
    echo "Words: $words"
    echo "OOV words: $oovw"
    echo "log10prob: $log10prob"
    echo "log10prob of OOV words: $oovwlog10p"
    echo "Perplexity: $perplexity"
    echo "Perplexity without OOV words: $perpwithoutoov"
fi
