#!/usr/local/bin/perl

# With a rule id, look up what lexicon file it came from
# Takes the name of an init file as an argument, reads its lexicon files 

if (@ARGV != 2) {
    die "Not enough arguments.\n";
}


$initfile = $ARGV[0];
@lexfiles = ();

open(INIT, "$initfile") or die $!;
while ($line = <INIT>) {
    if ($line =~ m/^loadlex/) {
	$line =~ s/\s*;.*$//;
	$line =~ m/^loadlex\s+(.*)\s*$/;
	$lexfile = $1;
	print "Lexfile $lexfile\n";
	push @lexfiles, $lexfile;
    }
}
close(INIT);

foreach $lexfile (@lexfiles) {
    #print "$lexfile\n";
    open(LEX, "$lexfile") or next;
    while ($line = <LEX>) {
	if ($line =~ m/\{([^\}]+)\}/) {
	    $idsrc{uc($1)} = $lexfile;
	}
    }
    close(LEX);
}

# Read in trace file, see where each lexical item came from

$nbestfile = $ARGV[1];
open(NBEST, "$nbestfile") or die $!;
while ($line = <NBEST>) {
    if ($line =~ m/^SrcSent/) { $firstbest = 1; };
    if ($line =~ m/^\s*$/) { $firstbest = 0; };
    if ($line =~ m/^\(/) {
	(@lexids) = ($line =~ m/\((\w+,\d+) \'/g);
	foreach $lexid (@lexids) {
	    #print "Lexid $lexid\n";
	    if ($firstbest == 1) {
		$total++;
		if (defined($idsrc{uc($lexid)})) {
		    $counts{$idsrc{uc($lexid)}} += 1;
		}
	    }
	}
    }
}

print "\n$nbestfile\n";
print "Total counts:\t$total\n";
foreach $lexfile (@lexfiles) {
    $lexfile =~ m/\/([^\/]+)$/;
    $lexshort = $1;
    $percent = (100*$counts{$lexfile}/$total);
    #$percent =~ s/^(\d+\.\d\d)\d+$/$1/;
    printf("%5.2f\t%7d\t%s\n", $percent, $counts{$lexfile}, $lexshort);
}
