| 1 | package Algorithm::LibLinear::DataSet; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 6 | ▶ | use 5.014; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 6 | ▶ | use Algorithm::LibLinear::Types qw/FeatureWithLabel/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 6 | ▶ | use Carp qw//; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 6 | ▶ | use List::MoreUtils qw/none/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 6 | ▶ | use Smart::Args::TypeTiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use Types::Standard qw/ArrayRef ClassName FileHandle InstanceOf Num Str/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 6 | my $InstanceOfPackage = InstanceOf[__PACKAGE__]; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 11 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 13 | 11 | args | |||||||||||||||||||
| 14 | my $class => ClassName, | ||||||||||||||||||||
| 15 | my $data_set => ArrayRef[FeatureWithLabel]; | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 11 | bless +{ data_set => $data_set } => $class; | |||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 5 | ▶ | sub load { | ||||||||||||||||||
|
Subroutine
load: called CC=5 SCAR=17.1
Pod
documented
| |||||||||||||||||||||
| 21 | 5 | args | |||||||||||||||||||
| 22 | my $class => ClassName, | ||||||||||||||||||||
| 23 | my $fh => +{ isa => FileHandle, optional => 1, }, | ||||||||||||||||||||
| 24 | my $filename => +{ isa => Str, optional => 1, }, | ||||||||||||||||||||
| 25 | my $string => +{ isa => Str, optional => 1, }; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 5 | ▶ | if (none { defined } ($fh, $filename, $string)) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 0 | Carp::croak('No source specified.'); | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | 5 | my $source = $fh; | |||||||||||||||||||
| 31 | 5 | ▶ | $source //= do { | ||||||||||||||||||
|
Condition66%2/3
$source //= do {
open my $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
MC/DC0%0/2
$source //= do {
open my $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
$source
do {
open my $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
Truth table66%2/3
A //= B
A= $sourceB= do {
open my $fh, "<", $filename // \$string or Carp::croak($!);
$fh
}
| |||||||||||||||||||||
| 32 | 4 | ▶ | open my $fh, '<', +($filename // \$string) or Carp::croak($!); | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$filename // \$string
MC/DC0%0/1
$filename // \$string
$filename
Truth table50%1/2
A
A= $filename
| |||||||||||||||||||||
| 33 | 4 | $fh; | |||||||||||||||||||
| 34 | }; | ||||||||||||||||||||
| 35 | 5 | $class->new(data_set => $class->parse_input_file($source)); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | ▶ | sub add_data { | ||||||||||||||||||
|
Subroutine
add_data: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 39 | 0 | args | |||||||||||||||||||
| 40 | my $self => $InstanceOfPackage, | ||||||||||||||||||||
| 41 | my $data => FeatureWithLabel; | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | push @{ $self->data_set }, $data; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 38 | ▶ | sub as_arrayref { $_[0]->{data_set} } | ||||||||||||||||||
|
Subroutine
as_arrayref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 12 | ▶ | sub as_problem { | ||||||||||||||||||
|
Subroutine
as_problem: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 49 | 12 | args | |||||||||||||||||||
| 50 | my $self => $InstanceOfPackage, | ||||||||||||||||||||
| 51 | my $bias => +{ isa => Num, default => -1.0, }; | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 12 | my (@features, @labels); | |||||||||||||||||||
| 54 | 12 | for my $data (@{ $self->as_arrayref }) { | |||||||||||||||||||
| 55 | 1626 | push @features, $data->{feature}; | |||||||||||||||||||
| 56 | 1626 | push @labels, $data->{label}; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | 12 | Algorithm::LibLinear::Problem->new(\@labels, \@features, $bias); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | ▶ | sub as_string { | ||||||||||||||||||
|
Subroutine
as_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 62 | 1 | args | |||||||||||||||||||
| 63 | my $self => $InstanceOfPackage; | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | my $result = ''; | |||||||||||||||||||
| 66 | 1 | for my $entry (@{ $self->as_arrayref }) { | |||||||||||||||||||
| 67 | 5 | my $feature = $entry->{feature}; | |||||||||||||||||||
| 68 | my @feature_dump = | ||||||||||||||||||||
| 69 | 5 | map { "$_:$feature->{$_}" } sort { $a <=> $b } keys %$feature; | |||||||||||||||||||
| 70 | 5 | $result .= join(' ', $entry->{label}, @feature_dump) . "\n"; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | 1 | return $result; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 5 | ▶ | sub parse_input_file { | ||||||||||||||||||
|
Subroutine
parse_input_file: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 76 | 5 | args_pos | |||||||||||||||||||
| 77 | my $class => ClassName, | ||||||||||||||||||||
| 78 | my $source => FileHandle; | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 5 | my @data_set; | |||||||||||||||||||
| 81 | 5 | ▶ | while (defined(my $line = <$source>)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 819 | chomp $line; | |||||||||||||||||||
| 83 | 819 | my ($label, @feature) = split /\s+/, $line; | |||||||||||||||||||
| 84 | 819 | $label += 0; | |||||||||||||||||||
| 85 | my %feature = map { | ||||||||||||||||||||
| 86 | 819 | my ($index, $value) = split /:/; | |||||||||||||||||||
| 87 | 10165 | $index += 0; | |||||||||||||||||||
| 88 | 10165 | $value += 0; | |||||||||||||||||||
| 89 | 10165 | ($index => $value); | |||||||||||||||||||
| 90 | } @feature; | ||||||||||||||||||||
| 91 | 819 | push @data_set, +{ feature => \%feature, label => $label, }; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 5 | return \@data_set; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 6 | ▶ | sub size { 0 + @{ $_[0]->as_arrayref } } | ||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 6 | 1; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | __DATA__ | ||||||||||||||||||||