script/disamb.pl untested

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 292 CC 13 SCAR 45.0
CC
13
Cov
0%
readConfigFile
156.0
askHelp
6.0
printHelp
2.0
CRAP 90.4
1 #!/usr/local/bin/perl -w
2 # (Updated: $Id: disamb.pl,v 1.12 2006/12/24 12:18:47 sidz1979 Exp $)
3 #
4 # disamb.pl version 0.09
5 #
6 # Program that reads in a Senseval-2 formatted lexical sample file, and
7 # disambiguates the instances using WordNet::SenseRelate::TargetWord.
8 #
9 # Copyright (c) 2005
10 #
11 # Ted Pedersen, University of Minnesota, Duluth
12 # tpederse@d.umn.edu
13 #
14 # Satanjeev Banerjee, Carnegie Mellon University
15 # banerjee+@cs.cmu.edu
16 #
17 # Siddharth Patwardhan, University of Utah
18 # sidd@cs.utah.edu
19 #
20 # This program is free software; you can redistribute it and/or
21 # modify it under the terms of the GNU General Public License
22 # as published by the Free Software Foundation; either version 2
23 # of the License, or (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #
33 #-----------------------------------------------------------------------------
34  
35 # Uses:
36 use strict;
37 use WordNet::SenseRelate::Reader::Senseval2;
38 use WordNet::SenseRelate::TargetWord;
39 use WordNet::SenseRelate::Tools;
40 use Getopt::Long;
41  
42 # Now get the options!
43 our ($opt_config, $opt_wnpath, $opt_trace, $opt_help, $opt_version);
44 &GetOptions("config=s", "wnpath=s", "trace", "help", "version");
45  
46 # If help is requested
47 if(defined $opt_help)
48 {
49 &printHelp();
50 exit;
51 }
52  
53 # If version information is requested
54 if(defined $opt_version)
55 {
56 &printVersion();
57 exit;
58 }
59  
60 # Options
61 my $options = {};
62  
63 # Load options if any
64 if(defined $opt_config)
65 {
66 # Make all the options undefined first
67 $options = {};
68 $options->{preprocess} = [];
69 $options->{preprocessconfig} = [];
70 $options->{postprocess} = [];
71 $options->{postprocessconfig} = [];
72 $options->{context} = undef;
73 $options->{contextconfig} = undef;
74 $options->{algorithm} = undef;
75 $options->{algorithmconfig} = undef;
76 $options->{wntools} = undef;
77  
78 # Read the configuration file
79 my $optStruct = &readConfigFile($opt_config);
80 &askHelp("disamb.pl: Configuration file format error.\n")
81 if (!defined $optStruct);
82  
83 # Now get the options from the options structure
84 # First, the preprocess modules and options
85 if (defined $optStruct->{'PREPROCESS'})
86 {
87 foreach my $element (@{$optStruct->{'PREPROCESS'}})
88 {
89 push(@{$options->{preprocess}}, $element->{'name'});
90 delete $element->{'name'};
91 push(@{$options->{preprocessconfig}}, $element);
92 }
93 }
94  
95 # Next, the postprocess modules and options
96 if (defined $optStruct->{'POSTPROCESS'})
97 {
98 foreach my $element (@{$optStruct->{'POSTPROCESS'}})
99 {
100 push(@{$options->{postprocess}}, $element->{'name'});
101 delete $element->{'name'};
102 push(@{$options->{postprocessconfig}}, $element);
103 }
104 }
105  
106 # Next, get the context selection module and options
107 if (defined $optStruct->{'CONTEXT'})
108 {
109 # There should be only one context selection module specified
110 &askHelp("disamb.pl: Multiple context selection modules specified in configuration file.\n")
111 if(scalar(@{$optStruct->{'CONTEXT'}}) > 1);
112  
113 # Get the context selection module name, and options
114 # If none are specified, the default will be used
115 my $element = shift(@{$optStruct->{'CONTEXT'}});
116 if(defined $element && defined $element->{'name'})
117 {
118 $options->{context} = $element->{'name'};
119 delete $element->{'name'};
120 $options->{contextconfig} = $element;
121 }
122 }
123  
124 # Next, get the algorithm module and options
125 if (defined $optStruct->{'ALGORITHM'})
126 {
127 # Only one algorithm module must be specified
128 &askHelp("disamb.pl: Multiple algorithm modules specified in configuration file.\n")
129 if(scalar(@{$optStruct->{'ALGORITHM'}}) > 1);
130  
131 # Get the algorithm module name, and options
132 # If none are specified, then the default will be used
133 my $element = shift(@{$optStruct->{'ALGORITHM'}});
134 if (defined $element && defined $element->{'name'})
135 {
136 $options->{algorithm} = $element->{'name'};
137 delete $element->{'name'};
138 $options->{algorithmconfig} = $element;
139 }
140 }
141 }
142  
143 # Load WordNet::SenseRelate::Tools:
144 print STDERR "Loading WordNet::SenseRelate::Tools... ";
145 my $wntools = WordNet::SenseRelate::Tools->new($opt_wnpath);
146 &askHelp("disamb.pl: Unable to load WordNet::SenseRelate::Tools.\n")
147 if (!defined $wntools);
148 print STDERR "done.\n";
149 $options->{'wntools'} = $wntools;
150  
151 # Read Senseval2 data:
152 print STDERR "Reading XML data... ";
153 my $reader = WordNet::SenseRelate::Reader::Senseval2->new(shift);
154 &askHelp("disamb.pl: Unable to read XML file.\n") if (!defined $reader);
155 print STDERR "done.\n";
156  
157 # Create WordNet::SenseRelate::TargetWord object:
158 my ($targetWord, $error) = WordNet::SenseRelate::TargetWord->new($options, ((defined $opt_trace) ? 1 : 0));
159 if (!defined $targetWord)
160 {
161 print STDERR "$error\n";
162 exit;
163 }
164  
165 # Get each instance from the reader object, and disambiguate
166 foreach my $i (0 .. ($reader->instanceCount() - 1))
167 {
168 my $instance = $reader->instance($i);
169 my ($sense, $errString) = $targetWord->disambiguate($instance);
170 if (!defined $sense && defined $errString && $errString ne "")
171 {
172 print STDERR "$errString\n";
173 exit;
174 }
175 print STDERR $targetWord->getTraceString() if(defined $opt_trace);
176 print $instance->{lexelt};
177 print " ";
178 print $instance->{id};
179 print " $sense" if (defined $sense);
180 print "\n";
181 }
182  
183 # Subroutine that reads the options file
184 sub readConfigFile
185 {
186 my $fname = shift;
187
188 open(CONFIG, "$fname") || &askHelp("disamb.pl: Unable to open specified configuration file.\n");
189 my $line = <CONFIG>;
190 $line =~ s/[\r\f\n]//;
191 $line =~ s/^\s+//;
192 $line =~ s/\s+$//;
193 &askHelp("disamb.pl: File format error (in header) in config file.\n")
194 if ($line !~ /^WordNet::SenseRelate::TargetWord$/);
195 my $struct = {};
196 my $section = "";
197 my $modname = "";
198 my $modata = undef;
199
200 while ($line = <CONFIG>)
201 {
202 $line =~ s/[\r\f\n]//;
203 $line =~ s/\#.*//;
204 $line =~ s/^\s+//;
205 $line =~ s/\s+$//;
206 if($line =~ /\[SECTION:([^\]]*)\]/)
207 {
208 $section = $1;
209 $struct->{$section} = []
210 if(defined $section
211 && $section ne ""
212 && !exists($struct->{$section}));
213 }
214 elsif($line =~ /\[START\s+([^\]]+)\]/)
215 {
216 return undef if(!defined $section || $section eq "");
217 $modname = $1;
218 $modata = {};
219 }
220 elsif($line =~ /\[END\]/)
221 {
222 return undef
223 if(!defined $section
224 || $section eq ""
225 || !defined $modname
226 || $modname eq "");
227 return undef if (!defined $modata);
228 $modata->{'name'} = $modname;
229 push(@{$struct->{$section}}, $modata);
230 $modname = "";
231 $modata = undef;
232 }
233 elsif($line =~ /([^=]+)\s*=\s*([^=]+)/)
234 {
235 return undef
236 if(!defined $section
237 || !defined $modname
238 || $section eq ""
239 || $modname eq "");
240 $modata->{$1} = $2;
241 }
242 elsif($line ne "")
243 {
244 return undef;
245 }
246 }
247 close(CONFIG);
248  
249 return $struct;
250 }
251  
252 # function to output help messages for this program
253 sub printHelp
254 {
255 print "Performs Word Sense Disambiguation on Senseval-2 lexical sample data.\n";
256 print "Usage: disamb.pl [ [--config FILE] [--wnpath WNPATH] [--trace] XMLFILE | --help | --version]\n";
257 print "--config Specifies a configuration file (FILE) to set up the\n";
258 print " various configuration options.\n";
259 print "--wnpath WNPATH specifies the path of the WordNet data files.\n";
260 print " Ordinarily, this path is determined from the \$WNHOME\n";
261 print " environment variable. But this option overides this\n";
262 print " behavior.\n";
263 print "--trace Indicates that trace information be printed.\n";
264 print "--help Displays this help screen.\n";
265 print "--version Displays version information.\n\n";
266 }
267  
268 # function to output "ask for help" message when the user's goofed up!
269 sub askHelp
270 {
271 my $message = shift;
272 print STDERR "$message\n" if (defined $message && $message ne "");
273 print STDERR "Type disamb.pl --help for help.\n";
274 exit;
275 }
276  
277 # Subroutine to print the version information
278 sub printVersion
279 {
280 print "disamb.pl version 0.09\n";
281 print "Copyright (c) 2005 Ted Pedersen, Satanjeev Banerjee, and Siddharth Patwardhan.\n";
282 }
283  
284 __END__