Categorizer/Learner.pm

filter: Statementstmt 68.9% 51 / 74 Branchbran 42.8% 12 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.2% 13 / 16 Podpod 71.4% 5 / 7 totaltotal 64.8% 81 / 125 CC 16 SCAR 15.4
CC
16
Cov
62%
__ANON__
12.0
categorize
7.7
categorize_collection
6.4
CRAP 4.7
« Categorizer/Learner/KNN.pm ↑ Summary Categorizer/Learner/DecisionTree.pm »
1 package AI::Categorizer::Learner;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Class::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use AI::Categorizer::Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use base qw(Class::Container AI::Categorizer::Storable);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Params::Validate qw(:types);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use AI::Categorizer::ObjectSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 __PACKAGE__->valid_params
12 (
13 knowledge_set => { isa => 'AI::Categorizer::KnowledgeSet', optional => 1 },
14 verbose => {type => SCALAR, default => 0},
15 );
16  
17 11 __PACKAGE__->contained_objects
18 (
19 hypothesis => {
20 class => 'AI::Categorizer::Hypothesis',
21 delayed => 1,
22 },
23 experiment => {
24 class => 'AI::Categorizer::Experiment',
25 delayed => 1,
26 },
27 );
28  
29 # Subclasses must override these virtual methods:
30 sub get_scores;
31 sub create_model;
32  
33 # Optional virtual method for on-line learning:
34 sub add_knowledge;
35  
36 143 ▶sub verbose {
Subroutine verbose: called CC=2 SCAR=7.6
Pod documented
37 143 my $self = shift;
38 143 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 143 143
39 0 $self->{verbose} = shift;
40 }
41 143 return $self->{verbose};
42 }
43  
44 70 ▶sub knowledge_set {
Subroutine knowledge_set: called CC=2 SCAR=7.6
Pod documented
45 70 my $self = shift;
46 70 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 70 70
47 0 $self->{knowledge_set} = shift;
48 }
49 70 return $self->{knowledge_set};
50 }
51  
52 21 ▶sub categories {
Subroutine categories: called CC=1 SCAR=0.0
Pod undocumented
53 21 my $self = shift;
54 21 return $self->knowledge_set->categories;
55 }
56  
57 7 ▶sub train {
Subroutine train: called CC=3 SCAR=11.0
Pod documented
58 7 my ($self, %args) = @_;
59 7 ▶ $self->{knowledge_set} = $args{knowledge_set} if $args{knowledge_set};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"knowledge_set"} 1 6 7
60 7 ▶ die "No knowledge_set provided" unless $self->{knowledge_set};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"knowledge_set"} 0 7 7
61  
62 7 $self->{knowledge_set}->finish;
63 7 $self->create_model; # Creates $self->{model}
64 7 $self->delayed_object_params('hypothesis',
65 all_categories => [map $_->name, $self->categories],
66 );
67 }
68  
69 0 ▶sub prog_bar {
Subroutine prog_bar: not called CC=2 SCAR=17.9
Pod undocumented
70 0 my ($self, $count) = @_;
71
72 0 ▶ return sub { print STDERR '.' } unless eval "use Time::Progress; 1";
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless eval "use Time::Progress; 1" 0 0 0
73
74 0 my $pb = 'Time::Progress'->new;
75 0 $pb->attr(max => $count);
76 0 my $i = 0;
77 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
78 0 $i++;
79 0 ▶ return if $i % 25;
Branch: 1 branch
BranchTrueFalseTotal
if $i % 25 0 0 0
80 0 my $string = '';
81 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
82 0 my $e = shift;
83 0 $string = sprintf " (maF1=%.03f, miF1=%.03f)", $e->macro_F1, $e->micro_F1;
84 }
85 0 print STDERR $pb->report("%50b %p ($i/$count)$string\r", $i);
86 0 return $i;
87 0 };
88 }
89  
90 5 ▶sub categorize_collection {
Subroutine categorize_collection: called CC=6 SCAR=18.5
Pod documented
91 5 my ($self, %args) = @_;
92 5 ▶ my $c = $args{collection} or die "No collection provided";
Branch: 1 branch
BranchTrueFalseTotal
my $c = $args{"collection"} or die "No collection provided" 5 0 5
93  
94 5 my @all_cats = map $_->name, $self->categories;
95 5 my $experiment = $self->create_delayed_object('experiment', categories => \@all_cats);
96 5 ▶ my $pb = $self->verbose ? $self->prog_bar($c->count_documents) : sub {};
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
$self->verbose ? : 0 5 5
97 5 ▶ while (my $d = $c->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $c->next) 20 5 25
98 20 my $h = $self->categorize($d);
99 20 $experiment->add_hypothesis($h, [map $_->name, $d->categories]);
100 20 $pb->($experiment);
101 20 ▶ if ($self->verbose > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 1) 0 20 20
102 0 printf STDERR ("%s: assigned=(%s) correct=(%s)\n",
103 $d->name,
104 join(', ', $h->categories),
105 join(', ', map $_->name, $d->categories));
106 }
107 }
108 5 ▶ print STDERR "\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 5 5
109  
110 5 return $experiment;
111 }
112  
113 41 ▶sub categorize {
Subroutine categorize: called CC=4 SCAR=20.4
Pod documented
114 41 my ($self, $doc) = @_;
115
116 41 my ($scores, $threshold) = $self->get_scores($doc);
117
118 41 ▶ if ($self->verbose > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 2) 0 41 41
119 0 ▶ warn "scores: @{[ %$scores ]}" if $self->verbose > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 3 0 0 0
120
121 0 foreach my $key (sort {$scores->{$b} <=> $scores->{$a}} keys %$scores) {
122 0 print "$key: $scores->{$key}\n";
123 }
124 }
125
126 41 return $self->create_delayed_object('hypothesis',
127 scores => $scores,
128 threshold => $threshold,
129 document_name => $doc->name,
130 );
131 }
132 11 1;
133  
134 __END__
« Categorizer/Learner/KNN.pm ↑ Summary Categorizer/Learner/DecisionTree.pm »