#!/usr/bin/perl -w
my $usage = << "EOF";
makeTriphoneDict.pl <UniphonesFile> <observedTriphonesFile> 

create triphone dictionary, defining the  unobserved triphone 'word' with the identical triphone 
'phone' if it has been observed in the training data, or the monophone 'phone' if the triphone 
has not been observed.


 Required:
   none
 Options:
   -h,	--help			brief help message

EOF


use strict;
use Getopt::Long;
use Pod::Usage;
use Carp;



my $help;
GetOptions (
         "help|h" => \$help) 
or die($usage);
die($usage) if (@ARGV<2);

my $uniphonesFname= shift;
my $obsTriphonesFname= shift;

open(UNIPHONES, "<$uniphonesFname") ||  confess  "$!: cannot open <$uniphonesFname";
my @uniphones = <UNIPHONES>;
close(UNIPHONES);
@uniphones = map {chomp;$_} @uniphones ;

open(OBSTRIPHONES, "<$obsTriphonesFname") ||  confess  "$!: cannot open <$obsTriphonesFname";
my @obsTriphones = <OBSTRIPHONES>;
close(OBSTRIPHONES);
@obsTriphones = map {chomp;$_} @obsTriphones ;
my %obsTriPhones;
@obsTriPhones{@obsTriphones}=@obsTriphones;


my @prefixes=@uniphones;
foreach my $pref (@uniphones){
	push @prefixes, (map ("$pref-$_",@uniphones));
}

my @triphones=@prefixes;
foreach my $pref (@prefixes){
	push @triphones, (map ("$pref+$_",@uniphones));
}

foreach (@triphones){
	if ($obsTriPhones{$_}){
		print "$_  $_\n";
	}
	else{ #define as monophone
		my $monophone = $_;
		$monophone =~ s/^.*-//;
		$monophone =~ s/\+.*$//;
		print "$_  $monophone\n";
	}
}



