#! /bin/bash -eu
# Last edited on 2026-04-25 06:07:00 by stolfi

# Compares the prob distr of EVA string pairs (mapped to classes)
# inside lines and across line breaks.

qmap="$1"; shift  # Type of character map (0 = delete 'q' etc, 1 keep)
size="$1"; shift  # Length of each string in EVA chars.
skip="$1"; shift  # Chars to skip between the wto strings.

ifile="../074/st_files/str-parags.ivt"

xskip=$( printf "%02d" ${skip} )
oprefix="out/pairs-skip-q${qmap}-n${size}-s${xskip}"

ffiles=()
for kind in 0 1; do 

  echo "collecting string pairs of kind = ${kind} with size = ${size} skip = ${skip} ..." 1>&2
  pfile="${oprefix}-k${kind}.txt"
  cat ${ifile} \
    | collect_string_pairs_skipping.gawk \
        -i error_funcs.gawk \
        -v qmap=${qmap} \
        -v kind=${kind} \
        -v size=${size} \
        -v skip=${skip} \
    > ${pfile}
  wc -l ${pfile} 1>&2
    
  echo "computing their frequencies ..." 1>&2
  ffile="${oprefix}-k${kind}.pfr"
  cat ${pfile} \
    | sed -e 's:[ ]:_:g' \
    | sort | uniq -c \
    | compute_freqs_from_counts.py \
         -encoding bytes -total '~TOT~' \
    > ${ffile}
  wc -l ${ffile} 1>&2
  
  echo "computing predictability ..." 1>&2
  efile="${oprefix}-k${kind}.ent"
  cat ${ffile} \
    | compute_predictability.gawk \
        -i error_funcs.gawk \
        -v kind=${kind} \
        -v size=${size} \
        -v skip=${skip} \
    > ${efile}
    
  ffiles+=( ${ffile} )
done

echo "joining the two count files and plotting ..." 1>&2
ofile="${oprefix}-stats.txt"
join -1 3 -2 3 -a 1 -a 2 -e '0' -o1.1,1.2,2.1,2.2,0 ${ffiles[0]} ${ffiles[1]}  \
  | gawk \
    -i error_funcs.gawk \
    ' BEGIN { totwd = 0; totsh = 0; }
      // { 
        ct0=$1; fr0=$2; ct1=$3; fr1=$4; wd = $5
        out(wd, ct0, fr0, ct1, fr1)
      }
      function out(wd,ct0,fr0,ct1,fr1,  df) {
        df = log((fr0 + 0.0005)/(fr1 + 0.0005))
        printf "%5d %5d %6.3f %6.3f %+6.2f %s ", ct0, ct1, fr0, fr1, df, wd
        if (wd != "~TOT~") {
          nX0 = int(100*fr0 + 0.5)
          nX1 = int(100*fr1 + 0.5)
          nB = 60 - length(wd) - nX0
          for (k = 0; k < nB; k++) { printf " " }
          for (k = 0; k < nX0; k++) { printf "X" }
          printf " | "
          for (k = 0; k < nX1; k++) { printf "X" }
        }
        printf "\n"
      }
    ' \
  > ${ofile}
  wc -l ${ofile} 1>&2

cat ${ofile} 1>&2 
