#!/afs/athena/contrib/perl/p

sub input {
        local($foo);
        print(">>>");
        $foo = <STDIN>;
        chop($foo);
        $foo;
}

sub fmatch {
    local($A, $B) = @_;
    local($l) = (length($A) < length($B)) ? length($A) : length($B);
    local($m) = 0;
    local($w) = 2;
    local($k);
    
    $A eq $B && return(1.0);
    $l > $w || return(0.0);
    
    for $k(0..$l-$w) { 
	local($s) = substr($A, $k, $w);
	#---escape r.e. characters in string A---
	$s =~ s/([()[\]*|?.{}\\])/\\$1/;
		 $B =~ $s && $m++;
	     }
	
	($m/($l-$w) > 0.50) ? 1 : 0;
    }


$rep = 'Hello';
    $know{'Hello'} = 'How are you?';
print("Hello.\n");
MAIN: while(1){
    $replied = 0;
    $in = &input();
    $know{$rep}.=$in.''; 
#    print("Learning that \"$rep\" --> \"$in\"\n");
    for $sm (keys %know){
	if(&fmatch($sm, $in)){
	    @resps = split('', $know{$sm});
	    $rep = $resps[rand($#resps-1)];
	    print("N: $#resps\n");
	    print($rep."\n");
	    $replied = 1;
	    next MAIN;
	}
    }
    if(!$replied){
	print("*");
	@foo = keys %know;
	@r = split('', $know{$foo[rand($#foo)]});
	$rep = $r[rand($#r-1)];
	print($rep."\n");
    }
	
}

