File Coverage

blib/lib/Algorithm/Classifier/NaiveBayes/App/Command/tokens.pm
Criterion Covered Total %
statement 28 31 90.3
branch 5 6 83.3
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::tokens;
2              
3 1     1   621 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         28  
5 1     1   4 use warnings;
  1         1  
  1         34  
6 1     1   4 use Algorithm::Classifier::NaiveBayes ();
  1         2  
  1         16  
7 1     1   3 use Algorithm::Classifier::NaiveBayes::App -command;
  1         1  
  1         7  
8              
9             sub options {
10             return (
11 3     3 0 19 [ 'm=s', 'Model JSON file path/name.', { 'default' => 'nb_model.json', 'completion' => 'files' } ],
12             [ 'c', 'Also print the count for each token.' ],
13             );
14             }
15              
16 0     0 1 0 sub abstract { 'List the tokens trained for a class' }
17              
18             sub description {
19 0     0 1 0 return 'List the tokens trained for the specified class, one per line.
20              
21             nb_tool tokens -m model.json spam
22             nb_tool tokens -m model.json -c spam
23             ';
24             }
25              
26             sub validate {
27 3     3 0 7 my ( $self, $opt, $args ) = @_;
28              
29 3 50       64 if ( !-f $opt->{'m'} ) {
30 0         0 $self->usage_error( '-m, "' . $opt->{'m'} . '", is not a file or does not exist' );
31             }
32              
33 3 100       10 if ( !defined( $args->[0] ) ) {
34 1         10 $self->usage_error('No class specified');
35             }
36              
37 2         4 return 1;
38             } ## end sub validate
39              
40             sub execute {
41 2     2 1 9 my ( $self, $opt, $args ) = @_;
42              
43 2         14 my $nb = Algorithm::Classifier::NaiveBayes->new;
44 2         13 $nb->load( $opt->{'m'} );
45              
46 2         4 my $class = $args->[0];
47 2         8 foreach my $token ( $nb->class_tokens($class) ) {
48 8 100       61 if ( $opt->{'c'} ) {
49 4         12 print $token. ': ' . $nb->{'model'}{'token_counts'}{$class}{$token} . "\n";
50             } else {
51 4         9 print $token. "\n";
52             }
53             }
54             } ## end sub execute
55              
56             1;