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