Jaro similarity: Difference between revisions

m
→‎{{header|Raku}}: a little more idiomatic, two more tests
m (→‎{{header|Perl}}: add strict & warnings, sundry simplifications, 2 more tests)
m (→‎{{header|Raku}}: a little more idiomatic, two more tests)
Line 2,429:
{{trans|Perl}}
<lang perl6>sub jaro ($s, $t) {
 
return 1 if $s eq $t;
 
my $s_len = + my @s = $s.comb;
my $t_len = + my @t = $t.comb;
 
my $match_distance = ($s_len max $t_len) div 2 - 1;
 
my ($matches, @s_matches, @t_matches);
my @t_matches;
my $matches = 0;
 
for ^@s -> $i {
 
my $start = 0 max $i - $match_distance;
my $end = $i + $match_distance min ($t_len - 1);
 
for $start .. $end -> $j {
next if @t_matches[$j] andor next@s[$i] ne @t[$j];
(@ss_matches[$i] eq, @tt_matches[$j]) = or(1, next1);
@s_matches[$i]matches++ =and 1last;
@t_matches[$j] = 1;
$matches++;
last;
}
}
myreturn $matches0 =unless 0$matches;
 
returnmy 0 if($k, $matchestranspositions) == (0, 0);
 
my $k = 0;
my $transpositions = 0;
 
for ^@s -> $i {
next unless @s_matches[$i] or next;
$k++ until @t_matches[$k] { ++$k };
$transpositions++ if @s[$i] eqne @t[$k] or ++$transpositions;
++$k++;
}
 
( $matches / $s_len + $matches / $t_len + (($matches - $transpositions/2) / $matches) ) / 3
(($matches - $transpositions / 2) / $matches)) / 3;
}
 
say jaro(.key, .value).fmt: '%.3f' for
printf("%f\n", jaro("MARTHA", "MARHTA"));
'MARTHA' => 'MARHTA', 'DIXON' => 'DICKSONX', 'JELLYFISH' => 'SMELLYFISH',
printf("%f\n", jaro("DIXON", "DICKSONX"));
'I repeat myself' => 'I repeat myself', '' => '';</lang>
printf("%f\n", jaro("JELLYFISH", "SMELLYFISH"));</lang>
{{out}}
<pre>0.944
0.944444767
0.766667896
1.000
0.896296
1.000</pre>
 
=={{header|REXX}}==
2,392

edits