Jaro similarity: Difference between revisions

m
→‎{{header|Perl}}: add strict & warnings, sundry simplifications, 2 more tests
m (→‎Python :: Composition of pure functions: Tidied, updated primitives.)
m (→‎{{header|Perl}}: add strict & warnings, sundry simplifications, 2 more tests)
Line 1,892:
 
=={{header|Perl}}==
<lang perl>use List::Util qw(min max)strict;
use warnings;
use List::Util qw(min max);
 
sub jaro {
my ($s, $t) = @_;
my(@s_matches, @t_matches, $matches);
 
myreturn $s_len1 =if length($s) eq $t;
my $t_len = length($t);
 
return 1 if my($s_len, @s) == 0 and(length $t_lens, ==split //, 0$s);
my($t_len, @t) = split(length $t, split //, $t);
 
my $match_distance = int(max($s_len, $t_len) / 2) - 1;
 
my @s_matches;
my @t_matches;
 
my @s = split(//, $s);
my @t = split(//, $t);
 
my $matches = 0;
foreach my $i (0 .. $#s) {
 
my $match_distance = int (max($s_len, $t_len) / 2) - 1;
foreachfor my $i (0 .. $#s) {
my $start = max(0, $i - $match_distance);
my $end = min($i + $match_distance + 1, $t_len);
for my $j ($start .. $end - 1) {
 
foreach my $j ($start ..next if $endt_matches[$j] -or 1)$s[$i] {ne $t[$j];
($s_matches[$i], $t_matches[$j]) = and(1, next1);
$s[$i]matches++ eqand $t[$j] or nextlast;
$s_matches[$i] = 1;
$t_matches[$j] = 1;
$matches++;
last;
}
}
myreturn $matches0 =unless 0$matches;
 
return 0 ifmy($k, $matchestranspositions) == (0, 0);
for my $i (0 .. $#s) {
 
my $k next unless = 0$s_matches[$i];
$k++ until $s_matchest_matches[$ik] = 1;
my $transpositions++ =if 0$s[$i] ne $t[$k];
 
foreach my $i (0 .. $#s) {k++;
$s_matches[$i] or next;
until ($t_matches[$k]) { ++$k }
$s[$i] eq $t[$k] or ++$transpositions;
++$k;
}
( $matches/$s_len + $matches/$t_len + (($matches - $transpositions / 2) / $matches) ) / 3;
 
(($matches / $s_len) + ($matches / $t_len) +
(($matches - $transpositions / 2) / $matches)) / 3;
}
 
printf( "%f.3f\n", jaro("MARTHA"@$_[0], @$_[1]) "MARHTA"));for
['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|Phix}}==
2,392

edits