#! /bin/bash -eu
# Last edited on 2026-06-04 18:12:21 by stolfi

temp="/tmp/$$"
??? ALL WRONG

max_noise=30
ch_noises=( 00 ${max_noise} )

txfiles=()
for noise in ${ch_noises[@]}; do
  for tones in 0 1; do 
    opref="res/chinovyese-t${tones}-n${noise}"
    txfile="${opref}.txt"
    make -f Makefile CH_TONES=${tones} CH_NOISE=${noise} single-ch-text
    echo "${txfile}:" 1>&2
    head -n 3 ${txfile}
    txfiles+=( ${txfile} )
    echo "======================================================================" 1>&2
  done
done

for ecorr in 0 1; do
  opref="res/voychinese-e${ecorr}"
  txfile="${opref}.txt"
  make -f Makefile VM_ECORR=${ecorr} single-vm-text
  echo "${txfile}:" 1>&2
  head -n 3 ${txfile}
  txfiles+=( ${txfile} )
  echo "======================================================================" 1>&2
done

rpfiles=()
ctfilee=()
for txfile in ${txfiles[@]}; do

  txname="${txfile%.txt}"
  txname="${txname##res/}"
  echo "~~~ counting words and repeats in ${txfile} ..." 1>&2
  
  ctfile="${txname}.wct"
  make -f Makefile TEXT_NAME=${txname} word-counts-file
  ctfiles+=( ${ctfile} )
    
  if [[ "${txname}" =~ voychinese ]]; then
    for simil in 0 1; do 
      rpfile="${txname}-s${simil}.rct"
      make -f Makefile TEXT_NAME="${txname}" RP_SIMIL=${simil} rep-counts-file
      rpfiles+=( ${rpfile} )
    done
  else
    rpfile="${txname}-s0.rct"
    make -f Makefile TEXT_NAME="${txname}" RP_SIMIL=0 rep-counts-file
    rpfiles+=( ${rpfile} )
  fi
  echo "======================================================================" 1>&2
done

# Plot the repetition graphs

n050_plot_two_repeat_histograms.sh \
  "sbj-x-sps"                        "Repeat sequences in SBJ and SPS" \
  "chinovyese-t1-n00-s0"             "SBJ" \
  "voychinese-e0-s0"                 "SPS" \

echo "======================================================================" 1>&2

n050_plot_four_repeat_histograms.sh \
  "chinovyese-reps"                  "Repeats in modified SBJ" \
  "chinovyese-t0-n00-s0"             "No tones" \
  "chinovyese-t1-n00-s0"             "Tones" \
  "chinovyese-t0-n${max_noise}-s0"   "No tones + ${max_noise}% Noise" \
  "chinovyese-t1-n${max_noise}-s0"   "Tones + ${max_noise}% Noise" \

echo "======================================================================" 1>&2

n050_plot_four_repeat_histograms.sh \
  "voychinese-reps"      "Repeats in modified SPS" \
  "voychinese-e0-s0"     "Natural" \
  "voychinese-e1-s0"     "Corrected" \
  "voychinese-e0-s1"     "Similarity" \
  "voychinese-e1-s1"     "Corrected + Similarity" \

echo "======================================================================" 1>&2

n050_plot_repeat_locations.sh \
  "chinovyese-locs"       "Location of SPS repeats" \
  "chinovyese-t1-n00-s0" \
  "chinovyese-t1-n00" \

echo "======================================================================" 1>&2

# Summary of token and lexeme counts:
for txfile in ${txfiles[@]}; do
  ctfile="${txfile/.txt/.wct}"

  printf "%-30s " ${txfile}
  cat ${ctfile} \
    | gawk \
        ' /^ *[0-9]/ {
            ct = $1; wd = $2
            t += ct; w += 1; if (ct >= 2) { s += 1 }
          }
          END { printf "%5d %5d %5d\n", t, w, s }
        '
done
