#! /bin/csh -f
# Last edited on 2000-05-18 04:20:09 by stolfi

set usage = "$0 [-v axiom=SYMBOL] < GRAMMAR.grx > LANGUAGE.prb"

# Reads a finite probabilistic grammar from stdin. Writes the
# generated words and probabilities (without derivation delimiters,
# with ambiguous strings collapsed) to stdout.

set axopt = ( )

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

if ( $#argv != 0 ) then
  echo "usage: ${usage}"; exit 1
endif

nice -n 19 \
  enum-derivations ${axopt} \
  | gawk '/./{w=$2; gsub(/[«»]/,"",w); print $1, (w==""?".":w);}' \
  | combine-probs \
  | sort -b +0 -1gr +1 -2