| 1 | # WordNet::SenseRelate::TargetWord v0.09 | ||
| 2 | # (Last Updated $Id: TargetWord.pm,v 1.16 2006/12/24 12:39:19 sidz1979 Exp $) | ||
| 3 | package WordNet::SenseRelate::TargetWord; | ||
| 4 | |||
| 5 | 4 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use WordNet::SenseRelate::Tools; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | require Exporter; | ||
| 12 | |||
| 13 | our @ISA = qw(Exporter); | ||
| 14 | our %EXPORT_TAGS = ('all' => [qw()]); | ||
| 15 | our @EXPORT_OK = (@{$EXPORT_TAGS{'all'}}); | ||
| 16 | our @EXPORT = qw(); | ||
| 17 | our $VERSION = '0.09'; | ||
| 18 | |||
| 19 | # CONSTRUCTOR: Creates new SenseRelate::TargetWord object. | ||
| 20 | # Returns the created object. | ||
| 21 | sub new | ||
| 22 | { | ||
| 23 | my $class = shift; | ||
| 24 | my $self = {}; | ||
| 25 | my $modules = {}; | ||
| 26 | |||
| 27 | # Create the TargetWord object | ||
| 28 | $class = ref $class || $class; | ||
| 29 | bless($self, $class); | ||
| 30 | |||
| 31 | # Set the default options first | ||
| 32 | $modules->{preprocess} = []; | ||
| 33 | |||
| 34 | # Compound detection no longer done by default | ||
| 35 | # push( | ||
| 36 | # @{$modules->{preprocess}}, | ||
| 37 | # "WordNet::SenseRelate::Preprocess::Compounds" | ||
| 38 | # ); | ||
| 39 | $modules->{preprocessconfig} = []; | ||
| 40 | $modules->{context} = "WordNet::SenseRelate::Context::NearestWords"; | ||
| 41 | $modules->{contextconfig} = undef; | ||
| 42 | $modules->{postprocess} = []; | ||
| 43 | $modules->{postprocessconfig} = []; | ||
| 44 | $modules->{algorithm} = "WordNet::SenseRelate::Algorithm::Local"; | ||
| 45 | $modules->{algorithmconfig} = undef; | ||
| 46 | $modules->{wntools} = undef; | ||
| 47 | |||
| 48 | # Get the options | ||
| 49 | my $options = shift; | ||
| 50 | my $trace = shift; | ||
| 51 | $trace = 0 if (!defined $trace); | ||
| 52 | if(defined $options && ref $options eq "HASH") | ||
| 53 | { | ||
| 54 | # Get the Preprocessor modules | ||
| 55 | if(defined $options->{preprocess} && ref $options->{preprocess} eq "ARRAY") | ||
| 56 | { | ||
| 57 | $modules->{preprocess} = []; | ||
| 58 | push(@{$modules->{preprocess}}, @{$options->{preprocess}}); | ||
| 59 | } | ||
| 60 | elsif(defined $options->{preprocess}) | ||
| 61 | { | ||
| 62 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (preprocess)."); | ||
| 63 | } | ||
| 64 | |||
| 65 | # Get configuration options for preprocessor modules | ||
| 66 | if(defined $options->{preprocessconfig} && ref $options->{preprocessconfig} eq "ARRAY") | ||
| 67 | { | ||
| 68 | $modules->{preprocessconfig} = []; | ||
| 69 | push(@{$modules->{preprocessconfig}}, @{$options->{preprocessconfig}}); | ||
| 70 | } | ||
| 71 | elsif(defined $options->{preprocessconfig}) | ||
| 72 | { | ||
| 73 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (preprocessconfig)."); | ||
| 74 | } | ||
| 75 | |||
| 76 | # Get context selection module | ||
| 77 | $modules->{context} = $options->{context} | ||
| 78 | if(defined $options->{context} && !ref $options->{context}); | ||
| 79 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (context).") | ||
| 80 | if(defined $options->{context} && ref $options->{context}); | ||
| 81 | |||
| 82 | # Get configuration options for context selection module | ||
| 83 | if(defined $options->{contextconfig} && ref $options->{contextconfig} eq "HASH") | ||
| 84 | { | ||
| 85 | $modules->{contextconfig} = $options->{contextconfig}; | ||
| 86 | } | ||
| 87 | elsif(defined $options->{contextconfig}) | ||
| 88 | { | ||
| 89 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (contextconfig)."); | ||
| 90 | } | ||
| 91 | |||
| 92 | # Get postprocess modules | ||
| 93 | if(defined $options->{postprocess} && ref $options->{postprocess} eq "ARRAY") | ||
| 94 | { | ||
| 95 | $modules->{postprocess} = []; | ||
| 96 | push(@{$modules->{postprocess}}, @{$options->{postprocess}}); | ||
| 97 | } | ||
| 98 | elsif(defined $options->{postprocess}) | ||
| 99 | { | ||
| 100 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (postprocess)."); | ||
| 101 | } | ||
| 102 | |||
| 103 | # Get configuration options for postprocess modules | ||
| 104 | if(defined $options->{postprocessconfig} && ref $options->{postprocessconfig} eq "ARRAY") | ||
| 105 | { | ||
| 106 | $modules->{postprocessconfig} = []; | ||
| 107 | push(@{$modules->{postprocessconfig}}, @{$options->{postprocessconfig}}); | ||
| 108 | } | ||
| 109 | elsif(defined $options->{postprocessconfig}) | ||
| 110 | { | ||
| 111 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (postprocessconfig)."); | ||
| 112 | } | ||
| 113 | |||
| 114 | # Get algorithm module | ||
| 115 | $modules->{algorithm} = $options->{algorithm} | ||
| 116 | if(defined $options->{algorithm} && !ref $options->{algorithm}); | ||
| 117 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (algorithm).") | ||
| 118 | if(defined $options->{algorithm} && ref $options->{algorithm}); | ||
| 119 | |||
| 120 | # Get configuration options for algorithm module | ||
| 121 | if(defined $options->{algorithmconfig} && ref $options->{algorithmconfig} eq "HASH") | ||
| 122 | { | ||
| 123 | $modules->{algorithmconfig} = $options->{algorithmconfig} | ||
| 124 | } | ||
| 125 | elsif(defined $options->{algorithmconfig}) | ||
| 126 | { | ||
| 127 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure (algorithmconfig)."); | ||
| 128 | } | ||
| 129 | |||
| 130 | # Get the WordNet::SenseRelate::Tools | ||
| 131 | $modules->{wntools} = $options->{wntools} | ||
| 132 | if(defined $options->{wntools} && ref($options->{wntools}) eq "WordNet::SenseRelate::Tools"); | ||
| 133 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Unknown/illegal WordNet::SenseRelate::Tools object given.") | ||
| 134 | if(defined $options->{wntools} && ref $options->{wntools} ne "WordNet::SenseRelate::Tools"); | ||
| 135 | } | ||
| 136 | elsif(defined($options)) | ||
| 137 | { | ||
| 138 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Malformed 'options' data structure."); | ||
| 139 | } | ||
| 140 | |||
| 141 | # Load WordNet::SenseRelate::Tools | ||
| 142 | my $wntools = $modules->{wntools}; | ||
| 143 | if(!defined $wntools || !ref $wntools || ref($wntools) ne "WordNet::SenseRelate::Tools") | ||
| 144 | { | ||
| 145 | $wntools = WordNet::SenseRelate::Tools->new($wntools); | ||
| 146 | return (undef, "WordNet::SenseRelate::TargetWord->new() -- Unable to load WordNet::SenseRelate::Tools") | ||
| 147 | if (!defined $wntools); | ||
| 148 | } | ||
| 149 | $self->{wntools} = $wntools; | ||
| 150 | |||
| 151 | # Load all the modules | ||
| 152 | my $module; | ||
| 153 | my $modulePath; | ||
| 154 | |||
| 155 | # Load Preprocessor modules | ||
| 156 | $self->{preprocess} = []; | ||
| 157 | foreach my $i (0 .. scalar(@{$modules->{preprocess}}) - 1) | ||
| 158 | { | ||
| 159 | my $preproc = $modules->{preprocess}->[$i]; | ||
| 160 | $modulePath = $modules->{preprocess}->[$i]; | ||
| 161 | $modulePath =~ s/::/\//g; | ||
| 162 | $modulePath .= ".pm"; | ||
| 163 | require $modulePath; | ||
| 164 | $module = | ||
| 165 | $preproc->new($wntools, $trace, $modules->{preprocessconfig}->[$i]); | ||
| 166 | return ( | ||
| 167 | undef, | ||
| 168 | "WordNet::SenseRelate::TargetWord->new() -- Unable to load preprocess module $preproc" | ||
| 169 | ) | ||
| 170 | if (!defined($module)); | ||
| 171 | push(@{$self->{preprocess}}, $module); | ||
| 172 | } | ||
| 173 | |||
| 174 | # Load Context Selection module | ||
| 175 | $modulePath = $modules->{context}; | ||
| 176 | $modulePath =~ s/::/\//g; | ||
| 177 | $modulePath .= ".pm"; | ||
| 178 | require $modulePath; | ||
| 179 | $module = | ||
| 180 | $modules->{context}->new($wntools, $trace, $modules->{contextconfig}); | ||
| 181 | return (undef, | ||
| 182 | "WordNet::SenseRelate::TargetWord->new() -- Unable to load context selection module " | ||
| 183 | . ($modules->{context})) | ||
| 184 | if (!defined($module)); | ||
| 185 | $self->{context} = $module; | ||
| 186 | |||
| 187 | # Load Postprocessor modules | ||
| 188 | $self->{postprocess} = []; | ||
| 189 | foreach my $i (0 .. scalar(@{$modules->{postprocess}}) - 1) | ||
| 190 | { | ||
| 191 | my $postproc = $modules->{postprocess}->[$i]; | ||
| 192 | $modulePath = $postproc; | ||
| 193 | $modulePath =~ s/::/\//g; | ||
| 194 | $modulePath .= ".pm"; | ||
| 195 | require $modulePath; | ||
| 196 | $module = | ||
| 197 | $postproc->new($wntools, $trace, $modules->{postprocessconfig}->[$i]); | ||
| 198 | return ( | ||
| 199 | undef, | ||
| 200 | "WordNet::SenseRelate::TargetWord->new() -- Unable to load postprocess module $postproc" | ||
| 201 | ) | ||
| 202 | if (!defined($module)); | ||
| 203 | push(@{$self->{postprocess}}, $module); | ||
| 204 | } | ||
| 205 | |||
| 206 | # Load Disambiguation Algorithm module | ||
| 207 | $modulePath = $modules->{algorithm}; | ||
| 208 | $modulePath =~ s/::/\//g; | ||
| 209 | $modulePath .= ".pm"; | ||
| 210 | require $modulePath; | ||
| 211 | $module = | ||
| 212 | $modules->{algorithm}->new($wntools, $trace, $modules->{algorithmconfig}); | ||
| 213 | return (undef, | ||
| 214 | "WordNet::SenseRelate::TargetWord->new() -- Unable to load disambiguation module " | ||
| 215 | . ($modules->{algorithm})) | ||
| 216 | if (!defined($module)); | ||
| 217 | $self->{algorithm} = $module; | ||
| 218 | $self->{contextpos} = $module->{contextpos}; | ||
| 219 | |||
| 220 | # Initialize the trace string | ||
| 221 | $self->{trace} = $trace; | ||
| 222 | $self->{tracestring} = ""; | ||
| 223 | if ($trace) | ||
| 224 | { | ||
| 225 | foreach my $tmpModName (@{$modules->{preprocess}}) | ||
| 226 | { | ||
| 227 | $self->{tracestring} .= | ||
| 228 | "WordNet::SenseRelate::TargetWord ~ Loaded preprocess module $tmpModName\n"; | ||
| 229 | } | ||
| 230 | $self->{tracestring} .= | ||
| 231 | "WordNet::SenseRelate::TargetWord ~ Loaded context selection module " | ||
| 232 | . ($modules->{context}) . "\n"; | ||
| 233 | foreach my $tmpModName (@{$modules->{postprocess}}) | ||
| 234 | { | ||
| 235 | $self->{tracestring} .= | ||
| 236 | "WordNet::SenseRelate::TargetWord ~ Loaded postprocess module $tmpModName\n"; | ||
| 237 | } | ||
| 238 | $self->{tracestring} .= | ||
| 239 | "WordNet::SenseRelate::TargetWord ~ Loaded algorithm module " | ||
| 240 | . ($modules->{algorithm}) . "\n"; | ||
| 241 | } | ||
| 242 | |||
| 243 | return ($self, undef); | ||
| 244 | } | ||
| 245 | |||
| 246 | # Takes an instance object and disambiguates the target word | ||
| 247 | # Returns the selected sense of the target word | ||
| 248 | sub disambiguate | ||
| 249 | { | ||
| 250 | my $self = shift; | ||
| 251 | my $instance = shift; | ||
| 252 | my $sense; | ||
| 253 | my $wntools = $self->{wntools}; | ||
| 254 | my $trace = $self->{trace}; | ||
| 255 | $trace = 0 if (!defined $trace); | ||
| 256 | |||
| 257 | return ( | ||
| 258 | undef, | ||
| 259 | "WordNet::SenseRelate::TargetWord->disambiguate() -- TargetWord object not found." | ||
| 260 | ) | ||
| 261 | if ( !defined($self) | ||
| 262 | || !ref($self) | ||
| 263 | || ref($self) ne "WordNet::SenseRelate::TargetWord"); | ||
| 264 | return ( | ||
| 265 | undef, | ||
| 266 | "WordNet::SenseRelate::TargetWord->disambiguate() -- No instance specified." | ||
| 267 | ) | ||
| 268 | if (!defined($instance) || !ref($instance)); | ||
| 269 | |||
| 270 | # Preprocess the instance | ||
| 271 | foreach my $preproc (@{$self->{preprocess}}) | ||
| 272 | { | ||
| 273 | $instance = $preproc->preprocess($instance); | ||
| 274 | return ( | ||
| 275 | undef, | ||
| 276 | "WordNet::SenseRelate::TargetWord->disambiguate() -- Error preprocessing instance." | ||
| 277 | ) | ||
| 278 | if (!defined $instance); | ||
| 279 | if ($trace) | ||
| 280 | { | ||
| 281 | $self->{tracestring} .= | ||
| 282 | "WordNet::SenseRelate::TargetWord ~ Preprocessing instance (" | ||
| 283 | . ($instance->{id}) . ").\n"; | ||
| 284 | $self->{tracestring} .= $preproc->getTraceString(); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | # Required processing of words: | ||
| 289 | # (a) Get the base forms of all words | ||
| 290 | # (b) Get the possible parts of speech | ||
| 291 | # (c) Get the possible senses | ||
| 292 | foreach my $i (0 .. scalar(@{$instance->{wordobjects}}) - 1) | ||
| 293 | { | ||
| 294 | # Get the sense for all the words | ||
| 295 | $instance->{wordobjects}->[$i]->retrieveSenses($wntools->{wn}); | ||
| 296 | |||
| 297 | # Apply contextpos (POS-capability of sim module) to all words | ||
| 298 | $instance->{wordobjects}->[$i]->restrictSenses($self->{contextpos}); | ||
| 299 | |||
| 300 | # Apply targetpos to target wordobject | ||
| 301 | $instance->{wordobjects}->[$i]->restrictSenses($instance->{targetpos}) | ||
| 302 | if(defined $instance->{target} | ||
| 303 | && $instance->{target} == $i | ||
| 304 | && defined $instance->{targetpos}); | ||
| 305 | } | ||
| 306 | |||
| 307 | # Select context | ||
| 308 | $instance = $self->{context}->process($instance); | ||
| 309 | return (undef, "WordNet::SenseRelate::TargetWord->disambiguate() -- Error selecting the context.") | ||
| 310 | if (!defined $instance); | ||
| 311 | $self->{tracestring} .= $self->{context}->getTraceString() if ($trace); | ||
| 312 | |||
| 313 | # Postprocess the instance | ||
| 314 | foreach my $postproc (@{$self->{postprocess}}) | ||
| 315 | { | ||
| 316 | $instance = $postproc->postprocess($instance); | ||
| 317 | return (undef, "WordNet::SenseRelate::TargetWord->disambiguate() -- Error postprocessing instance.") | ||
| 318 | if (!defined $instance); | ||
| 319 | if ($trace) | ||
| 320 | { | ||
| 321 | $self->{tracestring} .= "WordNet::SenseRelate::TargetWord ~ Postprocessing instance (".($instance->{id}).").\n"; | ||
| 322 | $self->{tracestring} .= $postproc->getTraceString(); | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | # Debug output... | ||
| 327 | #foreach my $wobj (@{$instance->{contextwords}}) | ||
| 328 | #{ | ||
| 329 | # my $daWord = $wobj->getWord(); | ||
| 330 | # my @daSenses = $wobj->getSenses(); | ||
| 331 | # print STDERR "$daWord: ".(join(", ", @daSenses))."\n"; | ||
| 332 | #} | ||
| 333 | |||
| 334 | # Get target sense | ||
| 335 | $sense = $self->{algorithm}->disambiguate($instance); | ||
| 336 | $self->{tracestring} .= $self->{algorithm}->getTraceString() if ($trace); | ||
| 337 | |||
| 338 | return ($sense, undef); | ||
| 339 | } | ||
| 340 | |||
| 341 | # Get the trace string, and reset the trace | ||
| 342 | sub getTraceString | ||
| 343 | { | ||
| 344 | my $self = shift; | ||
| 345 | return "" | ||
| 346 | if ( !defined $self | ||
| 347 | || !ref($self) | ||
| 348 | || ref($self) ne "WordNet::SenseRelate::TargetWord"); | ||
| 349 | my $returnString = ""; | ||
| 350 | $returnString = $self->{tracestring} if (defined $self->{tracestring}); | ||
| 351 | $self->{tracestring} = ""; | ||
| 352 | return $returnString; | ||
| 353 | } | ||
| 354 | |||
| 355 | 1; | ||
| 356 | |||
| 357 | __END__ | ||