File Coverage

blib/lib/Algorithm/Classifier/NaiveBayes/App/Command/info.pm
Criterion Covered Total %
statement 30 33 90.9
branch 3 4 75.0
condition n/a
subroutine 8 10 80.0
pod 3 5 60.0
total 44 52 84.6


line stmt bran cond sub pod time code
1             package Algorithm::Classifier::NaiveBayes::App::Command::info;
2              
3 1     1   652 use 5.006;
  1         4  
4 1     1   4 use strict;
  1         1  
  1         24  
5 1     1   3 use warnings;
  1         1  
  1         34  
6 1     1   4 use Algorithm::Classifier::NaiveBayes ();
  1         1  
  1         31  
7 1     1   3 use Algorithm::Classifier::NaiveBayes::App -command;
  1         1  
  1         8  
8              
9             sub options {
10 3     3 0 19 return ( [ 'm=s', 'Model JSON file path/name.', { 'default' => 'nb_model.json', 'completion' => 'files' } ], );
11             }
12              
13 0     0 1 0 sub abstract { 'Show settings and stats for a saved model' }
14              
15             sub description {
16 0     0 1 0 return 'Show the settings, classes, and stats for a saved model.
17              
18             nb_tool info -m model.json
19             ';
20             }
21              
22             sub validate {
23 3     3 0 9 my ( $self, $opt, $args ) = @_;
24              
25 3 50       65 if ( !-f $opt->{'m'} ) {
26 0         0 $self->usage_error( '-m, "' . $opt->{'m'} . '", is not a file or does not exist' );
27             }
28              
29 3         9 return 1;
30             }
31              
32             sub execute {
33 3     3 1 14 my ( $self, $opt, $args ) = @_;
34              
35 3         21 my $nb = Algorithm::Classifier::NaiveBayes->new;
36 3         11 $nb->load( $opt->{'m'} );
37              
38 3         8 my $model = $nb->{'model'};
39 3         7 foreach my $setting (
40             'format', 'version', 'lc_tokens', 'token_splitter',
41             'stop_regex', 'ngrams', 'smoothing', 'alpha',
42             'token_weighting', 'priors'
43             )
44             {
45 30 100       281 print $setting . ': ' . ( defined( $model->{$setting} ) ? $model->{$setting} : 'undef' ) . "\n";
46             }
47              
48 3         25 print 'total_docs: ' . $model->{'total_docs'} . "\n";
49 3         24 print 'vocabulary_size: ' . scalar( keys %{ $model->{'tokens'} } ) . "\n";
  3         9  
50 3         24 print "classes:\n";
51 3         32 foreach my $class ( $nb->classes ) {
52             print ' '
53             . $class
54             . ': docs='
55             . $model->{'class_counts'}{$class}
56             . ' tokens='
57 5         27 . $model->{'class_totals'}{$class} . "\n";
58             }
59             } ## end sub execute
60              
61             1;