#! /bin/bash -ue
# Last edited on 2025-05-04 22:50:01 by stolfi

# Creates an entry in the Wikimedia plot file description, for a given 
# {smpsec} = {lang}/{book}/{sec}.
# Tries to factor common parts.

prevsmpsec="$1"; shift  # The {smpsec} of the previous dataset in the plot, or "NONE".
smpsec="$1"; shift      # The {smpsec} for the dataset in question
nTksWhole="$1"; shift   # Number of tokens in the original input file.
nTksTrunc="$1"; shift   # Number of tokens in the truncated input file. 
nWdsTrunc="$1"; shift   # Number of distinct words (lexemes) in the truncated input file.

# Decompose ${prevsmpsec} and ${smpsec}:
flds=( `echo ${prevsmpsec} | tr '/' ' '` )
if [[ ( ${#flds[@]} != 3 ) ]]; then echo "** invalid lang/book/sec name '${prevsmpsec}'" 1>&2 ; exit 1; fi
prevlang="${flds[0]}"
prevbook="${flds[1]}"
prevsec="${flds[2]}"

flds=( `echo ${smpsec} | tr '/' ' '` )
if [[ ( ${#flds[@]} != 3 ) ]]; then echo "** invalid lang/book/sec name '${smpsec}'" 1>&2 ; exit 1; fi
lang="${flds[0]}"
book="${flds[1]}"
sec="${flds[2]}"

if [[ "${lang}" != "${prevlang}" ]]; then
  dFile="dat/${lang}/caption.wik"
  cat ${dFile} | tr '\012' ' '
fi

if [[ "${lang}/${book}" != "${prevlang}/${prevbook}" ]]; then
  dFile="dat/${lang}/${book}/caption.wik"
  cat ${dFile}
fi

# Print the {smpsec} description as a bullet parag:
printf "\n* "
dFile="dat/${smpsec}/caption.wik"
cat ${dFile} | sed -e '$s:[.]*$:.:g' | tr '\012' ' '

printf "File ${smpsec}/gud.wfr"

# Print the token counts:
if [[ ${nTksWhole} -eq ${nTksTrunc} ]]; then 
  printf " (%d tokens" "${nTksTrunc}"
else
  # printf "  %d\n" $(( ${nTksWhole} - ${nTksTrunc} )) 1>&2
  printf " (original %d tokens" "${nTksWhole}"
  printf ", truncated/filtered to %d tokens" "${nTksTrunc}"
fi
printf ", ''N'' = %d distinct).\n\n" "${nWdsTrunc}"
