#!/bin/csh

#######################################################################
#This system takes a supervised learning approach to word sense
#disambiguation, where three different classifiers are induced from
#sense-tagged training examples. Each classifier is based on the same
#feature set. A weighted vote is taken among these classifiers to assign
#senses to test examples. No information from WordNet is utilized by this
#system. 

#The three classifiers are a bagged J48 decision tree, a Naive Bayesian
#classifier, and a nearest neighbor classifier (IBk). 

#This system uses a filter to perform feature identification prior to
#learning. All non-consecutive bigrams (that may include zero, one, or two
#intervening words that are ingored) and that meet the following criteria
#form a set of candidate features: 

#1) occur more than 2 times and 
#2) have a log-likelihood ratio >= to 0.00 and 
#3) are not made up of stop-listed words. 

#The training examples are converted into feature vectors, where each
#feature represents whether a candidate feature occurs in the context of a
#specific training example. 

#The feature vectors are the input to the J48 learning algorithm, the IBk
#nearest neighbor learner (where the number of neighbors k=1), and a Naive
#Bayesian classifier. When presented with a test example, each classifier
#outputs a probability for each possible sense. These are summed and the
#sense with the maximum probability is assigned to a test example. 

#This is the same approach as taken in duluthA for English. The only
#differences are in the stop list and in the setting of the significance
#value for the log-likelihood ratio. 
#######################################################################


if ($#argv != 3) then
   echo "duluthX source-dir stoplist token"
   echo 
   echo "source-dir : directory where data resides"
   echo "in this distribution source-dir is LexSample"
   echo 
   echo "stop-list: text file of stop words"
   echo "in this distribution stop-list is stop.list"
   echo 
   echo "token : text file of token definitions"
   echo "in this distribution token is token1.txt"
   echo 
   echo "run from directory where source-dir, stop-list, and token reside"
   echo
   echo "if you don't want to use stop.list and/or token just create"
   echo "a blank file via echo > dummy and use that instead"
   exit 1
endif   

# specify the name of your stop list and token definition file
# for some reason things work better if you specify the full
# path name of the stoplist and token files

set sourcedir=$1
set stoplist=$PWD/$2
set token=$PWD/$3

if !(-e $sourcedir) then 
	echo "$sourcedir sourcedir does not exit"
	exit 1
endif

if !(-e $stoplist) then 
	echo "$stoplist stoplist does not exit"
	exit 1
endif

if !(-e $token) then 
	echo "$token token does not exit"
	exit 1
endif
             
# the methods are feature extraction routines that build views of the
# text for the machine learning system weka.

foreach method (f1-mod)

	# the results for each method are contained in a directory,
	# which has the same name as the method

 	if (-e $method) then
                echo $method already exists, aborting
                exit 1
        endif 

	mkdir $method

	# within each method directory, there is a directory for 
	# each word

	# step into source directory and find out the names of
	# all the files to be processed. Each file is named after
        # a word to be processed.

	cd $sourcedir
        set wordlist = (*)
	cd ..

	# now process each of those words

	foreach word ($wordlist)
        
		# move the text for a word into the appropriate directory

		cd $sourcedir
		cp -r $word ../$method
		cd ..
		cd $method/$word

		# now process that text with the desired method
		
		$method $word $stoplist $token

		# convert the text into a form that weka likes (arff)

		xml2arff $word
		
		# now run weka to do machine learning and tag the test data
		# first do a bagged decision tree

		bag $word 0.25 2 j48.J48 bag25m2
                                                       
		# score your results with senseval 2 scoring program
		# note that weka will provide some diagnostic output too
		
		score-word $word j48.J48.bag25m2     

		# second use a Naive Bayesian classifier

		wekarun $word NaiveBayes '-o'       
		score-word $word NaiveBayes

		# third use IBk (nearest neighbor)

		wekarun $word IBk ' '     
		score-word $word IBk

		# get out of this word directory and move to the next!

		cd ../..
	
	end
end

# duluthA is an ensemble of three different learning algorithms,
# where all three use the same view of the data

ensembleByDist.pl f1-mod

score ens-bag-bayes-ibk


