| 1 | # WordNet::SenseRelate::Context::NearestWords v0.09 | ||
| 2 | # (Last updated $Id: NearestWords.pm,v 1.16 2006/12/24 12:39:19 sidz1979 Exp $) | ||
| 3 | |||
| 4 | package WordNet::SenseRelate::Context::NearestWords; | ||
| 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 $trace = shift; | ||
| 19 | my $config = shift; | ||
| 20 | my $self = {}; | ||
| 21 | |||
| 22 | # Create the context selection object | ||
| 23 | $class = ref $class || $class; | ||
| 24 | bless($self, $class); | ||
| 25 | |||
| 26 | # Sanity check for wntools | ||
| 27 | if ( !defined $wntools | ||
| 28 | || !ref($wntools) | ||
| 29 | || ref($wntools) ne "WordNet::SenseRelate::Tools") | ||
| 30 | { | ||
| 31 | $wntools = WordNet::SenseRelate::Tools->new($wntools); | ||
| 32 | return undef if (!defined $wntools); | ||
| 33 | } | ||
| 34 | $self->{wntools} = $wntools; | ||
| 35 | $self->{trace} = (($trace) ? 1 : 0); | ||
| 36 | $self->{stop} = {}; | ||
| 37 | |||
| 38 | # Get options "window-size" and "stopwords". | ||
| 39 | if (defined $config && ref($config) eq "HASH") | ||
| 40 | { | ||
| 41 | $self->{count} = $config->{windowsize} | ||
| 42 | if (defined $config->{windowsize} | ||
| 43 | && $config->{windowsize} =~ /[0-9]+/); | ||
| 44 | if (defined $config->{windowstop}) | ||
| 45 | { | ||
| 46 | open(STOP, $config->{windowstop}) || return undef; | ||
| 47 | while (<STOP>) | ||
| 48 | { | ||
| 49 | s/[\r\f\n]//; | ||
| 50 | s/\s+$//; | ||
| 51 | s/^\s+//; | ||
| 52 | my ($stopword) = split(/\s+/); | ||
| 53 | $self->{stop}->{lc($stopword)} = 1; | ||
| 54 | } | ||
| 55 | close(STOP); | ||
| 56 | } | ||
| 57 | $self->{contextpos} = $config->{contextpos} | ||
| 58 | if (defined $config->{contextpos} | ||
| 59 | && $config->{contextpos} =~ /^[nvar]+$/); | ||
| 60 | } | ||
| 61 | |||
| 62 | # Initialize traces | ||
| 63 | $self->{tracestring} = ""; | ||
| 64 | |||
| 65 | # Options accepted by this module | ||
| 66 | $self->{optionlist} = {}; | ||
| 67 | $self->{optionlist}->{contextpos} = "s!!0!!nvar"; | ||
| 68 | $self->{optionlist}->{windowsize} = "i!!0!!5"; | ||
| 69 | $self->{optionlist}->{windowstop} = "f!!0!!"; | ||
| 70 | |||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | # Select the context words from the context | ||
| 75 | sub process | ||
| 76 | { | ||
| 77 | my $self = shift; | ||
| 78 | my $intext = shift; | ||
| 79 | my $count = $self->{count}; | ||
| 80 | my $poses = $self->{poses}; | ||
| 81 | my $contextpos = $self->{contextpos}; | ||
| 82 | my $targetpos = $intext->{targetpos}; | ||
| 83 | my $trace = $self->{trace}; | ||
| 84 | $count = 5 if (!defined $count || $count !~ /^[0-9]+$/); | ||
| 85 | $poses = "nvar" if (!defined $poses || $poses !~ /^[nvar]*$/); | ||
| 86 | return undef if (!defined $self || !ref $self); | ||
| 87 | return undef if (!defined $intext || !defined $intext->{wordobjects}); | ||
| 88 | return undef if (ref($intext->{wordobjects}) ne "ARRAY"); | ||
| 89 | my $i = 1; | ||
| 90 | my $done = 1; | ||
| 91 | return undef | ||
| 92 | if (!defined $i || $i < 0 || $i >= scalar(@{$intext->{wordobjects}})); | ||
| 93 | my $context = {}; | ||
| 94 | $context->{words} = []; | ||
| 95 | $context->{text} = []; | ||
| 96 | $context->{head} = -1; | ||
| 97 | $context->{target} = -1; | ||
| 98 | $context->{wordobjects} = []; | ||
| 99 | $context->{lexelt} = $intext->{lexelt} if (defined $intext->{lexelt}); | ||
| 100 | $context->{id} = $intext->{id} if (defined $intext->{id}); | ||
| 101 | $context->{answer} = $intext->{answer} if (defined $intext->{answer}); | ||
| 102 | $context->{targetpos} = $intext->{targetpos} | ||
| 103 | if (defined $intext->{targetpos}); | ||
| 104 | $context->{contextwords} = []; | ||
| 105 | |||
| 106 | # Check that the text segments exist | ||
| 107 | if (defined $intext->{text}) | ||
| 108 | { | ||
| 109 | foreach my $textseg (@{$intext->{text}}) | ||
| 110 | { | ||
| 111 | push(@{$context->{text}}, $textseg); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | $context->{head} = $intext->{head} if (defined $intext->{head}); | ||
| 115 | |||
| 116 | # Move the target to the context object | ||
| 117 | my $targetptr; | ||
| 118 | if ( defined $intext->{target} | ||
| 119 | && $intext->{target} >= 0 | ||
| 120 | && $intext->{target} < scalar(@{$intext->{wordobjects}})) | ||
| 121 | { | ||
| 122 | $context->{target} = $intext->{target}; | ||
| 123 | if (defined $intext->{words}) | ||
| 124 | { | ||
| 125 | foreach my $textseg (@{$intext->{words}}) | ||
| 126 | { | ||
| 127 | push(@{$context->{words}}, $textseg); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | if (defined $intext->{wordobjects}) | ||
| 131 | { | ||
| 132 | foreach my $textseg (@{$intext->{wordobjects}}) | ||
| 133 | { | ||
| 134 | push(@{$context->{wordobjects}}, $textseg); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | $targetptr = $intext->{target}; | ||
| 138 | $context->{targetword} = $intext->{words}->[$targetptr]; | ||
| 139 | $context->{targetwordobject} = $intext->{wordobjects}->[$targetptr]; | ||
| 140 | $context->{targetwordobject}->restrictSenses($targetpos) | ||
| 141 | if(defined $targetpos); | ||
| 142 | if ($trace) | ||
| 143 | { | ||
| 144 | $self->{tracestring} .= | ||
| 145 | "WordNet::SenseRelate::Context::NearestWords ~ Selected target word "; | ||
| 146 | $self->{tracestring} .= $context->{targetword}; | ||
| 147 | $self->{tracestring} .= "\n"; | ||
| 148 | $self->{tracestring} .= | ||
| 149 | "WordNet::SenseRelate::Context::NearestWords ~ Target word senses: "; | ||
| 150 | $self->{tracestring} .= | ||
| 151 | join(", ", $context->{targetwordobject}->getSenses()); | ||
| 152 | $self->{tracestring} .= "\n"; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | else | ||
| 156 | { | ||
| 157 | return undef; | ||
| 158 | } | ||
| 159 | while ( | ||
| 160 | $done < $count | ||
| 161 | && ( $targetptr - $i >= 0 | ||
| 162 | || $targetptr + $i < scalar(@{$intext->{wordobjects}})) | ||
| 163 | ) | ||
| 164 | { | ||
| 165 | if ($targetptr + $i < scalar(@{$intext->{wordobjects}})) | ||
| 166 | { | ||
| 167 | my $wordobj = $intext->{wordobjects}->[$i + $targetptr]; | ||
| 168 | $wordobj->restrictSenses($contextpos) | ||
| 169 | if (defined $contextpos); | ||
| 170 | foreach my $pos (split(//, $poses)) | ||
| 171 | { | ||
| 172 | if ( $wordobj->{poslist} =~ /$pos/ | ||
| 173 | && scalar($wordobj->getSenses()) > 0 | ||
| 174 | && !defined $self->{stop}->{lc($wordobj->getWord())}) | ||
| 175 | { | ||
| 176 | push(@{$context->{contextwords}}, $wordobj); | ||
| 177 | if ($trace) | ||
| 178 | { | ||
| 179 | $self->{tracestring} .= | ||
| 180 | "WordNet::SenseRelate::Context::NearestWords ~ Selected context word "; | ||
| 181 | $self->{tracestring} .= $wordobj->getWord(); | ||
| 182 | $self->{tracestring} .= "\n"; | ||
| 183 | $self->{tracestring} .= | ||
| 184 | "WordNet::SenseRelate::Context::NearestWords ~ Context word senses: "; | ||
| 185 | $self->{tracestring} .= | ||
| 186 | join(", ", $wordobj->getSenses()); | ||
| 187 | $self->{tracestring} .= "\n"; | ||
| 188 | } | ||
| 189 | $done++; | ||
| 190 | last; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | last if ($done >= $count); | ||
| 194 | } | ||
| 195 | if ($targetptr - $i >= 0) | ||
| 196 | { | ||
| 197 | my $wordobj = $intext->{wordobjects}->[$targetptr - $i]; | ||
| 198 | $wordobj->restrictSenses($contextpos) | ||
| 199 | if (defined $contextpos); | ||
| 200 | foreach my $pos (split(//, $poses)) | ||
| 201 | { | ||
| 202 | if ( $wordobj->{poslist} =~ /$pos/ | ||
| 203 | && scalar($wordobj->getSenses()) > 0 | ||
| 204 | && !defined $self->{stop}->{lc($wordobj->getWord())}) | ||
| 205 | { | ||
| 206 | push(@{$context->{contextwords}}, $wordobj); | ||
| 207 | if ($trace) | ||
| 208 | { | ||
| 209 | $self->{tracestring} .= | ||
| 210 | "WordNet::SenseRelate::Context::NearestWords ~ Selected context word "; | ||
| 211 | $self->{tracestring} .= $wordobj->getWord(); | ||
| 212 | $self->{tracestring} .= "\n"; | ||
| 213 | $self->{tracestring} .= | ||
| 214 | "WordNet::SenseRelate::Context::NearestWords ~ Context word senses: "; | ||
| 215 | $self->{tracestring} .= | ||
| 216 | join(", ", $wordobj->getSenses()); | ||
| 217 | $self->{tracestring} .= "\n"; | ||
| 218 | } | ||
| 219 | $done++; | ||
| 220 | last; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | last if ($done >= $count); | ||
| 224 | } | ||
| 225 | $i++; | ||
| 226 | } | ||
| 227 | |||
| 228 | return $context; | ||
| 229 | } | ||
| 230 | |||
| 231 | # Get the trace string, and reset the trace | ||
| 232 | sub getTraceString | ||
| 233 | { | ||
| 234 | my $self = shift; | ||
| 235 | return "" | ||
| 236 | if ( !defined $self | ||
| 237 | || !ref($self) | ||
| 238 | || ref($self) ne "WordNet::SenseRelate::Context::NearestWords"); | ||
| 239 | my $returnString = ""; | ||
| 240 | $returnString = $self->{tracestring} if (defined $self->{tracestring}); | ||
| 241 | $self->{tracestring} = ""; | ||
| 242 | return $returnString; | ||
| 243 | } | ||
| 244 | |||
| 245 | 1; | ||
| 246 | |||
| 247 | __END__ |