#!/bin/bash

usage() {
  echo "usage: infer-case [-c] METHOD DATADIR"
  echo " OPTIONS:"
  echo "    -c       :  output verbal choices with case frames" 
  echo "  METHOD:"
  echo "    baseline : just output the most common case system"
  echo "    grams    : infer a case system by the presence of grams"
  echo "    count    : infer a case system by gram counts"
  echo "  DATADIR:"
  echo "    the directory containing projected trees and IGT data"
  exit 1
}


while getopts ":c" opt; do
  case $opt in
    c)
      choices="choices"
      ;;
    \?)   
      usage 
      ;;
  esac
done

shift $((OPTIND-1))

test $# -eq 2 || usage
echo "$1" | grep -E '^(baseline|count|grams)$' -q || usage
test -d "$2" || usage

method="$1"
datadir="$2"

export PATH=scripts/:$PATH

case $method in
  baseline )
    echo "case-marking=none";;
  grams )
    if [ -n  "$choices" ]
    then
      # when user requests choices printout, we need to do the extract-case
      # tgrep wrapper in order to have the verbs and args picked out from the
      # string

      # note this method only returns items that we have a  gramtree for
      GLOSSTEXTGT=`igtall.py "$datadir/igt.all" gloss text gramtree`
      echo "$GLOSSTEXTGT" > gtgt

      # pick out all the grammtree strings  (every third line)
      echo "$GLOSSTEXTGT" | sed -n '3~3p' > gt
      pattern="%p\t%=verb=\t%=sbj=\t%=obj="
      
      # tgrep them for verb,sbj,obj tree fragments
      tgrep2 -p gt gt.tgc
      tgrep2 -b "\n" -m "$pattern" -c gt.tgc scripts/case.tgrep2 > tgr
      echo "" >> tgr
      
      # pass all this info into infer-case-from-grams
      exec 4<tgr
      exec 5<gtgt
      read -u4 tgrline
      while read -u4 tgrline;  do
        read -u5 gtline
          echo "$gtline"
        read -u5 gtline
          echo "$gtline"
        echo "$tgrline"
        read -u5 gtline
      done | infer-case-from-grams "$choices"

      # cleanup the tempfiles
      rm gtgt gt gt.tgc tgr 

    else
      ## don't worry about printing choices just infer case from grams 
      igtall.py "$datadir/igt.all" gloss | infer-case-from-grams 
  
    

    fi ;;
  count )
    if [ -n "$choices" ]; then
      ## as above, need to get gloss, text, grammtree all aligned
      ## in order to extract choices from this info
      GLOSSTEXTGT=`igtall.py "$datadir/igt.all" gloss text gramtree`
      echo "$GLOSSTEXTGT" > gtgt

      # pick out all the grammtree strings  (every third line)
      echo "$GLOSSTEXTGT" | sed -n '3~3p' > gt
      pattern="%p\t%=verb=\t%=sbj=\t%=obj="
      
      # tgrep them for verb,sbj,obj tree fragments
      tgrep2 -p gt gt.tgc
      tgrep2 -b "\n" -m "$pattern" -c gt.tgc scripts/case.tgrep2 > tgr
      echo "" >> tgr
      
      # pass all this info into infer-case-from-grams
      exec 4<tgr
      exec 5<gtgt
      read -u4 tgrline
      while read -u4 tgrline;  do
        read -u5 gtline
          echo "$gtline"
        read -u5 gtline
          echo "$gtline"
        echo "$tgrline"
        read -u5 gtline
      done | infer-case-from-counts "$choices"

      # cleanup the tempfiles
      rm gtgt gt gt.tgc tgr 

    else
    # 1. find 2-arg (NP or PP) verbs, avoid possible stopwords
    # 2. find 1-arg (NP or PP) verbs
    ####### (1-2 done by extract-case)
    # 3. get grams w/in NPs (on dets, NNs, etc), look for case markers
    # 4. label A and O for 2-arg verb args, S for 1-arg verb arg
    # 5. discard grams appearing on both A and O
    #    (with same tense on verb and same person val on args)
    # 6. Compare remaining grams
    #   a. no grams: no case
    #   b. A=S, O is diff: nom-acc
    #   c. S=0, A is diff: erg-abs
    #   d. S, A, O diff: tripartite
    #   e. 1-arg have 2 diff cases on S, consistent for verbs: split-s
    #   f. 1-arg diff cases on S, inconsistent for verbs: fluid-s
    #   g. case diff for some nouns: split-n
    #   h. s=a for some props on verb, s=o for others: split-v
    #   i. focus-case : not now
    ####### (3-6 done by infer-case-from-counts)
    extract-case "$datadir/igt.all" | infer-case-from-counts
 
    fi ;;
esac

