| 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
| |||||||||||
| 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__ | ||||||||||