« lib/WordNet/SenseRelate/Reader/Senseval2.pm
↑ Summary
lib/WordNet/SenseRelate/Algorithm/SenseOne.pm »
| 1 | # WordNet::SenseRelate::Algorithm::Random v0.09 | ||
| 2 | # (Last updated $Id: Random.pm,v 1.9 2006/12/24 12:18:45 sidz1979 Exp $) | ||
| 3 | |||
| 4 | package WordNet::SenseRelate::Algorithm::Random; | ||
| 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 | |||
| 21 | # Create the preprocessor object | ||
| 22 | $class = ref $class || $class; | ||
| 23 | bless($self, $class); | ||
| 24 | |||
| 25 | # Read in the wordnet data, if required | ||
| 26 | if ( !defined $wntools | ||
| 27 | || !ref($wntools) | ||
| 28 | || ref($wntools) ne "WordNet::SenseRelate::Tools") | ||
| 29 | { | ||
| 30 | my $wnpath = undef; | ||
| 31 | $wnpath = $wntools | ||
| 32 | if (defined $wntools && !ref($wntools) && $wntools ne ""); | ||
| 33 | $wntools = WordNet::SenseRelate::Tools->new($wnpath); | ||
| 34 | return undef if (!defined $wntools); | ||
| 35 | } | ||
| 36 | $self->{wntools} = $wntools; | ||
| 37 | |||
| 38 | # No options accepted by this module | ||
| 39 | $self->{optionlist} = {}; | ||
| 40 | |||
| 41 | # Initialize traces | ||
| 42 | $trace = 0 if (!defined $trace); | ||
| 43 | $self->{trace} = $trace; | ||
| 44 | $self->{tracestring} = ""; | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | # Select the intended sense of the target word | ||
| 50 | sub disambiguate | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | my $context = shift; | ||
| 54 | my $trace = $self->{trace}; | ||
| 55 | return undef if (!defined $self || !ref $self || !defined $context); | ||
| 56 | return undef | ||
| 57 | if ( !defined $context->{targetwordobject} | ||
| 58 | || !defined $context->{contextwords}); | ||
| 59 | return undef | ||
| 60 | if ( ref($context->{contextwords}) ne "ARRAY" | ||
| 61 | || ref($context->{targetwordobject}) ne "WordNet::SenseRelate::Word"); | ||
| 62 | my $targetWord = $context->{targetwordobject}; | ||
| 63 | my @targetSenses = $targetWord->getSenses(); | ||
| 64 | return undef if (scalar(@targetSenses) <= 0); | ||
| 65 | |||
| 66 | if ($trace) | ||
| 67 | { | ||
| 68 | $self->{tracestring} .= | ||
| 69 | "WordNet::SenseRelate::Algorithm::Random ~ Target senses: " | ||
| 70 | . (join(", ", @targetSenses)) . "\n"; | ||
| 71 | } | ||
| 72 | my $count = scalar(@targetSenses); | ||
| 73 | my $random = int(rand($count)); | ||
| 74 | if ($trace) | ||
| 75 | { | ||
| 76 | $self->{tracestring} .= | ||
| 77 | "WordNet::SenseRelate::Algorithm::Random ~ Random number ($random)\n"; | ||
| 78 | $self->{tracestring} .= | ||
| 79 | "WordNet::SenseRelate::Algorithm::Random ~ Selected sense = $targetSenses[$random]\n"; | ||
| 80 | } | ||
| 81 | return $targetSenses[$random]; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Get the trace string, and reset the trace | ||
| 85 | sub getTraceString | ||
| 86 | { | ||
| 87 | my $self = shift; | ||
| 88 | return "" | ||
| 89 | if ( !defined $self | ||
| 90 | || !ref($self) | ||
| 91 | || ref($self) ne "WordNet::SenseRelate::Algorithm::Random"); | ||
| 92 | my $returnString = ""; | ||
| 93 | $returnString = $self->{tracestring} if (defined $self->{tracestring}); | ||
| 94 | $self->{tracestring} = ""; | ||
| 95 | return $returnString; | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | __END__ |