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

open(DICT, "/afs/sipb/user/mkgray/ftp/tagger/data/brown/lexicon.txt");
$|=1;

while(<DICT>){
    next if /\d/;
    split;
    $type{$_[0]} .= $_[1]." ";
    $n++;
    if($n > 1000){
	print ".";
	$n=0;
    }
}
print "\n";
while(<>){
    $doc .= $_;
#    print("Reading $_\n");
}

$doc =~ s/\n/  /g;
(@sentences) = split(/\. +/, $doc);
for $s (@sentences){
    print("Processing sentence \"$s\"\n");
    @words = split(/[ \t,\(\)]+/, $s);
#    &calculate_grammar("", @words) unless ($#words > 8);
#    print("Done caclulating grammar\n");
    for $w (@words){
#	print "$w_{".$type{$w}."} ";
	for $t (split(" ", $type{$w})){
	    $twords{$t} .= "$w ";
	}
    }
    print("\n");
}


sub calculate_grammar {
    local($g, @w) = @_;
    local($t, @types);
#    print("Calculate grammar called with \"$g\" and sentance \"@w\"\n");
    if($#w >=0){
	@types = split(" ", $type{$w[0]});
	if($#types >= 0){
#	    print("$w[0] has ".($#types+1)." possible types\n");
	    for $t (@types){
		&calculate_grammar("$g $t", @w[1..$#w]);
	    }
#	    print("backing out...($#types, $#w)\n");
	    return;
	}			# 
	else{
#	    print("Hmph, ignoring $w[0]\n");
	    &calculate_grammar("$g", @w[1..$#w]);
	}
#	print("backing out...($#types, $#w)\n");
	return;
    }
    else{
#	print("Grammar $g\n");
	$grammar{$g}++;
    }
}

#@best_grammars = sort {$grammar{$a} <=> $grammar{$b};} keys %grammar;
#for $bg (@best_grammars){
#    print("$grammar{$bg} $bg\n");
#}

for $type (keys %twords){
    print("$type:: $twords{$type}\n");
}
