#! /bin/bash
# Last edited on 2022-05-01 19:43:42 by stolfi

# Zipf plot of a list of names

# Input is a file "${name}.txt" has pairs of "{COUNT} {NAME}" on each line.
# Assumes names have been converted to lowercase.
# Assumes names are in full (not just initials).
# Ignores '#' comments, blank lines, {NAME}="anonymous".
# Output plot is "${name}.png"

title="$1"; shift  # Plot title.
name="$1"; shift   # File name minus extension.

tmp="/tmp/$$" 

dfile="${name}.txt" # Raw input data file.
pfile="${name}.png" # Final plot file.

tdfile="${tmp}.txt"  # Temporary cleaned data file.
tpfile="${tmp}.png"  # Temporary plot file.
tnfile="${tmp}.num"  # Temporary file for Zipf constant.

# Clean up the data file:
cat ${dfile} \
  | gawk \
     ' //{ gsub(/[ ]*[#].*$/, "", $0) }
       /^[ ]*$/ { next; }
       // {
         c = $1; f = $2;
         if (f ! "anonymous") { print c, f }
       }
     ' \
  | sort -k1nr -k2 \
  > ${tdfile}
wc -l ${dfile} ${tdfile}

# Collect total count ${tref} of first ${nref} names:
rskip=100 # Data lines to skip.
rstop=200 # Data lines to take, including skip.
cat ${tdfile} \
  | gawk -v rskip=${rskip} -v rtake=${rtake} \
      ' //{ c=$1; n+=1.0; if ((n > rskip) && (r <= rtake)) { tc+=c; tw+=1/n }; next} 
        END { print int(tc/tw) } 
      ' \
  > ${tnfile}
tref=`cat ${tnfile}`
echo "tref = ${tref}" 1>&2 

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 1000,1000 font "arial,24"
set output "${tpfile}"

set title "${title}"

set logscale x
set logscale y

tref = ${tref} + 0.0

xv(k) = 1+column(k)
yv(k) = column(k)/tref
rv(k) = 1.0/(1+column(k))

plot \
  "${tdfile}" using (xv(0)):(yv(1)) notitle with linespoints pt 7 ps 2 lc rgb '#880000', \
  ""          using (xv(0)):(rv(0)) notitle with lines lc rgb '#008800'
quit
EOF

if [[ -s ${tpfile} ]]; then
  convert ${tpfile} -resize '50%' ${pfile}
  display ${pfile}
else
  echo "** plot failed" 1>&2
fi
rm -f ${tmp}.*
