Skip to content

Commit aafcd2c

Browse files
committed
feat:Implemented application of tag and tag type weights.
1 parent 35569a8 commit aafcd2c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/ML/SparseMatrixRecommender.rakumod

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,54 @@ class ML::SparseMatrixRecommender
437437
return self;
438438
}
439439

440+
##========================================================
441+
## Apply tag weights
442+
##========================================================
443+
method apply-tag-weights(@weights) {
444+
my $col-count = self.take-M.columns-count;
445+
die "The first argument is expected to be a list of numbers of length $col-count."
446+
unless @weights.elems == $col-count && @weights.all ~~ Numeric:D;
447+
448+
my $W = Math::SparseMatrix.new(diagonal => @weights);
449+
my $mat-res = self.take-M.clone.dot($W);
450+
451+
$mat-res.set-row-names(self.take-M.row-names);
452+
$mat-res.set-column-names(self.take-M.column-names);
453+
454+
self.set-M($mat-res);
455+
return self;
456+
}
457+
458+
##========================================================
459+
## Apply tag type weights
460+
##========================================================
461+
method apply-tag-type-weights($weights) {
462+
my @weights-list;
463+
464+
if $weights ~~ (Array:D | List:D | Seq:D) {
465+
die "A list first argument is expected to match the number of sub-matrices ({%!matrices.elems}) and have numeric elements."
466+
unless $weights.elems == %!matrices.elems && $weights.all ~~ Numeric:D;
467+
468+
my @keys = %!matrices.keys;
469+
for $weights.kv -> $i, $w {
470+
@weights-list.append( $w xx %!matrices{@keys[$i]}.columns-count );
471+
}
472+
} elsif $weights ~~ Map:D {
473+
my @unknown-keys = $weights.keys.grep({ %!matrices{$_}:!exists });
474+
die "Unknown tag types in weights argument: {@unknown-keys}."
475+
if @unknown-keys;
476+
477+
for %!matrices.keys -> $key {
478+
my $w = $weights{$key} // 1;
479+
@weights-list.append( $w xx %!matrices{$key}.columns-count );
480+
}
481+
} else {
482+
die "The first argument must be a list or a hashmap of length {%!matrices.elems}.";
483+
}
484+
485+
return self.apply-tag-weights(@weights-list);
486+
}
487+
440488
##========================================================
441489
## Profile
442490
##========================================================

t/02-basic-workflows.rakutest

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,18 @@ subtest {
8686
is $prof>>.value.all ~~ Numeric:D, True;
8787
}, 'expected recommend by profile object -- scored history';
8888

89+
## 6
90+
subtest {
91+
my %prof = "passengerClass:1st" => 1.2, "passengerSex:male" => 1.4;
92+
ok $smr.apply-tag-type-weights([1, 2, 4, 3]);
93+
isa-ok $smr.recommend-by-profile(%prof).take-value, Array:D;
94+
95+
dies-ok {$smr.apply-tag-type-weights({:10pasenger-class})}, 'failure for unknown tag types';
96+
ok $smr.apply-tag-type-weights({:10passengerClass});
97+
98+
isa-ok $smr.recommend-by-profile(%prof).take-value, Array:D;
99+
100+
# Note: no correctness test,
101+
}, 'changing tag type weights';
102+
89103
done-testing;

0 commit comments

Comments
 (0)