2 # acromang tries to solve acrostics, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1153 2008-04-08 00:04:20Z don $
17 acromang - try to solve acrostics
21 acromang m??s??c??o [options]
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 Default wordlist, should be gigantic; defaults to
37 =item B<--database-dir>
39 Directory for storing databases; defaults to ~/.acromang
43 Debug verbosity. (Default 0)
47 Display brief useage information.
66 use Params::Validate qw(:types validate_with);
69 use MLDBM qw(DB_File Storable);
70 use Fcntl qw/O_RDWR O_RDONLY O_CREAT O_TRUNC/;
71 # Use portable Storable images
72 $MLDBM::DumpMeth=q(portable);
76 use List::Util qw(min max);
78 my %options = (debug => 0,
81 wordlist => '/usr/share/dict/words',
82 database_dir => User->Home.'/.acromang/',
87 my @puzzle_types = qw(acrostic anagram substitution);
92 'wordlist=s','database_dir|database-dir=s',
94 'debug|d+','help|h|?','man|m');
96 pod2usage() if $options{help};
97 pod2usage({verbose=>2}) if $options{man};
99 $DEBUG = $options{debug};
102 if (not grep {exists $options{$_}} @puzzle_types) {
103 $options{acrostic} = 1
106 if (1 != grep {exists $options{$_}} @puzzle_types) {
107 push @USAGE_ERRORS, "You must pass exactly one of ".join(', ',map {"--$_ "} @puzzle_types);
110 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
116 my @words = map {s/[^A-Za-z\?]//g; length $_?$_:();} split /[\s\n]+/, $text;
118 print STDERR "There were no words in the input\n";
123 my @l_o = ('a'..'z');
125 @l_o{@l_o} = (0 .. 25);
127 return map {tr/A-Z/a-z/; tr/éüöáí/euoai/; $_ } @_
130 my $database = update_and_load_database($options{database_dir},$options{wordlist});
132 # run through and use the database
134 # for now, concentrate on finding single word solutions
135 if ($options{acrostic}) {
136 foreach my $word (@words) {
137 my $length = length($word);
138 my @let = split //, $word;
140 for my $p (0..$#let) {
141 next if $let[$p] eq '?';
144 my @allowable_words = find_allowable_words(database => $database,position => \%pos,length => $length);
145 if (@allowable_words > 200 and not $options{show_all}) {
146 print STDERR "Only showing 200 of the " . @allowable_words . " possible words\n";
147 @allowable_words = @allowable_words[0..199];
149 # multiword currently not enabled
150 # while ((@allowable_words == 0 or $options{multiword}) and
151 # (@allowable_words < 200 or $options{show_all})
153 # # try for multiple word solutions, start with the longest
156 # # try to split the number of known letters in half, and
157 # # start increasing and decreasing in both directions
159 # # don't attempt to split each part into bits unless there
162 # # avoid searching for words when we only have a length and
163 # # no position information
165 print map {$_,qq(\n)} sort @allowable_words;
168 elsif ($options{anagram}) {
169 foreach my $word (@words) {
170 my $length = length($word);
171 my $sorted_word = join ('', sort split //, $word);
172 my @allowable_words = find_allowable_words(database => $database,
173 anagram => $sorted_word,
175 print map {$_,qq(\n)} sort @allowable_words;
179 sub find_allowable_words {
180 my %param = validate_with(params => \@_,
181 spec => {database => {type => HASHREF,
183 position => {type => HASHREF,
186 length => {type => SCALAR,
189 substitution => {type => SCALAR,
192 rotation => {type => SCALAR,
195 anagram => {type => SCALAR,
201 my $database = $param{database};
203 if (exists $param{length} and defined $param{length} and $param{length} > 0) {
206 if (exists $param{position} and defined $param{position}) {
207 $must_match += scalar keys %{$param{position}};
209 if (exists $param{anagram} and defined $param{anagram}) {
213 if ($must_match <= 0) {
214 die "There must be something to try matching against";
217 if (exists $param{length} and defined $param{length} and $param{length} > 0) {
218 for my $word (@{$database->{length}{$param{length}}}) {
222 if (exists $param{position} and defined $param{position}) {
223 for my $position (keys %{$param{position}}) {
224 for my $word (@{$database->{position}{$position . ' ' . $param{position}{$position}}}) {
229 if (exists $param{anagram} and defined $param{anagram}) {
230 for my $word (@{$database->{anagram}{$param{anagram}}}) {
234 return grep {$words{$_} >= $must_match} keys %words;
238 sub update_and_load_database {
239 my ($dir,$wordlist) = @_;
240 # check to see if the wordlist is newer than our database
241 my @dbs = qw(length position substitution rotation anagram);
242 my $update_required = 0;
243 my $wordlist_time = 1;
244 if (! -r $wordlist) {
245 die "Word list $wordlist doesn't exist or isn't readable";
247 my $wordlist_stat = stat($wordlist);
248 $wordlist_time = max($wordlist_time,$wordlist_stat->mtime);
250 if (! -e "$dir/db_${db}") {
251 $update_required = 1;
253 elsif (stat("$dir/db_${db}")->mtime < $wordlist_time) {
254 $update_required = 1;
258 if ($update_required) {
261 mkdir($dir) or die "Unable to create directory $dir: $!";
264 $database->{$db} = {};
265 $disk_db->{$db} = {};
266 tie %{$disk_db->{$db}}, MLDBM => "$dir/db_${db}",
267 O_RDWR|O_CREAT|O_TRUNC, 0666
268 or die "Unable to open/create $dir/db_${db}: $!";
270 my $wordlist_fh = IO::File->new($wordlist,'r') or
271 die "Unable to open $wordlist for reading: $!";
273 while (<$wordlist_fh>) {
275 next unless length $_;
277 my ($word) = word_sanitize($_);
278 $word =~ s/[^a-z]//g;
279 next unless length $_;
280 next if exists $seen_words{$word};
281 $seen_words{$word} = 1;
282 if ((keys %seen_words) % 100 == 0) {
283 print STDERR "Handled ".(keys %seen_words) . " words, on $word\n";
285 my @l = split //, $word;
286 my $l = length($word);
288 # position from the beginning
289 my $temp = $database->{position}{"$p $l[$p]"};
290 $temp = [] if not defined $temp;
291 push @{$temp}, $word;
292 $database->{position}{"$p $l[$p]"} = $temp;
293 # this is the position from the end
294 $temp = $database->{position}{$p-$l . " " . $l[$p-$l]};
295 $temp = [] if not defined $temp;
296 push @{$temp}, $word;
297 $database->{position}{$p-$l . " " . $l[$p-$l]} = $temp;
299 my $temp = $database->{length}{$l};
300 $temp = [] if not defined $temp;
301 push @{$temp}, $word;
302 $database->{length}{$l} = $temp;
303 # this is the substitution database
304 # in it, hello and giddy both become abccd
307 if (exists $uc{$_}) {
315 @s_map{@uc_order} = ('a'..'z')[0..$#uc_order] if @uc_order;
316 my $mapped_word = join('',map {$s_map{$_}} @l);
317 $temp = $database->{substitution}{$mapped_word};
318 $temp = [] if not defined $temp;
319 push @{$temp}, $word;
320 $database->{substitution}{$mapped_word} = $temp;
322 # this is the rotation database (for ceaser cyphers)
323 # in it, hello becomes axeeh, giddy acxxs
325 my $index = $l_o{$fl};
326 if (not defined $index or grep {not exists $l_o{$_}} @l) {
327 print STDERR "Problem with some letters in '$word'\n";
329 $mapped_word = join('',map {$l_o[($l_o{$_} - $index + 26) % 26]} @l);
330 $temp = $database->{rotation}{$mapped_word};
331 $temp = [] if not defined $temp;
332 push @{$temp}, $word;
333 $database->{rotation}{$mapped_word} = $temp;
335 # this is the anagram database
336 # in it, hello becomes ehllo, giddy ddgiy
337 $mapped_word = join('',sort @l);
338 $temp = $database->{anagram}{$mapped_word};
339 $temp = [] if not defined $temp;
340 push @{$temp}, $word;
341 $database->{anagram}{$mapped_word} = $temp;
343 for my $key1 (keys %{$database}) {
344 for my $key2 (keys %{$database->{$key1}}) {
345 $disk_db->{$key1}{$key2} = $database->{$key1}{$key2};
351 $database->{$db} = {};
352 tie %{$database->{$db}}, MLDBM => "$dir/db_${db}",
354 or die "Unable to open $dir/db_${db}: $!";