Categorizer/FeatureSelector/CategorySelector.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 90 CC 9 SCAR 31.3
CC
9
Cov
0%
scan_features
42.0
prog_bar
20.0
rank_features
2.0
CRAP 22.8
1 package AI::Categorizer::FeatureSelector::CategorySelector;
2  
3 use strict;
4 use AI::Categorizer::FeatureSelector;
5 use base qw(AI::Categorizer::FeatureSelector);
6  
7 use Params::Validate qw(:types);
8  
9 __PACKAGE__->contained_objects
10 (
11 features => { class => 'AI::Categorizer::FeatureVector',
12 delayed => 1 },
13 );
14  
15 1;
16  
17  
18 sub reduction_function;
19  
20 # figure out the feature set before reading collection (default)
21  
22 sub scan_features {
23 my ($self, %args) = @_;
24 my $c = $args{collection} or
25 die "No 'collection' parameter provided to scan_features()";
26  
27 if(!($self->{features_kept})) {return;}
28  
29 my %cat_features;
30 my $coll_features = $self->create_delayed_object('features');
31 my $nbDocuments = 0;
32  
33 while (my $doc = $c->next) {
34 $nbDocuments++;
35 $args{prog_bar}->() if $args{prog_bar};
36 my $docfeatures = $doc->features->as_hash;
37 foreach my $cat ($doc->categories) {
38 my $catname = $cat->name;
39 if(!(exists $cat_features{$catname})) {
40 $cat_features{$catname} = $self->create_delayed_object('features');
41 }
42 $cat_features{$catname}->add($docfeatures);
43 }
44 $coll_features->add( $docfeatures );
45 }
46 print STDERR "\n* Computing Chi-Square values\n" if $self->verbose;
47  
48 my $r_features = $self->create_delayed_object('features');
49 my @terms = $coll_features->names;
50 my $progressBar = $self->prog_bar(scalar @terms);
51 my $allFeaturesSum = $coll_features->sum;
52 my %cat_features_sum;
53 while( my($catname,$features) = each %cat_features ) {
54 $cat_features_sum{$catname} = $features->sum;
55 }
56  
57 foreach my $term (@terms) {
58 $progressBar->();
59 $r_features->{features}{$term} = $self->reduction_function($term,
60 $nbDocuments,$allFeaturesSum,$coll_features,
61 \%cat_features,\%cat_features_sum);
62 }
63 print STDERR "\n" if $self->verbose;
64 my $new_features = $self->reduce_features($r_features);
65 return $coll_features->intersection( $new_features );
66 }
67  
68  
69 # calculate feature set after reading collection (scan_first=0)
70  
71 sub rank_features {
72 die "CategorySelector->rank_features is not implemented yet!";
73 # my ($self, %args) = @_;
74 #
75 # my $k = $args{knowledge_set}
76 # or die "No knowledge_set parameter provided to rank_features()";
77 #
78 # my %freq_counts;
79 # foreach my $name ($k->features->names) {
80 # $freq_counts{$name} = $k->document_frequency($name);
81 # }
82 # return $self->create_delayed_object('features', features => \%freq_counts);
83 }
84  
85  
86 # copied from KnowledgeSet->prog_bar by Ken Williams
87  
88 sub prog_bar {
89 my ($self, $count) = @_;
90  
91 return sub {} unless $self->verbose;
92 return sub { print STDERR '.' } unless eval "use Time::Progress; 1";
93  
94 my $pb = 'Time::Progress'->new;
95 $pb->attr(max => $count);
96 my $i = 0;
97 return sub {
98 $i++;
99 return if $i % 25;
100 print STDERR $pb->report("%50b %p ($i/$count)\r", $i);
101 };
102 }
103  
104  
105 __END__