| 1 | package AI::Categorizer::Category; | ||||||||||
| 2 | |||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use AI::Categorizer::ObjectSet; | ||||||||
|
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 | |||||||||||
| 8 | 11 | ▶ | use Params::Validate qw(:types); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 11 | ▶ | use AI::Categorizer::FeatureVector; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 0 | ▶ | sub { ! grep !UNIVERSAL::isa($_, 'AI::Categorizer::Document'), @_ }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 19 | }, | ||||||||||
| 20 | 11 | public => 0, | |||||||||
| 21 | }, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 11 | __PACKAGE__->contained_objects | |||||||||
| 25 | ( | ||||||||||
| 26 | features => { | ||||||||||
| 27 | class => 'AI::Categorizer::FeatureVector', | ||||||||||
| 28 | delayed => 1, | ||||||||||
| 29 | }, | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | 11 | my %REGISTRY = (); | |||||||||
| 33 | |||||||||||
| 34 | 14 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | 14 | my $self = shift()->SUPER::new(@_); | |||||||||
| 36 | 14 | $self->{documents} = new AI::Categorizer::ObjectSet( @{$self->{documents}} ); | |||||||||
| 37 | 14 | $REGISTRY{$self->{name}} = $self; | |||||||||
| 38 | 14 | return $self; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 83 | ▶ | sub by_name { | ||||||||
|
Subroutine
by_name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 42 | 83 | my ($class, %args) = @_; | |||||||||
| 43 | 83 | ▶ | return $REGISTRY{$args{name}} if exists $REGISTRY{$args{name}}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 14 | return $class->new(%args); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 246 | ▶ | sub name { $_[0]->{name} } | ||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 48 | |||||||||||
| 49 | 8 | ▶ | sub documents { | ||||||||
|
Subroutine
documents: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 50 | 8 | my $d = $_[0]->{documents}; | |||||||||
| 51 | 8 | ▶ | return wantarray ? $d->members : $d->size; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | sub contains_document { | ||||||||
|
Subroutine
contains_document: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 55 | 0 | return $_[0]->{documents}->includes( $_[1] ); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 40 | ▶ | sub add_document { | ||||||||
|
Subroutine
add_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 40 | my $self = shift; | |||||||||
| 60 | 40 | $self->{documents}->insert( $_[0] ); | |||||||||
| 61 | 40 | delete $self->{features}; # Could be more efficient? | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | sub features { | ||||||||
|
Subroutine
features: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||
| 65 | 2 | my $self = shift; | |||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 2 | $self->{features} = shift; | |||||||||
| 69 | } | ||||||||||
| 70 | 2 | ▶ | return $self->{features} if $self->{features}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | 2 | my $v = $self->create_delayed_object('features'); | |||||||||
| 73 | 2 | ▶ | return $self->{features} = $v unless $self->documents; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | |||||||||||
| 75 | 2 | foreach my $document ($self->documents) { | |||||||||
| 76 | 4 | $v->add( $document->features ); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 2 | return $self->{features} = $v; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 11 | 1; | |||||||||
| 83 | __END__ | ||||||||||