#!/usr/bin/perl use strict; use warnings; use Storable; use Text::LevenshteinXS 'distance'; my @word; open(my $fh, '<', 'four_dict.txt') or die $!; while (<$fh>) { tr/\r\n//d; push @word, $_; } my %dict; for my $i (0 .. $#word - 1) { my $i_score = score($word[$i]); for my $j ($i + 1 .. $#word) { if (distance($word[$i], $word[$j]) == 1) { my $j_score = score($word[$i]); $_ = $word[$i] ^ $word[$j]; /[^\0]/; my $pos = $-[0]; my $i_let = substr($word[$i], $pos, 1); my $j_let = substr($word[$j], $pos, 1); push @{$dict{$word[$i]}}, {word => $word[$j], score => $j_score, letter => $j_let, position => $pos}; push @{$dict{$word[$j]}}, {word => $word[$i], score => $i_score, letter => $i_let, position => $pos}; } } } store \%dict, 'word_list.db'; sub score { my ($word) = @_; my %point = ( A => 10, B => 40, C => 40, D => 30, E => 10, F => 50, G => 40, H => 40, I => 20, J => 80, K => 40, L => 20, M => 30, N => 20, O => 10, P => 30, Q => 100, R => 20, S => 10, T => 20, U => 20, V => 60, W => 80, X => 90, Y => 40, Z => 80 ); my $score = 0; for (split //, $word) { $_ = uc($_); $score += $point{$_}; } return $score; }