| 1 | package WordNet::Similarity::Visual::SimilarityInterface; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | WordNet::Similarity::Visual::SimilarityInterface | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | =head2 Basic Usage Example | ||
| 10 | |||
| 11 | use WordNet::Similarity::Visual::SimilarityInterface; | ||
| 12 | |||
| 13 | my $similarity = WordNet::Similarity::Visual::SimilarityInterface->new; | ||
| 14 | |||
| 15 | $similarity->initialize; | ||
| 16 | |||
| 17 | my ($result,$errors,$traces) = $similarity->compute_similarity($word1,$word2,$measure_index); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This package provides an interface to WordNet::Similarity. It also converts the | ||
| 22 | trace string to the meta-language. | ||
| 23 | |||
| 24 | =head2 Methods | ||
| 25 | |||
| 26 | The following methods are defined in this package: | ||
| 27 | |||
| 28 | =head3 Public methods | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | use 5.008004; | ||
| 35 | use strict; | ||
| 36 | use warnings; | ||
| 37 | our $VERSION = '0.07'; | ||
| 38 | use Gtk2 '-init'; | ||
| 39 | use Gnome2; | ||
| 40 | use WordNet::QueryData; | ||
| 41 | use WordNet::Similarity; | ||
| 42 | use WordNet::Similarity::path; | ||
| 43 | use WordNet::Similarity::hso; | ||
| 44 | use WordNet::Similarity::lesk; | ||
| 45 | use WordNet::Similarity::lin; | ||
| 46 | use WordNet::Similarity::random; | ||
| 47 | use WordNet::Similarity::wup; | ||
| 48 | use WordNet::Similarity::jcn; | ||
| 49 | use WordNet::Similarity::res; | ||
| 50 | use WordNet::Similarity::vector_pairs; | ||
| 51 | use WordNet::Similarity::lch; | ||
| 52 | use constant TRUE => 1; | ||
| 53 | use constant FALSE => 0; | ||
| 54 | my $STOPPED; | ||
| 55 | my %measure; | ||
| 56 | my $wn; | ||
| 57 | use constant CONFIG => $ENV{ HOME }."/.wordnet-similarity"; | ||
| 58 | |||
| 59 | =item $obj->new | ||
| 60 | |||
| 61 | The constructor for WordNet::Similarity::Visual::SimilarityInterface objects. | ||
| 62 | |||
| 63 | Return value: the new blessed object | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub new | ||
| 68 | { | ||
| 69 | my ($class) = @_; | ||
| 70 | my $self = {}; | ||
| 71 | bless $self, $class; | ||
| 72 | } | ||
| 73 | |||
| 74 | =item $obj->initialize | ||
| 75 | |||
| 76 | To initialize WordNet::Similarity. | ||
| 77 | |||
| 78 | Return Value: None | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub initialize | ||
| 83 | { | ||
| 84 | my ($self) = @_; | ||
| 85 | $self->{ wn } = WordNet::QueryData->new; | ||
| 86 | $self->{ measure }{"path"} = WordNet::Similarity::path->new($self->{ wn },CONFIG."/config-path.conf"); | ||
| 87 | $self->{ measure }{"hso"} = WordNet::Similarity::hso->new($self->{ wn },CONFIG."/config-hso.conf"); | ||
| 88 | $self->{ measure }{"lesk"} = WordNet::Similarity::lesk->new($self->{ wn }); | ||
| 89 | $self->{ measure }{"lin"} = WordNet::Similarity::lin->new($self->{ wn }); | ||
| 90 | $self->{ measure }{"random"} = WordNet::Similarity::random->new($self->{ wn }); | ||
| 91 | $self->{ measure }{"wup"} = WordNet::Similarity::wup->new($self->{ wn },CONFIG."/config-wup.conf"); | ||
| 92 | $self->{ measure }{"jcn"} = WordNet::Similarity::jcn->new($self->{ wn }); | ||
| 93 | $self->{ measure }{"res"} = WordNet::Similarity::res->new($self->{ wn }); | ||
| 94 | $self->{ measure }{"vector_pairs"} = WordNet::Similarity::vector_pairs->new($self->{ wn }); | ||
| 95 | $self->{ measure }{"lch"} = WordNet::Similarity::lch->new($self->{ wn },CONFIG."/config-lch.conf"); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | =item $obj->compute_similarity | ||
| 100 | |||
| 101 | Computes the similarity and relatedness scores for two words. | ||
| 102 | |||
| 103 | Parameter: Two Words and the Measure Index | ||
| 104 | "hso","lch","lesk","lin","jcn","path","random","res","vector_pairs","wup" | ||
| 105 | The measure index can have any of the following values | ||
| 106 | - 0 for "all measures" | ||
| 107 | |||
| 108 | - 1 for "Hirst & St-Onge" | ||
| 109 | |||
| 110 | - 2 for "Leacock and Chodorow" | ||
| 111 | |||
| 112 | - 3 for "Adapted Lesk" | ||
| 113 | |||
| 114 | - 4 for "Lin" | ||
| 115 | |||
| 116 | - 5 for "Jiang & Conrath" | ||
| 117 | |||
| 118 | - 6 for "Path Length" | ||
| 119 | |||
| 120 | - 7 for "Random" | ||
| 121 | |||
| 122 | - 8 for "Resnik" | ||
| 123 | |||
| 124 | - 9 for "Vector Pair" | ||
| 125 | |||
| 126 | - 10 for "Wu and Palmer" | ||
| 127 | |||
| 128 | Returns: Reference to Hashes containining | ||
| 129 | |||
| 130 | - semantic relatedness/similarity values for all the word senses combination and measures, | ||
| 131 | - errorStrings for the word senses and measure which did not return a similarity value | ||
| 132 | - TraceString for all the measures that had trace output on | ||
| 133 | |||
| 134 | =over | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub compute_similarity | ||
| 141 | { | ||
| 142 | my ($self,$word1, $word2, $measure_index) = @_; | ||
| 143 | my @allmeasures = ("hso","lch","lesk","lin","jcn","path","random","res","vector_pairs","wup"); | ||
| 144 | my @word1senses=[]; | ||
| 145 | my @word2senses=[]; | ||
| 146 | if ($self->{ STOPPED }==0) | ||
| 147 | { | ||
| 148 | @word1senses = $self->find_allsenses($word1); | ||
| 149 | } | ||
| 150 | if($self->{ STOPPED }==0) | ||
| 151 | { | ||
| 152 | @word2senses = $self->find_allsenses($word2); | ||
| 153 | } | ||
| 154 | my $measurename = $allmeasures[$measure_index-1]; | ||
| 155 | my $word1sense; | ||
| 156 | my $word2sense; | ||
| 157 | my %values=(); | ||
| 158 | my %errors=(); | ||
| 159 | my %measure=(); | ||
| 160 | my %traces=(); | ||
| 161 | my $module; | ||
| 162 | my $value; | ||
| 163 | foreach $word1sense (@word1senses) | ||
| 164 | { | ||
| 165 | while (Gtk2->events_pending) | ||
| 166 | { | ||
| 167 | Gtk2->main_iteration; | ||
| 168 | } | ||
| 169 | foreach $word2sense (@word2senses) | ||
| 170 | { | ||
| 171 | while (Gtk2->events_pending) | ||
| 172 | { | ||
| 173 | Gtk2->main_iteration; | ||
| 174 | } | ||
| 175 | if($self->{ STOPPED }==0) | ||
| 176 | { | ||
| 177 | if($measure_index != 0) | ||
| 178 | { | ||
| 179 | if($self->{ STOPPED } == 0) | ||
| 180 | { | ||
| 181 | $value=$self->{ measure }{$allmeasures[$measure_index-1]}->getRelatedness($word1sense,$word2sense); | ||
| 182 | my ($error, $errorString) = $self->{ measure }{$allmeasures[$measure_index-1]}->getError(); | ||
| 183 | if($error) | ||
| 184 | { | ||
| 185 | $values{$word1sense}{$word2sense}=-1; | ||
| 186 | $errors{$word1sense}{$word2sense}=$errorString; | ||
| 187 | } | ||
| 188 | else | ||
| 189 | { | ||
| 190 | $values{$word1sense}{$word2sense}=$value; | ||
| 191 | $traces{$word1sense}{$word2sense}{$allmeasures[$measure_index-1]}=$self->{ measure }{$allmeasures[$measure_index-1]}->getTraceString; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | else | ||
| 196 | { | ||
| 197 | foreach $module (@allmeasures) | ||
| 198 | { | ||
| 199 | if($self->{ STOPPED } == 0) | ||
| 200 | { | ||
| 201 | $value=$self->{ measure }{$module}->getRelatedness($word1sense,$word2sense); | ||
| 202 | my ($error, $errorString) = $self->{ measure }{$module}->getError(); | ||
| 203 | if($error) | ||
| 204 | { | ||
| 205 | $values{$word1sense}{$word2sense}{$module}=-1; | ||
| 206 | $errors{$word1sense}{$word2sense}{$module}=$errorString; | ||
| 207 | } | ||
| 208 | else | ||
| 209 | { | ||
| 210 | $values{$word1sense}{$word2sense}{$module}=$value; | ||
| 211 | $traces{$word1sense}{$word2sense}{$module}=$self->{ measure }{$module}->getTraceString; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | return (\%values, \%errors,\%traces); | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | sub find_allsenses | ||
| 229 | { | ||
| 230 | my ($self, $word)=@_; | ||
| 231 | my @temp = split '#',$word; | ||
| 232 | my $wordlevel = $#temp+1; | ||
| 233 | my $pos; | ||
| 234 | my @wordsenses = (); | ||
| 235 | my @wordsense; | ||
| 236 | if($wordlevel==1) | ||
| 237 | { | ||
| 238 | @temp=$self->{ wn }->queryWord($word); | ||
| 239 | foreach $pos (@temp) | ||
| 240 | { | ||
| 241 | @wordsense=$self->{ wn }->queryWord($pos); | ||
| 242 | push (@wordsenses, @wordsense); | ||
| 243 | @wordsense = (); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | elsif($wordlevel==2) | ||
| 247 | { | ||
| 248 | @wordsenses = $self->{ wn }->queryWord($word); | ||
| 249 | } | ||
| 250 | else | ||
| 251 | { | ||
| 252 | $wordsenses[0]=$word | ||
| 253 | } | ||
| 254 | return @wordsenses; | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | =item $obj->convert_to_meta | ||
| 259 | |||
| 260 | Converts the Trace String to Meta-language. | ||
| 261 | |||
| 262 | Parameter: The two Word senses, Trace String and the Measure name | ||
| 263 | |||
| 264 | Returns: A String, the equivalent metalanguage for the trace string. | ||
| 265 | |||
| 266 | =over | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | |||
| 271 | sub convert_to_meta | ||
| 272 | { | ||
| 273 | my ($self,$word1, $word2, $tracestring, $measure) = @_; | ||
| 274 | my @trace= split "\n", $tracestring; | ||
| 275 | my $length = $#trace; | ||
| 276 | my $i; | ||
| 277 | my %uniquepaths; | ||
| 278 | my $path; | ||
| 279 | my $w2tree; | ||
| 280 | my @synsets = (); | ||
| 281 | my $synset; | ||
| 282 | my %lcs_path; | ||
| 283 | my %tree; | ||
| 284 | my $wtree; | ||
| 285 | my $alt_path; | ||
| 286 | my %alt_paths; | ||
| 287 | my %allpaths; | ||
| 288 | my $maxdepth=0; | ||
| 289 | my $trace_return; | ||
| 290 | if($measure =~ /path/) | ||
| 291 | { | ||
| 292 | my @paths = grep /Shortest path/, @trace; | ||
| 293 | my @pathlengths = grep /Path length/, @trace; | ||
| 294 | my @hypertrees = grep /HyperTree/, @trace; | ||
| 295 | my $pathlength = $pathlengths[0]; | ||
| 296 | foreach $i (0...$#hypertrees) | ||
| 297 | { | ||
| 298 | if (length($hypertrees[$i])>$maxdepth) | ||
| 299 | { | ||
| 300 | $maxdepth = length($hypertrees[$i]); | ||
| 301 | } | ||
| 302 | $hypertrees[$i]=~ s/\*Root\*/Root/; | ||
| 303 | $hypertrees[$i]=~ s/HyperTree: //; | ||
| 304 | } | ||
| 305 | foreach $path (@paths) | ||
| 306 | { | ||
| 307 | $path=~ s/\*Root\*/Root/; | ||
| 308 | $path =~ s/Shortest path: //; | ||
| 309 | if(length($path)>0) | ||
| 310 | { | ||
| 311 | $uniquepaths{$path}=1; | ||
| 312 | $allpaths{$path}=1; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | my @syns1; | ||
| 316 | my @syns2; | ||
| 317 | my $syn; | ||
| 318 | my $all_lcs=""; | ||
| 319 | my @word1tree; | ||
| 320 | @syns1 = $self->{ wn }->querySense($word1,"syns"); | ||
| 321 | foreach $syn (@syns1) | ||
| 322 | { | ||
| 323 | push @word1tree, grep(/\b$syn\b/, @hypertrees); | ||
| 324 | } | ||
| 325 | my @word2tree; | ||
| 326 | @syns2 = $self->{ wn }->querySense($word2,"syns"); | ||
| 327 | foreach $syn (@syns2) | ||
| 328 | { | ||
| 329 | push @word2tree, grep(/\b$syn\b/, @hypertrees); | ||
| 330 | } | ||
| 331 | if($#word2tree == $#hypertrees) | ||
| 332 | { | ||
| 333 | @word2tree = (); | ||
| 334 | foreach $syn (@syns1) | ||
| 335 | { | ||
| 336 | push @word2tree, grep(!/\b$syn\b/, @hypertrees); | ||
| 337 | } | ||
| 338 | } | ||
| 339 | if($#word1tree == $#hypertrees) | ||
| 340 | { | ||
| 341 | @word1tree = (); | ||
| 342 | foreach $syn (@syns2) | ||
| 343 | { | ||
| 344 | push @word1tree, grep(!/\b$syn\b/, @hypertrees); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | @pathlengths = (); | ||
| 348 | @trace=(); | ||
| 349 | foreach $path (keys %uniquepaths) | ||
| 350 | { | ||
| 351 | @synsets=split " ", $path; | ||
| 352 | PATH: foreach $w2tree (@word2tree) | ||
| 353 | { | ||
| 354 | foreach $synset (@synsets) | ||
| 355 | { | ||
| 356 | if($w2tree=~/$synset/) | ||
| 357 | { | ||
| 358 | $lcs_path{$path}{$synset}=1; | ||
| 359 | if(length($all_lcs) > 0) | ||
| 360 | { | ||
| 361 | $all_lcs = $synset." ".$all_lcs; | ||
| 362 | } | ||
| 363 | else | ||
| 364 | { | ||
| 365 | $all_lcs = $synset | ||
| 366 | } | ||
| 367 | last PATH; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | } | ||
| 371 | } | ||
| 372 | my %lcs_root_paths = (); | ||
| 373 | my $lcs_root; | ||
| 374 | foreach $path (@hypertrees) | ||
| 375 | { | ||
| 376 | $lcs_root=''; | ||
| 377 | @synsets=split " ", $path; | ||
| 378 | foreach $synset (@synsets) | ||
| 379 | { | ||
| 380 | if(length($lcs_root)!=0 ) | ||
| 381 | { | ||
| 382 | $lcs_root=$synset." hypernym ".$lcs_root; | ||
| 383 | } | ||
| 384 | else | ||
| 385 | { | ||
| 386 | $lcs_root = $synset; | ||
| 387 | } | ||
| 388 | if($all_lcs=~/$synset/) | ||
| 389 | { | ||
| 390 | last; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | $lcs_root_paths{$lcs_root}++; | ||
| 394 | $allpaths{$lcs_root}=1; | ||
| 395 | $alt_paths{$lcs_root}=1; | ||
| 396 | } | ||
| 397 | my %w2_paths=(); | ||
| 398 | my $w2_path; | ||
| 399 | foreach $path (keys %uniquepaths) | ||
| 400 | { | ||
| 401 | $w2_path=''; | ||
| 402 | @synsets=split " ", $path; | ||
| 403 | foreach $synset (reverse @synsets) | ||
| 404 | { | ||
| 405 | if(length($w2_path)!=0 ) | ||
| 406 | { | ||
| 407 | $w2_path=$w2_path." hypernym ".$synset; | ||
| 408 | } | ||
| 409 | else | ||
| 410 | { | ||
| 411 | $w2_path = $synset; | ||
| 412 | } | ||
| 413 | if(exists $lcs_path{$path}{$synset}) | ||
| 414 | { | ||
| 415 | last; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | $w2_paths{$w2_path}++; | ||
| 419 | } | ||
| 420 | my %w1_paths=(); | ||
| 421 | my $w1_path; | ||
| 422 | foreach $path (keys %uniquepaths) | ||
| 423 | { | ||
| 424 | $w1_path=''; | ||
| 425 | @synsets=split " ", $path; | ||
| 426 | foreach $synset (@synsets) | ||
| 427 | { | ||
| 428 | if(length($w1_path)!=0 ) | ||
| 429 | { | ||
| 430 | $w1_path=$w1_path." hypernym ".$synset; | ||
| 431 | } | ||
| 432 | else | ||
| 433 | { | ||
| 434 | $w1_path = $synset; | ||
| 435 | } | ||
| 436 | if(exists $lcs_path{$path}{$synset}) | ||
| 437 | { | ||
| 438 | $all_lcs = $synset; | ||
| 439 | last; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | $w1_paths{$w1_path}++; | ||
| 443 | } | ||
| 444 | # my $flag=1; | ||
| 445 | # my $flag2=0; | ||
| 446 | # foreach $wtree (@hypertrees) | ||
| 447 | # { | ||
| 448 | # @synsets = split " ", $wtree; | ||
| 449 | # foreach $i (reverse 0...$#synsets) | ||
| 450 | # { | ||
| 451 | # $flag=1; | ||
| 452 | # foreach $path (keys %allpaths) | ||
| 453 | # { | ||
| 454 | # if($path=~/\b$synsets[$i]\b/) | ||
| 455 | # { | ||
| 456 | # $flag=0; | ||
| 457 | # last; | ||
| 458 | # } | ||
| 459 | # } | ||
| 460 | # if ($flag==1) | ||
| 461 | # { | ||
| 462 | # if($flag2==1) | ||
| 463 | # { | ||
| 464 | # $alt_path=$alt_path." hypernym ".$synsets[$i]; | ||
| 465 | # } | ||
| 466 | # else | ||
| 467 | # { | ||
| 468 | # $flag2=1; | ||
| 469 | # $alt_path = $synsets[$i+1]." hypernym ".$synsets[$i]; | ||
| 470 | # } | ||
| 471 | # } | ||
| 472 | # elsif($flag2==1) | ||
| 473 | # { | ||
| 474 | # $flag2=0; | ||
| 475 | # $alt_path=$alt_path." hypernym ".$synsets[$i]; | ||
| 476 | # $alt_paths{$alt_path}=1; | ||
| 477 | # $allpaths{$alt_path}=1; | ||
| 478 | # $alt_path=''; | ||
| 479 | # } | ||
| 480 | # } | ||
| 481 | # if($flag2==1) | ||
| 482 | # { | ||
| 483 | # $flag2=0; | ||
| 484 | # $alt_paths{$alt_path}=1; | ||
| 485 | # $allpaths{$alt_path}=1; | ||
| 486 | # $alt_path=''; | ||
| 487 | # } | ||
| 488 | # } | ||
| 489 | my $key; | ||
| 490 | $trace_return=$measure."\n"; | ||
| 491 | my $j=0; | ||
| 492 | foreach $key (keys %w1_paths) | ||
| 493 | { | ||
| 494 | if($j==0) | ||
| 495 | { | ||
| 496 | $trace_return=$trace_return.$key; | ||
| 497 | } | ||
| 498 | else | ||
| 499 | { | ||
| 500 | $trace_return=$trace_return." OR ".$key; | ||
| 501 | } | ||
| 502 | $j++; | ||
| 503 | } | ||
| 504 | $trace_return=$trace_return."\n"; | ||
| 505 | $j=0; | ||
| 506 | foreach $key (keys %w2_paths) | ||
| 507 | { | ||
| 508 | if($j==0) | ||
| 509 | { | ||
| 510 | $trace_return=$trace_return.$key; | ||
| 511 | } | ||
| 512 | else | ||
| 513 | { | ||
| 514 | $trace_return=$trace_return." OR ".$key; | ||
| 515 | } | ||
| 516 | $j++; | ||
| 517 | } | ||
| 518 | $trace_return=$trace_return."\n"; | ||
| 519 | foreach $key (keys %alt_paths) | ||
| 520 | { | ||
| 521 | if($key =~ /$all_lcs/) | ||
| 522 | { | ||
| 523 | $trace_return=$trace_return.$key."\n"; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | $trace_return=$trace_return."Max Depth = ".$maxdepth."\n"; | ||
| 527 | $trace_return=$trace_return.$pathlength."\n"; | ||
| 528 | } | ||
| 529 | elsif($measure=~/wup/) | ||
| 530 | { | ||
| 531 | my @depth = grep /Depth/, @trace; | ||
| 532 | my @word1_depth; | ||
| 533 | my $syn; | ||
| 534 | my @syns1; | ||
| 535 | my @syns2; | ||
| 536 | @syns1 = $self->{ wn }->querySense($word1,"syns"); | ||
| 537 | foreach $syn (@syns1) | ||
| 538 | { | ||
| 539 | push @word1_depth, grep /$syn/, @depth; | ||
| 540 | } | ||
| 541 | my @word2_depth; | ||
| 542 | @syns2 = $self->{ wn }->querySense($word2,"syns"); | ||
| 543 | foreach $syn (@syns2) | ||
| 544 | { | ||
| 545 | push @word2_depth, grep /$syn/, @depth; | ||
| 546 | } | ||
| 547 | my @lcs_depth = grep /Lowest\sCommon\sSubsumers/, @depth; | ||
| 548 | my @hypertrees = grep /HyperTree/, @trace; | ||
| 549 | foreach $i (0...$#hypertrees) | ||
| 550 | { | ||
| 551 | if (length($hypertrees[$i])>$maxdepth) | ||
| 552 | { | ||
| 553 | $maxdepth = length($hypertrees[$i]); | ||
| 554 | } | ||
| 555 | $hypertrees[$i]=~ s/\*Root\*/Root/; | ||
| 556 | $hypertrees[$i]=~ s/HyperTree: //; | ||
| 557 | } | ||
| 558 | my $w_tree; | ||
| 559 | my $tree; | ||
| 560 | my %trace_trees; | ||
| 561 | foreach $w_tree (@hypertrees) | ||
| 562 | { | ||
| 563 | $tree=''; | ||
| 564 | @synsets=split " ", $w_tree; | ||
| 565 | foreach $synset (reverse @synsets) | ||
| 566 | { | ||
| 567 | if(length($tree)!=0 ) | ||
| 568 | { | ||
| 569 | $tree=$tree." hypernym ".$synset; | ||
| 570 | } | ||
| 571 | else | ||
| 572 | { | ||
| 573 | $tree = $synset; | ||
| 574 | } | ||
| 575 | } | ||
| 576 | $trace_trees{$tree}++; | ||
| 577 | } | ||
| 578 | my $lcs = $lcs_depth[0]; | ||
| 579 | my $key; | ||
| 580 | my $temptree = join " ", @hypertrees; | ||
| 581 | $lcs =~ s/Lowest\sCommon\sSubsumers:\s//; | ||
| 582 | $lcs =~ s/\*Root\*/Root/; | ||
| 583 | $lcs =~ s/\s\(Depth=\d\)//; | ||
| 584 | my $depth_word1; | ||
| 585 | my $depth_word2; | ||
| 586 | my @temp; | ||
| 587 | if($#word1_depth>0) | ||
| 588 | { | ||
| 589 | @temp = split /=/,$word1_depth[0]; | ||
| 590 | $depth_word1 = $word1." = ".$temp[1]; | ||
| 591 | } | ||
| 592 | else | ||
| 593 | { | ||
| 594 | foreach $syn (@syns1) | ||
| 595 | { | ||
| 596 | if($temptree =~ /$syn/) | ||
| 597 | { | ||
| 598 | $depth_word1=$syn; | ||
| 599 | last; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | } | ||
| 603 | if($#word2_depth>0) | ||
| 604 | { | ||
| 605 | @temp = split /=/,$word2_depth[0]; | ||
| 606 | $depth_word2 = $word2." = ".$temp[1]; | ||
| 607 | } | ||
| 608 | else | ||
| 609 | { | ||
| 610 | foreach $syn (@syns2) | ||
| 611 | { | ||
| 612 | if($temptree=~/$syn/) | ||
| 613 | { | ||
| 614 | $depth_word2=$syn; | ||
| 615 | last; | ||
| 616 | } | ||
| 617 | } | ||
| 618 | } | ||
| 619 | @temp = split /=/,$lcs_depth[0]; | ||
| 620 | $temp[1] =~ s/\)//; | ||
| 621 | $lcs = $lcs." = ".$temp[1]; | ||
| 622 | $trace_return = $measure."\n"; | ||
| 623 | foreach $key (keys %trace_trees) | ||
| 624 | { | ||
| 625 | $trace_return=$trace_return.$key."\n"; | ||
| 626 | } | ||
| 627 | $trace_return=$trace_return.$depth_word1."\n"; | ||
| 628 | $trace_return=$trace_return.$depth_word2."\n"; | ||
| 629 | $trace_return=$trace_return.$lcs; | ||
| 630 | } | ||
| 631 | elsif($measure=~/lch/) | ||
| 632 | { | ||
| 633 | @trace = split /Lowest\sCommon\sSubsumer\(s\):\s/, $tracestring; | ||
| 634 | my @hypertrees = split /\n/, $trace[0]; | ||
| 635 | my @lcs_temp = split /\n/, $trace[1]; | ||
| 636 | my $key; | ||
| 637 | my @lcs_split; | ||
| 638 | my %lcs; | ||
| 639 | foreach $key (@lcs_temp) | ||
| 640 | { | ||
| 641 | @lcs_split = split " ",$key; | ||
| 642 | $lcs{$lcs_split[0]}=1; | ||
| 643 | } | ||
| 644 | foreach $i (0...$#hypertrees) | ||
| 645 | { | ||
| 646 | if (length($hypertrees[$i])>$maxdepth) | ||
| 647 | { | ||
| 648 | $maxdepth = length($hypertrees[$i]); | ||
| 649 | } | ||
| 650 | $hypertrees[$i]=~ s/\*Root\*/Root/; | ||
| 651 | $hypertrees[$i]=~ s/HyperTree: //; | ||
| 652 | } | ||
| 653 | my @syns1; | ||
| 654 | my @syns2; | ||
| 655 | my $syn; | ||
| 656 | my @word1tree; | ||
| 657 | @syns1 = $self->{ wn }->querySense($word1,"syns"); | ||
| 658 | foreach $syn (@syns1) | ||
| 659 | { | ||
| 660 | push @word1tree, grep(/\b$syn\b/, @hypertrees); | ||
| 661 | } | ||
| 662 | my @word2tree; | ||
| 663 | @syns2 = $self->{ wn }->querySense($word2,"syns"); | ||
| 664 | foreach $syn (@syns2) | ||
| 665 | { | ||
| 666 | push @word2tree, grep(/\b$syn\b/, @hypertrees); | ||
| 667 | } | ||
| 668 | if($#word1tree == $#hypertrees) | ||
| 669 | { | ||
| 670 | @word1tree = (); | ||
| 671 | foreach $syn (@syns1) | ||
| 672 | { | ||
| 673 | push @word1tree, grep(!/\b$syn\b/, @hypertrees); | ||
| 674 | } | ||
| 675 | } | ||
| 676 | if($#word2tree == $#hypertrees) | ||
| 677 | { | ||
| 678 | @word2tree = (); | ||
| 679 | foreach $syn (@syns2) | ||
| 680 | { | ||
| 681 | push @word2tree, grep(!/\b$syn\b/, @hypertrees); | ||
| 682 | } | ||
| 683 | } | ||
| 684 | my %w1_paths=(); | ||
| 685 | my $w1_path; | ||
| 686 | my $j=0; | ||
| 687 | my $all_lcs=""; | ||
| 688 | foreach $path (@word1tree) | ||
| 689 | { | ||
| 690 | $w1_path=''; | ||
| 691 | $j=0; | ||
| 692 | @synsets=split " ", $path; | ||
| 693 | foreach $synset (reverse @synsets) | ||
| 694 | { | ||
| 695 | $j++; | ||
| 696 | if(length($w1_path)!=0 ) | ||
| 697 | { | ||
| 698 | $w1_path=$w1_path." hypernym ".$synset; | ||
| 699 | } | ||
| 700 | else | ||
| 701 | { | ||
| 702 | $w1_path = $synset; | ||
| 703 | } | ||
| 704 | if(exists $lcs{$synset}) | ||
| 705 | { | ||
| 706 | $all_lcs=$synset; | ||
| 707 | last; | ||
| 708 | } | ||
| 709 | } | ||
| 710 | if (length($all_lcs)>0) | ||
| 711 | { | ||
| 712 | $w1_paths{$all_lcs}{$w1_path}=$j; | ||
| 713 | } | ||
| 714 | else | ||
| 715 | { | ||
| 716 | $w1_paths{"Root#n#1"}{$w1_path}=$j; | ||
| 717 | } | ||
| 718 | } | ||
| 719 | my %w2_paths=(); | ||
| 720 | my $w2_path; | ||
| 721 | $j=0; | ||
| 722 | foreach $path (@word2tree) | ||
| 723 | { | ||
| 724 | $w2_path=''; | ||
| 725 | $j=0; | ||
| 726 | @synsets=split " ", $path; | ||
| 727 | foreach $synset (reverse @synsets) | ||
| 728 | { | ||
| 729 | $j++; | ||
| 730 | if(length($w2_path)!=0 ) | ||
| 731 | { | ||
| 732 | $w2_path=$w2_path." hypernym ".$synset; | ||
| 733 | } | ||
| 734 | else | ||
| 735 | { | ||
| 736 | $w2_path = $synset; | ||
| 737 | } | ||
| 738 | if(exists $lcs{$synset}) | ||
| 739 | { | ||
| 740 | $all_lcs=$synset; | ||
| 741 | last; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | if (length($all_lcs)>0) | ||
| 745 | { | ||
| 746 | $w2_paths{$all_lcs}{$w2_path}=$j; | ||
| 747 | } | ||
| 748 | else | ||
| 749 | { | ||
| 750 | $w2_paths{"Root#n#1"}{$w2_path}=$j; | ||
| 751 | } | ||
| 752 | } | ||
| 753 | my $lcs_synset; | ||
| 754 | my $length = 100; | ||
| 755 | foreach $lcs_synset (keys %w1_paths) | ||
| 756 | { | ||
| 757 | foreach $w1_path (keys %{$w1_paths{$lcs_synset}}) | ||
| 758 | { | ||
| 759 | foreach $w2_path (keys %{$w2_paths{$lcs_synset}}) | ||
| 760 | { | ||
| 761 | if($length > $w1_paths{$lcs_synset}{$w1_path}+$w2_paths{$lcs_synset}{$w2_path}) | ||
| 762 | { | ||
| 763 | $path = $w1_path."\n".$w2_path; | ||
| 764 | $length = $w1_paths{$lcs_synset}{$w1_path}+$w2_paths{$lcs_synset}{$w2_path}; | ||
| 765 | } | ||
| 766 | elsif($length == $w1_paths{$lcs_synset}{$w1_path}+$w2_paths{$lcs_synset}{$w2_path}) | ||
| 767 | { | ||
| 768 | $path = $path."\n".$w1_path."\n".$w2_path; | ||
| 769 | } | ||
| 770 | } | ||
| 771 | } | ||
| 772 | } | ||
| 773 | |||
| 774 | # my $length = 100; | ||
| 775 | # foreach $w1_path (keys %w1_paths) | ||
| 776 | # { | ||
| 777 | # foreach $w2_path (keys %w2_paths) | ||
| 778 | # { | ||
| 779 | # if($length > $w1_paths{$w1_path}+$w2_paths{$w2_path}) | ||
| 780 | # { | ||
| 781 | # $path = $w1_path."\n".$w2_path; | ||
| 782 | # $length = $w1_paths{$w1_path}+$w2_paths{$w2_path}; | ||
| 783 | # } | ||
| 784 | # elsif($length == $w1_paths{$w1_path}+$w2_paths{$w2_path}) | ||
| 785 | # { | ||
| 786 | # $path = $path."\n".$w1_path."\n".$w2_path; | ||
| 787 | # } | ||
| 788 | # } | ||
| 789 | # } | ||
| 790 | $trace_return = $measure."\n"; | ||
| 791 | $length--; | ||
| 792 | $trace_return = $trace_return.$path."\n".$length; | ||
| 793 | return $trace_return; | ||
| 794 | } | ||
| 795 | elsif ($measure =~ /hso/) | ||
| 796 | { | ||
| 797 | my $trace_return = "hso"; | ||
| 798 | if( $tracestring =~ /MedStrong\srelation\spath\.\.\./) | ||
| 799 | { | ||
| 800 | $tracestring =~ s/\n//g; | ||
| 801 | @trace = split /MedStrong\srelation\spath\.\.\./, $tracestring; | ||
| 802 | my @path; | ||
| 803 | foreach $i(1...$#trace) | ||
| 804 | { | ||
| 805 | chomp $trace[$i]; | ||
| 806 | $path[$i-1] = $trace[$i]; | ||
| 807 | $path[$i-1] =~ s/\[U\]/hyponym/g; | ||
| 808 | $path[$i-1] =~ s/\[D\]/hypernym/g; | ||
| 809 | $path[$i-1] =~ s/\[H\]/merynym/g; | ||
| 810 | $trace_return = $trace_return."\n".$path[$i-1]; | ||
| 811 | } | ||
| 812 | } | ||
| 813 | elsif($tracestring=~/Strong\sRel\s\(Synset\sMatch\)/) | ||
| 814 | { | ||
| 815 | $tracestring =~ s/\n//g; | ||
| 816 | @trace = split /Strong\sRel\s\(Synset\sMatch\)\s:\s/, $tracestring; | ||
| 817 | $trace_return = $trace_return."\n".$trace[1]; | ||
| 818 | } | ||
| 819 | else | ||
| 820 | { | ||
| 821 | $trace_return = $trace_return."\n-1"; | ||
| 822 | } | ||
| 823 | return $trace_return; | ||
| 824 | } | ||
| 825 | } | ||
| 826 | 1; | ||
| 827 | __END__ |