script/senserelate.pl untested

filter: Statementstmt 0.0% 0 / 768 Branchbran 0.0% 0 / 136 Conditioncond 0.0% 0 / 81 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 1066 CC 53 SCAR 47.9
CC
53
Cov
0%
setupModuleOptions
240.0
readConfigFile
182.0
getOptionsFromFields
156.0
CRAP 120.4
1 #!/usr/local/bin/perl -w
2 # (Updated: $Id: senserelate.pl,v 1.4 2006/12/24 13:06:57 sidz1979 Exp $)
3 #
4 # senserelate.pl version 0.09
5 #
6 # This program is a graphical interface for the
7 # WordNet::SenseRelate::TargetWord word sense disambiguation system. It
8 # allows the user to enter pieces of text, and select a word from the text
9 # the target word to be disambiguated. The system then disambiguates the
10 # sense of the target word using the algorithm by Banerjee and Pedersen
11 # (2003).
12 #
13 # Copyright (c) 2005
14 #
15 # Ted Pedersen, University of Minnesota, Duluth
16 # tpederse@d.umn.edu
17 #
18 # Satanjeev Banerjee, Carnegie Mellon University, Pittsburgh
19 # banerjee+@cs.cmu.edu
20 #
21 # Siddharth Patwardhan, University of Utah, Salt Lake City
22 # sidd@cs.utah.edu
23 #
24 # This program is free software; you can redistribute it and/or
25 # modify it under the terms of the GNU General Public License
26 # as published by the Free Software Foundation; either version 2
27 # of the License, or (at your option) any later version.
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to the Free Software
35 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #
37 #-----------------------------------------------------------------------------
38  
39 # Uses:
40 use strict;
41 use WordNet::SenseRelate::TargetWord;
42 use WordNet::SenseRelate::Tools;
43 use WordNet::SenseRelate::Word;
44 use Getopt::Long;
45 use Gtk;
46  
47 # Now get the options!
48 our ($opt_config, $opt_wnpath, $opt_help, $opt_version);
49 &GetOptions("config=s", "wnpath=s", "help", "version");
50  
51 # My global variables
52 my $VERSION = '0.09';
53 my $options = {};
54 my $lastDir = undef;
55 my $instanceData = undef;
56 my $wntools;
57 my $targetWord;
58 my $error;
59 my $esColor = {};
60 my @warnings = ();
61 my %posMap = ('n' => 'NOUN',
62 'v' => 'VERB',
63 'a' => 'ADJECTIVE',
64 'r' => 'ADVERB');
65 my %moduleNames = ('PREPROCESS' => ['WordNet::SenseRelate::Preprocess::Compounds'],
66 'POSTPROCESS' => [],
67 'CONTEXT' => ['WordNet::SenseRelate::Context::NearestWords'],
68 'ALGORITHM' => ['WordNet::SenseRelate::Algorithm::Local',
69 'WordNet::SenseRelate::Algorithm::Global',
70 'WordNet::SenseRelate::Algorithm::SenseOne',
71 'WordNet::SenseRelate::Algorithm::Random']);
72 my %moduleOptions = ('WordNet::SenseRelate::Preprocess::Compounds' => {},
73 'WordNet::SenseRelate::Context::NearestWords' => {'contextpos' => 's!!0!!nvar',
74 'windowsize' => 'i!!0!!5',
75 'windowstop' => 'f!!0!!'},
76 'WordNet::SenseRelate::Algorithm::Local' => {'measure' => 'm!!0!!WordNet::Similarity::vector',
77 'measureconfig' => 'f!!0!!'},
78 'WordNet::SenseRelate::Algorithm::Global' => {'measure' => 'm!!0!!WordNet::Similarity::vector',
79 'measureconfig' => 'f!!0!!'},
80 'WordNet::SenseRelate::Algorithm::SenseOne' => {},
81 'WordNet::SenseRelate::Algorithm::Random' => {});
82  
83 # If help is requested
84 if (defined $opt_help)
85 {
86 &printHelp();
87 exit;
88 }
89  
90 # If version information is requested
91 if (defined $opt_version)
92 {
93 &printVersion();
94 exit;
95 }
96  
97 # Load configuration options, if config file specified.
98 # (Pop warning message box, if error reading config file)
99 # Else set up default configuration
100 if(defined $opt_config)
101 {
102 # Make all the options undefined first
103 $options = {};
104 $options->{preprocess} = [];
105 $options->{preprocessconfig} = [];
106 $options->{postprocess} = [];
107 $options->{postprocessconfig} = [];
108 $options->{context} = undef;
109 $options->{contextconfig} = undef;
110 $options->{algorithm} = undef;
111 $options->{algorithmconfig} = undef;
112 $options->{wntools} = undef;
113  
114 # Read the configuration file
115 my $optStruct = &readConfigFile($opt_config);
116 if (!defined $optStruct)
117 {
118 &askHelp("senserelate.pl: Configuration file format error.\n");
119 $options = {};
120 }
121 else
122 {
123 # Now get the options from the options structure
124 # First, the preprocess modules and options
125 if (defined $optStruct->{'PREPROCESS'})
126 {
127 foreach my $element (@{$optStruct->{'PREPROCESS'}})
128 {
129 push(@{$options->{preprocess}}, $element->{'name'});
130 delete $element->{'name'};
131 push(@{$options->{preprocessconfig}}, $element);
132 }
133 }
134
135 # Next, the postprocess modules and options
136 if (defined $optStruct->{'POSTPROCESS'})
137 {
138 foreach my $element (@{$optStruct->{'POSTPROCESS'}})
139 {
140 push(@{$options->{postprocess}}, $element->{'name'});
141 delete $element->{'name'};
142 push(@{$options->{postprocessconfig}}, $element);
143 }
144 }
145  
146 # Next, get the context selection module and options
147 if (defined $optStruct->{'CONTEXT'})
148 {
149
150 # There should be only one context selection module specified
151 &askHelp("senserelate.pl: Multiple context selection modules specified in configuration file. Using first.")
152 if (scalar(@{$optStruct->{'CONTEXT'}}) > 1);
153  
154 # Get the context selection module name, and options
155 # If none are specified, the default will be used
156 my $element = shift(@{$optStruct->{'CONTEXT'}});
157 if (defined $element && defined $element->{'name'})
158 {
159 $options->{context} = $element->{'name'};
160 delete $element->{'name'};
161 $options->{contextconfig} = $element;
162 }
163 }
164  
165 # Next, get the algorithm module and options
166 if (defined $optStruct->{'ALGORITHM'})
167 {
168  
169 # Only one algorithm module must be specified
170 &askHelp("senserelate.pl: Multiple algorithm modules specified in configuration file. Using first.")
171 if (scalar(@{$optStruct->{'ALGORITHM'}}) > 1);
172  
173 # Get the algorithm module name, and options
174 # If none are specified, then the default will be used
175 my $element = shift(@{$optStruct->{'ALGORITHM'}});
176 if (defined $element && defined $element->{'name'})
177 {
178 $options->{algorithm} = $element->{'name'};
179 delete $element->{'name'};
180 $options->{algorithmconfig} = $element;
181 }
182 }
183 }
184 }
185  
186 # Load WordNet::SenseRelate::Tools...
187 # Exit with warning message, if unable to load...
188 print STDERR "Loading WordNet::SenseRelate::Tools... ";
189 $wntools = WordNet::SenseRelate::Tools->new($opt_wnpath);
190 if(!defined $wntools)
191 {
192 print STDERR "senserelate.pl: Unable to load WordNet::SenseRelate::Tools.\n";
193 print STDERR "Type \'senserelate.pl --help\' for help.\n";
194 exit;
195 }
196 print STDERR "done.\n";
197 $options->{'wntools'} = $wntools;
198  
199 # Load WordNet::SenseRelate::TargetWord...
200 # Exit with warning message, if unable to load...
201 print STDERR "Loading WordNet::SenseRelate::TargetWord... ";
202 ($targetWord, $error) = WordNet::SenseRelate::TargetWord->new($options, 1);
203 if (!defined $targetWord)
204 {
205 print STDERR "$error\n";
206 exit;
207 }
208 print STDERR "done.\n";
209  
210 # Initialize Gtk.
211 print STDERR "Starting interface... \n";
212 set_locale Gtk;
213 init Gtk;
214  
215 # Initialize colors
216 $esColor->{'red'} = 0xFFFF;
217 $esColor->{'green'} = 0xD000;
218 $esColor->{'blue'} = 0xD000;
219 my $cmap = Gtk::Gdk::Colormap->get_system();
220 &askHelp("Couldn't allocate green color.") unless(defined($cmap->color_alloc($esColor)));
221  
222 # Build the graphical user interface
223 # Create the main window...
224 my $window = Gtk::Window->new('toplevel');
225 $window->set_usize(800, 515);
226 $window->set_policy(0, 0, 1);
227 #$window->set_resizable(0);
228 $window->signal_connect('destroy', sub{ Gtk->exit(0); });
229 $window->set_title("WordNet::SenseRelate::TargetWord");
230 $window->set_position('center');
231 $window->border_width(0);
232  
233 # Create a menubar
234 my @menu_items = ( { path => '/_File',
235 type => '<Branch>' },
236 { path => '/File/_Read Text File',
237 accelerator => '<control>R',
238 callback => \&readFileDialog },
239 { path => '/File/_Setup',
240 accelerator => '<control>S',
241 callback => \&setupDialog },
242 { path => '/File/sep1',
243 type => '<Separator>'},
244 { path => '/File/E_xit',
245 callback => sub { Gtk->exit(0); } },
246 { path => '/_Help',
247 type => '<Branch>' },
248 { path => '/_Help/About',
249 callback => \&showAbout } );
250 my $accel_group = new Gtk::AccelGroup();
251 my $item_factory = new Gtk::ItemFactory('Gtk::MenuBar', '<main>', $accel_group);
252 $item_factory->create_items(@menu_items);
253 $window->add_accel_group($accel_group);
254 my $menubar = $item_factory->get_widget('<main>');
255 $menubar->show();
256  
257 # Create a text input box
258 my $textBox = Gtk::Text->new();
259 $textBox->set_usize(780, 300);
260 $textBox->set_editable(1);
261 $textBox->set_word_wrap(1);
262 $textBox->set_line_wrap(1);
263 $textBox->show();
264  
265 # Create the scrollbar
266 my $vscrollbar = Gtk::VScrollbar->new($textBox->vadj);
267 $vscrollbar->show();
268  
269 # Create a HBox
270 my $textHBox = Gtk::HBox->new(0, 0);
271 $textHBox->pack_end($vscrollbar, 0, 0, 0);
272 $textHBox->pack_end($textBox, 0, 0, 0);
273 $textHBox->show();
274  
275 # Create output Label
276 my $outLabel = Gtk::Label->new("Selected Sense: ");
277 $outLabel->set_justify('left');
278 $outLabel->show();
279  
280 # Create label for selected word
281 my $selword = Gtk::Label->new("Selected Word: ");
282 $selword->set_justify('left');
283 $selword->show();
284  
285 # Create disambiguate button
286 my $disambBox = Gtk::Button->new("Disambiguate");
287 $disambBox->signal_connect('clicked', \&disambiguateInstance);
288 $disambBox->show();
289  
290 # Create text box
291 my $wordbox = Gtk::Entry->new();
292 $wordbox->show();
293  
294 # Create table for Labels
295 my $labTable = Gtk::Table->new(1, 5, 1);
296 $labTable->attach_defaults($outLabel, 3, 5, 0, 1);
297 $labTable->attach_defaults($disambBox, 2, 3, 0, 1);
298 $labTable->attach_defaults($selword, 0, 1, 0, 1);
299 $labTable->attach_defaults($wordbox, 1, 2, 0, 1);
300 $labTable->show();
301  
302 # Create output textbox
303 my $outText = Gtk::Text->new();
304 $outText->set_usize(800, 50);
305 $outText->set_editable(0);
306 $outText->set_word_wrap(1);
307 $outText->set_line_wrap(1);
308 $outText->show();
309  
310 # Create VBox for output
311 my $outVBox = Gtk::VBox->new(0, 0);
312 $outVBox->pack_start($labTable, 0, 0, 0);
313 $outVBox->pack_end($outText, 1, 1, 0);
314 $outVBox->show();
315  
316 # Create frame
317 my $pFrame = Gtk::Frame->new("Process");
318 $pFrame->add($outVBox);
319 $pFrame->show();
320  
321 # Create a VBox
322 my $vbox = Gtk::VBox->new(0, 5);
323 $vbox->pack_start($menubar, 0, 0, 0);
324 $vbox->pack_start($textHBox, 1, 1, 0);
325 $vbox->pack_start($pFrame, 0, 0, 0);
326 $vbox->show();
327  
328 # Show the main window
329 $window->add($vbox);
330 $window->show();
331  
332 # Display warnings from startup, if any
333 &displayStartupWarnings(@warnings) if(scalar(@warnings));
334  
335 # Gtk event loop
336 main Gtk;
337  
338 # Should never get here
339 exit;
340  
341 # Create read Senseval file dialog
342 sub readFileDialog
343 {
344 my $fDialog = Gtk::FileSelection->new("Select Text File");
345 $fDialog->set_policy(0, 0, 1);
346 # $fDialog->set_resizable(0);
347 $fDialog->set_position('center');
348 $fDialog->border_width(0);
349 $fDialog->set_modal(1);
350 $fDialog->set_filename($lastDir) if(defined $lastDir);
351  
352 # Connect Signals
353 $fDialog->ok_button->signal_connect("clicked", \&fileSelect, $fDialog);
354 $fDialog->cancel_button->signal_connect("clicked", sub{ $fDialog->destroy(); });
355  
356 # Display it
357 $fDialog->show();
358 }
359  
360 # This function is a callback for the file
361 # selection dialog. This is where the filename
362 # selected by the user is determined.
363 sub fileSelect
364 {
365 # Get the parameters
366 my ($widget, $dialog) = @_;
367 my $fname;
368  
369 # The dialog pointer must be given
370 return if(!defined $dialog);
371  
372 # Get the filename
373 $fname = $dialog->get_filename();
374 $lastDir = $fname if(defined $fname);
375 $lastDir =~ s|\/[^\/]*$||;
376 $lastDir .= "/";
377 $dialog->destroy();
378  
379 # Read the file
380 # Show message box "Unable to read file" if not defined $fname && return
381 if(!defined $fname)
382 {
383 &showMessageBox("Error", "Unable to read XML file.");
384 return;
385 }
386  
387 # Show "Reading file... " window
388 my $messybox = Gtk::Window->new('dialog');
389 $messybox->set_usize(400, 100);
390 $messybox->set_policy(0, 0, 1);
391 # $messybox->set_resizable(0);
392 $messybox->set_title("Information: Reading File... Please Wait.");
393 $messybox->set_position('center');
394 $messybox->border_width(0);
395 $messybox->set_modal(1);
396  
397 #my $mesLab = Gtk::Label->new("Reading file. Please Wait.");
398 #$mesLab->set_justify('left');
399 #$mesLab->show();
400  
401 my $mesLab = Gtk::Text->new();
402 $mesLab->set_usize(200, 50);
403 $mesLab->set_editable(0);
404 $mesLab->set_word_wrap(1);
405 $mesLab->set_line_wrap(1);
406 $mesLab->freeze();
407 $mesLab->insert(undef, undef, undef, "Reading file. Please Wait.");
408 $mesLab->thaw();
409 $mesLab->show();
410  
411 my $meshbox = Gtk::VBox->new(0, 20);
412 $meshbox->pack_start($mesLab, 0, 0, 0);
413 $meshbox->show();
414  
415 $messybox->add($meshbox);
416 $messybox->show_now();
417  
418 # Read the text file
419 my $textData = "";
420 my $textCount = 0;
421 $textBox->freeze();
422 if(!open(FNAME, $fname))
423 {
424 close(FNAME);
425 $textBox->thaw();
426 $messybox->destroy();
427 &showMessageBox("Error", "Unable to read XML file.");
428 return;
429 }
430 while(<FNAME>)
431 {
432 s/[\r\f\n]//;
433 my $line = $_;
434 my @textChars = split(//, $line);
435 $textCount += scalar(@textChars);
436 if($textCount > 1000)
437 {
438 close(FNAME);
439 my $tLength = $textBox->get_length();
440 $textBox->set_point($tLength);
441 $textBox->backward_delete($tLength);
442 $textBox->insert(undef, undef, undef, $textData);
443 $textBox->thaw();
444 $messybox->destroy();
445 &showMessageBox("Error", "Too much text in file. Truncating.");
446 return;
447 }
448 $textData .= "$line ";
449 }
450  
451 # Remove "Reading file... " window, && Show message box of errors, if any, && return
452 close(FNAME);
453 my $tLength = $textBox->get_length();
454 $textBox->set_point($tLength);
455 $textBox->backward_delete($tLength);
456 $textBox->insert(undef, undef, undef, $textData);
457 $textBox->thaw();
458 $messybox->destroy();
459 }
460  
461 # Disambiguate the current instance
462 sub disambiguateInstance
463 {
464 my $traceWindow = Gtk::Window->new('dialog');
465 $traceWindow->set_usize(500, 400);
466 $traceWindow->set_policy(0, 0, 1);
467 # $traceWindow->set_resizable(0);
468 $traceWindow->set_title("Trace Information: Disambiguating Instance...");
469 $traceWindow->set_position('center');
470 $traceWindow->border_width(0);
471 $traceWindow->set_modal(1);
472  
473 my $traceBox = Gtk::Text->new();
474 $traceBox->set_usize(480, 360);
475 $traceBox->set_editable(0);
476 $traceBox->set_word_wrap(1);
477 $traceBox->set_line_wrap(1);
478 $traceBox->show();
479  
480 my $tracebar = Gtk::VScrollbar->new($traceBox->vadj);
481 $tracebar->show();
482  
483 my $traceHBox = Gtk::HBox->new(0, 0);
484 $traceHBox->pack_end($tracebar, 0, 0, 0);
485 $traceHBox->pack_end($traceBox, 0, 0, 0);
486 $traceHBox->show();
487  
488 my $closeButton = Gtk::Button->new("Close");
489 $closeButton->signal_connect('clicked', sub{ $traceWindow->destroy(); });
490 $closeButton->set_sensitive(0);
491 $closeButton->show();
492  
493 my $closeTable = Gtk::Table->new(1, 7);
494 $closeTable->attach_defaults($closeButton, 3, 4, 0, 1);
495 $closeTable->show();
496  
497 my $traceVBox = Gtk::VBox->new();
498 $traceVBox->pack_start($traceHBox, 0, 0, 0);
499 $traceVBox->pack_start($closeTable, 0, 0, 0);
500 $traceVBox->show();
501  
502 $traceWindow->add($traceVBox);
503 $traceWindow->show_now();
504  
505 # Stuff goes here...
506 my $theWord = $wordbox->get_text();
507 my $wholeText = $textBox->get_chars(0, -1);
508  
509 unless($wholeText =~ /\b($theWord)\b/i)
510 {
511 $traceWindow->destroy();
512 &showMessageBox("Error", "Word not found in text.");
513 return;
514 }
515 my $before = $`;
516 my $head = $&;
517 my $after = $'; #'
518  
519 $after = "" if(!defined $after);
520 $before = "" if(!defined $before);
521  
522 my $instance = {};
523 $instance->{text} = [($before, $head, $after)];
524 $instance->{head} = 1;
525 $instance->{id} = "";
526 $before =~ s/^\s+//;
527 $before =~ s/\s+$//;
528 while($before =~ /(\w+)/g)
529 {
530 push(@{$instance->{words}}, $1);
531 push(@{$instance->{wordobjects}}, WordNet::SenseRelate::Word->new($1));
532 }
533 $instance->{target} = scalar(@{$instance->{words}});
534 my $cpos = $targetWord->{contextpos};
535 $cpos = "nvar" if(!defined $cpos);
536 push(@{$instance->{words}}, $head);
537 push(@{$instance->{wordobjects}}, WordNet::SenseRelate::Word->new($head."\#$cpos"));
538 $after =~ s/^\s+//;
539 $after =~ s/\s+$//;
540 while($after =~ /(\w+)/g)
541 {
542 push(@{$instance->{words}}, $1);
543 push(@{$instance->{wordobjects}}, WordNet::SenseRelate::Word->new($1));
544 }
545  
546 my ($sense, $error) = $targetWord->disambiguate($instance);
547 my $traceStuff = $targetWord->getTraceString();
548 if(!defined $sense)
549 {
550 my $errString = "Error: ";
551 $errString .= $error if(defined $error && $error ne "");
552 $traceWindow->destroy();
553 &showMessageBox("Error", $errString);
554 return;
555 }
556
557 $outLabel->set_text("Selected Sense: $sense");
558  
559 $outText->freeze();
560 # Delete all the text in there.
561 my $tLength = $outText->get_length();
562 $outText->set_point($tLength);
563 $outText->backward_delete($tLength);
564 $outText->insert(undef, undef, undef, $wntools->{wn}->querySense($sense, "glos"));
565 $outText->thaw();
566 $traceBox->insert(undef, undef, undef, $traceStuff);
567  
568 $closeButton->set_sensitive(1);
569 }
570  
571 # General purpose message box
572 sub showMessageBox
573 {
574 my $title = shift;
575 my $message = shift;
576 return if(!defined $title || !defined $message);
577 my $mbox = Gtk::Window->new('dialog');
578 $mbox->set_usize(400, 100);
579 $mbox->set_policy(0, 0, 1);
580 # $mbox->set_resizable(0);
581 $mbox->set_title($title);
582 $mbox->set_position('center');
583 $mbox->border_width(0);
584 $mbox->set_modal(1);
585  
586 my $mesLabel = Gtk::Label->new($message);
587 $mesLabel->show();
588  
589 my $okButton = Gtk::Button->new("Ok");
590 $okButton->signal_connect('clicked', sub{ $mbox->destroy(); });
591 $okButton->show();
592  
593 my $okTable = Gtk::Table->new(1, 5, 1);
594 $okTable->attach_defaults($okButton, 2, 3, 0, 1);
595 $okTable->show();
596  
597 my $mesBox = Gtk::VBox->new(0, 20);
598 $mesBox->pack_start($mesLabel, 0, 0, 0);
599 $mesBox->pack_start($okTable, 0, 0, 0);
600 $mesBox->show();
601  
602 $mbox->add($mesBox);
603 $mbox->show();
604 }
605  
606 # Create read setup options dialog
607 sub setupDialog
608 {
609 # Create the setup window
610 my $setupWindow = Gtk::Window->new('dialog');
611 $setupWindow->set_usize(600, 350);
612 $setupWindow->set_policy(0, 0, 1);
613 $setupWindow->set_title("WordNet::SenseRelate::TargetWord Setup: Select Modules");
614 $setupWindow->set_position('center');
615 $setupWindow->border_width(0);
616 $setupWindow->set_modal(1);
617  
618 my $prelist = Gtk::CList->new_with_titles("Preprocess Modules");
619 $prelist->set_selection_mode('multiple');
620 $prelist->set_column_justification(0, 'center');
621 $prelist->set_column_width(0, 250);
622 $prelist->column_titles_passive();
623 foreach my $mod (@{$moduleNames{'PREPROCESS'}})
624 {
625 $prelist->append($mod);
626 }
627 $prelist->show();
628
629 # Create a Scrolled Window for CList
630 my $scroll = Gtk::ScrolledWindow->new();
631 $scroll->set_policy('automatic', 'always');
632 $scroll->set_usize(300, 150);
633 $scroll->add($prelist);
634 $scroll->show();
635
636 my $postlist = Gtk::CList->new_with_titles("Postprocess Modules");
637 $postlist->set_selection_mode('extended');
638 $postlist->set_column_justification(0, 'center');
639 $postlist->set_column_width(0, 70);
640 $postlist->column_titles_passive();
641 foreach my $mod (@{$moduleNames{'POSTPROCESS'}})
642 {
643 $postlist->append($mod);
644 }
645 $postlist->show();
646
647 # Create a Scrolled Window for CList
648 my $scroll2 = Gtk::ScrolledWindow->new();
649 $scroll2->set_policy('automatic', 'always');
650 $scroll2->set_usize(300,150);
651 $scroll2->add($postlist);
652 $scroll2->show();
653  
654 # Create HBox for clist-window and text-vbox
655 my $modHBox = Gtk::HBox->new(0, 0);
656 $modHBox->pack_start($scroll, 0, 0, 0);
657 $modHBox->pack_start($scroll2, 1, 1, 0);
658 $modHBox->show();
659  
660 my $conlist = Gtk::CList->new_with_titles("Context Modules");
661 $conlist->set_selection_mode('browse');
662 $conlist->set_column_justification(0, 'center');
663 $conlist->column_titles_passive();
664 foreach my $mod (@{$moduleNames{'CONTEXT'}})
665 {
666 $conlist->append($mod);
667 }
668 $conlist->show();
669
670 # Create a Scrolled Window for CList
671 my $scroll3 = Gtk::ScrolledWindow->new();
672 $scroll3->set_policy('automatic', 'always');
673 $scroll3->set_usize(300, 150);
674 $scroll3->add($conlist);
675 $scroll3->show();
676
677 my $algolist = Gtk::CList->new_with_titles("Algorithm Modules");
678 $algolist->set_selection_mode('browse');
679 $algolist->set_column_justification(0, 'center');
680 $algolist->column_titles_passive();
681 foreach my $mod (@{$moduleNames{'ALGORITHM'}})
682 {
683 $algolist->append($mod);
684 }
685 $algolist->show();
686
687 # Create a Scrolled Window for CList
688 my $scroll4 = Gtk::ScrolledWindow->new();
689 $scroll4->set_policy('automatic', 'always');
690 $scroll4->set_usize(300,150);
691 $scroll4->add($algolist);
692 $scroll4->show();
693  
694 # Create HBox for clist-window and text-vbox
695 my $modHBox2 = Gtk::HBox->new(0, 0);
696 $modHBox2->pack_start($scroll3, 0, 0, 0);
697 $modHBox2->pack_start($scroll4, 1, 1, 0);
698 $modHBox2->show();
699  
700 my $canButton = Gtk::Button->new("Cancel");
701 $canButton->show();
702 $canButton->signal_connect('clicked', sub{ $setupWindow->destroy(); });
703 my $nextButton = Gtk::Button->new("Next >");
704 $nextButton->show();
705 $nextButton->signal_connect('clicked', \&setupModuleOptions, $setupWindow, $prelist, $postlist, $conlist, $algolist);
706  
707 my $cTable = Gtk::Table->new(1, 5);
708 $cTable->attach_defaults($canButton, 1, 2, 0, 1);
709 $cTable->attach_defaults($nextButton, 3, 4, 0, 1);
710 $cTable->show();
711  
712 # Create VBox for clist-window and text-vbox
713 my $modVBox = Gtk::VBox->new(0, 0);
714 $modVBox->pack_start($modHBox, 0, 0, 0);
715 $modVBox->pack_start($modHBox2, 1, 1, 0);
716 $modVBox->pack_end($cTable, 0, 0, 0);
717 $modVBox->show();
718  
719 $setupWindow->add($modVBox);
720 $setupWindow->show();
721 }
722  
723 sub setupModuleOptions
724 {
725 my $widget = shift;
726 my $prevWindow = shift;
727 my $prelist = shift;
728 my $postlist = shift;
729 my $conlist = shift;
730 my $algolist = shift;
731  
732 my @preMods = ();
733 my @postMods = ();
734 my @conMod = ();
735 my @algoMod = ();
736 my %codes = ('i' => 'Integer',
737 'f' => 'File Name',
738 's' => 'String',
739 'm' => 'Module Name');
740  
741 @preMods = $prelist->selection() if(defined $prelist);
742 @postMods = $postlist->selection() if(defined $postlist);
743 @conMod = $conlist->selection() if(defined $conlist);
744 @algoMod = $algolist->selection() if(defined $algolist);
745  
746 $prevWindow->destroy();
747
748 my $setupWindow = Gtk::Window->new('dialog');
749 $setupWindow->set_usize(700, 350);
750 $setupWindow->set_policy(0, 0, 1);
751 $setupWindow->set_title("WordNet::SenseRelate::TargetWord Setup: Select Module Options");
752 $setupWindow->set_position('center');
753 $setupWindow->border_width(0);
754 $setupWindow->set_modal(1);
755
756 my $modVBox = Gtk::VBox->new(0, 0);
757  
758 my %textboxHash;
759 my $moduleList = {preprocess => [],
760 postprocess => [],
761 context => undef,
762 algorithm => undef};
763  
764 foreach my $presel (@preMods)
765 {
766 my $mName = $moduleNames{'PREPROCESS'}->[$presel];
767 push(@{$moduleList->{preprocess}}, $mName);
768 foreach my $oName (keys(%{$moduleOptions{$mName}}))
769 {
770 my $theCode = $moduleOptions{$mName}->{$oName};
771 my ($typo, $reqd, $defval) = split(/!!/, $theCode);
772 my $label = Gtk::Label->new("$mName $oName ($codes{$typo}, ".(($reqd)?(""):("Not "))."Required) ");
773 $label->show();
774 my $tbox = Gtk::Entry->new();
775 $tbox->set_text($defval) if(defined $defval && $defval ne "");
776 $tbox->show();
777 $textboxHash{$mName}{$oName} = $tbox;
778 my $hhbox = Gtk::HBox->new(0, 0);
779 $hhbox->pack_start($label, 0, 0, 0);
780 $hhbox->pack_end($tbox, 1, 1, 0);
781 $hhbox->show();
782 $modVBox->pack_start($hhbox, 0, 0, 0);
783 }
784 }
785  
786 foreach my $postsel (@postMods)
787 {
788 my $mName = $moduleNames{'POSTPROCESS'}->[$postsel];
789 push(@{$moduleList->{postprocess}}, $mName);
790 foreach my $oName (keys(%{$moduleOptions{$mName}}))
791 {
792 my $theCode = $moduleOptions{$mName}->{$oName};
793 my ($typo, $reqd, $defval) = split(/!!/, $theCode);
794 my $label = Gtk::Label->new("$mName $oName ($codes{$typo}, ".(($reqd)?(""):("Not "))."Required) ");
795 $label->show();
796 my $tbox = Gtk::Entry->new();
797 $tbox->set_text($defval) if(defined $defval && $defval ne "");
798 $tbox->show();
799 $textboxHash{$mName}{$oName} = $tbox;
800 my $hhbox = Gtk::HBox->new(0, 0);
801 $hhbox->pack_start($label, 0, 0, 0);
802 $hhbox->pack_end($tbox, 1, 1, 0);
803 $hhbox->show();
804 $modVBox->pack_start($hhbox, 0, 0, 0);
805 }
806 }
807  
808 my $consel = shift(@conMod);
809 if(defined $consel && $consel ne "")
810 {
811 my $mName = $moduleNames{'CONTEXT'}->[$consel];
812 $moduleList->{context} = $mName;
813 foreach my $oName (keys(%{$moduleOptions{$mName}}))
814 {
815 my $theCode = $moduleOptions{$mName}->{$oName};
816 my ($typo, $reqd, $defval) = split(/!!/, $theCode);
817 my $label = Gtk::Label->new("$mName $oName ($codes{$typo}, ".(($reqd)?(""):("Not "))."Required) ");
818 $label->show();
819 my $tbox = Gtk::Entry->new();
820 $tbox->set_text($defval) if(defined $defval && $defval ne "");
821 $tbox->show();
822 $textboxHash{$mName}{$oName} = $tbox;
823 my $hhbox = Gtk::HBox->new(0, 0);
824 $hhbox->pack_start($label, 0, 0, 0);
825 $hhbox->pack_end($tbox, 1, 1, 0);
826 $hhbox->show();
827 $modVBox->pack_start($hhbox, 0, 0, 0);
828 }
829 }
830  
831 my $algosel = shift(@algoMod);
832 if(defined $algosel && $algosel ne "")
833 {
834 my $mName = $moduleNames{'ALGORITHM'}->[$algosel];
835 $moduleList->{algorithm} = $mName;
836 foreach my $oName (keys(%{$moduleOptions{$mName}}))
837 {
838 my $theCode = $moduleOptions{$mName}->{$oName};
839 my ($typo, $reqd, $defval) = split(/!!/, $theCode);
840 my $label = Gtk::Label->new("$mName $oName ($codes{$typo}, ".(($reqd)?(""):("Not "))."Required) ");
841 $label->show();
842 my $tbox = Gtk::Entry->new();
843 $tbox->set_text($defval) if(defined $defval && $defval ne "");
844 $tbox->show();
845 $textboxHash{$mName}{$oName} = $tbox;
846 my $hhbox = Gtk::HBox->new(0, 0);
847 $hhbox->pack_start($label, 0, 0, 0);
848 $hhbox->pack_end($tbox, 1, 1, 0);
849 $hhbox->show();
850 $modVBox->pack_start($hhbox, 0, 0, 0);
851 }
852 }
853  
854 my $canButton = Gtk::Button->new("Cancel");
855 $canButton->show();
856 $canButton->signal_connect('clicked', sub{ $setupWindow->destroy(); });
857 my $nextButton = Gtk::Button->new("Next >");
858 $nextButton->show();
859 $nextButton->signal_connect('clicked', \&getOptionsFromFields, $setupWindow, $moduleList, \%textboxHash);
860  
861 my $cTable = Gtk::Table->new(1, 5);
862 $cTable->attach_defaults($canButton, 1, 2, 0, 1);
863 $cTable->attach_defaults($nextButton, 3, 4, 0, 1);
864 $cTable->show();
865  
866 # Create VBox for clist-window and text-vbox
867 $modVBox->pack_end($cTable, 0, 0, 0);
868 $modVBox->show();
869  
870 $setupWindow->add($modVBox);
871 $setupWindow->show();
872 }
873  
874 sub getOptionsFromFields
875 {
876 my $widget = shift;
877 my $prevWindow = shift;
878 my $modList = shift;
879 my $textList = shift;
880
881 $modList->{preprocessconfig} = [];
882 $modList->{postprocessconfig} = [];
883 $modList->{contextconfig} = undef;
884 $modList->{algorithmconfig} = undef;
885 $modList->{wntools} = $wntools;
886  
887 foreach my $premod ($modList->{preprocess})
888 {
889 my $modOptions = {};
890 if(exists $textList->{$premod})
891 {
892 foreach my $theopt (keys %{$textList->{$premod}})
893 {
894 my $theText = $textList->{$premod}->{$theopt}->get_text();
895 $modOptions->{$theopt} = $theText if(defined $theText && $theText ne "");
896 }
897 }
898 push(@{$modList->{preprocessconfig}}, $modOptions);
899 }
900  
901 foreach my $premod ($modList->{postprocess})
902 {
903 my $modOptions = {};
904 if(exists $textList->{$premod})
905 {
906 foreach my $theopt (keys %{$textList->{$premod}})
907 {
908 my $theText = $textList->{$premod}->{$theopt}->get_text();
909 $modOptions->{$theopt} = $theText if(defined $theText && $theText ne "");
910 }
911 }
912 push(@{$modList->{postprocessconfig}}, $modOptions);
913 }
914  
915 my $conmod = $modList->{context};
916 if(defined $conmod && $conmod ne "")
917 {
918 my $modOptions = {};
919 if(exists $textList->{$conmod})
920 {
921 foreach my $theopt (keys %{$textList->{$conmod}})
922 {
923 my $theText = $textList->{$conmod}->{$theopt}->get_text();
924 $modOptions->{$theopt} = $theText if(defined $theText && $theText ne "");
925 }
926 }
927 $modList->{contextconfig} = $modOptions;
928 }
929
930 $conmod = $modList->{algorithm};
931 if(defined $conmod && $conmod ne "")
932 {
933 my $modOptions = {};
934 if(exists $textList->{$conmod})
935 {
936 foreach my $theopt (keys %{$textList->{$conmod}})
937 {
938 my $theText = $textList->{$conmod}->{$theopt}->get_text();
939 $modOptions->{$theopt} = $theText if(defined $theText && $theText ne "");
940 }
941 }
942 $modList->{algorithmconfig} = $modOptions;
943 }
944  
945 $prevWindow->destroy();
946  
947 # Load WordNet::SenseRelate::TargetWord...
948 # Exit with warning message, if unable to load...
949 # Show "Loading TargetWord... " window
950 my $messybox = Gtk::Window->new('dialog');
951 $messybox->set_usize(400, 100);
952 $messybox->set_policy(0, 0, 1);
953 # $messybox->set_resizable(0);
954 $messybox->set_title("Information: Reloading WordNet::SenseRelate::TargetWord... Please Wait.");
955 $messybox->set_position('center');
956 $messybox->border_width(0);
957 $messybox->set_modal(1);
958  
959 #my $mesLab = Gtk::Label->new("Reading XML file. Please Wait.");
960 #$mesLab->set_justify('left');
961 #$mesLab->show();
962  
963 my $mesLab = Gtk::Text->new();
964 $mesLab->set_usize(200, 50);
965 $mesLab->set_editable(0);
966 $mesLab->set_word_wrap(1);
967 $mesLab->set_line_wrap(1);
968 $mesLab->freeze();
969 $mesLab->insert(undef, undef, undef, "Reloading WordNet::SenseRelate::TargetWord. Please Wait.");
970 $mesLab->thaw();
971 $mesLab->show();
972  
973 my $meshbox = Gtk::VBox->new(0, 20);
974 $meshbox->pack_start($mesLab, 0, 0, 0);
975 $meshbox->show();
976  
977 $messybox->add($meshbox);
978 $messybox->show_now();
979  
980 my ($newTargetWord, $newError) = WordNet::SenseRelate::TargetWord->new($modList, 1);
981  
982 # Remove "Reloading... " window, && Show message box of errors, if any, && return
983 $messybox->destroy();
984 if(!defined $newTargetWord || !ref($newTargetWord))
985 {
986 &showMessageBox("Error", "Error loading module: $newError");
987 return;
988 }
989  
990 $targetWord = $newTargetWord;
991 }
992  
993 # Create Help->About window
994 sub showAbout
995 {
996 # Create the about window
997 my $about = Gtk::Window->new('dialog');
998 $about->set_usize(360, 250);
999 $about->set_policy(0, 0, 1);
1000 $about->set_title("About WordNet::SenseRelate::TargetWord");
1001 $about->set_position('center');
1002 $about->border_width(0);
1003 $about->set_modal(1);
1004  
1005 # Create a TextBox
1006 my $aboutText = Gtk::Text->new();
1007 $aboutText->set_editable(0);
1008 $aboutText->set_word_wrap(1);
1009 $aboutText->set_line_wrap(1);
1010 $aboutText->set_usize(360, 220);
1011 my $name = "-misc-fixed-bold-r-normal--15-140-75-75-c-90-iso8859-1";
1012 my $fixed_font = Gtk::Gdk::Font->load($name);
1013 $aboutText->insert($fixed_font, undef, undef, "\n WordNet::SenseRelate::TargetWord\n\n");
1014 $name = "-misc-fixed-medium-r-normal--14-130-75-75-c-70-iso8859-1";
1015 $fixed_font = Gtk::Gdk::Font->load($name);
1016 $aboutText->insert($fixed_font, undef, undef, " v$VERSION\n");
1017 $aboutText->insert($fixed_font, undef, undef, " Copyright (c) 2005\n\n");
1018 $aboutText->insert($fixed_font, undef, undef, " Ted Pedersen\n");
1019 $aboutText->insert($fixed_font, undef, undef, " tpederse\@d.umn.edu\n\n");
1020 $aboutText->insert($fixed_font, undef, undef, " Siddharth Patwardhan\n");
1021 $aboutText->insert($fixed_font, undef, undef, " sidd\@cs.utah.edu\n\n");
1022 $aboutText->insert($fixed_font, undef, undef, " Satanjeev Banerjee\n");
1023 $aboutText->insert($fixed_font, undef, undef, " banerjee+\@cs.cmu.edu\n");
1024 $aboutText->show();
1025  
1026 # Create a close Button
1027 my $aboutButton = Gtk::Button->new("Close");
1028 $aboutButton->signal_connect('clicked', sub{ $about->destroy(); });
1029 $aboutButton->show();
1030
1031 # Create a VBox
1032 my $aboutVBox = Gtk::VBox->new(0, 5);
1033 $aboutVBox->pack_start($aboutText, 0, 0, 0);
1034 $aboutVBox->pack_start($aboutButton, 0, 0, 0);
1035 $aboutVBox->show();
1036  
1037 # Display it
1038 $about->add($aboutVBox);
1039 $about->show();
1040 }
1041  
1042 # Show startup warnings
1043 sub displayStartupWarnings
1044 {
1045 return if(scalar(@_) <= 0);
1046 my $mbox = Gtk::Window->new('dialog');
1047 $mbox->set_usize(400, 150);
1048 $mbox->set_policy(0, 0, 1);
1049 # $mbox->set_resizable(0);
1050 $mbox->set_title("Warning(s)");
1051 $mbox->set_position('center');
1052 $mbox->border_width(0);
1053 $mbox->set_modal(1);
1054  
1055 my $message = "Warning(s):\n".(join("\n", @_))."\n";
1056 my $mesLabel = Gtk::Label->new($message);
1057 $mesLabel->show();
1058  
1059 my $okButton = Gtk::Button->new("Ok");
1060 $okButton->signal_connect('clicked', sub{ $mbox->destroy(); });
1061 $okButton->show();
1062  
1063 my $okTable = Gtk::Table->new(1, 5, 1);
1064 $okTable->attach_defaults($okButton, 2, 3, 0, 1);
1065 $okTable->show();
1066  
1067 my $mesBox = Gtk::VBox->new(0, 20);
1068 $mesBox->pack_start($mesLabel, 0, 0, 0);
1069 $mesBox->pack_start($okTable, 0, 0, 0);
1070 $mesBox->show();
1071  
1072 $mbox->add($mesBox);
1073 $mbox->show();
1074 }
1075  
1076 # Subroutine that reads the options file
1077 sub readConfigFile
1078 {
1079 my $fname = shift;
1080  
1081 if(!open(CONFIG, "$fname"))
1082 {
1083 &askHelp("senserelate.pl: Unable to open specified configuration file.");
1084 return undef;
1085 }
1086 my $line = <CONFIG>;
1087 $line =~ s/[\r\f\n]//;
1088 $line =~ s/^\s+//;
1089 $line =~ s/\s+$//;
1090 if ($line !~ /^WordNet::SenseRelate::TargetWord$/)
1091 {
1092 &askHelp("senserelate.pl: File format error (in header) in config file.");
1093 return undef;
1094 }
1095 my $struct = {};
1096 my $section = "";
1097 my $modname = "";
1098 my $modata = undef;
1099  
1100 while ($line = <CONFIG>)
1101 {
1102 $line =~ s/[\r\f\n]//;
1103 $line =~ s/\#.*//;
1104 $line =~ s/^\s+//;
1105 $line =~ s/\s+$//;
1106 if ($line =~ /\[SECTION:([^\]]*)\]/)
1107 {
1108 $section = $1;
1109 $struct->{$section} = []
1110 if ( defined $section
1111 && $section ne ""
1112 && !exists($struct->{$section}));
1113 }
1114 elsif ($line =~ /\[START\s+([^\]]+)\]/)
1115 {
1116 return undef if (!defined $section || $section eq "");
1117 $modname = $1;
1118 $modata = {};
1119 }
1120 elsif ($line =~ /\[END\]/)
1121 {
1122 return undef
1123 if ( !defined $section
1124 || $section eq ""
1125 || !defined $modname
1126 || $modname eq "");
1127 return undef if (!defined $modata);
1128 $modata->{'name'} = $modname;
1129 push(@{$struct->{$section}}, $modata);
1130 $modname = "";
1131 $modata = undef;
1132 }
1133 elsif ($line =~ /([^=]+)\s*=\s*([^=]+)/)
1134 {
1135 return undef
1136 if ( !defined $section
1137 || !defined $modname
1138 || $section eq ""
1139 || $modname eq "");
1140 $modata->{$1} = $2;
1141 }
1142 elsif ($line ne "")
1143 {
1144 return undef;
1145 }
1146 }
1147 close(CONFIG);
1148  
1149 return $struct;
1150 }
1151  
1152 # function to output help messages for this program
1153 sub printHelp
1154 {
1155 print "This program is a graphical interface to the WordNet::SenseRelate::TargetWord\n";
1156 print "Word Sense Disambiguation module. It allows the user to enter pieces of text,\n";
1157 print "and select a word from the text the target word to be disambiguated. The system\n";
1158 print "then disambiguates the sense of the target word using the algorithm by Banerjee\n";
1159 print "and Pedersen (2003).\n";
1160 print "Usage: senserelate.pl [ [--config FILE] [--wnpath WNPATH] | --help | --version]\n";
1161 print "--config Specifies a configuration file (FILE) to set up the\n";
1162 print " various configuration options.\n";
1163 print "--wnpath WNPATH specifies the path of the WordNet data files.\n";
1164 print " Ordinarily, this path is determined from the \$WNHOME\n";
1165 print " environment variable. But this option overides this\n";
1166 print " behavior.\n";
1167 print "--help Displays this help screen.\n";
1168 print "--version Displays version information.\n\n";
1169 }
1170  
1171 # function to output "ask for help" message when the user's goofed up!
1172 sub askHelp
1173 {
1174 my $message = shift;
1175 push(@warnings, $message) if (defined $message && $message ne "");
1176 }
1177  
1178 # Subroutine to print the version information
1179 sub printVersion
1180 {
1181 print "senserelate.pl version $VERSION\n";
1182 print "Copyright (c) 2005 Ted Pedersen, Satanjeev Banerjee, and Siddharth Patwardhan.\n";
1183 }
1184  
1185 __END__