line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Digest::bBitMinHash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
862
|
use 5.008005; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
33
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
5
|
1
|
|
|
1
|
|
13
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
775
|
use Digest::MurmurHash3; |
|
1
|
|
|
|
|
4326
|
|
|
1
|
|
|
|
|
54
|
|
8
|
1
|
|
|
1
|
|
726
|
use Math::Random::MT; |
|
1
|
|
|
|
|
1095
|
|
|
1
|
|
|
|
|
5
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "0.0.0_02"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
0
|
|
|
0
|
0
|
|
my ($class, $b, $k, $random_seed, $hash_seeds) = @_; |
14
|
0
|
0
|
0
|
|
|
|
if ((defined $b) && (ref $b eq "HASH")) { |
15
|
0
|
0
|
|
|
|
|
$k = $b->{k} if (exists $b->{k}); |
16
|
0
|
0
|
|
|
|
|
$random_seed = $b->{random_seed} if (exists $b->{random_seed}); |
17
|
0
|
0
|
|
|
|
|
$hash_seeds = $b->{hash_seeds} if (exists $b->{hash_seeds}); |
18
|
0
|
0
|
|
|
|
|
$b = $b->{b} if (exists $b->{b}); |
19
|
|
|
|
|
|
|
} |
20
|
0
|
0
|
0
|
|
|
|
$b = 1 unless ((defined $b) && (ref $b ne "HASH") && ($b > 0)); |
|
|
|
0
|
|
|
|
|
21
|
0
|
0
|
0
|
|
|
|
$k = 128 unless ((defined $k) && ($k > 0)); |
22
|
0
|
|
|
|
|
|
$random_seed = 4294967296; |
23
|
0
|
0
|
0
|
|
|
|
unless ((defined $hash_seeds) && (ref $hash_seeds eq "ARRAY") && (($#$hash_seeds + 1) >= $k)) { |
|
|
|
0
|
|
|
|
|
24
|
0
|
|
|
|
|
|
$hash_seeds = Digest::bBitMinHash->get_hash_seeds($k, $random_seed); |
25
|
|
|
|
|
|
|
} |
26
|
0
|
|
|
|
|
|
my %hash = ( |
27
|
|
|
|
|
|
|
'b' => $b, |
28
|
|
|
|
|
|
|
'k' => $k, |
29
|
|
|
|
|
|
|
'random_seed' => $random_seed, |
30
|
|
|
|
|
|
|
'hash_seeds' => $hash_seeds, |
31
|
|
|
|
|
|
|
); |
32
|
0
|
|
|
|
|
|
bless \%hash, $class; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub get_hash_seeds { |
36
|
0
|
|
|
0
|
0
|
|
my ($self, $k, $random_seed) = @_; |
37
|
0
|
|
|
|
|
|
my @seed_arr = (); |
38
|
0
|
|
|
|
|
|
my $mt_seed = 13714; # mean less |
39
|
0
|
|
|
|
|
|
my $mt = Math::Random::MT->new($mt_seed); |
40
|
0
|
|
|
|
|
|
for (my $i = 0; $i < $k; $i++) { |
41
|
0
|
|
|
|
|
|
my $rand = $mt->rand($random_seed); |
42
|
0
|
|
|
|
|
|
push @seed_arr, $rand; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
|
return \@seed_arr; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub get { |
48
|
0
|
|
|
0
|
0
|
|
my ($self, $data_arr_ref) = @_; |
49
|
0
|
|
|
|
|
|
return $self->get_bit_vectors($data_arr_ref); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub get_bit_vectors { |
53
|
0
|
|
|
0
|
0
|
|
my ($self, $data_arr_ref) = @_; |
54
|
0
|
|
|
|
|
|
my @bit_vectors = (); |
55
|
0
|
|
|
|
|
|
for (my $h = 0; $h < $self->{b}; $h++) { |
56
|
0
|
|
|
|
|
|
my $tmp_val = 0; |
57
|
0
|
|
|
|
|
|
push @bit_vectors, $tmp_val; |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
for (my $i = 0; $i < $self->{k}; $i++) { |
60
|
0
|
|
|
|
|
|
my $min_hash_val; |
61
|
0
|
|
|
|
|
|
my $seed = $self->{hash_seeds}->[$i]; |
62
|
0
|
|
|
|
|
|
for (my $j = 0; $j <= $#$data_arr_ref; $j++) { |
63
|
0
|
|
|
|
|
|
my $data = $data_arr_ref->[$j]; |
64
|
0
|
|
|
|
|
|
my $varies = Digest::MurmurHash3::murmur32($data, $seed); |
65
|
0
|
0
|
|
|
|
|
if (defined $min_hash_val) { |
66
|
0
|
0
|
|
|
|
|
$min_hash_val = $varies if ($min_hash_val > $varies); |
67
|
|
|
|
|
|
|
} else { |
68
|
0
|
|
|
|
|
|
$min_hash_val = $varies; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
for (my $l = 0; $l < $self->{b}; $l++) { |
72
|
0
|
|
|
|
|
|
my $bit = vec($min_hash_val, $l, 1); |
73
|
0
|
|
|
|
|
|
$bit_vectors[$l] = $bit_vectors[$l] << 1; |
74
|
0
|
|
|
|
|
|
$bit_vectors[$l] = $bit_vectors[$l] | $bit; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
0
|
|
|
|
|
|
return \@bit_vectors; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub compare { |
81
|
0
|
|
|
0
|
0
|
|
my ($self, $vectors_1, $vectors_2) = @_; |
82
|
0
|
|
|
|
|
|
return $self->compare_bit_vectors($vectors_1, $vectors_2); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub compare_bit_vectors { |
86
|
0
|
|
|
0
|
0
|
|
my ($self, $set_1, $set_2) = @_; |
87
|
0
|
|
|
|
|
|
my $match = 0; |
88
|
0
|
|
|
|
|
|
for (my $i = 0; $i < $self->{k}; $i++) { |
89
|
0
|
|
|
|
|
|
my $bit_val = 1; |
90
|
0
|
|
|
|
|
|
for (my $j = 0; $j < $self->{b}; $j++) { |
91
|
0
|
|
|
|
|
|
$bit_val = $bit_val * ((vec($set_1->[$j], $i, 1) eq vec($set_2->[$j], $i, 1))); |
92
|
|
|
|
|
|
|
} |
93
|
0
|
|
|
|
|
|
$match = $match + $bit_val; |
94
|
|
|
|
|
|
|
} |
95
|
0
|
|
|
|
|
|
return $match; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get_uniq_element_num { |
99
|
0
|
|
|
0
|
0
|
|
my ($self, $data1, $data2) = @_; |
100
|
0
|
|
|
|
|
|
my %hash = (); |
101
|
0
|
|
|
|
|
|
my $num = 0; |
102
|
0
|
|
|
|
|
|
my @data = (@{$data1}, @{$data2}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
foreach my $e (@data) { |
104
|
0
|
0
|
|
|
|
|
unless (exists $hash{$e}) { |
105
|
0
|
|
|
|
|
|
$hash{$e} = 1; |
106
|
0
|
|
|
|
|
|
$num++; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
0
|
|
|
|
|
|
return $num; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub estimate_resemblance { |
113
|
0
|
|
|
0
|
0
|
|
my ($self, $data1, $data2, $hit_count) = @_; |
114
|
0
|
|
|
|
|
|
my $score = 0.0; |
115
|
0
|
|
|
|
|
|
my $f1 = $#{$data1} + 1; |
|
0
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $f2 = $#{$data2} + 1; |
|
0
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $D = $self->get_uniq_element_num($data1, $data2); |
118
|
0
|
|
|
|
|
|
my $r1 = $f1 / $D; |
119
|
0
|
|
|
|
|
|
my $r2 = $f2 / $D; |
120
|
0
|
|
|
|
|
|
my $A1 = ($r1 * ((1 - $r1) ** (2 ** ($self->{b} - 1)))) / (1 - ((1 - $r1) ** (2 ** ($self->{b})))); |
121
|
0
|
|
|
|
|
|
my $A2 = ($r2 * ((1 - $r2) ** (2 ** ($self->{b} - 1)))) / (1 - ((1 - $r2) ** (2 ** ($self->{b})))); |
122
|
0
|
|
|
|
|
|
my $r = $r1 + $r2; |
123
|
0
|
|
|
|
|
|
my $C1 = $A1 * ($r2 / $r) + $A2 * ($r1 / $r); |
124
|
0
|
|
|
|
|
|
my $C2 = $A1 * ($r1 / $r) + $A2 * ($r2 / $r); |
125
|
0
|
|
|
|
|
|
my $E = $hit_count / $self->{k}; |
126
|
0
|
|
|
|
|
|
my $R = ($E - $C1) / (1 - $C2); |
127
|
0
|
|
|
|
|
|
$score = $R; |
128
|
0
|
|
|
|
|
|
return $score; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub estimate { |
132
|
0
|
|
|
0
|
0
|
|
my ($self, $data1, $data2) = @_; |
133
|
0
|
|
|
|
|
|
my $bit_vectors1 = $self->get_bit_vectors($data1); |
134
|
0
|
|
|
|
|
|
my $bit_vectors2 = $self->get_bit_vectors($data2); |
135
|
0
|
|
|
|
|
|
my $match_bit_count = $self->compare_bit_vectors($bit_vectors1, $bit_vectors2); |
136
|
0
|
|
|
|
|
|
my $score = $self->estimate_resemblance($bit_vectors1, $bit_vectors2, $match_bit_count); |
137
|
0
|
|
|
|
|
|
return $score; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |