#! /bin/bash -u
# Last edited on 2026-07-24 00:01:24 by stolfi

echo "ensuring that every sample has a tex-encoding function ..." 1>&2

export ftri="reencode_word_trivial.gawk"
export ftex="reencode_words_for_tex.gawk"
for smpdir in dat/????/??? ; do
  filter="${smpdir}/${ftex}"
  if [[ ! -x ${filter} ]]; then
    echo "linking ${filter} -> work/${ftri}" 1>&2
    ( cd $smpdir && ln -s work/${ftri} ${ftex} )
  fi
done

echo "creating formatted lists of top and bottom lexemes, per section ..." 1>&2
  
smpszs=( $( cat sample-sizes.txt ) )

for sn in ${smpszs[@]} ; do
  sna=( ${sn/:/ } );
  smp="${sna[0]}"; 
  nLexTop="${sna[1]}";
  smpdir="dat/${smp}"
  secs=( $( cat ${smpdir}/sections-ok.tags ) tot.1 ) 
  echo "  nLex = ${nLexTop}" 1>&2
  for sec in ${secs[@]} ; do
    wfrFile="${smpdir}/${sec}/whole/gud.wfr"
    echo "  wfrFile = ${wfrFile}" 1>&2
    
    # Specified number of top words WITH counts and freqs,
    # separate for each section, in 4 columns:
    mkdir -p "tex/${smp}/${sec}"
    texTopFile="${smp}/${sec}/top-${nLexTop}-words.tex"
    echo "  texFile = ${texTopFile}" 1>&2
    cat ${wfrFile} \
      | ./get_top_words_from_wfr.sh ${nLexTop} ${sec} \
      | gawk '/./{ print $2, $3, $6; }' \
      | ${smpdir}/reencode_words_for_tex.gawk -v field=3 \
      | ./tex_format_word_freqs.gawk \
          -v ncols=4 \
          -v showCounts=1 -v showFreqs=1 \
      > dat/${texTopFile}
    cat dat/${texTopFile} | head -n 10 1>&2
    update_paper_include.sh dat/${texTopFile} tex/${texTopFile}
    
    # Various numbers of words at bottom, WITHOUT counts and freqs,
    # separate for each section, in 8 columns:
    for nLex in 24 40 ; do 
      texFile="${smp}/${sec}/bot-${nLex}-words.tex";
      echo "  nLex = ${nLex}" 1>&2
      echo "  texFile = ${texFile}" 1>&2
      cat ${wfrFile} \
        | ./get_bot_words_from_wfr.sh ${nLex} ${sec} \
        | gawk '/./{ print $1, $2, $3, $6; }' \
        | dat/${smp}/reencode_words_for_tex.gawk -v field=4 \
        | ./tex_format_word_freqs.gawk \
            -v ncols=8 \
            -v showCounts=0 -v showFreqs=0 \
        > dat/${texFile}
      cat dat/${texFile} | head -n 10 1>&2
      update_paper_include.sh dat/${texFile} tex/${texFile}
    done
  done
done

echo "creating lists of top lexemes comparing sections ..." 1>&2

sampsecs=( \
  voyn/prs/pha,hea,heb,cos,str,zod,bio \
  voyn/prs/pha,hea,heb,cos,str,zod,bio \
  engl/cul/pre,her,rec \
  latn/ptt/gen,exo,lev,num,deu \
  latn/nwt/mat,mrk,luk,joh \
  grek/nwt/mat,mrk,luk,joh \
  span/qvi/one,two \
  russ/ptt/gen,exo,lev,num,deu \
  viet/ptt/gen,exo,lev,num,deu \
  viet/nwt/mat,mrk,luk,jhn \
  chin/ptt/gen,exo,lev,num,deu \
  chin/ptn/gen,exo,lev,num,deu  \
)

for nLex in 16 24 40 ; do
  for ss in ${sampsecs[@]} ; do
    echo "${ss}"  1>&2
    smp="${ss%/*}"
    secscm="${ss##*/}"
    mainsecs=( ${secscm//,/ } )
    texFile="${smp}/top-${nLex}-words-per-section.tex";
    echo "smp = ${smp}  mainsecs = (${mainsecs[@]})  nLex = ${nLex}  tex file = ${texFile}" 1>&2
    tempFile="/tmp/.top"; rm -f ${tempFile}
    for sec in ${mainsecs[@]}; do
      wfrFile="${smpdir}/${sec}/whole/gud.wfr"
      echo "  wfrFile = ${wfrFile}" 1>&2
      cat ${wfrFile} \
        | ./get_top_words_from_wfr.sh ${nLex} ${sec} \
        | gawk '/./{ print $1, $2, $3, $6; }' \
        >> ${tempFile}
    done
    texFile="{smp}/top-${nLex}-words-by-section.tex";
    cat${tempFile} \
      | ${smpdir}/reencode_words_for_tex.gawk -v field=4 \
      | ./tex_format_word_freqs_by_section.gawk \
          -v showCounts=0 -v showFreqs=1 \
      > dat/${texFile}
    cat dat/${texFile} | head -n 10 1>&2
    update_paper_include.sh dat/${texFile}  tex/${texFile}
  done
done

make_tex_top_words_by_section.sh
