| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # WordNet::Similarity::random.pm version 2.04 | 
| 2 |  |  |  |  |  |  | # (Last updated $Id: random.pm,v 1.15 2008/03/27 06:21:17 sidz1979 Exp $) | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # Random semantic distance generator module. | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # Copyright (c) 2005, | 
| 7 |  |  |  |  |  |  | # | 
| 8 |  |  |  |  |  |  | # Ted Pedersen, University of Minnesota Duluth | 
| 9 |  |  |  |  |  |  | # tpederse at d.umn.edu | 
| 10 |  |  |  |  |  |  | # | 
| 11 |  |  |  |  |  |  | # Siddharth Patwardhan, University of Utah, Salt Lake City | 
| 12 |  |  |  |  |  |  | # sidd at cs.utah.edu | 
| 13 |  |  |  |  |  |  | # | 
| 14 |  |  |  |  |  |  | # This program is free software; you can redistribute it and/or | 
| 15 |  |  |  |  |  |  | # modify it under the terms of the GNU General Public License | 
| 16 |  |  |  |  |  |  | # as published by the Free Software Foundation; either version 2 | 
| 17 |  |  |  |  |  |  | # of the License, or (at your option) any later version. | 
| 18 |  |  |  |  |  |  | # | 
| 19 |  |  |  |  |  |  | # This program is distributed in the hope that it will be useful, | 
| 20 |  |  |  |  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 21 |  |  |  |  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 22 |  |  |  |  |  |  | # GNU General Public License for more details. | 
| 23 |  |  |  |  |  |  | # | 
| 24 |  |  |  |  |  |  | # You should have received a copy of the GNU General Public License | 
| 25 |  |  |  |  |  |  | # along with this program; if not, write to | 
| 26 |  |  |  |  |  |  | # | 
| 27 |  |  |  |  |  |  | # The Free Software Foundation, Inc., | 
| 28 |  |  |  |  |  |  | # 59 Temple Place - Suite 330, | 
| 29 |  |  |  |  |  |  | # Boston, MA  02111-1307, USA. | 
| 30 |  |  |  |  |  |  | # | 
| 31 |  |  |  |  |  |  | # ------------------------------------------------------------------ | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | package WordNet::Similarity::random; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =head1 NAME | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | WordNet::Similarity::random - Perl module for computing semantic relatedness | 
| 38 |  |  |  |  |  |  | of word senses using a random measure. | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | use WordNet::Similarity::random; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | use WordNet::QueryData; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | my $wn = WordNet::QueryData->new(); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | my $random = WordNet::Similarity::random->new($wn); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | my $value = $random->getRelatedness("car#n#1", "bus#n#2"); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | ($error, $errorString) = $random->getError(); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | die "$errorString\n" if($error); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | print "car (sense 1) <-> bus (sense 2) = $value\n"; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | This module generates random numbers as a measure of semantic relatedness | 
| 61 |  |  |  |  |  |  | of word senses. It is possible to assign a random value for a word sense | 
| 62 |  |  |  |  |  |  | pair and return the same value if the same word sense pair is passed as | 
| 63 |  |  |  |  |  |  | input. It is also possible to generate a new random value for the same | 
| 64 |  |  |  |  |  |  | word sense pair every time. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =head2 Methods | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =over | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =cut | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 1 |  |  | 1 |  | 1610 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 1 |  |  | 1 |  | 29 | use WordNet::Similarity; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | @ISA = qw(WordNet::Similarity); | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | %EXPORT_TAGS = (); | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | @EXPORT_OK = (); | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | @EXPORT = (); | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | $VERSION = '2.04'; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | WordNet::Similarity::addConfigOption ('maxrand', 1, 'f', 1.0); | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =item $random->setPosList() | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | This method is internally called to determine the parts of speech | 
| 93 |  |  |  |  |  |  | this measure is capable of dealing with. | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | Parameters: none. | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | Returns: none. | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =cut | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub setPosList | 
| 102 |  |  |  |  |  |  | { | 
| 103 |  |  |  |  |  |  | my $self = shift; | 
| 104 |  |  |  |  |  |  | $self->{n} = 1; | 
| 105 |  |  |  |  |  |  | $self->{v} = 1; | 
| 106 |  |  |  |  |  |  | $self->{a} = 1; | 
| 107 |  |  |  |  |  |  | $self->{r} = 1; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =item $random->initialize($file) | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | Overrides the initialize method in the parent class. This method | 
| 113 |  |  |  |  |  |  | essentially initializes the measure for use. | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Parameters: $file -- configuration file. | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | Returns: none. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =cut | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | # Initialization of the WordNet::Similarity::random object... parses the config file and sets up | 
| 122 |  |  |  |  |  |  | # global variables, or sets them to default values. | 
| 123 |  |  |  |  |  |  | # INPUT PARAMS  : $paramFile .. File containing the module specific params. | 
| 124 |  |  |  |  |  |  | # RETURN VALUES : (none) | 
| 125 |  |  |  |  |  |  | sub initialize | 
| 126 |  |  |  |  |  |  | { | 
| 127 |  |  |  |  |  |  | my $self = shift; | 
| 128 |  |  |  |  |  |  | $self->SUPER::initialize (@_); | 
| 129 |  |  |  |  |  |  | $self->{maxCacheSize} = WordNet::Similarity::UNLIMITED_CACHE; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =item $random->getRelatedness ($synset1, $synset2) | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | Returns a value for the relatedness of the two synsets.  This value is | 
| 135 |  |  |  |  |  |  | a random number greater-than or equal-to zero and less-than 'maxrand'. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | If the synsets are not properly formed word#pos#sense strings, or if they | 
| 138 |  |  |  |  |  |  | are not found in WordNet, then the error level will be set to non-zero and | 
| 139 |  |  |  |  |  |  | an error string will be generated. | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =cut | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | sub getRelatedness | 
| 144 |  |  |  |  |  |  | { | 
| 145 |  |  |  |  |  |  | my $self = shift; | 
| 146 |  |  |  |  |  |  | my $wps1 = shift; | 
| 147 |  |  |  |  |  |  | my $wps2 = shift; | 
| 148 |  |  |  |  |  |  | my $wn = $self->{wn}; | 
| 149 |  |  |  |  |  |  | my $class = ref $self || $self; | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | # check if the synsets are well-formed and are found in WordNet | 
| 152 |  |  |  |  |  |  | my $ref = $self->parseWps ($wps1, $wps2); | 
| 153 |  |  |  |  |  |  | ref $ref or return $ref; | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | # Initialize traces. | 
| 156 |  |  |  |  |  |  | $self->{traceString} = ""; | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | # Now check if the similarity value for these two synsets is in | 
| 159 |  |  |  |  |  |  | # fact in the cache... if so return the cached value. | 
| 160 |  |  |  |  |  |  | my $relatedness = | 
| 161 |  |  |  |  |  |  | $self->{doCache} ? $self->fetchFromCache ($wps1, $wps2) : undef; | 
| 162 |  |  |  |  |  |  | defined $relatedness and return $relatedness; | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | # Now get down to really finding the relatedness of these two. | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | my $score = rand ($self->{maxrand}); | 
| 167 |  |  |  |  |  |  | $score = sprintf ("%.3f", $score); | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | $self->{doCache} and $self->storeToCache ($wps1, $wps2, $score); | 
| 170 |  |  |  |  |  |  | return $score; | 
| 171 |  |  |  |  |  |  | } | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =item $random->traceOptions() | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | This method is internally called to determine the extra options | 
| 176 |  |  |  |  |  |  | specified by this measure (apart from the default options specified | 
| 177 |  |  |  |  |  |  | in the WordNet::Similarity base class). | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | Parameters: none. | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | Returns: none. | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =cut | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | # 12/5/03 JM (#1) | 
| 186 |  |  |  |  |  |  | # show all config options specific to this module | 
| 187 |  |  |  |  |  |  | sub traceOptions { | 
| 188 |  |  |  |  |  |  | my $self = shift; | 
| 189 |  |  |  |  |  |  | $self->{traceString} .= "maxrand :: $self->{maxrand}\n"; | 
| 190 |  |  |  |  |  |  | $self->SUPER::traceOptions(); | 
| 191 |  |  |  |  |  |  | } | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | 1; | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | __END__ |