| 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
Condition33%1/3
$args{"stopwords"} and UNIVERSAL::isa($args{"stopwords"}, "ARRAY")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 0 | local *FH; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | open FH, $self->{category_file} or die "Can't open $self->{category_file}: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | while (<FH>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 41 | 0 | my %stopwords; | |||||||||||||||||||||||||||
| 42 | 0 | local *FH; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | open FH, "< $self->{stopword_file}" or die "$self->{stopword_file}: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | while (<FH>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | $self->rewind; | |||||||||||||||||||||||||||
| 63 | 0 | my $count = 0; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | $count++ while $self->next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||