#!/usr/bin/perl -w
#
# Given a string of letters, checks a word list for words that contain
# any substring that differs from the given string by the addition of
# one letter.
#
# http://www.aigeek.com/wordsprout/
# Seth Golub <seth@aigeek.com>
# Oct 2000

use strict;
use CGI;

my $cgi = new CGI;
my $seedword = $cgi->param('s');
my $dictfile = "/usr/share/dict/words";

my @regexps = ( ".$seedword" );
my $len = length($seedword);
for my $i ( 1 .. $len )
{
  push( @regexps, sprintf("%s[^%s]%s", substr( $seedword, 0, $i ),
                                       substr( $seedword, $i-1, 1 ),
                                       substr( $seedword, $i, $len ) ) );
}

open( DICT, $dictfile ) or die "Failed to open $dictfile   ";
my @dict = <DICT>;
chop @dict;
close( DICT );

my %unique_words;

foreach my $regex ( @regexps )
{
  map { $unique_words{$_} = 1 } ( grep { /$regex/ } @dict );
}

print "Content: text/plain\n\n";
print "Valid goal words with one-letter additions to \"$seedword\":\n\n";
print join( "\n", sort( keys( %unique_words ) ) ) . "\n";
print "\n--- the end ---"

__END__
