line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Set::Similarity::CosinePP; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
715
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
390
|
use parent 'Set::Similarity'; |
|
1
|
|
|
|
|
253
|
|
|
1
|
|
|
|
|
4
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.016'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub from_sets { |
11
|
21
|
|
|
21
|
1
|
12152
|
my ($self, $set1, $set2) = @_; |
12
|
21
|
|
|
|
|
39
|
my $cosine = $self->_cosine( |
13
|
|
|
|
|
|
|
$self->_normalize($self->_make_vector( $set1 )), |
14
|
|
|
|
|
|
|
$self->_normalize($self->_make_vector( $set2 )) |
15
|
|
|
|
|
|
|
); |
16
|
21
|
|
|
|
|
121
|
return $cosine; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _make_vector { |
20
|
42
|
|
|
42
|
|
59
|
my ( $self, $tokens ) = @_; |
21
|
42
|
|
|
|
|
48
|
my %elements; |
22
|
42
|
|
|
|
|
84
|
do { $_++ } for @elements{@$tokens}; |
|
122
|
|
|
|
|
147
|
|
23
|
42
|
|
|
|
|
77
|
return \%elements; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Assumes both incoming vectors are normalized |
27
|
21
|
|
|
21
|
|
40
|
sub _cosine { shift->_dot( @_ ) } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _norm { |
30
|
42
|
|
|
42
|
|
46
|
my $self = shift; |
31
|
42
|
|
|
|
|
45
|
my $vector = shift; |
32
|
42
|
|
|
|
|
50
|
my $sum = 0; |
33
|
42
|
|
|
|
|
93
|
for my $key (keys %$vector) { |
34
|
122
|
|
|
|
|
177
|
$sum += $vector->{$key} ** 2; |
35
|
|
|
|
|
|
|
} |
36
|
42
|
|
|
|
|
87
|
return sqrt $sum; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _normalize { |
40
|
42
|
|
|
42
|
|
55
|
my $self = shift; |
41
|
42
|
|
|
|
|
44
|
my $vector = shift; |
42
|
|
|
|
|
|
|
|
43
|
42
|
|
|
|
|
62
|
return $self->_div( |
44
|
|
|
|
|
|
|
$vector, |
45
|
|
|
|
|
|
|
$self->_norm($vector) |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _dot { |
50
|
21
|
|
|
21
|
|
24
|
my $self = shift; |
51
|
21
|
|
|
|
|
36
|
my $vector1 = shift; |
52
|
21
|
|
|
|
|
21
|
my $vector2 = shift; |
53
|
|
|
|
|
|
|
|
54
|
21
|
|
|
|
|
23
|
my $dotprod = 0; |
55
|
|
|
|
|
|
|
|
56
|
21
|
|
|
|
|
42
|
for my $key (keys %$vector1) { |
57
|
64
|
100
|
|
|
|
117
|
$dotprod += $vector1->{$key} * $vector2->{$key} if ($vector2->{$key}); |
58
|
|
|
|
|
|
|
} |
59
|
21
|
|
|
|
|
39
|
return $dotprod; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# divides each vector entry by a given divisor |
64
|
|
|
|
|
|
|
sub _div { |
65
|
42
|
|
|
42
|
|
50
|
my $self = shift; |
66
|
42
|
|
|
|
|
48
|
my $vector = shift; |
67
|
42
|
|
|
|
|
50
|
my $divisor = shift; |
68
|
|
|
|
|
|
|
|
69
|
42
|
|
|
|
|
103
|
my $vector2 = {}; |
70
|
42
|
|
|
|
|
65
|
for my $key (keys %$vector) { |
71
|
122
|
|
|
|
|
205
|
$vector2->{$key} = $vector->{$key} / $divisor; |
72
|
|
|
|
|
|
|
} |
73
|
42
|
|
|
|
|
79
|
return $vector2; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
__END__ |