#!/usr/local/bin/perl -w

# qd2wn.pl version 0.1
# Program to create a word#pos#sense (QueryData style) to sense-key (WordNet style) mapping.
#
# Copyright (C) 2000-2002
# Ted Pedersen, University of Minnesota, Duluth
# tpederse@d.umn.edu
# Satanjeev Banerjee, University of Minnesota, Duluth
# bane0025@d.umn.edu
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#-----------------------------------------------------------------------------
#                              Start of program
#-----------------------------------------------------------------------------

# we have to use commandline options, so use the necessary package!
use Getopt::Long;

# now get the options!
GetOptions("help");

# if help has been requested, print out help!
if ( defined $opt_help )
{
    $opt_help = 1;
    showHelp();
    exit;
}

# get the path to the various files we'll use. by default ./
$path = ($path = shift) ? $path : ".";

# open index.sense file
if (!defined (open (SENSE, "$path/index.sense")))
{
    print STDERR "Couldn't open $path/index.sense.\n";
    askHelp();
    exit;
}

# Load up the sense file in a hash. Keys of the hash will be
# lemma::offset - This is unique to each sense key. Values of the hash
# will be the actual sense keys.

print STDERR "Loading index.sense file... ";

while (<SENSE>)
{
    chomp;

    if ($_ !~ /((\S+)%\S+)\s+(\d+)/)
    { 
	print "Error in index.sense file. Read \"$_\". Expecting \"<sense-key> <offset> ...\"\n";
	exit;
    }

    my $key = $2 . "::" . $3;
    my $value = $1;

    if (defined $senseHash{$key}) { print "$key already defined!\n"; }
    $senseHash{$key} = $value;
}

print STDERR "done!\n";

# done with the index.sense file 
close(SENSE);

# now do everything for each part of speech!
my $pos;
foreach $pos ("noun", "verb", "adj", "adv")
{
    print STDERR "Processing index.$pos file... ";

    # open index file
    open (INDEX, "$path/index.$pos") || die "Couldn't open $path/index.$pos.\n";

    while (<INDEX>)
    {
	next if (/^ /);
	chomp;
	
	# get the lemma and the number of senses
	my ($lemma, $p, $noOfSenses, @rest) = split /\s+/;

	# for each sense of this lemma, create the key (for the above
	# hash) and find the 
	for ($i = $#rest - $noOfSenses + 1; $i <= $#rest; $i++)
	{
	    my $key = $lemma . "::" . $rest[$i];
	    if (!defined $senseHash{$key}) { print "$key not defined!!\n"; exit; }

	    printf("%s#%s#%d %s %s\n", $lemma, $p, $i - ($#rest - $noOfSenses + 1) + 1, $senseHash{$key}, $rest[$i]);
	}
    }

    close(INDEX);
    print STDERR "done!\n";
}

# function to output help messages for this program
sub showHelp
{
    print "Usage: qd2wn.pl [ <PATH> | --help ]\n\n";
	  
    print "Creates a word#pos#sense to sensekey mapping. Requires the\n";
    print "following files from the WordNet 1.7 /dict directory: index.sense\n";
    print "index.noun, index.verb, index.adj and index.adv. Run this\n";
    print "program with the path to these files (they should all be in\n";
    print "the same directory) or with no parameters, if these files are\n";
    print "in the current directory. The output mapping goes to STDOUT,\n";
    print "status messages to STDERR. The mapping (without the status\n";
    print "messages) may be redirected to an output file.\n\n";
}

# function to output "ask for help" message when the user's goofed up!
sub askHelp
{
    print "Type qd2wn.pl --help for help.\n";
}
