Skip to content

Commit

Permalink
Improved robustness of percentagematch function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamieljv committed Jul 18, 2024
1 parent 350658c commit 34e1e1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
12 changes: 7 additions & 5 deletions app/Helpers/vectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ function percentageMatch(array $a, array $b)
if ($va->length() == 0) {
return 0;
}
// normalize vectors
$va = $va->normalize();
$vb = $vb->normalize();
// compute scalar projection of a on b (dot(a, b)/len(b))
return $va->dotProduct($vb) / $vb->length() * 100;

// compute scalar projection of a on b (dot(a, b)/len(b)), divided by len(b) again to get percentage
try {
return min(max($va->dotProduct($vb) / $vb->length() ** 2 * 100, 10), 100);
} catch (DivisionByZeroError $e) {
return null;
}
}
9 changes: 6 additions & 3 deletions resources/views/actiewijzer/result.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class="mt-10"
@foreach($referentie_types as $rt)
<div>
<a href="#{{str_replace(' ', '_', $rt->title)}}" class="hover:font-bold">{{$rt->title}}
&nbsp;<span class="text-pink-600 font-bold">{{$rt->match_perc}}%</span>
&nbsp;
@if($rt->match_perc)<span class="text-pink-600">{{$rt->match_perc}}%</span>@endif
</a>
</div>
@endforeach
Expand All @@ -56,7 +57,7 @@ class="mt-10"
<div class="mt-10 md:gap-6 grid grid-cols-2">
<div class="w-full col-span-2 md:col-span-1">
<h4 class="text-lg mb-2">Thema's</h4>
<div class="flex">
<div class="flex flex-wrap">
@foreach ($themes as $t)
<div
class="relative self-start inline-block px-2 py-1 mr-1 mb-1 text-xs font-medium leading-5 uppercase rounded"
Expand All @@ -79,7 +80,9 @@ class="relative self-start inline-block px-2 py-1 mr-1 mb-1 text-xs font-medium

@foreach($referentie_types as $rt)
<div class="mt-20">
<h2 id="{{str_replace(' ', '_', $rt->title)}}">{{$rt->title}}&nbsp;<span class="text-pink-600">{{$rt->match_perc}}%</span></h2>
<h2 id="{{str_replace(' ', '_', $rt->title)}}">{{$rt->title}}&nbsp;
@if($rt->match_perc)<span class="text-pink-600">{{$rt->match_perc}}%</span>@endif
</h2>
<p>{!! filterScripts($rt->description) !!}</p>
@if ($rt->title == config('app.actiewijzer.demonstrations_section_name'))
<p><i>Demonstraties voor
Expand Down

0 comments on commit 34e1e1a

Please sign in to comment.