Jaro similarity: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: add strict & warnings, sundry simplifications, 2 more tests) |
SqrtNegInf (talk | contribs) 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; |
|||
⚫ | |||
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] |
next if @t_matches[$j] or @s[$i] ne @t[$j]; |
||
@ |
(@s_matches[$i], @t_matches[$j]) = (1, 1); |
||
$matches++ and last; |
|||
@t_matches[$j] = 1; |
|||
$matches++; |
|||
last; |
|||
} |
} |
||
} |
} |
||
⚫ | |||
my ($k, $transpositions) = (0, 0); |
|||
my $k = 0; |
|||
my $transpositions = 0; |
|||
for ^@s -> $i { |
for ^@s -> $i { |
||
@s_matches[$i] |
next unless @s_matches[$i]; |
||
until @t_matches[$k] |
$k++ until @t_matches[$k]; |
||
@s[$i] |
$transpositions++ if @s[$i] ne @t[$k]; |
||
$k++; |
|||
} |
} |
||
($matches |
( $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. |
0.767 |
||
0. |
0.896 |
||
1.000 |
|||
0.896296 |
|||
</pre> |
1.000</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |