Categorizer/Collection.pm

filter: Statementstmt 46.6% 21 / 45 Branchbran 16.6% 3 / 18 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 5 / 6 Podpod 100% 2 / 2 totaltotal 42.1% 32 / 76 CC 11 SCAR 32.9
CC
11
Cov
38%
new
43.2
count_documents
12.0
BEGIN
1.0
CRAP 26.8
↑ Summary Categorizer/FeatureSelector/CategorySelector.pm »
1 package AI::Categorizer::Collection;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 11 ▶use Params::Validate qw(:types);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Class::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use base qw(Class::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 __PACKAGE__->valid_params
8 (
9 verbose => {type => SCALAR, default => 0},
10 stopword_file => { type => SCALAR, optional => 1 },
11 category_hash => { type => HASHREF, default => {} },
12 category_file => { type => SCALAR, optional => 1 },
13 );
14  
15 11 __PACKAGE__->contained_objects
16 (
17 document => { class => 'AI::Categorizer::Document::Text',
18 delayed => 1 },
19 );
20  
21 7 ▶sub new {
Subroutine new: called CC=9 SCAR=37.7
Pod documented
22 7 my ($class, %args) = @_;
23
24 # Optimize so every document doesn't have to convert the stopword list to a hash
25 7 ▶ if ($args{stopwords} and UNIVERSAL::isa($args{stopwords}, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"stopwords"} and UNIVERSAL::isa($args{"stopwords"}, "ARRAY")) 0 7 7
Condition33%1/3
$args{"stopwords"} and UNIVERSAL::isa($args{"stopwords"}, "ARRAY")
!ll&&!rl&&r
700
MC/DC0%0/2
$args{"stopwords"} and UNIVERSAL::isa($args{"stopwords"}, "ARRAY")
$args{"stopwords"} UNIVERSAL::isa($args{"stopwords"}, "ARRAY")
Truth table33%1/3
A and B
A= $args{"stopwords"}B= UNIVERSAL::isa($args{"stopwords"}, "ARRAY")
ABresult
0X0
100
111
26 0 $args{stopwords} = { map {+$_ => 1} @{ $args{stopwords} } };
27 }
28
29 7 my $self = $class->SUPER::new(%args);
30  
31 7 ▶ if ($self->{category_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"category_file"}) 0 7 7
32 0 local *FH;
33 0 ▶ open FH, $self->{category_file} or die "Can't open $self->{category_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, $self->{"category_file"} or die qq[Can't open $self->{"category_file"}: $!] 0 0 0
34 0 ▶ while (<FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 0 0 0
35 0 my ($doc, @cats) = split;
36 0 $self->{category_hash}{$doc} = \@cats;
37 }
38 0 close FH;
39 }
40 7 ▶ if (exists $self->{stopword_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"stopword_file"}) 0 7 7
41 0 my %stopwords;
42 0 local *FH;
43 0 ▶ open FH, "< $self->{stopword_file}" or die "$self->{stopword_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, qq[< $self->{"stopword_file"}] or die qq[$self->{"stopword_file"}: $!] 0 0 0
44 0 ▶ while (<FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 0 0 0
45 0 chomp;
46 0 $stopwords{$_} = 1;
47 }
48 0 close FH;
49  
50 0 $self->delayed_object_params('document', stopwords => \%stopwords);
51 }
52  
53 7 return $self;
54 }
55  
56 # This should usually be replaced in subclasses with a faster version that doesn't
57 # need to create actual documents each time through
58 0 ▶sub count_documents {
Subroutine count_documents: not called CC=3 SCAR=24.8
Pod documented
59 0 my $self = shift;
60 0 ▶ return $self->{document_count} if exists $self->{document_count};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"document_count"} 0 0 0
61  
62 0 $self->rewind;
63 0 my $count = 0;
64 0 ▶ $count++ while $self->next;
Branch: 1 branch
BranchTrueFalseTotal
$self->next and do { ++$count } 0 0 0
65 0 $self->rewind;
66  
67 0 return $self->{document_count} = $count;
68 }
69  
70 # Abstract methods
71 sub next;
72 sub rewind;
73  
74 11 1;
75 __END__
↑ Summary Categorizer/FeatureSelector/CategorySelector.pm »