# I'm ignoring all words that have 4 letters or less.
# No sense in having the question and answer the same.
# 
# I'm also ignoring all characters other than [a-z0-9].
# So, "ain't" will be ignored
# and "anybody'd" will have "odyd" as one of its blocks.
# 
# I've used a hash of arrays to store all combinations,
# however, as the dictionary gets larger so thus the
# memory requirements.
# 
# I've written another version which will write all the
# block<=>word combination to a file, sort the file
# and then use the sorted file to create the questions
# and answers files. This will reduce the memory requirement
# since no hash is used, but there is a weak link.
# 
# The weak link in this program is while sorting
# the file. I've used the sort utility in Unix to sort the
# file, but I'm assuming that inorder to sort a file, 'sort'
# will anyway have to load all lines into memory.
# 
# I could be wrong though...
# 
# I'm sending the other version in another email.
# 
# Bye.
# George P.


#! /opt/bin/perl5 -w

## Ignoring all words which are of 4 words or less
use strict;
use Getopt::Std;

my (%opts, $err);
getopts('d:', \%opts);
$err = "Usage: $0 -d <dictionary_file>\n";
die("$err\n") if (!defined $opts{d} || $opts{d} =~ /^ *$/);

my ($tmpdir, $qfl, $afl, %words);
$tmpdir = "/tmp";
$qfl = "$tmpdir/questions";
$afl = "$tmpdir/answers";

open (DICT, "< $opts{d}") or die("ERROR: Cannot open $opts{d}: $!\n");
while (my $wrd = lc <DICT>)
	{
	chomp $wrd;
	my ($tmpwrd, %blks);
	($tmpwrd = $wrd) =~ s/[^a-z0-9]//g;
	next if (length $tmpwrd < 5);
	while (length $tmpwrd >= 4)
		{
		$blks{substr($tmpwrd, 0, 4)} = 1;
		$tmpwrd =~ s/^.//;
		}
	foreach (keys %blks)
		{ push (@{$words{$_}}, $wrd) if ($#{$words{$_}} < 1); }
	}
close DICT;

open (QUES, "> $qfl") or die("ERROR: Cannot open $qfl: $!\n");
open (ANS, "> $afl") or die("ERROR: Cannot open $afl: $!\n");

foreach my $key (sort keys %words)
	{
	next if ($#{$words{$key}} > 0);
	print QUES "$key\n";
	print ANS "$words{$key}[0]\n";
	}
close ANS;
close QUES;

