File Coverage

blib/lib/Algorithm/Classifier/NaiveBayes/App/Command/classify.pm
Criterion Covered Total %
statement 43 46 93.4
branch 9 10 90.0
condition n/a
subroutine 9 11 81.8
pod 3 5 60.0
total 64 72 88.8


line stmt bran cond sub pod time code
1             package Algorithm::Classifier::NaiveBayes::App::Command::classify;
2              
3 1     1   149133 use 5.006;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         28  
5 1     1   3 use warnings;
  1         2  
  1         78  
6 1     1   588 use Algorithm::Classifier::NaiveBayes ();
  1         4  
  1         38  
7 1     1   9 use Algorithm::Classifier::NaiveBayes::App -command;
  1         1  
  1         23  
8 1     1   453 use JSON::PP ();
  1         2  
  1         399  
9              
10             sub options {
11             return (
12 4     4 0 36 [ 'm=s', 'Model JSON file path/name.', { 'default' => 'nb_model.json', 'completion' => 'files' } ],
13             [ 's', 'Also print the log score of every class.' ],
14             [ 'p', 'Also print the probability of every class.' ],
15             [ 'json', 'Print the class, scores, and probs as JSON instead.' ],
16             );
17             }
18              
19 0     0 1 0 sub abstract { 'Classify the specified text' }
20              
21             sub description {
22 0     0 1 0 return 'Classify the specified text using a saved model.
23              
24             The text is taken from the remaining args joined by a space, or from
25             stdin if no args are given. The best matching class is printed.
26              
27             nb_tool classify -m model.json cheap pills for sale
28             cat some_message.txt | nb_tool classify -m model.json -p
29             ';
30             } ## end sub description
31              
32             sub validate {
33 4     4 0 8 my ( $self, $opt, $args ) = @_;
34              
35 4 100       134 if ( !-f $opt->{'m'} ) {
36 1         14 $self->usage_error( '-m, "' . $opt->{'m'} . '", is not a file or does not exist' );
37             }
38              
39 3         9 return 1;
40             }
41              
42             sub execute {
43 3     3 1 13 my ( $self, $opt, $args ) = @_;
44              
45 3         21 my $nb = Algorithm::Classifier::NaiveBayes->new;
46 3         13 $nb->load( $opt->{'m'} );
47              
48 3         16 my ( $class, $scores, $probs ) = $nb->classify( $self->text_from($args) );
49 3 50       8 if ( !defined($class) ) {
50 0         0 die('The model has not been trained yet');
51             }
52              
53 3 100       6 if ( $opt->{'json'} ) {
54 1         4 print JSON::PP->new->canonical->pretty->encode( { 'class' => $class, 'scores' => $scores, 'probs' => $probs } );
55 1         438 return;
56             }
57              
58 2         13 print $class. "\n";
59              
60 2 100       74 if ( $opt->{'s'} ) {
61 1         5 print "scores:\n";
62 1         10 foreach my $possible ( sort { $scores->{$b} <=> $scores->{$a} } keys %{$scores} ) {
  1         5  
  1         4  
63 2         17 print ' ' . $possible . ': ' . $scores->{$possible} . "\n";
64             }
65             }
66 2 100       38 if ( $opt->{'p'} ) {
67 1         2 print "probs:\n";
68 1         8 foreach my $possible ( sort { $probs->{$b} <=> $probs->{$a} } keys %{$probs} ) {
  1         2  
  1         3  
69 2         25 print ' ' . $possible . ': ' . $probs->{$possible} . "\n";
70             }
71             }
72             } ## end sub execute
73              
74             1;