Jaro similarity: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl}}: add strict & warnings, sundry simplifications, 2 more tests)
m (→‎{{header|Raku}}: a little more idiomatic, two more tests)
Line 2,429: Line 2,429:
{{trans|Perl}}
{{trans|Perl}}
<lang perl6>sub jaro ($s, $t) {
<lang perl6>sub jaro ($s, $t) {

return 1 if $s eq $t;
return 1 if $s eq $t;


my $s_len = + my @s = $s.comb;
my $s_len = + my @s = $s.comb;
my $t_len = + my @t = $t.comb;
my $t_len = + my @t = $t.comb;

my $match_distance = ($s_len max $t_len) div 2 - 1;
my $match_distance = ($s_len max $t_len) div 2 - 1;


my @s_matches;
my ($matches, @s_matches, @t_matches);
my @t_matches;
my $matches = 0;

for ^@s -> $i {
for ^@s -> $i {

my $start = 0 max $i - $match_distance;
my $start = 0 max $i - $match_distance;
my $end = $i + $match_distance min ($t_len - 1);
my $end = $i + $match_distance min ($t_len - 1);


for $start .. $end -> $j {
for $start .. $end -> $j {
@t_matches[$j] and next;
next if @t_matches[$j] or @s[$i] ne @t[$j];
@s[$i] eq @t[$j] or next;
(@s_matches[$i], @t_matches[$j]) = (1, 1);
@s_matches[$i] = 1;
$matches++ and last;
@t_matches[$j] = 1;
$matches++;
last;
}
}
}
}
return 0 unless $matches;


return 0 if $matches == 0;
my ($k, $transpositions) = (0, 0);

my $k = 0;
my $transpositions = 0;

for ^@s -> $i {
for ^@s -> $i {
@s_matches[$i] or next;
next unless @s_matches[$i];
until @t_matches[$k] { ++$k }
$k++ until @t_matches[$k];
@s[$i] eq @t[$k] or ++$transpositions;
$transpositions++ if @s[$i] ne @t[$k];
++$k;
$k++;
}
}


($matches / $s_len + $matches / $t_len +
( $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}}
{{out}}
<pre>
<pre>0.944
0.944444
0.767
0.766667
0.896
1.000
0.896296
</pre>
1.000</pre>


=={{header|REXX}}==
=={{header|REXX}}==