#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
use POSIX;
use Util;
use Carp;

####################################################################
my $smooth=2; # 0 is no smoothing, 1 is add-one, 2 is simple good-turing
my $help;
my $triphonefile;
my $bigramFname;
GetOptions (
            "help|h" => \$help) 
or pod2usage(2);
$bigramFname = shift;
$triphonefile = shift;
pod2usage(-exitstatus => 2, -verbose => 3) if (!$triphonefile || !$bigramFname);


#load the adjacency matrix
my %bigram = %{loadDumper($bigramFname)};
print STDERR "triphone adjacency matrix loaded\n";
my %illegalTriphones=();

open(TRIPHONES,"<$triphonefile") || confess "cannot open <$triphonefile";
my $totalWeight=0;
while(<TRIPHONES>){
	chomp;
	(my $weight, my $triphone) = split;
	if(defined($illegalTriphones{$triphone})){
		#print STDERR "ignoring $triphone because $illegalTriphones{$triphone} is already teed\n";
	}
	else{
		$totalWeight += $weight;
		print "$triphone\n";
		#put it's neighbors into the illegal list
		map {$illegalTriphones{$_}=$triphone} keys %{$bigram{$triphone}};
		foreach my $preNeighbor (keys %bigram){
			$illegalTriphones{$preNeighbor}=$triphone if (defined($bigram{$preNeighbor}{$triphone}));
		}
	}
}
print STDERR "teed total weight $totalWeight\n";
close TRIPHONES;



__END__

=head1 NAME

calcTees.pl - given a list of weighted triphones (in descending weight order) and an adjacency matrix, prints only triphones which are not adjacent to each other,
greedily maximizing the weight of printed triphones.

=head1 SYNOPSIS

calcTees.pl <AdjMat.pld> <triphones.txt>

 Options:
   -h,	--help			brief help message

=head1 OPTIONS

=over 4

=back

=head1 DESCRIPTION


=cut
