#! /bin/csh -f
# Last edited on 2000-06-07 13:30:52 by stolfi

set usage = "$0 [ -freqs ] [ -prec NUM ] CLASS PART SECTION... > GRAMMAR"

# Merges the rule counts/frequencies for the grammars
# gram/CLASS/SECTION/PART.grx for each SECTION that was given.

set freqs = 0; set titct = "counts"
set prec = ''

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 1  ) && ( "/$1" == "/-freqs" ) ) then
    set freqs = 1; set titct = "freqs"; shift;
  else if ( ( $#argv >= 2  ) && ( "/$1" == "/-prec" ) ) then
    set prec = "$2"; shift; shift; 
  else
    echo "invalid option $1"
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv < 3) then
  echo "usage: ${usage}"; exit 1
endif

set grxfile = "$1"; shift;
set part = "$1"; shift;
set sections = ( $* )

set tmp = "/tmp/$$"

set tmpfiles = ( )
foreach sec ( ${sections} )
  set frqfile = prob/obs/${sec}/${part}.frq
  set tmpfile = ${tmp}-${sec}-${part}.grx
  echo ${grxfile} + ${frqfile} -> ${tmpfile}
  cat ${grxfile} \
    | parse-and-tally \
        -v maxderivs=1 \
        -v countprec=2 \
        -v ignorecounts=1 \
        -v wordcounts=${frqfile} \
    > ${tmpfile}
  set tmpfiles = ( ${tmpfiles} ${tmpfile} )
end

compare-grammars \
  -v freqs="${freqs}" \
  -v prec="${prec}" \
  -v title="Comparing ${titct} for ${sections}" \
  ${tmpfiles}
  
/bin/rm ${tmpfiles}