| 1 |
|
| #!/usr/local/bin/perl -w |
| 2 |
|
| # (Updated: $Id: wps2sk.pl,v 1.8 2006/12/24 12:18:47 sidz1979 Exp $) |
| 3 |
|
| # |
| 4 |
|
| # wps2sk.pl version 0.09 |
| 5 |
|
| # |
| 6 |
|
| # Program to convert SENSEVAL answer files with answers in the |
| 7 |
|
| # word#pos#sense format to mnemonics using the word#pos#sense - |
| 8 |
|
| # mnemonic mapping from WordNet. |
| 9 |
|
| # |
| 10 |
|
| # Copyright (c) 2005 |
| 11 |
|
| # |
| 12 |
|
| # Ted Pedersen, University of Minnesota, Duluth |
| 13 |
|
| # tpederse@d.umn.edu |
| 14 |
|
| # |
| 15 |
|
| # Satanjeev Banerjee, Carnegie Mellon University |
| 16 |
|
| # banerjee+@cs.cmu.edu |
| 17 |
|
| # |
| 18 |
|
| # Siddharth Patwardhan, University of Utah |
| 19 |
|
| # sidd@cs.utah.edu |
| 20 |
|
| # |
| 21 |
|
| # This program is free software; you can redistribute it and/or |
| 22 |
|
| # modify it under the terms of the GNU General Public License |
| 23 |
|
| # as published by the Free Software Foundation; either version 2 |
| 24 |
|
| # of the License, or (at your option) any later version. |
| 25 |
|
| # This program is distributed in the hope that it will be useful, |
| 26 |
|
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
|
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
|
| # GNU General Public License for more details. |
| 29 |
|
| # |
| 30 |
|
| # You should have received a copy of the GNU General Public License |
| 31 |
|
| # along with this program; if not, write to the Free Software |
| 32 |
|
| # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 33 |
|
| # |
| 34 |
|
| #----------------------------------------------------------------------------- |
| 35 |
|
| |
| 36 |
|
| # we have to use commandline options, so use the necessary package! |
| 37 |
|
| use Getopt::Long; |
| 38 |
|
| |
| 39 |
|
| # now get the options! |
| 40 |
|
| &GetOptions("quiet", "help", "wnpath=s", "version"); |
| 41 |
|
| our ($opt_quiet, $opt_help, $opt_wnpath, $opt_version); |
| 42 |
|
| |
| 43 |
|
| # If the version information has been requested |
| 44 |
|
| if (defined $opt_version) |
| 45 |
|
| { |
| 46 |
|
| $opt_version = 1; |
| 47 |
|
| &printVersion(); |
| 48 |
|
| exit; |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| # if help has been requested, print out help! |
| 52 |
|
| if (defined $opt_help) |
| 53 |
|
| { |
| 54 |
|
| $opt_help = 1; |
| 55 |
|
| &showHelp(); |
| 56 |
|
| exit; |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| # Check if path to WordNet Data files has been provided ... If so ... save it. |
| 60 |
|
| if (defined $opt_wnpath) |
| 61 |
|
| { |
| 62 |
|
| $wnPCPath = $opt_wnpath; |
| 63 |
|
| $wnUnixPath = $opt_wnpath; |
| 64 |
|
| } |
| 65 |
|
| else |
| 66 |
|
| { |
| 67 |
|
| $wnPCPath = |
| 68 |
|
| (defined $ENV{"WNHOME"}) |
| 69 |
|
| ? $ENV{"WNHOME"} |
| 70 |
|
| : "C:\\Program Files\\WordNet\\1.7"; |
| 71 |
|
| $wnUnixPath = |
| 72 |
|
| (defined $ENV{"WNHOME"}) ? $ENV{"WNHOME"} : "/usr/local/wordnet1.7"; |
| 73 |
|
| $wnPCPath = |
| 74 |
|
| (defined $ENV{"WNSEARCHDIR"}) |
| 75 |
|
| ? $ENV{"WNSEARCHDIR"} |
| 76 |
|
| : $wnPCPath . "\\dict"; |
| 77 |
|
| $wnUnixPath = |
| 78 |
|
| (defined $ENV{"WNSEARCHDIR"}) |
| 79 |
|
| ? $ENV{"WNSEARCHDIR"} |
| 80 |
|
| : $wnUnixPath . "/dict"; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| # Open sense index file |
| 84 |
|
| open(SENSE, $wnUnixPath . "/index.sense") |
| 85 |
|
| || open(SENSE, $wnPCPath . "\\sense.idx") |
| 86 |
|
| || die "Unable to open sense index file.\n"; |
| 87 |
|
| |
| 88 |
|
| # Load up the sense file in a hash. Keys of the hash will be |
| 89 |
|
| # lemma::offset - This is unique to each sense key. Values of the hash |
| 90 |
|
| # will be the actual sense keys. |
| 91 |
|
| |
| 92 |
|
| print STDERR "Loading sense index file... " if (!defined $opt_quiet); |
| 93 |
|
| |
| 94 |
|
| while (<SENSE>) |
| 95 |
|
| { |
| 96 |
|
| s/[\r\f\n]//g; |
| 97 |
|
| |
| 98 |
|
| if ($_ !~ /((\S+)%\S+)\s+(\d+)/) |
| 99 |
|
| { |
| 100 |
|
| print STDERR "Error in sense index file. "; |
| 101 |
|
| print STDERR "Read \"$_\". Expecting \"<sense-key> <offset> ...\"\n"; |
| 102 |
|
| exit; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| my $key = $2 . "::" . $3; |
| 106 |
|
| my $value = $1; |
| 107 |
|
| |
| 108 |
|
| if (defined $senseHash{$key}) { print STDERR "$key already defined!\n"; } |
| 109 |
|
| $senseHash{$key} = $value; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| print STDERR "done!\n" if (!defined $opt_quiet); |
| 113 |
|
| |
| 114 |
|
| # done with the index.sense file |
| 115 |
|
| close(SENSE); |
| 116 |
|
| |
| 117 |
|
| # now do everything for each part of speech! |
| 118 |
|
| my $pos; |
| 119 |
|
| my %mapHash = (); |
| 120 |
|
| foreach $pos ("noun", "verb", "adj", "adv") |
| 121 |
|
| { |
| 122 |
|
| print STDERR "Processing index.$pos file... " if (!defined $opt_quiet); |
| 123 |
|
| |
| 124 |
|
| # open index file |
| 125 |
|
| open(INDEX, $wnUnixPath . "/index.$pos") |
| 126 |
|
| || open(INDEX, $wnPCPath . "\\$pos.idx") |
| 127 |
|
| || die "Unable to open index file.\n"; |
| 128 |
|
| |
| 129 |
|
| while (<INDEX>) |
| 130 |
|
| { |
| 131 |
|
| next if (/^ /); |
| 132 |
|
| s/[\r\f\n]//g; |
| 133 |
|
| |
| 134 |
|
| # get the lemma and the number of senses |
| 135 |
|
| my ($lemma, $p, $noOfSenses, @rest) = split /\s+/; |
| 136 |
|
| |
| 137 |
|
| # for each sense of this lemma, create the key (for the above |
| 138 |
|
| # hash) and find the |
| 139 |
|
| for ($i = $#rest - $noOfSenses + 1 ; $i <= $#rest ; $i++) |
| 140 |
|
| { |
| 141 |
|
| my $key = $lemma . "::" . $rest[$i]; |
| 142 |
|
| if (!defined $senseHash{$key}) |
| 143 |
|
| { |
| 144 |
|
| print STDERR "$key not defined!!\n"; |
| 145 |
|
| exit; |
| 146 |
|
| } |
| 147 |
|
| my $tmpNum = ($i - ($#rest - $noOfSenses + 1) + 1); |
| 148 |
|
| $mapHash{"$lemma\#$p\#$tmpNum"} = $senseHash{$key}; |
| 149 |
|
| } |
| 150 |
|
| } |
| 151 |
|
| close(INDEX); |
| 152 |
|
| print STDERR "done!\n" if (!defined $opt_quiet); |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| while (<>) |
| 156 |
|
| { |
| 157 |
|
| s/[\r\f\n]//g; |
| 158 |
|
| |
| 159 |
|
| # get the index part and the answer part |
| 160 |
|
| my @answers = (); |
| 161 |
|
| if (/^(\S+\s+\S+)\s+(.*)\s*/) |
| 162 |
|
| { |
| 163 |
|
| my $part1 = $1; |
| 164 |
|
| @answers = split(/\s+/, $2); |
| 165 |
|
| $part1 =~ s/\.[nvar]//; |
| 166 |
|
| print "$part1 "; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| # convert answers |
| 170 |
|
| my $ans; |
| 171 |
|
| foreach $ans (@answers) |
| 172 |
|
| { |
| 173 |
|
| if (defined $mapHash{$ans}) { print "$mapHash{$ans} "; } |
| 174 |
|
| else { print STDERR "Couldnt find mapping for $ans\n"; } |
| 175 |
|
| } |
| 176 |
|
| print "\n"; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| # function to output help messages for this program |
| 180 |
|
| sub showHelp |
| 181 |
|
| { |
| 182 |
|
| print |
| 183 |
|
| "Creates a word#pos#sense to sensekey mapping of a Senseval-2 answer file\n"; |
| 184 |
|
| print "(output by disamb.pl).\n"; |
| 185 |
|
| print |
| 186 |
|
| "Usage: wps2sk.pl [ [ --wnpath WNPATH] [FILE...] | --help | --version ]\n"; |
| 187 |
|
| print |
| 188 |
|
| "--wnpath WNPATH specifies the path of the WordNet data files.\n"; |
| 189 |
|
| print |
| 190 |
|
| " Ordinarily, this path is determined from the \$WNHOME\n"; |
| 191 |
|
| print |
| 192 |
|
| " environment variable. But this option overides this\n"; |
| 193 |
|
| print " behavior.\n"; |
| 194 |
|
| print "--quiet Run in quiet mode -- do not print informational\n"; |
| 195 |
|
| print " messages.\n"; |
| 196 |
|
| print "--help Displays this help screen.\n"; |
| 197 |
|
| print "--version Displays version information.\n\n"; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
|
| # function to output "ask for help" message when the user's goofed up! |
| 201 |
|
| sub askHelp |
| 202 |
|
| { |
| 203 |
|
| print "Type wps2sk.pl --help for help.\n"; |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
|
| # Subroutine to print the version information |
| 207 |
|
| sub printVersion |
| 208 |
|
| { |
| 209 |
|
| print "wps2sk.pl version 0.09\n"; |
| 210 |
|
| print |
| 211 |
|
| "Copyright (c) 2005 Ted Pedersen, Satanjeev Banerjee, and Siddharth Patwardhan.\n"; |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
|
| |
| 215 |
|
| __END__ |