File Coverage

blib/lib/Algorithm/Classifier/NaiveBayes/App/Command/tweak.pm
Criterion Covered Total %
statement 27 30 90.0
branch 3 4 75.0
condition 4 6 66.6
subroutine 8 10 80.0
pod 3 5 60.0
total 45 55 81.8


line stmt bran cond sub pod time code
1             package Algorithm::Classifier::NaiveBayes::App::Command::tweak;
2              
3 1     1   701 use 5.006;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         16  
5 1     1   3 use warnings;
  1         2  
  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         2  
  1         7  
8              
9             sub options {
10             return (
11 4     4 0 27 [ 'm=s', 'Model JSON file path/name.', { 'default' => 'nb_model.json', 'completion' => 'files' } ],
12             [ 'smoothing=s', 'Smoothing to use... laplace or lidstone.' ],
13             [ 'alpha=f', 'Alpha for lidstone smoothing.' ],
14             [ 'priors=s', 'How class priors are computed... trained or uniform.' ],
15             );
16             }
17              
18 0     0 1 0 sub abstract { 'Change scoring settings on a saved model' }
19              
20             sub description {
21 0     0 1 0 return 'Change scoring settings on a saved model.
22              
23             Only smoothing, alpha, and priors may be changed as they only affect
24             scoring, not the trained counts. Settings that shape the trained data,
25             such as ngrams and token-weighting, would make the model inconsistent
26             with what was trained... for those, create a new model and retrain.
27              
28             nb_tool tweak -m model.json --smoothing lidstone --alpha 0.1
29             nb_tool tweak -m model.json --priors uniform
30             ';
31             } ## end sub description
32              
33             sub validate {
34 4     4 0 8 my ( $self, $opt, $args ) = @_;
35              
36 4 50       91 if ( !-f $opt->{'m'} ) {
37 0         0 $self->usage_error( '-m, "' . $opt->{'m'} . '", is not a file or does not exist' );
38             }
39              
40 4 100 66     24 if ( !defined( $opt->{'smoothing'} ) && !defined( $opt->{'alpha'} ) && !defined( $opt->{'priors'} ) ) {
      66        
41 1         10 $self->usage_error('Nothing to change... at least one of --smoothing, --alpha, or --priors is needed');
42             }
43              
44 3         8 return 1;
45             } ## end sub validate
46              
47             sub execute {
48 3     3 1 14 my ( $self, $opt, $args ) = @_;
49              
50 3         20 my $nb = Algorithm::Classifier::NaiveBayes->new;
51 3         12 $nb->load( $opt->{'m'} );
52              
53             $nb->tweak(
54             'smoothing' => $opt->{'smoothing'},
55             'alpha' => $opt->{'alpha'},
56 3         19 'priors' => $opt->{'priors'},
57             );
58 2         10 $nb->save( $opt->{'m'} );
59              
60 2         6 foreach my $setting ( 'smoothing', 'alpha', 'priors' ) {
61 6         76 print $setting . ': ' . $nb->{'model'}{$setting} . "\n";
62             }
63             } ## end sub execute
64              
65             1;