| 1 | package AI::CBR::Sim; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | our @ISA = ('Exporter'); | |||||||||||||||||||||||||||
| 8 | 6 | our @EXPORT_OK = qw(sim_dist sim_frac sim_eq sim_set); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | AI::CBR::Sim - collection of basic similarity functions | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Import similarity functions for case construction. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use AI::CBR::Sim qw(sim_dist sim_eq); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ... | ||||||||||||||||||||||||||||
| 23 | ... | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =item * sim_dist | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =item * sim_frac | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item * sim_eq | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item * sim_set | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =back | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 sim_dist | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Works for any numeric values. | ||||||||||||||||||||||||||||
| 46 | Suitable when you are interested into the difference of values in a given range. | ||||||||||||||||||||||||||||
| 47 | Returns the fraction of the difference of the values with respect to a given maximum range of interest. | ||||||||||||||||||||||||||||
| 48 | The madatory third argument is this range. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sim_dist(26, 22, 10); # returns 0.4 | ||||||||||||||||||||||||||||
| 51 | sim_dist(-2, 1, 100); # returns 0.03 | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | sub sim_dist { | ||||||||||||||||||||||||||
|
Subroutine
sim_dist: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 9 | my ($a, $b, $range) = @_; | |||||||||||||||||||||||||||
| 57 | 9 | ▶ | return 1 if $a == $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 9 | my $dist = abs($a - $b); | |||||||||||||||||||||||||||
| 59 | 9 | ▶ | return 0 if $dist >= $range; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 8 | return 1 - $dist / $range; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head2 sim_frac | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Works for non-negative numeric values. | ||||||||||||||||||||||||||||
| 67 | Suitable when you are only interested into their relative difference with respect to 0. | ||||||||||||||||||||||||||||
| 68 | Returns the fraction of the smaller argument with respect to the higher one. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sim_frac(3, 2); # returns 0.67 | ||||||||||||||||||||||||||||
| 71 | sim_frac(40, 50); # returns 0.8 | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 10 | ▶ | sub sim_frac { | ||||||||||||||||||||||||||
|
Subroutine
sim_frac: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 10 | my ($a, $b) = @_; | |||||||||||||||||||||||||||
| 77 | 10 | ▶ | return 1 if $a == $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 8 | ▶ | return 0 if $a * $b == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 6 | ▶ | return $a > $b ? $b / $a : $a / $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 sim_eq | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Works for any textual value. | ||||||||||||||||||||||||||||
| 86 | Suitable when you are interested only into equality/inequality. | ||||||||||||||||||||||||||||
| 87 | Returns 1 in case of equality, 0 in case of inequality. | ||||||||||||||||||||||||||||
| 88 | No third argument. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sim_eq('foo', 'bar'); # returns 0 | ||||||||||||||||||||||||||||
| 91 | sim_eq('foo', 'foo'); # returns 1 | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 16 | ▶ | sub sim_eq { | ||||||||||||||||||||||||||
|
Subroutine
sim_eq: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 16 | ▶ | return $_[0] eq $_[1] ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 sim_set | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Works for sets/lists of textual values. | ||||||||||||||||||||||||||||
| 103 | Suitable when you are interested into overlap of the two sets. | ||||||||||||||||||||||||||||
| 104 | Arguments are two array references with textual values. | ||||||||||||||||||||||||||||
| 105 | Returns the number of elements in the intersection | ||||||||||||||||||||||||||||
| 106 | divided by the number of elements in the union. | ||||||||||||||||||||||||||||
| 107 | No third argument. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sim_set([qw/a b c/], [qw/b c d/]); # returns 0.5 | ||||||||||||||||||||||||||||
| 110 | sim_set([qw/a b c/], [qw/c/]); # returns 0.33 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 13 | ▶ | sub sim_set { | ||||||||||||||||||||||||||
|
Subroutine
sim_set: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 13 | my ($a, $b) = @_; | |||||||||||||||||||||||||||
| 116 | 13 | ▶ | return 1 if int @$a == 0 && int @$b == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
int @$a == 0 and int @$b == 0
MC/DC100%2/2
int @$a == 0 and int @$b == 0
int @$a == 0
int @$b == 0
Truth table100%3/3
A and B
A= int @$a == 0B= int @$b == 0
| |||||||||||||||||||||||||||||
| 117 | 12 | ▶ | return 0 unless int @$a && int @$b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
int @$a and int @$b
MC/DC100%2/2
int @$a and int @$b
int @$a
int @$b
Truth table100%3/3
A and B
A= int @$aB= int @$b
| |||||||||||||||||||||||||||||
| 118 | 10 | my %a = map { ($_ => 1) } @$a; | |||||||||||||||||||||||||||
| 119 | 10 | my $union = int keys %a; | |||||||||||||||||||||||||||
| 120 | 10 | my $intersection = 0; | |||||||||||||||||||||||||||
| 121 | map { | ||||||||||||||||||||||||||||
| 122 | 10 | ▶ | $a{$_} ? $intersection++ : $union++ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } @$b; | ||||||||||||||||||||||||||||
| 124 | 10 | return $intersection / $union; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | See L<AI::CBR> for an overview of the framework. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | Darko Obradovic, C<< <dobradovic at gmx.de> >> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 BUGS | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Please report any bugs or feature requests to C<bug-ai-cbr at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 140 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-CBR>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 141 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | perldoc AI::CBR::Sim | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | You can also look for information at: | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =over 4 | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AI-CBR> | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | L<http://annocpan.org/dist/AI-CBR> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | L<http://cpanratings.perl.org/d/AI-CBR> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | L<http://search.cpan.org/dist/AI-CBR> | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =back | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Copyright 2009 Darko Obradovic, all rights reserved. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 181 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 6 | 1; # End of AI::CBR::Sim | |||||||||||||||||||||||||||