#!/usr/bin/perl
#
# ==========
# en1tofr.pl
# ==========

# quick hack to translate en1 (English) on stdin to use fr (French) voices
# Mike Hamilton (mikeh@hamilton.net.au) December 2000

# Map English phonemes to French

%en1tofr1map=(
  "5"  => "l",
  "p"  => "p",   # pat
  "b"  => "b",   # but
  "t"  => "t",   # ten
  "d"  => "d",   # den
  "k"  => "k",   # can
  "m"  => "m",   # man
  "n"  => "n",   # not
  "l"  => "l",   # like
  "r"  => "R",   # run
  "f"  => "f",   # full
  "v"  => "v",   # very
  "s"  => "s",   # some
  "z"  => "s",   # zeal
  "h"  => "h",   # hat
  "w"  => "w",   # went
  "g"  => "g",   # game
  "tS" => "S" ,  # chain
  "dZ" => "Z",  # Jane
  "N"  => "N",	 # long
  "T" =>  "t",  # thin
  "D" =>  "b",   # then
  "S" =>  "S",   # ship
  "Z" =>  "Z",   # measure
  "j" =>  "j",   # yes
  "i:" => "i",   # bean
  "A:" => "a",   # barn
  "O:" => "O",   # born
  "u:" => "u",   # boon
  "3:" => "2",   # burn
  "I"  => "i",   # pit
  "e"  => "e",   # pet
  "{"  => "e~",  # pat
  "V"  => "a",   # putt
  "Q"  => "O",   # pot
  "U"  => "u",   # good
  "@"  => "@",	 # about
  "eI" => "i",   # bay
  "aI" => "i",  # buy
  "OI" => "i",  # boy
  "\@U"=> "u",  # no
  "aU" => "u",  # now
  "I\@"=> "@",  # peer
  "e\@"=> "E",  # pair
  "U\@"=> "@"   # poor
);


# -------------
# MakeFrenchLine
# -------------

sub MakeFrenchLine
{
  local ($phon,$line)=@_;
  local ($FrenchLine);

  $FrenchLine="$phon ";
  @fields=split(" ",$line);
  $nFields=$#fields;

  for ($i=1; $i<=$nFields; ++$i) {
     $FrenchLine .= "$fields[$i]";
     $FrenchLine .=" " if ($i !=$nFields);
  }
  return $FrenchLine;

}

# ----------
# ReadmBrola
# ----------

# read, eating comments

sub ReadmBrola
{
  return -1 if (!($lookAhead=<>));  # eof

  chop ($lookahead);

  while ($lookAhead =~ /^;/) # comment
  {
    last if (!($lookAhead=<>));  # eof
    chop ($lookAhead);
  }

  $lookAhead =~  s/^\s+|\s+$//g; # strip leading/trailing spaces
}

# -----------
# ProcessLine
# -----------

sub ProcessLine
{
   print "\n", return if ($line eq "");   # empty line

   @fields=split(" ",$lookAhead);
   $nextEnglishPhon=$fields[0];
   $nextFrenchPhon=$en1tofr1map{$nextEnglishPhon};

   @fields=split(" ",$line);
   $EnglishPhon=$fields[0];

   if ($EnglishPhon eq "h") {
     $line ="_ 10";
      print "$line\n";
      $lastLineOutput=$line;
      next;
   }

   $FrenchPhon=$en1tofr1map{$EnglishPhon};

   # make English eI from e-i
   if ($EnglishPhon eq "eI") {
     print "e 40\n";
   }

   # make English aI from a-i
   if ($EnglishPhon eq "aI") {
     print "a 40\n";
   }

   # make English OI from o-i
   if ($EnglishPhon eq "OI") {
     print "o 40\n";
   }

   # make English @U from @-u
   if ($EnglishPhon eq "\@U") {
     print "@ 40\n";
   }

   # make English aU from a-u
   if ($EnglishPhon eq "aU") {
     print "a 40\n";
   }

   # make English U@ from u-@
   if ($EnglishPhon eq "U\@") {
     print "u 40\n";
   }

   $InsertPhonAfter="";

   if ($line =~ /^[_#]/) {
      print "$line\n";
      $lastLineOutput=$line;
    }
    else {
      $lastOutputLine=&MakeFrenchLine($FrenchPhon,$line);
      print "$lastOutputLine\n";
    }

    if ($InsertPhonAfter ne "") {
      print "$InsertPhonAfter\n";
      $lastOutputLine=$InsertPhonAfter;
    }


}

# --------
# en1tofr1
# --------

sub en1tofr1
{
  $lastLineOutput="";

  &ReadmBrola;

  while (1) {
    @fields=split(" ",$lastOutputLine);
    $lastOutputPhon=$fields[0];

    $line=$lookAhead;

    last if (&ReadmBrola == -1); # eof
    &ProcessLine;
  }

  &ProcessLine; # deal with the last line

}

&en1tofr1;

1;

