Categorizer/Learner/KNN.pm

filter: Statementstmt 89.7% 61 / 68 Branchbran 77.2% 17 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 90.9% 10 / 11 Podpod 50.0% 2 / 4 totaltotal 86.3% 95 / 110 CC 18 SCAR 17.2
CC
18
Cov
87%
get_scores
8.4
add
8.0
threshold
6.0
CRAP 5.6
« Categorizer/Document/XML.pm ↑ Summary Categorizer/Learner.pm »
1 package AI::Categorizer::Learner::KNN;
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  
8 1 __PACKAGE__->valid_params
9 (
10 threshold => {type => SCALAR, default => 0.4},
11 k_value => {type => SCALAR, default => 20},
12 knn_weighting => {type => SCALAR, default => 'score'},
13 max_instances => {type => SCALAR, default => 0},
14 );
15  
16 2 ▶sub create_model {
Subroutine create_model: called CC=2 SCAR=6.9
Pod undocumented
17 2 my $self = shift;
18 2 foreach my $doc ($self->knowledge_set->documents) {
19 8 $doc->features->normalize;
20 }
21 2 $self->knowledge_set->features; # Initialize
22 }
23  
24 0 ▶sub threshold {
Subroutine threshold: not called CC=2 SCAR=17.9
Pod documented
25 0 my $self = shift;
26 0 ▶ $self->{threshold} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
27 0 return $self->{threshold};
28 }
29  
30 2 ▶sub categorize_collection {
Subroutine categorize_collection: called CC=2 SCAR=7.4
Pod documented
31 2 my $self = shift;
32
33 2 my $f_class = $self->knowledge_set->contained_class('features');
34 2 ▶ if ($f_class->can('all_features')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_class->can("all_features")) 0 2 2
35 0 $f_class->all_features([$self->knowledge_set->features->names]);
36 }
37 2 $self->SUPER::categorize_collection(@_);
38 }
39  
40 16 ▶sub get_scores {
Subroutine get_scores: called CC=8 SCAR=21.2
Pod undocumented
41 16 my ($self, $newdoc) = @_;
42 16 my $currentDocName = $newdoc->name;
43 #print "classifying $currentDocName\n";
44  
45 16 my $features = $newdoc->features->intersection($self->knowledge_set->features)->normalize;
46 16 my $q = AI::Categorizer::Learner::KNN::Queue->new(size => $self->{k_value});
47  
48 16 my @docset;
49 16 ▶ if ($self->{max_instances}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'max_instances'}) { } 0 16 16
50 # Use (approximately) max_instances documents, chosen randomly from corpus
51 0 my $probability = $self->{max_instances} / $self->knowledge_set->documents;
52 0 @docset = grep {rand() < $probability} $self->knowledge_set->documents;
53 } else {
54 # Use the whole corpus
55 16 @docset = $self->knowledge_set->documents;
56 }
57
58 16 foreach my $doc (@docset) {
59 64 my $score = $doc->features->dot( $features );
60 64 ▶ warn "Score for ", $doc->name, " (", ($doc->categories)[0]->name, "): $score" if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 64 64
61 64 $q->add($doc, $score);
62 }
63
64 16 my %scores = map {+$_->name, 0} $self->categories;
65 16 foreach my $e (@{$q->entries}) {
66 32 foreach my $cat ($e->{thing}->categories) {
67 32 ▶ $scores{$cat->name} += ($self->{knn_weighting} eq 'score' ? $e->{score} : 1); #increment cat score
Branch: 1 branch
BranchTrueFalseTotal
$self->{'knn_weighting'} eq 'score' ? : 16 16 32
68 }
69 }
70
71 16 $_ /= $self->{k_value} foreach values %scores;
72
73 16 return (\%scores, $self->{threshold});
74 }
75  
76 ###################################################################
77 package AI::Categorizer::Learner::KNN::Queue;
78  
79 17 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
80 17 my ($pkg, %args) = @_;
81 return bless {
82 size => $args{size},
83 17 entries => [],
84 }, $pkg;
85 }
86  
87 70 ▶sub add {
Subroutine add: called CC=8 SCAR=20.8
88 70 my ($self, $thing, $score) = @_;
89  
90 # scores may be (0.2, 0.4, 0.4, 0.8) - ascending
91  
92 70 return unless (@{$self->{entries}} < $self->{size} # Queue not filled
93 70 ▶ or $score > $self->{entries}[0]{score}); # Found a better entry
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"entries"};} < $self->{"size"} or $score > $self->{"entries"}[0]{"score"} 17 53 70
Condition100%3/3
@{$self->{"entries"};} < $self->{"size"} or $score > $self->{"entries"}[0]{"score"}
l!l&&r!l&&!r
351817
MC/DC100%2/2
@{$self->{"entries"};} < $self->{"size"} or $score > $self->{"entries"}[0]{"score"}
@{$self->{"entries"};} < $self->{"size"} $score > $self->{"entries"}[0]{"score"}
Truth table100%3/3
A or B
A= @{$self->{"entries"};} < $self->{"size"}B= $score > $self->{"entries"}[0]{"score"}
ABresult
1X1
011
000
94
95 53 my $i;
96 53 ▶ if (!@{$self->{entries}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @{$self->{'entries'};}) { } 17 36 53
elsif ($score > $self->{'entries'}[-1]{'score'}) { } 19 17 36
97 17 $i = 0;
98 } elsif ($score > $self->{entries}[-1]{score}) {
99 19 $i = @{$self->{entries}};
100 } else {
101 17 ▶ for ($i = 0; $i < @{$self->{entries}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"entries"};}) 23 2 25
102 23 ▶ last if $score < $self->{entries}[$i]{score};
Branch: 1 branch
BranchTrueFalseTotal
if $score < $self->{"entries"}[$i]{"score"} 15 8 23
103 }
104 }
105 53 splice @{$self->{entries}}, $i, 0, { thing => $thing, score => $score};
106 53 ▶ shift @{$self->{entries}} if @{$self->{entries}} > $self->{size};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"entries"};} > $self->{"size"} 18 35 53
107 }
108  
109 17 ▶sub entries {
Subroutine entries: called CC=1 SCAR=0.0
110 17 return shift->{entries};
111 }
112  
113 1 1;
114  
115 __END__
« Categorizer/Document/XML.pm ↑ Summary Categorizer/Learner.pm »