File Coverage

blib/lib/AI/Categorizer/Hypothesis.pm
Criterion Covered Total %
statement 37 38 97.3
branch 6 8 75.0
condition n/a
subroutine 11 11 100.0
pod 7 7 100.0
total 61 64 95.3


line stmt bran cond sub pod time code
1             package AI::Categorizer::Hypothesis;
2              
3 12     12   6183 use strict;
  12         20  
  12         353  
4              
5 12     12   896 use Class::Container;
  12         14864  
  12         244  
6 12     12   60 use base qw(Class::Container);
  12         33  
  12         930  
7 12     12   57 use Params::Validate qw(:types);
  12         21  
  12         6038  
8              
9             __PACKAGE__->valid_params
10             (
11             all_categories => {type => ARRAYREF},
12             scores => {type => HASHREF},
13             threshold => {type => SCALAR},
14             document_name => {type => SCALAR, optional => 1},
15             );
16              
17 10     10 1 19 sub all_categories { @{$_[0]->{all_categories}} }
  10         56  
18 20     20 1 62 sub document_name { $_[0]->{document_name} }
19 40     40 1 147 sub threshold { $_[0]->{threshold} }
20              
21             sub best_category {
22 22     22 1 251 my ($self) = @_;
23 22         43 my $sc = $self->{scores};
24 22 50       61 return unless %$sc;
25              
26 22         50 my ($best_cat, $best_score) = each %$sc;
27 22         76 while (my ($key, $val) = each %$sc) {
28 46 100       181 ($best_cat, $best_score) = ($key, $val) if $val > $best_score;
29             }
30 22         95 return $best_cat;
31             }
32              
33             sub in_category {
34 44     44 1 464 my ($self, $cat) = @_;
35 44 100       175 return '' unless exists $self->{scores}{$cat};
36 43         216 return $self->{scores}{$cat} > $self->{threshold};
37             }
38              
39             sub categories {
40 41     41 1 3626 my $self = shift;
41 41 50       126 return @{$self->{cats}} if $self->{cats};
  0         0  
42 5         12 $self->{cats} = [sort {$self->{scores}{$b} <=> $self->{scores}{$a}}
  106         751  
43 41         111 grep {$self->{scores}{$_} >= $self->{threshold}}
44 41         56 keys %{$self->{scores}}];
45 41         71 return @{$self->{cats}};
  41         2840  
46             }
47              
48             sub scores {
49 40     40 1 60 my $self = shift;
50 40         54 return @{$self->{scores}}{@_};
  40         461  
51             }
52              
53             1;
54              
55             __END__