blib/lib/UMLS/Similarity/lesk.pm untested

filter: Statementstmt 0.0% 0 / 398 Branchbran 0.0% 0 / 160 Conditioncond 0.0% 0 / 34 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 605 SCAR 88.0
CC
81
Cov
0%
getRelatedness
3660.0
new
240.0
findCompoundWord
72.0
CRAP 6642.0
1 # UMLS::Similarity::lesk.pm
2 #
3 # Module implementing the semantic relatedness measure described
4 # by Banerjee and Pedersen(2002)
5 #
6 # Copyright (c) 2009-2011,
7 #
8 # Bridget T McInnes, University of Minnesota, Twin Cities
9 # bthomson at umn.edu
10 #
11 # Siddharth Patwardhan, University of Utah, Salt Lake City
12 # sidd at cs.utah.edu
13 #
14 # Serguei Pakhomov, University of Minnesota, Twin Cities
15 # pakh002 at umn.edu
16 #
17 # Ted Pedersen, University of Minnesota, Duluth
18 # tpederse at d.umn.edu
19 #
20 # Ying Liu, University of Minnesota, Twin Cities
21 # liux0935 at umn.edu
22 #
23 # This program is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License
25 # as published by the Free Software Foundation; either version 2
26 # of the License, or (at your option) any later version.
27 #
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to
35 #
36 # The Free Software Foundation, Inc.,
37 # 59 Temple Place - Suite 330,
38 # Boston, MA 02111-1307, USA.
39
40
41 package UMLS::Similarity::lesk;
42
43 use strict;
44 use warnings;
45
46 use UMLS::Similarity;
47 use Text::OverlapFinder;
48 use Lingua::Stem::En;
49 use UMLS::Similarity::ErrorHandler;
50
51 use vars qw($VERSION);
52 $VERSION = '0.07';
53
54 my $compoundfile = "";
55 my $debugfile = "";
56 my $stoplist = "";
57 my $stopregex = "";
58 my $finder = "";
59 my $dictfile = "";
60 my $doubledef = "";
61 my $defraw_option = 0;
62 my $stem = "";
63 my $stemmed_words = "";
64 my $score = 0;
65 my %dictionary = ();
66 my %ddef = ();
67 my %complist = ();
68
69 local(*DEBUG);
70
71 sub new
72 {
73 my $className = shift;
74
75 return undef if(ref $className);
76
77 my $interface = shift;
78 my $params = shift;
79
80 $params = {} if(!defined $params);
81
82 $stoplist = $params->{'stoplist'};
83 $debugfile = $params->{'debugfile'};
84 $dictfile = $params->{'dictfile'};
85 $doubledef = $params->{'doubledef'};
86 $stem = $params->{'stem'};
87 $compoundfile = $params->{'compoundfile'};
88
89 my $defraw = $params->{'defraw'};
90
91 my $self = {};
92
93 # Bless the object.
94 bless($self, $className);
95
96 # set the finder handler
97 $finder = Text::OverlapFinder->new;
98
99 # The backend interface object.
100 $self->{'interface'} = $interface;
101
102 # check the configuration file if defined
103 my $errorhandler = UMLS::Similarity::ErrorHandler->new("lesk", $interface);
104 if(!$errorhandler) {
105 print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n";
106 exit;
107 }
108
109 if(defined $defraw) {
110 $defraw_option = 1;
111 }
112
113 if(defined $debugfile) {
114 if(-e $debugfile) {
115 print "Debug file $debugfile already exists! Overwrite (Y/N)? ";
116 my $reply = <STDIN>;
117 chomp $reply;
118 $reply = uc $reply;
119 exit 0 if ($reply ne "Y");
120 }
121
122 open(DEBUG, ">$debugfile")
123 || die "Could not open debug file: $debugfile\n";
124
125 $debugfile = 1;
126 }
127
128 # Check for stoplist
129 if(defined $stoplist) {
130
131 open(STP, $stoplist) || die "Could not open stoplist: $stoplist\n";
132
133 $stopregex = "(";
134 while(<STP>) {
135 chomp;
136 if ($_ ne ""){
137 $_=~s/\///g;
138 $stopregex .= "$_|";
139 }
140 }
141 chop $stopregex; $stopregex .= ")";
142 close STP;
143
144 }
145
146 # read in the doubledef for --doubledef option
147 if (defined $doubledef) {
148 open(DDEF, "<$doubledef")
149 or die("Error: cannot open doubledef file ($doubledef).\n");
150
151 while(<DDEF>) {
152 chomp;
153 if($_=~/^\s*$/) { next; }
154
155 my @defs = split (":", $_);
156 my $concept = $defs[0];
157 $concept =~ s/^\s+//;
158 $concept =~ s/\s+$//;
159 my $definition = $defs[1];
160 $ddef{$concept} = $definition;
161 }
162 close DDEF;
163 }
164
165 if(defined $dictfile) {
166 open(DICT, "$dictfile")
167 || die("Error: cannot open dictionary file ($dictfile)\n");
168
169 while(<DICT>) {
170 chomp;
171 if($_=~/^\s*$/) { next; }
172
173 my @defs = split(":", $_);
174 my $concept = $defs[0];
175 $concept =~ s/^\s+//;
176 $concept =~ s/\s+$//;
177 my $definition = $defs[1];
178 $dictionary{$concept} = $definition;
179 }
180 close DICT;
181 }
182
183 if(defined $compoundfile) {
184
185 #replace the compound words in the definition
186 open(LST, "$compoundfile") or die ("Error: cannot open file $compoundfile for input.\n");
187
188 # read the compound txt and put them in the hash array.
189 while (my $line = <LST>)
190 {
191 chomp($line);
192 my $lower_case = lc($line);
193 my @string = split('_', $lower_case);
194 my $head = shift(@string);
195
196 my $rest = join (' ', @string);
197 push (@{$complist{$head}}, $rest);
198 }
199 close LST;
200
201 # sort the compound txt
202 foreach my $h (sort (keys (%complist)) )
203 {
204 my @sort_list = sort(@{$complist{$h}});
205 for my $i (0..$#sort_list)
206 {
207 $complist{$h}[$i] = $sort_list[$i];
208 }
209 }
210
211 }
212
213 return $self;
214 }
215
216
217 sub getRelatedness
218 {
219 my $self = shift;
220 return undef if(!defined $self || !ref $self);
221 my $concept1 = shift;
222 my $concept2 = shift;
223
224 if(defined $debugfile) {
225 print DEBUG "$concept1<>$concept2\n";
226 }
227
228 # set up the interface
229 my $interface = $self->{'interface'};
230
231 my $def1 = "";
232 my $def2 = "";
233
234 if(!defined $dictfile) {
235 if($concept1 =~ /C[0-9]+/)
236 {
237 my $defs1 = $interface->getExtendedDefinition($concept1);
238 if(defined $debugfile) {
239 print DEBUG "DEFINITIONS FOR CONCEPT 1: $concept1 \n";
240 }
241 my $i = 1;
242 foreach my $def (@{$defs1}) {
243 if(defined $debugfile) {
244 print DEBUG "$i. $def\n";
245 $i++;
246 }
247 $def=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
248 #$def1 .= $5 . " " . "definitionstop" . " ";
249 my $temp = $5;
250 if(defined $temp) {
251 $def1 .= $temp . " " . "definitionstop" . " ";
252 }
253
254 }
255
256 #if the definition is empty, return -1;
257 if($def1 eq "")
258 {
259 return -1;
260 }
261 }
262 if($concept2 =~ /C[0-9]+/)
263 {
264 my $defs2 = $interface->getExtendedDefinition($concept2);
265 if(defined $debugfile) {
266 print DEBUG "DEFINITIONS FOR CONCEPT 2: $concept2 \n";
267 }
268 my $i = 1;
269 foreach my $def (@{$defs2}) {
270 if(defined $debugfile) {
271 print DEBUG "$i. $def\n";
272 $i++;
273 }
274 $def=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
275 #$def2 .= $5 . " " . "definitionstop" . " ";
276 my $temp = $5;
277 if(defined $temp) {
278 $def2 .= $temp . " " . "definitionstop" . " ";
279 }
280 }
281 #if the definition is empty, return -1;
282 if($def2 eq "")
283 {
284 return -1;
285 }
286 }
287 } # end of WITHOUT --dictfile option
288
289 if(defined $dictfile){
290 my $defs1;
291 my $defs2;
292 my $term1;
293 my $term2;
294 my $term1_def = "";
295 my $term2_def = "";
296
297 my @dictfile_term1;
298 my @dictfile_term2;
299
300 if($concept1 =~ /^(C[0-9]+)(\#)(.*?)$/)
301 {
302 my $cui1 = $1;
303 $term1 = $3;
304
305 $defs1 = $interface->getExtendedDefinition($cui1);
306 $term1_def = $dictionary{$term1} if (defined $dictionary{$term1});
307
308
309 # check the cui's associated term's def in dictfile
310 #@dictfile_term1 = $interface->getTermList($cui1);
311 #foreach my $t (@dictfile_term1)
312 #{
313 #if(defined ($dictionary{$t}))
314 #{
315 #my $term1_def = $dictionary{$t};
316 #$def1 .= "$term1_def" . " ";
317 #}
318 #}
319
320
321 }
322 else
323 {
324 if (defined $dictionary{$concept1}) {
325 $term1_def = $dictionary{$concept1};
326 }
327 else{
328 if (defined $debugfile) {
329 print DEBUG "$concept1: not defined\n"; }
330 return -1; }
331 }
332
333 if($concept2 =~ /^(C[0-9]+)(\#)(.*?)$/)
334 {
335 my $cui2 = $1;
336 $term2 = $3;
337
338 $defs2 = $interface->getExtendedDefinition($cui2);
339 $term2_def = $dictionary{$term2} if (defined $dictionary{$term2});
340
341
342 # check the cui's associated term's def in dictfile
343 #@dictfile_term2 = $interface->getTermList($cui2);
344 #foreach my $t (@dictfile_term2)
345 #{
346 #if(defined ($dictionary{$t}))
347 #{
348 #my $term2_def = $dictionary{$t};
349 #$def2 .= "$term2_def" . " ";
350 #}
351 #}
352
353 }
354 else
355 {
356 if (defined $dictionary{$concept2}) {
357 $term2_def = $dictionary{$concept2};
358 }
359 else{
360 if (defined $debugfile) {
361 print DEBUG "$concept2: not defined\n"; }
362 return -1; }
363 }
364
365 # if debug setting is on print out definition one information
366 if(defined $debugfile) { print DEBUG "DEFINITIONS FOR CONCEPT 1: $concept1 \n"; }
367
368 # set up the definition string - note the format is:
369 # CUI REL CUI SAB : <definition>
370 my $i = 1;
371 foreach my $def (@{$defs1}) {
372 if(defined $debugfile) {
373 print DEBUG "$i. $def\n";
374 $i++;
375 }
376 $def=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
377 #$def1 .= $5 . " " . "definitionstop" . " ";
378 my $temp = $5;
379 if(defined $temp) {
380 $def1 .= $temp . " " . "definitionstop" . " ";
381 }
382 }
383 if(defined $term1_def)
384 {
385 if(defined $debugfile)
386 {
387 print DEBUG "$i. $term1_def\n";
388 }
389 $def1 .= $term1_def;
390 }
391
392 #if the definition is empty, return -1;
393 if($def1 eq "")
394 {
395 return -1;
396 }
397
398 # if debug setting is on print out definition two information
399 if(defined $debugfile) { print DEBUG "DEFINITIONS FOR CONCEPT 2: $concept2 \n"; }
400
401 # set up the definition string - note the format is:
402 # CUI REL CUI SAB : <definition>
403 my $j = 1;
404 foreach my $def (@{$defs2}) {
405 if(defined $debugfile) {
406 print DEBUG "$j. $def\n";
407 $j++;
408 }
409 $def=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
410 #$def2 .= $5 . " " . "definitionstop" . " ";
411 my $temp = $5;
412 if(defined $temp) {
413 $def2 .= $temp . " " . "definitionstop" . " ";
414 }
415 }
416 if(defined $term2_def)
417 {
418 if(defined $debugfile)
419 {
420 print DEBUG "$j. $term2_def\n";
421 }
422 $def2 .= $term2_def;
423 }
424
425 #if the definition is empty, return -1;
426 if($def2 eq "")
427 {
428 return -1;
429 }
430
431 } # end of WITH --dictfile option
432
433
434 if (defined $doubledef)
435 {
436 my @def1_array = split(/\s/, $def1);
437 my @def2_array = split(/\s/, $def2);
438
439 my %unique = (); # for every word, only check its definition once
440 foreach my $w (@def1_array)
441 {
442 $unique{$w}++;
443 if ((defined $ddef{$w}) and ($unique{$w}==1))
444 {
445 my $def = $ddef{$w};
446 if (defined $debugfile)
447 {
448 print DEBUG "ddef1 $w: $def\n";
449 }
450 $def1 .= "$def" . " ";
451 }
452 }
453
454 %unique = ();
455 foreach my $w (@def2_array)
456 {
457 $unique{$w}++;
458 if ((defined $ddef{$w}) and ($unique{$w}==1))
459 {
460 my $def = $ddef{$w};
461 if (defined $debugfile)
462 {
463 print DEBUG "ddef2 $w: $def\n";
464 }
465 $def2 .= "$def" . " ";
466 }
467 }
468
469 if (defined $debugfile)
470 {
471 print DEBUG "after --doubledef processing\n";
472 print DEBUG "concept 1: $def1\n";
473 print DEBUG "concept 2: $def2\n";
474 }
475 } #end of defined --doubledef option
476
477
478 # if the --defraw option is not set clean up the defintions
479 if($defraw_option == 0)
480 {
481 $def1 = lc($def1); $def2 = lc($def2);
482
483 # remove punctuation doesn't contain '<' and '>'
484 $def1=~s/[\.\,\?\/\'\"\;\:\[\]\{\}\!\@\#\$\%\^\&\*\(\)\-\_\+\-\=]//g;
485 $def2=~s/[\.\,\?\/\'\"\;\:\[\]\{\}\!\@\#\$\%\^\&\*\(\)\-\_\+\-\=]//g;
486
487 if (defined $debugfile)
488 {
489 print DEBUG "after --defraw processing\n";
490 print DEBUG "concept 1: $def1\n";
491 print DEBUG "concept 2: $def2\n";
492 }
493 }
494
495
496 if(defined $compoundfile)
497 {
498 $def1 = findCompoundWord($def1, \%complist);
499 $def2 = findCompoundWord($def2, \%complist);
500 if (defined $debugfile)
501 {
502 print DEBUG "after --compoundfile processing\n";
503 print DEBUG "concept 1: $def1\n";
504 print DEBUG "concept 2: $def2\n";
505 }
506 }
507
508
509 # remove stop words
510 if (defined $stoplist)
511 {
512 my @d1 = split(/\s/, $def1);
513 my @d2 = split(/\s/, $def2);
514 my $new_def1 = "";
515 my $new_def2 = "";
516
517 foreach my $check (@d1)
518 {
519 if(!($check =~ /$stopregex/))
520 {
521 $new_def1 .= "$check ";
522 }
523 }
524
525 foreach my $check (@d2)
526 {
527 if(!($check =~ /$stopregex/))
528 {
529 $new_def2 .= "$check ";
530 }
531 }
532
533 $def1 = $new_def1;
534 $def2 = $new_def2;
535 if (defined $debugfile)
536 {
537 print DEBUG "after --stoplist processing\n";
538 print DEBUG "concept 1: $def1\n";
539 print DEBUG "concept 2: $def2\n";
540 }
541 }
542
543
544 if (defined $stem)
545 {
546 my @def1_words = split(/\s/, $def1);
547 my @def2_words = split(/\s/, $def2);
548 my $stemmed_words1 = Lingua::Stem::En::stem({ -words => \@def1_words, -locale => 'en'});
549 my $stemmed_words2 = Lingua::Stem::En::stem({ -words => \@def2_words, -locale => 'en'});
550
551 $def1 = join(" ", @{$stemmed_words1});
552 $def2 = join(" ", @{$stemmed_words2});
553
554 if (defined $debugfile)
555 {
556 print DEBUG "after --stem processing\n";
557 print DEBUG "concept 1: $def1\n";
558 print DEBUG "concept 2: $def2\n";
559 }
560 }
561
562 # find the overlap
563 my $overlaps = "";
564 my $len1 = 0;
565 my $len2 = 0;
566 ($overlaps, $len1, $len2) = $finder->getOverlaps($def1, $def2);
567
568
569 # calculate lesk on the overlaps which doesn't cross defs
570 if (defined $debugfile)
571 {
572 print DEBUG "Overlap string and their value\n";
573 }
574 my $score = 0;
575 foreach my $overlap (keys %{$overlaps})
576 {
577 #my $length = length ($overlap);
578 #print "overlap: $length\n";
579 my $length = 0;
580 my $num = 0;
581 my $value = 0;
582
583
584 if ($overlap =~ /definitionstop/)
585 {
586 my @array = split/\s+/, $overlap;
587 my $array_length = $#array + 1;
588 my $i = 0;
589 my $overlap_string = "";
590 foreach my $s (@array)
591 {
592 $i++;
593 if ($s !~ /definitionstop/) # count the length of the overlap
594 {
595 $length++;
596 $overlap_string .= "$s ";
597 }
598 if (($s =~ /definitionstop/) and ($length<$array_length) and ($length > 0)) # definitionstop in the middle of the overlap
599 {
600 $num = $overlaps->{$overlap};
601 $value = $num * ($length**2);
602 $score += $value;
603 $length = 0;
604 if (defined $debugfile)
605 {
606 print DEBUG "$overlap_string $value\n";
607 }
608 $overlap_string = "";
609 }
610 if (($s =~ /definitionstop/) and ($length==0)) # definitionstop at the beginning of the overlap
611 {
612 next;
613 }
614 if (($s !~ /definitionstop/) and ($i==$array_length)) # reach the end of the overlap
615 {
616 $overlap_string .= "$s ";
617 $num = $overlaps->{$overlap};
618 $value = $num * ($length**2);
619 $score += $value;
620 $length = 0;
621 if (defined $debugfile)
622 {
623 print DEBUG "$overlap_string $value\n";
624 }
625 $overlap_string = "";
626 }
627 }
628 }
629 else
630 {
631 my @array = split/\s+/, $overlap;
632 $length = $#array + 1;
633 $num = $overlaps->{$overlap};
634 $value = $num * ($length**2);
635 $score += $value;
636
637 if (defined $debugfile)
638 {
639 print DEBUG "$overlap $value\n";
640 }
641
642 }
643 }
644
645 return $score;
646 }
647
648 sub findCompoundWord
649 {
650 my $def = shift;
651 my $ref_complist = shift;
652 my $new_def = "";
653
654 my @words = split(' ', $def);
655 my $size_line = @words;
656 for (my $i=0; $i<$size_line; $i++)
657 {
658 my $w = $words[$i];
659 my $flag_print_w = 0;
660 my $flag_comp = 0;
661 if(defined $ref_complist->{$w})
662 {
663 # get the compound list start with word $w
664 my @comps = @{$ref_complist->{$w}};
665 foreach my $c (@comps)
666 {
667 #compare the rest of the compound word
668 my @string = split(' ', $c);
669 my $count = 1;
670 foreach my $s (@string)
671 {
672 if (($i+$count)<$size_line)
673 {
674 if ($s eq $words[$i+$count])
675 {
676 $flag_comp = 1;
677 $count++;
678 }
679 else
680 {
681 $flag_comp = 0;
682 last;
683 }
684 }
685 } # test one compound word start by $w
686 # connect the compound word
687 if ($flag_comp==1)
688 {
689 unshift(@string, "$w");
690
691 my $comp = join('_', @string);
692 $new_def .= "$comp ";
693 if (defined $debugfile)
694 {
695 print DEBUG "compounds: $comp\n";
696 }
697 my $skip = @string-1;
698 $i = $i + $skip;
699 last;
700 }
701 } # test all the compound word start by $w
702
703 # print out the $w if it doesn't match any compound words
704 if (($flag_print_w==0) and ($flag_comp==0))
705 {
706 $new_def .= "$w ";
707 $flag_print_w = 1;
708 }
709
710 } # end of defined compound word start by $w
711
712 if(!defined $ref_complist->{$w})
713 {
714 $new_def .= "$w ";
715 }
716 } # end of one definition
717
718 return $new_def;
719
720 }
721
722 1;
723 __END__