#!/usr/bin/perl
# dict2mlf.pl: Mark Hasegawa-Johnson, 4/3/2003
# Illinois Speech and Language Engineering (ISLE group)
#
# Usage (printed if dict2mlf.pl is called with no arguments):
#
$USAGE=<<ENDUSAGE;

DICT2MLF: Convert dictionary file to an MLF mock-transcription
 
Usage:
  dict2mlf.pl input.dict > output.mlf

This script is primarily useful in combination with some format
conversion (e.g., arpa2sphinx.pl or arpa2lm.pl), followed by
mlf2dict.pl.

Example: 
  dict2mlf.pl pronlex.dict switchboard.dict icsi.dict
  arpa2lm.pl pronlex.mlf switchboard.mlf icsi.mlf
  mlf2dict pronlex.lm.mlf switchboard.lm.mlf icsi.lm.mlf
  
For each file specified on the commane line, dict2mlf.pl
creates an output file with the same rootname, but with extension
MLF.  Each line in the input dictionary is converted into 
a pseudo-transcription, e.g., the line "cat k ae t" becomes
three lines:

k cat
ae
t

ENDUSAGE

####################################################################
# GLOBAL VARIABLES
$MLFPERIOD = 0.0000001;

# Math functions
sub abs { my($x)=shift; ($x>0) ? $x : -$x ; }

###################################################
# Subroutine to read, convert, and write to output files
# phoneme data corresponding to one waveform.
#
# Syntax: process_file( $infile, $outfile, \@EXTS, \*fh )
#
# foreach $ext (@EXTS) { Search for files of the form "$infile\.$ext" }
# Merge the resulting transcriptions
# Write the result to $fh with pseudo-label filename $outfile
#
sub process_file {
}  
  
###################################################
# Subroutine to read DICT-format entry
#  as if it were a pseudo-transcriptions
#
# Syntax: $data = read_DICT_entry($line)
#
# $data is an arrayref of arrayrefs.
# @$$data[$i][2..$#{$$tran[0]}] is the sequence of labels in the $i_th entry
#
sub read_DICT_entry {
  # Initialize @data to null
  my(@data) = ();
  my($line) = $_[0];
  
  # Assume every line is word followed by phonemes
  my(@labels) = split(/\s+/,$line);

  # Push a reference to the first phoneme + word onto @data
  #    create pseudo-time equal to the number of labels
  push(@data, [ 0, 0.01, $labels[1], $labels[0] ]);
  # Push a new line for each succeeding phoneme
  foreach $p (2..$#labels) {
    push(@data, [ 0.01*($p-1), 0.01*$p, $labels[$p]]); 
  }

  return(\@data);
}

###################################################
# Print phoneme data to an MLF file
#
# Syntax: print_MLF_entry("root_filename", \@phndata, $filehandleref);
#
# The output filehandle MLF must be globally defined
# and already open.
#
sub print_MLF_entry {
  my($filename) = $_[0];
  my($phndata) = $_[1];
  my($fh) = $_[2];

  # Print the filename to the MLF
  $filename =~ s:.*/::g;
  $filename =~ s/\..*//g;
  print $fh "\"*/$filename\.lab\"\n";

  # Print out the converted data into MLF, and to the label file
  foreach $r (@$phndata) {
    if($$r[0] =~ /\d/ && $$r[1] =~ /\d/) 
      { print $fh sprintf("%d %d ",$$r[0]/$MLFPERIOD,$$r[1]/$MLFPERIOD); }
    print $fh "@$r[2..$#$r]\n";
  }
  # Print final period that separates sentences
  print $fh '.',"\n";
}

###########################################
# Main Program

if (@ARGV==0) { print $USAGE; exit; }

# Process input files
foreach $filename (@ARGV) {
  # Get rid of any file extension
  open(INFILE, $filename) || die "Unable to read from $filename\n";

  print '#!MLF!#',"\n";

  foreach (<INFILE>) { 
    chomp; chomp;
    if(/^(\S+)/) { $word=$1; }
    $dictdata = read_DICT_entry($_);

    # Change period to apostrophe
    $word =~ s/\./\'/;

    print_MLF_entry($word, $dictdata, \*STDOUT);
  }
}
