Skip to content

Commit

Permalink
Use LibSVM::Grammar for .load-problem
Browse files Browse the repository at this point in the history
  • Loading branch information
titsuki committed Oct 21, 2019
1 parent 523ca1d commit fb930fd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Algorithm/LibSVM.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ method !_load-problem(\lines) {

my $y-idx = 0;
for lines -> $line {
my ($label, $features) = $line.trim.split(/\s+/,2);
my @feature-list = $features.split(/\s+/);
my $parsed = Algorithm::LibSVM::Grammar.parse($line, actions => Algorithm::LibSVM::Actions).made;
my ($label, $feature) = $parsed.head<label>, $parsed.head<pairs>;

my $next = Algorithm::LibSVM::Node.new(index => -1, value => 0e0);
for @feature-list>>.split(":", :skip-empty).map({ .[0] => .[1] }).sort(-*.key).map({ .key, .value }) -> ($index, $value) {
for @($feature).sort(-*.key).map({ .key, .value }) -> ($index, $value) {
$!nr-feature = ($!nr-feature, $index.Int).max;
$next = Algorithm::LibSVM::Node.new(index => $index.Int, value => $value.Num, next => $next);
}
Expand Down
4 changes: 4 additions & 0 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ use Algorithm::LibSVM::Model;
lives-ok { my $libsvm = Algorithm::LibSVM.new }, "Algorithm::LibSVM.new should create a instance";
}

{
lives-ok { my $libsvm = Algorithm::LibSVM.new.load-problem(("1 1:0 # comment",)) }, "Algorithm::LibSVM.load-problem should read lines include comments";
}

done-testing;

0 comments on commit fb930fd

Please sign in to comment.