lib/WordNet/SenseRelate/Algorithm/Global.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 242 CC 25 SCAR 47.0
CC
25
Cov
0%
new
182.0
disambiguate
110.0
getTraceString
12.0
CRAP 110.1
1 # WordNet::SenseRelate::Algorithm::Global v0.09
2 # (Last updated $Id: Global.pm,v 1.10 2006/12/24 12:18:45 sidz1979 Exp $)
3  
4 package WordNet::SenseRelate::Algorithm::Global;
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::Global ~ Target senses:"
115 . (join(", ", @targetSenses)) . "\n";
116 }
117  
118 # Get all possible combinations of the sense lists
119 my $nameHash = {};
120 foreach my $targetSense (@targetSenses)
121 {
122 $self->_getCombinations($targetSense, $context->{contextwords},
123 0, $nameHash);
124 }
125  
126 # Get the score for each combination
127 my $traceHash = {};
128 foreach my $nameCombo (keys(%{$nameHash}))
129 {
130  
131 # Get the senses in the combination
132 my @sensesInCombo = split(/::/, $nameCombo);
133 foreach my $i (0 .. ($#sensesInCombo - 1))
134 {
135  
136 # Get the relatedness of that sense with the other senses
137 foreach my $j ($i + 1 .. $#sensesInCombo)
138 {
139 $nameHash->{$nameCombo} +=
140 $measure->getRelatedness($sensesInCombo[$i],
141 $sensesInCombo[$j]);
142  
143 # Get the trace string from the similarity module
144 if ($measure->{'trace'})
145 {
146 $traceHash->{$nameCombo} = ""
147 if (!defined $traceHash->{$nameCombo});
148 $traceHash->{$nameCombo} .= $measure->getTraceString();
149 }
150 }
151 }
152 }
153  
154 # Add a little trace information here
155 if ($trace)
156 {
157 foreach my $nameCombo (sort { $nameHash->{$b} <=> $nameHash->{$a} }
158 keys %{$nameHash})
159 {
160 $self->{tracestring} .=
161 "WordNet::SenseRelate::Algorithm::Global ~ Combination score ($nameCombo) = ";
162 $self->{tracestring} .= $nameHash->{$nameCombo};
163 $self->{tracestring} .= "\n";
164 $self->{tracestring} .= $traceHash->{$nameCombo}
165 if (defined $traceHash->{$nameCombo});
166 }
167 }
168  
169 # Get the max sense, and return
170 my ($maxSense) =
171 sort { $nameHash->{$b} <=> $nameHash->{$a} } keys %{$nameHash};
172 my $maxScore = $nameHash->{$maxSense};
173 my ($selectedSense) = split(/::/, $maxSense);
174 $self->{tracestring} .=
175 "WordNet::SenseRelate::Algorithm::Global ~ Selected sense = $selectedSense (Score = $maxScore)\n";
176  
177 return $selectedSense;
178 }
179  
180 # Get the trace string, and reset the trace
181 sub getTraceString
182 {
183 my $self = shift;
184 return ""
185 if ( !defined $self
186 || !ref($self)
187 || ref($self) ne "WordNet::SenseRelate::Algorithm::Global");
188 my $returnString = "";
189 $returnString = $self->{tracestring} if (defined $self->{tracestring});
190 $self->{tracestring} = "";
191 return $returnString;
192 }
193  
194 sub _getCombinations
195 {
196 my $self = shift;
197 my $currString = shift;
198 my $contextWords = shift;
199 my $level = shift;
200 my $nameHash = shift;
201  
202 if ($level >= scalar(@{$contextWords}))
203 {
204 $nameHash->{$currString} = 0;
205 return;
206 }
207 my $word = $contextWords->[$level];
208  
209 foreach my $sense ($word->getSenses())
210 {
211 $self->_getCombinations($currString . "::" . $sense,
212 $contextWords, $level + 1, $nameHash);
213 }
214 }
215  
216 1;
217  
218 __END__