| 1 | package AI::Categorizer::FeatureVector; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 122 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 4 | 122 | my ($package, %args) = @_; | |||||||||||||
| 5 | 122 | ▶ | $args{features} ||= {}; | ||||||||||||
|
Condition100%2/2
$args{"features"} ||= {}
MC/DC100%1/1
$args{"features"} ||= {}
$args{"features"}
Truth table100%2/2
A
A= $args{"features"}
| |||||||||||||||
| 6 | 122 | return bless {features => $args{features}}, $package; | |||||||||||||
| 7 | } | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 3 | ▶ | sub names { | ||||||||||||
|
Subroutine
names: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 10 | 3 | my $self = shift; | |||||||||||||
| 11 | 3 | return keys %{$self->{features}}; | |||||||||||||
| 12 | } | ||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | ▶ | sub set { | ||||||||||||
|
Subroutine
set: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 15 | 1 | my $self = shift; | |||||||||||||
| 16 | 1 | ▶ | $self->{features} = (ref $_[0] ? $_[0] : {@_}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | } | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 154 | ▶ | sub as_hash { | ||||||||||||
|
Subroutine
as_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 20 | 154 | my $self = shift; | |||||||||||||
| 21 | 154 | return $self->{features}; | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 49 | ▶ | sub euclidean_length { | ||||||||||||
|
Subroutine
euclidean_length: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 25 | 49 | my $self = shift; | |||||||||||||
| 26 | 49 | my $f = $self->{features}; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 49 | my $total = 0; | |||||||||||||
| 29 | 49 | foreach (values %$f) { | |||||||||||||
| 30 | 296 | $total += $_**2; | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | 49 | return sqrt($total); | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 48 | ▶ | sub normalize { | ||||||||||||
|
Subroutine
normalize: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 36 | 48 | my $self = shift; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 48 | my $length = $self->euclidean_length; | |||||||||||||
| 39 | 48 | ▶ | return $length ? $self->scale(1/$length) : $self; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 50 | ▶ | sub scale { | ||||||||||||
|
Subroutine
scale: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 43 | 50 | my ($self, $scalar) = @_; | |||||||||||||
| 44 | 50 | $_ *= $scalar foreach values %{$self->{features}}; | |||||||||||||
| 45 | 50 | return $self; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 16 | ▶ | sub as_boolean_hash { | ||||||||||||
|
Subroutine
as_boolean_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 49 | 16 | my $self = shift; | |||||||||||||
| 50 | 16 | return { map {($_ => 1)} keys %{$self->{features}} }; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | ▶ | sub length { | ||||||||||||
|
Subroutine
length: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 54 | 0 | my $self = shift; | |||||||||||||
| 55 | 0 | return scalar keys %{$self->{features}}; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 4 | ▶ | sub clone { | ||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 59 | 4 | my $self = shift; | |||||||||||||
| 60 | 4 | return ref($self)->new( features => { %{$self->{features}} } ); | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 16 | ▶ | sub intersection { | ||||||||||||
|
Subroutine
intersection: called CC=6 SCAR=20.5
Pod
undocumented
| |||||||||||||||
| 64 | 16 | my ($self, $other) = @_; | |||||||||||||
| 65 | 16 | ▶ | $other = $other->as_hash if UNIVERSAL::isa($other, __PACKAGE__); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | 16 | my $common; | |||||||||||||
| 68 | 16 | ▶ | if (UNIVERSAL::isa($other, 'ARRAY')) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 69 | 0 | ▶ | $common = {map {exists $self->{features}{$_} ? ($_ => $self->{features}{$_}) : ()} @$other}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | } elsif (UNIVERSAL::isa($other, 'HASH')) { | ||||||||||||||
| 71 | 16 | ▶ | $common = {map {exists $self->{features}{$_} ? ($_ => $self->{features}{$_}) : ()} keys %$other}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | } | ||||||||||||||
| 73 | 16 | return ref($self)->new( features => $common ); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 114 | ▶ | sub add { | ||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 77 | 114 | my ($self, $other) = @_; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 114 | ▶ | $other = $other->as_hash if UNIVERSAL::isa($other, __PACKAGE__); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 114 | ▶ | while (my ($k,$v) = each %$other) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 741 | $self->{features}{$k} += $v; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 82 | ▶ | sub dot { | ||||||||||||
|
Subroutine
dot: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 86 | 82 | my ($self, $other) = @_; | |||||||||||||
| 87 | 82 | ▶ | $other = $other->as_hash if UNIVERSAL::isa($other, __PACKAGE__); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | |||||||||||||||
| 89 | 82 | my $sum = 0; | |||||||||||||
| 90 | 82 | my $f = $self->{features}; | |||||||||||||
| 91 | 82 | ▶ | while (my ($k, $v) = each %$f) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 456 | ▶ | $sum += $other->{$k} * $v if exists $other->{$k}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | } | ||||||||||||||
| 94 | 82 | return $sum; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | ▶ | sub sum { | ||||||||||||
|
Subroutine
sum: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 98 | 0 | my ($self) = @_; | |||||||||||||
| 99 | |||||||||||||||
| 100 | # Return total of values in this vector | ||||||||||||||
| 101 | 0 | my $total = 0; | |||||||||||||
| 102 | 0 | $total += $_ foreach values %{ $self->{features} }; | |||||||||||||
| 103 | 0 | return $total; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 17 | ▶ | sub includes { | ||||||||||||
|
Subroutine
includes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 107 | 17 | return exists $_[0]->{features}{$_[1]}; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 5 | ▶ | sub value { | ||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 111 | 5 | return $_[0]->{features}{$_[1]}; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | ▶ | sub values { | ||||||||||||
|
Subroutine
values: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||
| 116 | 0 | return @{ $self->{features} }{ @_ }; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 12 | 1; | |||||||||||||
| 120 | __END__ | ||||||||||||||