#!/usr/bin/perl -w

# etapad -- pad an ETA program into words.
# SCCSID("%Z%%P%	%I%")
#
# This is a rather dumb program that reads an ETA program and writes
# an equivalent one, made up of words where possible.  It does this by
# repeatedly calling etaword on chunks of the program, making no
# attempt at grammar or style in the output.

use strict;

sub findword($);

while (<>) {
    tr/ETAOINSH/etaoinsh/;
    s/[^etaoinsh]//g;

    while ($_) {
	my $n = 5;		# tunable: higher => slower but better
				# expirically, success for n>5 is rare.
	my $word;
	$n = length($_) if $n > length($_);
	while ($n > 0) {
	    $word = findword(substr($_, 0, $n));
	    last if $word;
	    $n--;
	}
	if ($n == 0) {
	    $n = 1;
	    $word = uc(substr($_, 0, 1));
	}
	$_ = substr($_, $n);
	print "$word ";
    }
    print "\n";
}

sub findword($) {
    my $word = shift();
    my @res = `etaword $word`;
    shift @res;			# discard single-line header
    return undef if !@res;	# no actual words
    my $x;
    if (1) {
	$x = (reverse sort @res)[0];
    } else {
	$x = $res[int(rand() * @res)];
    }
    chomp $x;
    return $x;
}
