#!/bin/bash 

ulimit -s unlimited
ulimit -f unlimited

export ASSERT=@ASSERT@

export YAMCHA=$ASSERT/packages/yamcha-0.23
export TINY_SVM=$ASSERT/packages/TinySVM-0.09
export CHARNIAK_PARSER=$ASSERT/packages/CharniakParser
export MORPH=$ASSERT/packages/morph-1.5
export TGREP2=$ASSERT/packages/Tgrep2

#--- maybe this can be kept under $ASSERT/lib ---#
export PERL5LIB=$ASSERT/bin  
export PYTHONPATH=$ASSERT/packages:$ASSERT/lib/python-lib

export LD_LIBRARY_PATH='.:$YAMCHA/lib:/usr/local/lib:/usr/local/include:/usr/include'
export TK_LIBRARY='/home/CU/packages/tk8.2.2/library'
export TCL_LIBRARY='/home/CU/packages/tcl8.2.2/library'

export PATH='.:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/bin/X11:/usr/X11R6/bin:/bin:$ASSERT/util/bin:$ASSERT/bin:$ASSERT/scripts/batch:$ASSERT/scripts/client:$ASSERT/scripts/remote:$TGREP2:$CHARNIAK_PARSER/bin:$YAMCHA/bin:$TINY_SVM/bin'

#--- some of the recent redhat uses UTF-8 char set which is incompatible with some routines, so this requirement ---#
LANG=en_US.iso8859-1
export LANG

usage()
{
    cat <<EOF

Usage: assert [OPTIONS] <filename.ext>

Options:
  [--tag[=argument|theta|opinion|custom]] default: argument
  [--targets-file[=filename]] default: all targets
  [--mode[=batch|client|remote] default: batch
  [--model-file=[model-file]] required for --tag=custom option
  [--feature-extractor=[custom feature-extraction-script]] required for --tag=custom option
  [--plain|--html] default: plain
  [--url=[URL]] URL where the server is running
  [--help]
  [--version] 

EOF
    exit $1
}

#--- if no arguments are specified, then just print usage ---#
if test $# -eq 0; then
    usage 1
fi

tag="argument"
model="$ASSERT/models/propbank-arguments.model"
mode="batch"
target=0
target_file=""
parse_format="plain"
feature_extractor=""

check_opt=`echo "$1" | sed 's/--//'`

while test $1 != $check_opt; do
    case "$1" in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` 
			;;
    *) optarg="" 
			;;
    esac

    case "$1" in
		--tag=*)
			tag=$optarg
			if test $tag = "theta"; then
				model="$ASSERT/models/thematic-roles.model"
				tag="theta"
			elif test $tag = "opinion"; then
				model="$ASSERT/models/opinion+holder.model"
				tag="opinion"
			elif test $tag = "argument"; then
				model="$ASSERT/models/propbank-arguments.model"
				tag="argument"
			elif test $tag = "custom"; then
				tag="custom"
			else
				usage 0
				exit 1
			fi
			;;
		
		--url=*)
			url=$optarg
			;;

		--mode=*)
			mode=$optarg
			if [[ $mode != "client" && $mode != "batch" && $mode != "remote" ]]; then
				echo ""
				echo "ERROR: illegal \"mode\" specified, check usage"
				usage 0
				exit 1
			fi
			;;

		--targets-file=*)
			target_file=$optarg
			target=1
			;;

		--model=*)
			model=$optarg
			;;

		--feature-extractor=*)
			feature_extractor=$optarg
			;;

		--html)
			parse_format="html"
			;;
		
		--plain)
			parse_format="plain"
			;;
		
		--help)
			usage 0
			;;
		
		--version)
			echo "0.11b"
			exit 0
			;;
		*)
			usage 0
			exit 1
			;;
    esac
    shift
    check_opt=`echo "$1" | sed 's/--//'`
done


filename=$1

#---- perform a few sanity checks ----#
#--- check for inconsistencies in tagging and models ----#
if test "$tag" = "custom"; then

	#--- check that the model is specified, and is not one of the standard models ---#
	#if test "$model" = "$ASSERT/models/propbank-arguments.model" -o "$model" = "$ASSERT/models/thematic-roles.model" -o "$model" = "$ASSERT/models/opinion+holder.model"; then
	if test "$model" = "$ASSERT/models/thematic-roles.model" -o "$model" = "$ASSERT/models/opinion+holder.model"; then
		echo "ERROR: a custom tagging requires you to provide a different model"
		exit 1
	fi

	#--- check if the feature extractor script is provided, it exists and is executable ---#
	if ! [[ $feature_extractor != "" && -e $feature_extractor && -x $feature_extractor ]]; then
		echo "ERROR: check if the feature extraction script is specified, exists, and is executable"
	fi

else
	if test "$model" != "$ASSERT/models/propbank-arguments.model" -a "$model" != "$ASSERT/models/thematic-roles.model" -a "$model" != "$ASSERT/models/opinion+holder.model"; then
		echo "ERROR: cannot provide a model when not performing custom tagging"
		exit 1
	fi
fi
#-------------------------------------#



#--- check if the file exists ---#
if test ! -e $filename; then
    echo "ERROR: File '$filename' does not exist!"
    exit 1
fi
#--------------------------------#




#--- handle remote servicing request and pass it on to the respective python script ---#
if test "$mode" = "remote"; then
	if test "$url" = ""; then
		echo "ERROR: please specify the URL"
		exit 1
	fi

 	$ASSERT/bin/assert-remote.py --url=$url --format=$parse_format --roles=$tag $filename
	exit 1
fi
#--------------------------------------------------------------------------------------#




#---- remove the path string and the extension from the filename ----#
filestem=`echo "$filename" | awk -F/ '{print $NF}' | sed 's/\.[^.][^.]*$//g'`
cp $filename $filestem.sentences


#--- prepare sentences in the format required for feature extraction ---#
if test $target = 0; then
	#--- tag all ---#
	if test "$mode" = "batch"; then
		echo $ASSERT/bin/prepare-for-framenet-all-targets-batch.csh $filestem.sentences $filestem.fn-test 
		$ASSERT/bin/prepare-for-framenet-all-targets-batch.csh $filestem.sentences $filestem.fn-test 
	elif test "$mode" = "client"; then
		$ASSERT/bin/prepare-for-framenet-all-targets-client.csh $filestem.sentences $filestem.fn-test 
	fi
else
	#--- tag specific targets ---#
	if test "$mode" = "batch"; then
		$ASSERT/bin/prepare-for-framenet-specific-targets-batch.csh $filestem.sentences $target_file $filestem.fn-test 
	elif test "$mode" = "client"; then
		$ASSERT/bin/prepare-for-framenet-specific-targets-client.csh $filestem.sentences $target_file $filestem.fn-test 
	fi
fi
#-----------------------------------------------------------------------#




#--- classify passive predicates ---#
$ASSERT/bin/classify_passives.py ${filestem}.fn-test $filestem.passives $filestem.tmp $filestem.corpus




#---- feature extraction routines ----#
if test "$tag" = "argument"; then
	$ASSERT/bin/extract-propbank-features.csh $filestem.fn-test $filestem.passives
elif [[ "$tag" = "theta" ]]; then
	$ASSERT/bin/extract-baseline-features.csh $filestem.fn-test $filestem.passives
elif test "$tag" = "opinion"; then
	$ASSERT/bin/extract-baseline-features.csh $filestem.fn-test $filestem.passives
	$ASSERT/bin/PrintWordFeatureData.py --word-score-sum=$ASSERT/data/opinion-word.scores --phrase-type=ADJP $filestem.fn-test $filestem.data > $filestem.data.1
	/bin/mv $filestem.data.1 $filestem.data
elif [[ "$tag" = "custom" ]]; then
	echo feature_extractor = $feature_extractor
	$feature_extractor $filestem.fn-test $filestem.passives
else
	echo "ERROR: the code should not come here!"
	exit 1
fi
#---------------------------------------------#




#--- perform classification ---#
#echo "classifying the nodes..."
#echo model = $model
$YAMCHA/bin/yamcha -V -m $model < $filestem.data > $filestem.svm-scores




#--- post process scores file if required ---#
if [[ "$tag" = "argument" || "$tag" = "theta" || "$tag" = "opinion" ]]; then
	#--- remove overlaps ---#
	$ASSERT/bin/remove-overlaps.py $filestem.svm-scores > $filestem.svm-scores-no-overlap
	/bin/mv $filestem.svm-scores-no-overlap $filestem.svm-scores
fi
#--------------------------------------------#




#--- print parses ---#
if test "$parse_format" = "plain"; then
	$ASSERT/bin/print-plain-parse.py $filestem.svm-scores $filestem.sentences.framenet $filestem.sentences.LINES |tee $filestem.parses
else
	$ASSERT/bin/print-parse.py $filestem.svm-scores $filestem.sentences.framenet |tee $filestem.parses
fi

#--- clean up ---#
/bin/rm -f sentences $filestem.data* $filestem.fn-test $filestem.passives $filestem.sentences* $filestem.svm* 

exit 0
