line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::JA::OkapiBM25; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
1140
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
96
|
|
4
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
70
|
|
5
|
3
|
|
|
3
|
|
1451
|
use Lingua::JA::OkapiBM25::Result; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use base qw( Lingua::JA::TFIDF); |
7
|
|
|
|
|
|
|
use 5.008_001; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.00001'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors($_) for qw( param_k1 param_b avg_doc_length ); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
|
|
|
|
|
|
my $class = shift; |
15
|
|
|
|
|
|
|
my %args = @_; |
16
|
|
|
|
|
|
|
my $self = $class->SUPER::new(%args); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub bm25 { |
20
|
|
|
|
|
|
|
my $self = shift; |
21
|
|
|
|
|
|
|
my $text = shift; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $length = length($text); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $data = $self->_calc_tf( \$text ); |
26
|
|
|
|
|
|
|
$self->_calc_idf($data); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $k1 = $self->param_k1 || 2.0; |
29
|
|
|
|
|
|
|
my $b = $self->param_b || 0.75; |
30
|
|
|
|
|
|
|
my $avg_doc_length = $self->avg_doc_length || 2000; |
31
|
|
|
|
|
|
|
my $N = 25000000000; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
while ( my ( $key, $ref ) = each %{$data} ) { |
34
|
|
|
|
|
|
|
my $tf = $ref->{tf}; |
35
|
|
|
|
|
|
|
my $idf = log( $N / $ref->{df} ); |
36
|
|
|
|
|
|
|
my $bm25 = |
37
|
|
|
|
|
|
|
$idf * |
38
|
|
|
|
|
|
|
( $tf * |
39
|
|
|
|
|
|
|
( $k1 + 1 ) / |
40
|
|
|
|
|
|
|
( $tf + $k1 * ( 1 - $b + $b * ( $length / $avg_doc_length ) ) ) ); |
41
|
|
|
|
|
|
|
$data->{$key}->{bm25} = $bm25; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
return Lingua::JA::OkapiBM25::Result->new($data); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
1; |
48
|
|
|
|
|
|
|
__END__ |