| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Algorithm::AM::DataSet::Item; | 
| 2 | 11 |  |  | 11 |  | 49609 | use strict; | 
|  | 11 |  |  |  |  | 56 |  | 
|  | 11 |  |  |  |  | 257 |  | 
| 3 | 11 |  |  | 11 |  | 49 | use warnings; | 
|  | 11 |  |  |  |  | 17 |  | 
|  | 11 |  |  |  |  | 356 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = '3.11'; | 
| 5 |  |  |  |  |  |  | # ABSTRACT: A single item for classification training and testing | 
| 6 | 11 |  |  | 11 |  | 52 | use Carp; | 
|  | 11 |  |  |  |  | 18 |  | 
|  | 11 |  |  |  |  | 694 |  | 
| 7 |  |  |  |  |  |  | our @CARP_NOT = qw(Algorithm::AM::DataSet); | 
| 8 |  |  |  |  |  |  | use Exporter::Easy ( | 
| 9 | 11 |  |  |  |  | 75 | OK => ['new_item'] | 
| 10 | 11 |  |  | 11 |  | 601 | ); | 
|  | 11 |  |  |  |  | 2318 |  | 
| 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 | 6513 | my ($class, %args) = @_; | 
| 50 | 266 | 100 | 100 |  |  | 985 | if(!exists $args{features} || | 
| 51 |  |  |  |  |  |  | 'ARRAY' ne ref $args{features}){ | 
| 52 | 3 |  |  |  |  | 37 | croak q[Must provide 'features' parameter of type array ref]; | 
| 53 |  |  |  |  |  |  | } | 
| 54 | 263 |  |  |  |  | 405 | my $self = {}; | 
| 55 | 263 |  |  |  |  | 439 | for(qw(features class comment)){ | 
| 56 | 789 |  |  |  |  | 1180 | $self->{$_} = $args{$_}; | 
| 57 | 789 |  |  |  |  | 1109 | delete $args{$_}; | 
| 58 |  |  |  |  |  |  | } | 
| 59 | 263 | 100 |  |  |  | 661 | if(my $extra_keys = join ',', sort keys %args){ | 
| 60 | 1 |  |  |  |  | 9 | croak "Unknown parameters: $extra_keys"; | 
| 61 |  |  |  |  |  |  | } | 
| 62 | 262 |  |  |  |  | 433 | $self->{id} = $current_id; | 
| 63 | 262 |  |  |  |  | 333 | $current_id++; | 
| 64 | 262 |  |  |  |  | 343 | bless $self, $class; | 
| 65 | 262 |  |  |  |  | 713 | 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 | 2204 | my %args = @_; | 
| 79 | 3 |  |  |  |  | 15 | 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 | 182953 | my ($self) = @_; | 
| 90 | 90917 |  |  |  |  | 184518 | 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 | 60808 |  |  | 60808 | 1 | 131996 | my ($self) = @_; | 
| 103 |  |  |  |  |  |  | # make a safe copy | 
| 104 | 60808 |  |  |  |  | 67868 | return [@{ $self->{features} }]; | 
|  | 60808 |  |  |  |  | 566951 |  | 
| 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 | 402 | my ($self) = @_; | 
| 115 | 76 | 100 |  |  |  | 185 | if(!defined $self->{comment}){ | 
| 116 | 1 |  |  |  |  | 2 | $self->{comment} = join ',', @{ $self->{features} }; | 
|  | 1 |  |  |  |  | 4 |  | 
| 117 |  |  |  |  |  |  | } | 
| 118 | 76 |  |  |  |  | 428 | 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 | 759 | my ($self) = @_; | 
| 128 | 481 |  |  |  |  | 619 | return scalar @{$self->features}; | 
|  | 481 |  |  |  |  | 815 |  | 
| 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 | 25 | my ($self) = @_; | 
| 139 | 13 |  |  |  |  | 51 | return $self->{id}; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | 1; | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | __END__ |