Categorizer/Learner/NaiveBayes.pm

filter: Statementstmt 90.6% 29 / 32 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 40.0% 2 / 5 totaltotal 81.6% 40 / 49 CC 3 SCAR 6.9
CC
3
Cov
85%
threshold
6.0
create_model
2.0
BEGIN
1.0
CRAP 2.0
« Categorizer/FeatureSelector/ChiSquare.pm ↑ Summary Categorizer/ObjectSet.pm »
1 package AI::Categorizer::Learner::NaiveBayes;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use AI::Categorizer::Learner;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(AI::Categorizer::Learner);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Params::Validate qw(:types);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Algorithm::NaiveBayes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 __PACKAGE__->valid_params
10 (
11 threshold => {type => SCALAR, default => 0.3},
12 );
13  
14 2 ▶sub create_model {
Subroutine create_model: called CC=2 SCAR=6.9
Pod undocumented
15 2 my $self = shift;
16 2 my $m = $self->{model} = Algorithm::NaiveBayes->new;
17  
18 2 foreach my $d ($self->knowledge_set->documents) {
19 8 $m->add_instance(attributes => $d->features->as_hash,
20 label => [ map $_->name, $d->categories ]);
21 }
22 2 $m->train;
23 }
24  
25 9 ▶sub get_scores {
Subroutine get_scores: called CC=1 SCAR=0.0
Pod undocumented
26 9 my ($self, $newdoc) = @_;
27  
28 return ($self->{model}->predict( attributes => $newdoc->features->as_hash ),
29 9 $self->{threshold});
30 }
31  
32 0 ▶sub threshold {
Subroutine threshold: not called CC=2 SCAR=17.9
Pod documented
33 0 my $self = shift;
34 0 ▶ $self->{threshold} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
35 0 return $self->{threshold};
36 }
37  
38 1 ▶sub save_state {
Subroutine save_state: called CC=1 SCAR=0.0
Pod documented
39 1 my $self = shift;
40 1 local $self->{knowledge_set}; # Don't need the knowledge_set to categorize
41 1 $self->SUPER::save_state(@_);
42 }
43  
44 3 ▶sub categories {
Subroutine categories: called CC=1 SCAR=0.0
Pod undocumented
45 3 my $self = shift;
46 3 return map AI::Categorizer::Category->by_name( name => $_ ), $self->{model}->labels;
47 }
48  
49 1 1;
50  
51 __END__
« Categorizer/FeatureSelector/ChiSquare.pm ↑ Summary Categorizer/ObjectSet.pm »