File Coverage

blib/lib/AI/Categorizer/Category.pm
Criterion Covered Total %
statement 41 42 97.6
branch 7 10 70.0
condition n/a
subroutine 12 13 92.3
pod 7 7 100.0
total 67 72 93.0


line stmt bran cond sub pod time code
1             package AI::Categorizer::Category;
2              
3 11     11   86 use strict;
  11         20  
  11         330  
4 11     11   52 use AI::Categorizer::ObjectSet;
  11         19  
  11         185  
5 11     11   48 use Class::Container;
  11         16  
  11         325  
6 11     11   47 use base qw(Class::Container);
  11         17  
  11         760  
7              
8 11     11   49 use Params::Validate qw(:types);
  11         16  
  11         1716  
9 11     11   54 use AI::Categorizer::FeatureVector;
  11         18  
  11         4886  
10              
11             __PACKAGE__->valid_params
12             (
13             name => {type => SCALAR, public => 0},
14             documents => {
15             type => ARRAYREF,
16             default => [],
17             callbacks => { 'all are Document objects' =>
18             sub { ! grep !UNIVERSAL::isa($_, 'AI::Categorizer::Document'), @_ },
19             },
20             public => 0,
21             },
22             );
23              
24             __PACKAGE__->contained_objects
25             (
26             features => {
27             class => 'AI::Categorizer::FeatureVector',
28             delayed => 1,
29             },
30             );
31              
32             my %REGISTRY = ();
33              
34             sub new {
35 14     14 1 118 my $self = shift()->SUPER::new(@_);
36 14         2044 $self->{documents} = new AI::Categorizer::ObjectSet( @{$self->{documents}} );
  14         168  
37 14         40 $REGISTRY{$self->{name}} = $self;
38 14         114 return $self;
39             }
40              
41             sub by_name {
42 83     83 1 1318 my ($class, %args) = @_;
43 83 100       514 return $REGISTRY{$args{name}} if exists $REGISTRY{$args{name}};
44 14         62 return $class->new(%args);
45             }
46              
47 246     246 1 1337 sub name { $_[0]->{name} }
48              
49             sub documents {
50 8     8 1 49 my $d = $_[0]->{documents};
51 8 100       30 return wantarray ? $d->members : $d->size;
52             }
53              
54             sub contains_document {
55 0     0 1 0 return $_[0]->{documents}->includes( $_[1] );
56             }
57              
58             sub add_document {
59 40     40 1 46 my $self = shift;
60 40         207 $self->{documents}->insert( $_[0] );
61 40         144 delete $self->{features}; # Could be more efficient?
62             }
63              
64             sub features {
65 2     2 1 3 my $self = shift;
66              
67 2 50       6 if (@_) {
68 2         4 $self->{features} = shift;
69             }
70 2 50       6 return $self->{features} if $self->{features};
71              
72 2         11 my $v = $self->create_delayed_object('features');
73 2 50       8 return $self->{features} = $v unless $self->documents;
74              
75 2         5 foreach my $document ($self->documents) {
76 4         14 $v->add( $document->features );
77             }
78            
79 2         9 return $self->{features} = $v;
80             }
81              
82             1;
83             __END__