| 1 | # WordNet::SenseRelate::Algorithm::Local v0.09 | ||
| 2 | # (Last updated $Id: Local.pm,v 1.10 2006/12/24 12:18:45 sidz1979 Exp $) | ||
| 3 | |||
| 4 | package WordNet::SenseRelate::Algorithm::Local; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Exporter; | ||
| 9 | |||
| 10 | our @ISA = qw(Exporter); | ||
| 11 | our $VERSION = '0.09'; | ||
| 12 | |||
| 13 | # Constructor for this module | ||
| 14 | sub new | ||
| 15 | { | ||
| 16 | my $class = shift; | ||
| 17 | my $wntools = shift; | ||
| 18 | my $self = {}; | ||
| 19 | my $trace = shift; | ||
| 20 | my $config = shift; | ||
| 21 | $trace = 0 if (!defined $trace); | ||
| 22 | |||
| 23 | # Create the preprocessor object | ||
| 24 | $class = ref $class || $class; | ||
| 25 | bless($self, $class); | ||
| 26 | |||
| 27 | # Read in the wordnet data, if required | ||
| 28 | if ( !defined $wntools | ||
| 29 | || !ref($wntools) | ||
| 30 | || ref($wntools) ne "WordNet::SenseRelate::Tools") | ||
| 31 | { | ||
| 32 | my $wnpath = undef; | ||
| 33 | $wnpath = $wntools | ||
| 34 | if (defined $wntools && !ref($wntools) && $wntools ne ""); | ||
| 35 | $wntools = WordNet::SenseRelate::Tools->new($wnpath); | ||
| 36 | return undef if (!defined $wntools); | ||
| 37 | } | ||
| 38 | $self->{wntools} = $wntools; | ||
| 39 | |||
| 40 | # Load Similarity module | ||
| 41 | my $modulePath = "WordNet::Similarity::jcn"; | ||
| 42 | my $moduleName = $modulePath; | ||
| 43 | my $moduleConfig = undef; | ||
| 44 | if (defined $config && ref($config) eq "HASH") | ||
| 45 | { | ||
| 46 | $modulePath = $config->{measure} if (defined $config->{measure}); | ||
| 47 | $moduleName = $modulePath; | ||
| 48 | $moduleConfig = $config->{measureconfig} | ||
| 49 | if (defined $config->{measureconfig}); | ||
| 50 | } | ||
| 51 | $modulePath =~ s/::/\//g; | ||
| 52 | $modulePath .= ".pm"; | ||
| 53 | require $modulePath; | ||
| 54 | my $module = $moduleName->new($wntools->{wn}, $moduleConfig); | ||
| 55 | return undef if (!defined($module)); | ||
| 56 | $module->{'trace'} = 2 if ($trace); | ||
| 57 | $self->{measure} = $module; | ||
| 58 | |||
| 59 | # Get the parts of speech for this module | ||
| 60 | my $measurepos = ""; | ||
| 61 | foreach my $mypos ('n', 'v', 'a', 'r') | ||
| 62 | { | ||
| 63 | $measurepos .= $mypos if (defined $module->{$mypos}); | ||
| 64 | } | ||
| 65 | $measurepos = "nvar" if ($measurepos eq ""); | ||
| 66 | $self->{contextpos} = $measurepos; | ||
| 67 | |||
| 68 | # Sanity check on the similarity module | ||
| 69 | return undef | ||
| 70 | if ( !defined $self->{measure} | ||
| 71 | || !ref($self->{measure}) | ||
| 72 | || !($self->{measure}->can('getRelatedness'))); | ||
| 73 | |||
| 74 | # Options accepted by this module | ||
| 75 | $self->{optionlist} = {}; | ||
| 76 | $self->{optionlist}->{measure} = "m!!0!!WordNet::Similarity::jcn"; | ||
| 77 | $self->{optionlist}->{measureconfig} = "f!!0!!"; | ||
| 78 | |||
| 79 | # Initialize traces | ||
| 80 | $self->{tracestring} = ""; | ||
| 81 | $self->{trace} = $trace; | ||
| 82 | $self->{trace} = 0 if (!defined $self->{trace}); | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Select the intended sense of the target word | ||
| 88 | sub disambiguate | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | my $context = shift; | ||
| 92 | |||
| 93 | # Sanity checks on self and context | ||
| 94 | return undef if (!defined $self || !ref $self || !defined $context); | ||
| 95 | return undef | ||
| 96 | if ( !defined $context->{targetwordobject} | ||
| 97 | || !defined $context->{contextwords}); | ||
| 98 | return undef | ||
| 99 | if ( ref($context->{contextwords}) ne "ARRAY" | ||
| 100 | || ref($context->{targetwordobject}) ne "WordNet::SenseRelate::Word" | ||
| 101 | || scalar(@{$context->{contextwords}}) <= 0); | ||
| 102 | |||
| 103 | # Get the input required | ||
| 104 | my $trace = $self->{trace}; | ||
| 105 | my $measure = $self->{measure}; | ||
| 106 | my $targetWord = $context->{targetwordobject}; | ||
| 107 | my @targetSenses = $targetWord->getSenses(); | ||
| 108 | return undef if (scalar(@targetSenses) <= 0); | ||
| 109 | |||
| 110 | # Print the list of target senses to the trace string | ||
| 111 | if ($trace) | ||
| 112 | { | ||
| 113 | $self->{tracestring} .= | ||
| 114 | "WordNet::SenseRelate::Algorithm::Local ~ Target senses:" | ||
| 115 | . (join(", ", @targetSenses)) . "\n"; | ||
| 116 | } | ||
| 117 | |||
| 118 | # Iterate over the target senses | ||
| 119 | my %senseScores = (); | ||
| 120 | my %sensePairScores = (); | ||
| 121 | my $max = 0; | ||
| 122 | my $maxSense = ""; | ||
| 123 | foreach my $targetSense (@targetSenses) | ||
| 124 | { | ||
| 125 | |||
| 126 | # Iterate over the context words | ||
| 127 | $senseScores{$targetSense} = 0; | ||
| 128 | foreach my $wObject (@{$context->{contextwords}}) | ||
| 129 | { | ||
| 130 | |||
| 131 | # Iterate over the senses of the context word | ||
| 132 | $max = 0; | ||
| 133 | $maxSense = ""; | ||
| 134 | foreach my $wordSense ($wObject->getSenses()) | ||
| 135 | { | ||
| 136 | |||
| 137 | # Get similarity | ||
| 138 | my $similarity = | ||
| 139 | $measure->getRelatedness($targetSense, $wordSense); | ||
| 140 | my ($errorCode, $errorString) = $measure->getError(); | ||
| 141 | |||
| 142 | # If error do what needs to be done | ||
| 143 | if ($errorCode == 1) { printf STDERR "$errorString\n"; } | ||
| 144 | elsif ($errorCode == 2) | ||
| 145 | { | ||
| 146 | return (0, $errorCode, $errorString); | ||
| 147 | } | ||
| 148 | |||
| 149 | # Check against max | ||
| 150 | if ($similarity > $max) | ||
| 151 | { | ||
| 152 | $max = $similarity; | ||
| 153 | $maxSense = $wordSense; | ||
| 154 | } | ||
| 155 | if ($trace) | ||
| 156 | { | ||
| 157 | $self->{tracestring} .= | ||
| 158 | "WordNet::SenseRelate::Algorithm::Local ~ sim($targetSense, $wordSense) = $similarity\n"; | ||
| 159 | $self->{tracestring} .= $measure->getTraceString(); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | $senseScores{$targetSense} += $max; | ||
| 163 | $sensePairScores{"$targetSense, $maxSense"} = $max; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | # More tracing | ||
| 168 | if ($trace) | ||
| 169 | { | ||
| 170 | foreach my $keySense (keys(%sensePairScores)) | ||
| 171 | { | ||
| 172 | $self->{tracestring} .= | ||
| 173 | "WordNet::SenseRelate::Algorithm::Local ~ Maximum score for pair ($keySense) = "; | ||
| 174 | $self->{tracestring} .= $sensePairScores{$keySense}; | ||
| 175 | $self->{tracestring} .= "\n"; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | my ($intended) = | ||
| 180 | sort { $senseScores{$b} <=> $senseScores{$a} } keys %senseScores; | ||
| 181 | $self->{tracestring} .= | ||
| 182 | "WordNet::SenseRelate::Algorithm::Local ~ Selected sense = $intended\n" | ||
| 183 | if ($trace); | ||
| 184 | |||
| 185 | return $intended; | ||
| 186 | } | ||
| 187 | |||
| 188 | # Get the trace string, and reset the trace | ||
| 189 | sub getTraceString | ||
| 190 | { | ||
| 191 | my $self = shift; | ||
| 192 | return "" | ||
| 193 | if ( !defined $self | ||
| 194 | || !ref($self) | ||
| 195 | || ref($self) ne "WordNet::SenseRelate::Algorithm::Local"); | ||
| 196 | my $returnString = ""; | ||
| 197 | $returnString = $self->{tracestring} if (defined $self->{tracestring}); | ||
| 198 | $self->{tracestring} = ""; | ||
| 199 | return $returnString; | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | |||
| 204 | __END__ |