I went to the Code Catacombs to post my script for finding cryptographic matches for a given pattern, and discovered that merlyn had already posted pat. (Some time ago, in fact. :) merlyn's script also has the nice feature of allowing the user to specify literal letters, which appear unchanged in the matching word(s).
By the way, depending on your system, /usr/dict/words may not be very useful at all for solving cryptograms. /usr/dict/words was originally intended for use with the spell utility, which does stemming to guess if a word is spelled correctly. Thus, /usr/dict/words may contain only root words and irregular forms, i.e. talk but not talks, talked, or talking.
I've been working on my own program in Perl to solve cryptograms. I recommend the ENABLE word list.
P.S. Oh, what the heck. My code works somewhat differently from merlyn's, so I'll go ahead and post it here. I expect his use of regexes will be faster than my use of split and hashes, though.
#!/usr/local/bin/perl -w
use strict;
use Getopt::Std;
use vars qw($opt_w);
getopts('w:') || die "Bad options.\n";
my $dict = $opt_w || 'wordlist';
open(DICT, $dict) or die "Can't open $dict: $!\n";
my $crypto = shift;
my $match = &crypto_canon($crypto);
while (<DICT>) {
chomp;
next if length $match != length $_;
next if $match ne &crypto_canon($_);
print "$_\n";
}
sub crypto_canon {
my($word) = @_;
my $canon;
my %letters;
my $next = 'a';
foreach (split //, $word) {
if ($_ !~ /[a-z]/) {
$canon .= $_;
next;
} elsif (not exists $letters{$_}) {
$letters{$_} = $next++;
}
$canon .= $letters{$_};
}
$canon;
}
|