#!/bin/csh

######################### LAST UPDATED: Dec,2003 ##########################

###########################################################################
#
# This shell script may be used to pre-tag and/or restore split sentences
# in any text file containing data in Senseval-2 data format. 
#
# USAGE :  proc DATA TAG RESTORE NEWLINE REPLACE UNICODE OUTPUT SOURCE
#
#	  DATA specifies the type of data being processed. 
#	  It can take any of the following numeric values:
#
#		1	Senseval-1 training data in Senseval-2 data format
#		2	Senseval-1 evaluation data in Senseval-2 data format
#		3	Senseval-2 training data
#		4 	Senseval-2 evaluation data
#		5	Any other data in Senseval-2 data format
#
#	  TAG is used to specify the level of pre-tagging to be done. 
#	  It can take any of the following numeric values... 
#
#		1	Pre-Tagging based on head word Morphology
#		2	Pre-Tagging based on head word Morphology superceded by
#			Specific instance Pre-Tagging 
#		0	No Pre-tagging
#
#	      Information for morphology based Pre-Tagging of all head words 
#             of Senseval-1, Senseval-2, line, hard, serve and interest data is
#	      provided with this package.
#             Please see README for information on how to customise.
#
#	      Information for specific instance Pre-Tagging of all capitalized 
#	      noun head words of Senseval-1 and Senseval-2 data is provided 
#	      with this package. 
#	      Please see README for information on how to customise.
#			
#	   RESTORE specifies if any split sentences are to be restored.
#	   It can take any of the following numeric values...
#	
#		1	Restore split sentences
#		0 	No split sentence restoration
#
#	      Information for split sentence restoration of Senseval-1 and
#             and Senseval-2 data provided with this package. 
#	      Please see README for information on how to customise
#
#          NEWLINE specifies if sentences are to be placed on new lines. 
#	
#	 	1	Eliminate sentence boundary markers <s> and </s> 
#			(if present) and place the sentence on new line.
#		2	In addition to 1 stated above, check text within 
#			every pair of new line characters to see if 
#			consisting of multiple sentences. If yes, place
#			each sentence on a new line.	
#		0  	No action	
#
#	  REPLACE specifies if contexts of certain instances are to be 
# 	  replaced with modified/new contexts. The instances whose 
#	  contexts are to be replaced and the modified/new contexts are 
#	  specified via a CONTEXT file. 
#
#		1	replace contexts
#		0	No action
#
#	  UNICODE specifies if special unicode characters are to be 
#	  replaced by appropriate xml representations as listed in the file
#	  unicodemap.txt.
#
#		1	replace unicode characters
#		0 	No action
#
#	  OUTPUT is the name of the directory where all the output files
#	  are to be created. The directory specified will be created by
#	  the script and should not be existing already. 
#
#	  SOURCE is the name of the Senseval-2 evaluation file. A complete 
#	  or relative path where the file exists may be specified.
#
###########################################################################

set okfl = 1
set crdir = 0
set tag = 0
set restore = 0
echo
# echo "Verifying command line arguments and existence of appropriate data files."

# Store present directory in variable
set prdir = `pwd`

if ($#argv < 8) then
    echo
    echo "All command line arguments not specified."
    set okfl = 0
else
    set Sval = $1
    set tag = $2
    set restore = $3
    set mark = $4
    set replace = $5
# CHANGE Dec, 03
    set unifl = $6
    set fn = $7
    set evalfl = $8
    if ($tag == 0 && $restore == 0 && $mark == 0) then 
	echo
	echo "No action Requested. Exiting."	
	echo 
	exit()
    endif
endif

if ($okfl) then
    if (-d $fn) then
	echo
	echo "The directory specified already exists."
	set okfl = 0
    endif
endif

if ($okfl) then
    mkdir -p $fn 
    if !(-d $fn) then
	echo
	echo "Failed to create the output directory"
	set okfl = 0
    else
	# In case relative path to output directory is specified 
	# capture complete path
	cd $fn
	set fn = `pwd`
	cd $prdir
	set crdir = 1
    endif
endif

if ($okfl) then
    if !(-f $evalfl) then
    	echo
    	echo "The SOURCE file does not exist."
    	set okfl = 0
    endif
endif

if ($okfl) then
    cp $evalfl $fn/1.txt
    cd $fn

    echo "Pre-Tagging and/or split sentence restoration"
    echo 

switch ($Sval)
    case 1:
     	set nouns = "train-nouns-S1.txt"
   	set types = "train-types-S1.txt"
	set lines = "train-lines-S1.txt"
	set repfl = "train-rep-S1.txt"
	breaksw
    case 2:
    	set nouns = "test-nouns-S1.txt"
    	set types = "test-types-S1.txt"
    	set lines = "test-lines-S1.txt"
	set repfl = "test-rep-S1.txt"
	breaksw
    case 3:
    	set nouns = "train-nouns-S2.txt"
        set types = "train-types-S2.txt" 
        set lines = "train-lines-S2.txt"
	set repfl = "train-rep-S2.txt"
	breaksw
    case 4:
    	set nouns = "test-nouns-S2.txt"
        set types = "test-types-S2.txt"
        set lines = "test-lines-S2.txt"
	set repfl = "test-rep-S2.txt"
	breaksw
    case 5:
    	set nouns = "nouns-O.txt"
        set types = "types-O.txt"
        set lines = "lines-O.txt"
	set repfl = "rep-O.txt"
	breaksw
    default:
    	echo "Invalid value for Data Type"
	set okfl = 0
endsw
endif

if ($okfl) then
    switch ($tag)
	case 0:
	    echo "No Pre-Tagging Requested"
	    cp 1.txt pretag.xml
	    breaksw
	case 1:
    	    set nouns = "dummy.txt"
	case 2:
    	    echo "Begining Pre-Tagging"
	    pretag.pl -PNouns $REFINEHOME/data/PRETAG/$nouns $REFINEHOME/data/PRETAG/$types pretag.xml 1.txt
	    breaksw
	deafult:	
    	    echo "Invalid value for Pre-Tagging level"
     	    set $okfl = 0
    endsw
endif

if ($okfl) then
    switch ($restore)
	case 0:
	    echo "No split sentence restoration requested"
	    cp pretag.xml join.xml
	    breaksw
	case 1:
    	    echo "Begining Split sentences restoration"
	    join.pl $REFINEHOME/data/SPLIT/$lines pretag.xml join.xml
	    breaksw
	case 2:
    	    echo "No Split sentence Restoration"
            breaksw
	default:	
    	    echo "Invalid value for RESTORE" 
            set $okfl = 0
    endsw 
endif

if ($okfl) then
    switch ($mark)
        case 0:
            echo "No sentence boundary marking requested"
	    cp join.xml multisent.xml
            breaksw
	case 1:
            echo "Eliminating sentence boundary markers(if present) and "
	    echo "placing sentences on new lines."
	    mark.pl marked.xml join.xml
            breaksw
        case 2:
	    echo "Eliminating sentence boundary markers(if present) and "
            echo "placing sentences on new lines."
            mark.pl marked.xml join.xml

            echo "Begining multiple sentence detection."
            multisent.pl multisent.xml marked.xml
            breaksw
        default:
            echo "Invalid value for NEWLINE"
            set $okfl = 0
    endsw
endif

if ($okfl) then
    switch ($replace)
        case 0:
            echo "No context replacement requested."
	    cp multisent.xml unicode.xml
            breaksw
        case 1:
            echo "Replacing contexts of specific instances."
            replace.pl $REFINEHOME/data/REPLACE/$repfl replace.xml multisent.xml
            breaksw
        default:
            echo "Invalid value for REPLACE"
            set $okfl = 0
    endsw
endif

if ($okfl) then
    switch ($unifl)
        case 0:
            echo "No unicode replacement requested."
            breaksw
        case 1:
            echo "Replacing unicode characters."
            unicode2xml.pl $REFINEHOME/unicodemap.txt unicode.xml replace.xml 
            breaksw
        default:
            echo "Invalid value for UNICODE"
            set $okfl = 0
    endsw
endif


if ( $okfl == 1 && $tag == 0) then
    rm pretag.xml
endif
if ( $okfl == 1 && $restore == 0) then
    rm join.xml
endif
if ( $okfl == 1 && $mark == 0) then
    rm multisent.xml
endif
if ( $okfl == 1 && $replace == 0) then
    rm replace.xml
endif

if !($okfl) then
    echo
    echo "Usage: proc DATA TAG RESTORE NEWLINE OUTPUT SOURCE"
    echo "Where-"
    echo "      DATA specifies the type of data being processed."
    echo "      It can take any of the following numeric values:"
    echo
    echo "      1       Senseval-1 training data in Senseval-2 data format"
    echo "      2       Senseval-1 evaluation data in Senseval-2 data format"
    echo "      3       Senseval-2 training data"
    echo "      4       Senseval-2 evaluation data"
    echo "      5       Any other data in Senseval-2 data format"
    echo
    echo "      TAG is used to specify the level of pre-tagging to be done."
    echo "      It can take any of the following numeric values..."
    echo
    echo "      1       Pre-Tagging based on head word Morphology"
    echo "      2       Pre-Tagging based on head word Morphology superceded by"
    echo "              Specific instance Pre-Tagging"
    echo "      0       No Pre-tagging"
    echo
    echo "      RESTORE specifies if any split sentences are to be restored."
    echo "      It can take any of the following numeric values..."
    echo
    echo "      1       Restore split sentences"
    echo "      0       No split sentence restoration"
    echo
    echo "      NEWLINE specifies if sentences are to be placed on new lines." 
    echo
    echo "      1       Eliminate sentence boundary markers <s> and </s>"
    echo "              (if present) and place the sentence on new line."
    echo "      2       In addition to 1 stated above, check text within"
    echo "              every pair of new line characters to see if"
    echo "              consisting of multiple sentences. If yes, place" 
    echo "              each sentence on a new line." 
    echo "      0       No action" 
    echo
    echo "      REPLACE specifies if contexts of certain instances are to be"
    echo "      replaced with modified/new contexts. The instances whose"
    echo "      contexts are to be replaced and the modified/new contexts are"
    echo "      specified via a CONTEXT file."
    echo
    echo "      1       replace contexts"
    echo "      0       No action"
    echo
    echo "      UNICODE specifies if special unicode characters are to be replaced"
    echo "      by appropriate xml representations as listed in the file"
    echo "      unicodemap.txt."
    echo 
    echo "      1       replace unicode characters"
    echo "      0       No action"
    echo 
    echo "      OUTPUT is the name of the directory where all the output files"
    echo "      are to be created. The directory specified will be created by"
    echo "      the script and should not be existing already."
    echo
    echo "      SOURCE is the name of the senseval1 test data input file."
    echo "      A complete or relative path where the file exists may be specified."
    echo
    if ($crdir) then
	cd $prdir
        rm -r $fn
    endif
    exit()
else
    echo
    echo "Pre-Tagging and/or Sentence Repairs Successful."
    echo
endif

