File Coverage

blib/lib/Algorithm/AM/DataSet/Item.pm
Criterion Covered Total %
statement 42 42 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 7 7 100.0
total 69 69 100.0


line stmt bran cond sub pod time code
1             package Algorithm::AM::DataSet::Item;
2 11     11   17172 use strict;
  11         10  
  11         222  
3 11     11   33 use warnings;
  11         10  
  11         302  
4             our $VERSION = '3.10';
5             # ABSTRACT: A single item for classification training and testing
6 11     11   30 use Carp;
  11         11  
  11         635  
7             our @CARP_NOT = qw(Algorithm::AM::DataSet);
8             use Exporter::Easy (
9 11         51 OK => ['new_item']
10 11     11   832 );
  11         1962  
11              
12             # use to assign unique ids to new items; not meant to be secure
13             # or anything, just unique.
14             my $current_id = 'a';
15              
16              
17             #pod =head1 SYNOPSIS
18             #pod
19             #pod use Algorithm::AM::DataSet::Item 'new_item';
20             #pod
21             #pod my $item = new_item(
22             #pod features => ['a', 'b', 'c'],
23             #pod class => 'x',
24             #pod comment => 'a sample, meaningless item'
25             #pod );
26             #pod
27             #pod =head1 DESCRIPTION
28             #pod
29             #pod This class represents a single item contained in a data set. Each
30             #pod item has a feature vector and possibly a class label and comment
31             #pod string. Once created, the item is immutable.
32             #pod
33             #pod =head1 METHODS
34             #pod
35             #pod =head2 C
36             #pod
37             #pod Creates a new Item object. The only required argument is
38             #pod 'features', which should be an array ref containing the feature
39             #pod vector. Each element of this array should be a string indicating the
40             #pod value of the feature at the given index. 'class' and 'comment'
41             #pod arguments are also accepted, where 'class' is the classification
42             #pod label and 'comment' can be any string to be associated with the item.
43             #pod A missing or undefined 'class' value is assumed to mean that the item
44             #pod classification is unknown. For the feature vector, empty strings are
45             #pod taken to indicate null values.
46             #pod
47             #pod =cut
48             sub new {
49 266     266 1 3468 my ($class, %args) = @_;
50 266 100 100     950 if(!exists $args{features} ||
51             'ARRAY' ne ref $args{features}){
52 3         25 croak q[Must provide 'features' parameter of type array ref];
53             }
54 263         268 my $self = {};
55 263         327 for(qw(features class comment)){
56 789         720 $self->{$_} = $args{$_};
57 789         715 delete $args{$_};
58             }
59 263 100       518 if(my $extra_keys = join ',', sort keys %args){
60 1         7 croak "Unknown parameters: $extra_keys";
61             }
62 262         251 $self->{id} = $current_id;
63 262         185 $current_id++;
64 262         221 bless $self, $class;
65 262         590 return $self;
66             }
67              
68             #pod =head2 C
69             #pod
70             #pod This is an exportable shortcut for the new method. If exported, then
71             #pod instead of calling C<new>>, you may
72             #pod simply call C.
73             #pod
74             #pod =cut
75             sub new_item {
76             # unpack here so that warnings about odd numbers of elements are
77             # reported for this function, not for the new method
78 3     3 1 1000 my %args = @_;
79 3         10 return __PACKAGE__->new(%args);
80             }
81              
82             #pod =head2 C
83             #pod
84             #pod Returns the classification label for this item, or undef if the class
85             #pod is unknown.
86             #pod
87             #pod =cut
88             sub class {
89 90917     90917 1 88942 my ($self) = @_;
90 90917         149803 return $self->{class};
91             }
92              
93             #pod =head2 C
94             #pod
95             #pod Returns the feature vector for this item. This is an arrayref
96             #pod containing the string value for each feature. An empty string
97             #pod indicates that the feature value is null (meaning that it has
98             #pod no value).
99             #pod
100             #pod =cut
101             sub features {
102 60807     60807 1 68365 my ($self) = @_;
103             # make a safe copy
104 60807         32662 return [@{ $self->{features} }];
  60807         489319  
105             }
106              
107             #pod =head2 C
108             #pod
109             #pod Returns the comment for this item. By default, the comment is
110             #pod just a comma-separated list of the feature values.
111             #pod
112             #pod =cut
113             sub comment {
114 76     76 1 235 my ($self) = @_;
115 76 100       151 if(!defined $self->{comment}){
116 1         1 $self->{comment} = join ',', @{ $self->{features} };
  1         3  
117             }
118 76         299 return $self->{comment};
119             }
120              
121             #pod =head2 C
122             #pod
123             #pod Returns the length of the feature vector for this item.
124             #pod
125             #pod =cut
126             sub cardinality {
127 481     481 1 403 my ($self) = @_;
128 481         311 return scalar @{$self->features};
  481         536  
129             }
130              
131             #pod =head2 C
132             #pod
133             #pod Returns a unique string id for this item, for use as a hash key or
134             #pod similar situations.
135             #pod
136             #pod =cut
137             sub id {
138 13     13 1 11 my ($self) = @_;
139 13         42 return $self->{id};
140             }
141              
142             1;
143              
144             __END__