Categorizer/Learner/Boolean.pm

filter: Statementstmt 84.7% 39 / 46 Branchbran 40.0% 4 / 10 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 4 totaltotal 71.4% 55 / 77 CC 11 SCAR 19.5
CC
11
Cov
75%
create_model
12.3
threshold
6.0
get_scores
2.0
CRAP 7.0
« Categorizer/Util.pm ↑ Summary Categorizer/Collection/DBI.pm »
1 package AI::Categorizer::Learner::Boolean;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use AI::Categorizer::Learner;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw(AI::Categorizer::Learner);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Params::Validate qw(:types);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use AI::Categorizer::Util qw(random_elements);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 __PACKAGE__->valid_params
10 (
11 max_instances => {type => SCALAR, default => 0},
12 threshold => {type => SCALAR, default => 0.5},
13 );
14  
15 2 ▶sub create_model {
Subroutine create_model: called CC=9 SCAR=25.1
Pod undocumented
16 2 my $self = shift;
17 2 ▶ my $m = $self->{model} ||= {};
Condition100%2/2
$self->{"model"} ||= {}
l!l
11
MC/DC100%1/1
$self->{"model"} ||= {}
$self->{"model"}
Truth table100%2/2
A
A= $self->{"model"}
Aresult
11
00
18 2 my $mi = $self->{max_instances};
19  
20 2 foreach my $cat ($self->knowledge_set->categories) {
21 4 my (@p, @n);
22 4 foreach my $doc ($self->knowledge_set->documents) {
23 16 ▶ if ($doc->is_in_category($cat)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->is_in_category($cat)) { } 8 8 16
24 8 push @p, $doc;
25 } else {
26 8 push @n, $doc;
27 }
28 }
29 4 ▶ if ($mi and @p + @n > $mi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mi and @p + @n > $mi) 0 4 4
Condition33%1/3
$mi and @p + @n > $mi
!ll&&!rl&&r
400
MC/DC0%0/2
$mi and @p + @n > $mi
$mi @p + @n > $mi
Truth table33%1/3
A and B
A= $miB= @p + @n > $mi
ABresult
0X0
100
111
30 # Get rid of random instances from training set, preserving
31 # current positive/negative ratio
32 0 my $ratio = $mi / (@p + @n);
33 0 @p = random_elements(\@p, @p * $ratio);
34 0 @n = random_elements(\@n, @n * $ratio);
35
36 0 ▶ warn "Limiting to ". @p ." positives and ". @n ." negatives\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
37 }
38  
39 4 ▶ warn "Creating model for ", $cat->name, "\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
40 4 $m->{learners}{ $cat->name } = $self->create_boolean_model(\@p, \@n, $cat);
41 }
42 }
43  
44 sub create_boolean_model; # Abstract method
45  
46 16 ▶sub get_scores {
Subroutine get_scores: called CC=2 SCAR=6.9
Pod undocumented
47 16 my ($self, $doc) = @_;
48 16 my $m = $self->{model};
49 16 my %scores;
50 16 foreach my $cat (keys %{$m->{learners}}) {
51 32 $scores{$cat} = $self->get_boolean_score($doc, $m->{learners}{$cat});
52 }
53 16 return (\%scores, $self->{threshold});
54 }
55  
56 sub get_boolean_score; # Abstract method
57  
58 0 ▶sub threshold {
Subroutine threshold: not called CC=2 SCAR=17.9
Pod undocumented
59 0 my $self = shift;
60 0 ▶ $self->{threshold} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
61 0 return $self->{threshold};
62 }
63  
64 4 ▶sub categories {
Subroutine categories: called CC=1 SCAR=0.0
Pod undocumented
65 4 my $self = shift;
66 4 return map AI::Categorizer::Category->by_name( name => $_ ), keys %{ $self->{model}{learners} };
67 }
68  
69 2 1;
70 __END__
« Categorizer/Util.pm ↑ Summary Categorizer/Collection/DBI.pm »