line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package AI::Categorizer::Learner::NaiveBayes; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
762
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
5
|
use AI::Categorizer::Learner; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
5
|
1
|
|
|
1
|
|
4
|
use base qw(AI::Categorizer::Learner); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
62
|
|
6
|
1
|
|
|
1
|
|
5
|
use Params::Validate qw(:types); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
134
|
|
7
|
1
|
|
|
1
|
|
5
|
use Algorithm::NaiveBayes; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
269
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
__PACKAGE__->valid_params |
10
|
|
|
|
|
|
|
( |
11
|
|
|
|
|
|
|
threshold => {type => SCALAR, default => 0.3}, |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub create_model { |
15
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
16
|
2
|
|
|
|
|
19
|
my $m = $self->{model} = Algorithm::NaiveBayes->new; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
3439
|
foreach my $d ($self->knowledge_set->documents) { |
19
|
8
|
|
|
|
|
385
|
$m->add_instance(attributes => $d->features->as_hash, |
20
|
|
|
|
|
|
|
label => [ map $_->name, $d->categories ]); |
21
|
|
|
|
|
|
|
} |
22
|
2
|
|
|
|
|
121
|
$m->train; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub get_scores { |
26
|
9
|
|
|
9
|
0
|
15
|
my ($self, $newdoc) = @_; |
27
|
|
|
|
|
|
|
|
28
|
9
|
|
|
|
|
34
|
return ($self->{model}->predict( attributes => $newdoc->features->as_hash ), |
29
|
|
|
|
|
|
|
$self->{threshold}); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub threshold { |
33
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
34
|
0
|
0
|
|
|
|
0
|
$self->{threshold} = shift if @_; |
35
|
0
|
|
|
|
|
0
|
return $self->{threshold}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub save_state { |
39
|
1
|
|
|
1
|
1
|
48
|
my $self = shift; |
40
|
1
|
|
|
|
|
4
|
local $self->{knowledge_set}; # Don't need the knowledge_set to categorize |
41
|
1
|
|
|
|
|
14
|
$self->SUPER::save_state(@_); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub categories { |
45
|
3
|
|
|
3
|
0
|
5
|
my $self = shift; |
46
|
3
|
|
|
|
|
14
|
return map AI::Categorizer::Category->by_name( name => $_ ), $self->{model}->labels; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
1; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
__END__ |