#!/bin/csh

######################### LAST UPDATED: Jul,2003 ##########################

###########################################################################
#
# This shell script may be used to parse sentence which have been  
# part of speech tagged by the Brill tagger. 
#
# USAGE : parse COLLIN MODEL OUTPUT SENSEVAL2 POS [train]
#
#	  COLLIN is the Collin Parsers home directory. This directory has
#	  the README for the parser and subdirectories such as `code' and
#	  `models'. The directory path is utilized to access apropriate
#	  parser executable.
#
#	  MODEL specifies which model of the Collins Parser is to be used.
#	  It can take one of three values: 1,2 or 3 corresponding to 
#	  the respective models.
#
#         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.
#
#	  SENSEVAL2 is the original Senseval-2 data format file.
#
#         POS is the name of the input file which has sentences which
#	  are part of speech tagged. The format of the
#	  sentences is expected to be the format in which the Brill tagger
#	  produces the part of speech tagged sentences. 
#         A complete or relative path to the file may be specified.
#
#	  The fourth command line arguement, is specified as `train' 
#	  causes the final parsed xml and count files to be named as 
#	  training files. If not specified the files are named as test
#	  files. Note: This argument does not in any way influence the 
#	  parsing or contents of the file.
#
###########################################################################

echo
echo "Verifying command line arguments and existence of appropriate data files."

set okfl = 1
set crdir = 0
# Store present directory in variable
set prdir = `pwd`

if ($#argv < 4) then
   echo
   echo "Insufficient command line arguments."
   set okfl = 0
else
   set COLLIN = $1
   # CHANGE Dec, 03
   set model = $2
   set op = $3
   set sval2 = $4
   set ip = $5
endif

set tsttrn = "test"
if ($#argv > 4) then
   set tsttrn = $6
endif

# CHANGE Dec, 03
if(!(($model == 1) || ($model == 2) || ($model == 3))) then
        echo "Invalid value for MODEL"
        set okfl = 0
endif

if (!((-e $COLLIN) || (-e $COLLIN/code) || (-e $COLLIN/models/model3/events.gz) || (-e $COLLIN/code/parser) || (-e $COLLIN/models/model3/grammar))) then
    echo "The path to the Collins parser is inappropriate or some of the required parser files are missing." 
else
    cd $COLLIN
    set COLLIN = `pwd`
    cd $prdir
endif

if($okfl) then
    if (-e $op) then
        echo
        echo "A file or directory by the name of the output directory, already exists."
        set okfl = 0
    endif
endif

if($okfl) then
    if (!(-f $sval2)) then
        echo "The Senseval-2 data file specified, does not exist."
        set okfl = 0
    endif
endif

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

if($okfl) then
    mkdir -p $op
    if !(-d $op) 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
        set crdir = 1
        cd $op
        set op = `pwd`
        cd $prdir
	cp $ip $op/1.txt
	cp $sval2 $op/sval2.xml
    endif
endif

# if alls not ok print usage message and exit
if !($okfl) then
    echo
    echo "USAGE : parse COLLIN OUTPUT SENSEVAL2 POS [train]"
    echo
    echo "      COLLIN is the Collin Parsers home directory. This directory has"
    echo "      the README for the parser and subdirectories such as *code* and"
    echo "      *models*. The directory path is utilized to access apropriate"
    echo "      parser executable."
    echo
    echo "      MODEL specifies which model of the Collins Parser is to be used."
    echo "      It can take one of three values: 1,2 or 3 corresponding to"
    echo "      the respective models."
    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 "      SENSEVAL2 is the original Senseval-2 data format file."
    echo
    echo "      POS is the name of the input file which has sentences which" 
    echo "      are part of speech tagged. The format of the sentences is "
    echo "      expected to be the format in which the Brill tagger" 
    echo "      produces the part of speech tagged sentences." 
    echo "      A complete or relative path to the file may be specified." 
    echo
    echo "      The fourth command line arguement, is specified as train"
    echo "      causes the final parsed xml and count files to be named as"
    echo "      training files. If not specified the files are named as test"
    echo "      files. Note: This argument does not in any way influence the"
    echo "      parsing or contents of the file."
    cd $prdir
    exit()
endif

# Got Output directory
cd $op
echo "All systems go"
echo
echo "Preprocessing the sentences to bring them to a format acceptable"
echo "by the Collins Parser"
echo

ColinPrep.pl 119 ready2parse 1.txt > filter
rm -f 1.txt
echo "Splitting the input file if necessary"
mkdir -p SPLIT
cd SPLIT
# Splitting file into files of size - 2400 sentences each
chunk.pl 2400 parse ../ready2parse
set fils = (parse*)
# Parse each file
foreach fil ($fils)
	set opfl = $fil.op
	
	# CHANGE Dec, 03
	switch ($model)
    	case 1:
		gunzip -c $COLLIN/models/model1/events.gz | $COLLIN/code/parser ./$fil $COLLIN/models/model1/grammar 10000 1 1 1 1 > ./$opfl
		breaksw
	case 2:
		gunzip -c $COLLIN/models/model2/events.gz | $COLLIN/code/parser ./$fil $COLLIN/models/model2/grammar 10000 1 1 1 1 > ./$opfl
		breaksw
	case 3:
		gunzip -c $COLLIN/models/model3/events.gz | $COLLIN/code/parser ./$fil $COLLIN/models/model3/grammar 10000 1 1 1 1 > ./$opfl 
		breaksw
	endsw
end

# Concatenate the various split up parsed files
set fils = (parse.?.op)
foreach fil ($fils)
        cat $fil >> parse.txt
end
if (-f parse.10.op) then
        set fils = (parse.??.op)
        foreach fil ($fils)
                cat $fil >> parse.txt
        end
endif

# XMLize and put bacl original xml tags
parse2xml.pl parse.xml1 parse.txt
xml2sval.pl parse.xml ../sval2.xml parse.xml1
rm -f ../sval2.xml
# Count number of sentences actually parsed
numparse.pl count parse.xml

# Create individual lexical element directories within lexelts
# and store individual files therein
mkdir LexSample
cd LexSample
cp ../parse.xml ./output.xml
echo "BEGINING preprocess.pl"
# nontoken file removes all xmltags in the context other than pos tag, 
# parse tags and head tags
preprocess.pl output.xml --nontoken $PARSESENSEVAL/non-token.txt --token $PARSESENSEVAL/token2.txt --putSentenceTags
echo "Preprocess over"
set lexs = (*.count)
foreach lex ($lexs)
        set lexcnt = $lex
        set lexxml = `echo $lex | sed 's/\.count/\.xml/'`

        set lextcnt = `echo $lex | sed 's/\.count/-test\.count/'`
        set lexdir = `echo $lex | sed 's/\.count//'`
        set lextxml = `echo $lex | sed 's/\.count/-test\.xml/'`
        # check to see if evaluation or training file specified
        if ($tsttrn == "train") then
                set lextcnt = `echo $lex | sed 's/\.count/-training\.count/'`
                set lextxml = `echo $lex | sed 's/\.count/-training\.xml/'`
        endif

        newline.pl $lextcnt $lexcnt
        newline.pl $lextxml $lexxml
        rm -rf $lexcnt
        rm -rf $lexxml

        mkdir $lexdir
        mv $lextcnt ./$lexdir/
        mv $lextxml ./$lexdir/
end
/bin/rm output.xml
cd ..

 echo
 echo "PARSING COMPLETE"

