#!/usr/bin/perl

use File::Slurp; 
use Data::Dumper; 
sub metric($$) 
{ 
    $w1 = shift; 
    $w2 = shift;  
    my $c = 0; 
    @ww1 = split//,$w1; 
    @ww2 = split//,$w2; 
    $diff = 0; 
    for $l (@ww1) 
    {  
        if ($l ne $ww2[$c]) 
        { 
#            if ($l =~ /[aeiouy]/ && $ww2[$c] =~ /[aeiouy]/) 
#            { 
                $pr = "$l$ww2[$c]"; $diff++; 
#            } 
#            else { return (-1,"") }
        } 

        $c++; 
    } 

    return ($diff,$pr); 
}  

@dic = read_file("slowa.txt"); 
@A = 'a'..'z'; # qw/a e i o u y/; 
$i = 1; 

for ($c = 0; $c < scalar(@A); $c++)
{
    for ($d = 0; $d < scalar(@A); $d++)
#    for ($d = $c+1; $d < scalar(@A); $d++)
    {
        $a = $A[$c];    
        $b = $A[$d];    

        next if $a eq $b; 
        print "$i. $a $b\n"; 
        $i++; 
        $pair{"$a$b"} = 0 if $a lt $b;
    }
} 

for ($c = 0; $c < scalar(@dic); $c++) 
{ 
    $x = $dic[$c]; 
    chomp $x;  
    
    $y=$dic[$c+1]; 
    chomp $y; 

    next if $x eq $y; 
    next if length($x) != length($y); 
    ($n,$pr) = metric($x,$y);
 
    if ($n == 1) 
    { 
        $pr =~ m/(.)(.)/;
        $pr = "$2$1" if $2 lt $1;

        if (exists $pair{$pr}) 
        { 
            $pair{$pr}++; 
            print "$pr [$x] [$y]\n";
        } 
    }
}

# print Dumper \%pair;
for $k (sort { $pair{$a} <=> $pair{$b} } keys %pair)
{
    print "$k => $pair{$k}\n";
}
