#! /usr/local/bin/perl -w

## Key:
## RH = Random House Dictionary of the English Language
## R&C = Robert&Collins Senior French&English dictionary
## LPD = Longman Pronunciation Dictionary
## @ = RH has '@' , R&C has [@] , LPD has [@]
## @. = RH has '@' , R&C has [I] , LPD has [I] with [@] as variant  {weak [I]}
## @: = RH has 'e-' , R&C has [I] , LPD has [i]
## @! = R&C has [U] , LPD has [u]
## @| = R&C has [U] , LPD has [U] {weak [U]}
## @+ = RH has '@r' , R&C has [@] , LPD has [@] for RP and [r-] for GenAm
## a = RH has 'a' , R&C has [&] , LPD has [&]
## a_ = RH has 'a-' , R&C has [eI] , LPD has [eI]
## a^ = RH has 'a^' , R&C has [E@] , LPD has [e@] for RP and [e] for GenAm
## a^+ = RH has 'a^r' , R&C has [E@] , LPD has [e@] for RP and [er] for GenAm
## a" = RH has 'a"' , R&C has [A:] , LPD has [A:]
## a"+ = RH has 'a"r' , R&C has [A:] , LPD has [A:] for RP and [A:r] for GenAm
## b = RH has 'b' , R&C has [b] , LPD has [b]
## cH = RH has 'ch' (tied) , R&C has [tS] , LPD has [tS] (in same syll.)
## d = RH has 'd' , R&C has [d] , LPD has [d]
## d? = RH has 'd' , R&C has [d] , LPD has [d] {pronounced as [t?]}
## dH = RH has 'th' (barred) , R&C has [D]
## e = RH has 'e' , R&C has [e] , LPD has [e]
## e_ = RH has 'e-' , R&C has [i:] , LPD has [i:]
## e- = RH has 'e-' , R&C has [I@] , LPD has [I@] for RP and [I] for GenAm
## e-+ = RH has 'e-r' , R&C has [I@] , LPD has [I@] for RP and [Ir] for GenAm
## f = RH has 'f' , R&C has [f] , LPD has [f]
## g = RH has 'g' , R&C has [g] , LPD has [g]
## h = RH has 'h' , R&C has [h] , LPD has [h]
## i = RH has 'i' , R&C has [I] , LPD has [I]
## i_ = RH has 'i-' , R&C has [aI] , LPD has [aI]
## j = RH has 'j' , R&C has [dZ] , LPD has [dZ]
## l = RH has 'l' , R&C has [l] , LPD has [l]
## l` = RH has '*l' , R&C has [l] (or [@l]) , LPD has [l] ([@l])
## k = RH has 'k' , R&C has [k] , LPD has [k]
## m = RH has 'm' , R&C has [m] , LPD has [m]
## n = RH has 'n' , R&C has [n] , LPD has [n]
## n` = RH has '*n' , R&C has [n] (or [@n]) , LPD has [n] ([@n])
## nG = RH has 'ng' (tied) , R&C has [N] , LPD has [N]
## o = RH has 'o' , R&C has [A.] , LPD has [A.] for RP and [A:] for GenAm
## o^ = RH has 'o^' , R&C has [O:] , LPD has [O:]
## o^+ = RH has 'o^r' , R&C has [O:] , LPD has [O:] for RP and [O:r] for GenAm
## o_ = RH has 'o-' , R&C has [@U] , LPD has [@U] for RP and [oU] for GenAm
## oI = RH has 'oi' , R&C has [OI] , LPD has [OI]
## oO = RH has 'oo~' , R&C has [U] , LPD has [U]
## oO. = RH has 'oo~' , R&C has [U@] , LPD has [U@] for RP and [U] for GenAm
## oO.+ = RH has 'oo~r' , R&C has [U@] , LPD has [U@] for RP and [Ur] for GenAm
## oO_ = RH has 'oo=' , R&C has [u:] , LPD has [u:]
## oU = RH has 'ou' , R&C has [aU] , LPD has [aU]
## p = RH has 'p' , R&C has [p] , LPD has [p]
## r = RH has 'r' , R&C has [r] , LPD has [r]  {intervocalic r only}
## s = RH has 's' , R&C has [s] , LPD has [s]
## sH = RH has 'sh' (tied) , R&C has [S]
## t = RH has 't' , R&C has [t] , LPD has [t]
## t? = RH has 't' , R&C has [t] , LPD has [t<vcd>]
## tH = RH has 'th' (tied) , R&C has [T]
## u = RH has 'u' , R&C has [V] , LPD has [V]
## u^+ = RH has 'u^r' , R&C has [V":] , LPD has [V":] for RP and [V"<r>:] for GenAm
## v = RH has 'v' , R&C has [v]
## w = RH has 'w' , R&C has [w] , LPD has [w]
## w? = RH has 'hw' , R&C has [w] , LPD has [w] for RP and [hw] for GenAm
## y = RH has 'y' , R&C has [j]
## z = RH has 'z' , R&C has [z] , LPD has [z]
## zH = RH has 'zh' (tied) , R&C has [Z]

use strict;
use Getopt::Std;

my %opts;
getopts("a", \%opts);
my $am = $opts{"a"};

while ( <> ) {
    while ( 1 ) {
	if ( /^$/ || /^\#.*$/ ) {
	    print "\n";
	    last;
	} elsif ( s/^\((.*?)\|(.*?)\)// ) {
	    if ( $am ) {
		$_ = $2 . $_;
	    } else {
		$_ = $1 . $_;
	    }
	} elsif ( s/^\'// ) {
	    print "[501]";  ## Primary stress
	} elsif ( s/^\,// ) {
	    print "[502]";  ## Secondary stress
	} elsif ( s/^\\\.// ) {
	    print "[506]";  ## Syllable break
	} elsif ( s/^\@\+// ) {
	    if ( $am ) {
		print "[327]";  ## [@<r>] - as single char
	    } else {
		print "[322]";  ## [@]
	    }
	} elsif ( s/^\@\.// ) {
	    print "[319]";  ## [I]
	} elsif ( s/^\@\:// ) {
	    if ( $am ) {
		print "[301]";  ## [i]
	    } else {
		print "[319]";  ## [I]
	    }
	} elsif ( s/^\@\!// ) {
	    if ( $am ) {
		print "[308]";  ## [u]
	    } else {
		print "[321]";  ## [U]
	    }
	} elsif ( s/^\@// ) {
	    print "[322]";  ## [@]
	} elsif ( s/^a\_// ) {
	    if ( $am ) {
		print "[302][503]";  ## [e:]
	    } else {
		print "[302][319]";  ## [eI]
	    }
	} elsif ( s/^a\^\+// ) {
	    if ( $am ) {
		print "[303][151]";  ## [Er]
	    } else {
		print "[303][322]";  ## [E@]
	    }
	} elsif ( s/^a\^// ) {
	    if ( $am ) {
		print "[303]";  ## [E]
	    } else {
		print "[303][322]";  ## [E@]
	    }
	} elsif ( s/^a\"\+// ) {
	    if ( $am ) {
		print "[305][503][151]";  ## [A:r]
	    } else {
		print "[305][503]";  ## [A:]
	    }
	} elsif ( s/^a\"// ) {
	    print "[305][503]";  ## [A:]
	} elsif ( s/^a// ) {
	    print "[325]";  ## [&]
	} elsif ( s/^b// ) {
	    print "[102]";  ## [b]
	} elsif ( s/^cH// ) {
	    print "[213]";  ## [tS] digraph
	} elsif ( s/^dH// ) {
	    print "[131]";  ## [D]
	} elsif ( s/^d\?// ) {
	    if ( $am ) {
		print "[124]";  ## [*]
	    } else {
		print "[104]";  ## [d]
	    }
	} elsif ( s/^d// ) {
	    print "[104]";  ## [d]
	} elsif ( s/^e\_// ) {
	    print "[301][503]";  ## [i:]
	} elsif ( s/^e\-\+// ) {
	    if ( $am ) {
		print "[319][151]";  ## [Ir]
	    } else {
		print "[319][322]";  ## [I@]
	    }
	} elsif ( s/^e\-// ) {
	    if ( $am ) {
		print "[319]";  ## [I]
	    } else {
		print "[319][322]";  ## [I@]
	    }
	} elsif ( s/^e// ) {
	    print "[303]";  ## [E]
	} elsif ( s/^f// ) {
	    print "[128]";  ## [f]
	} elsif ( s/^g// ) {
	    print "[110]";  ## [g]
	} elsif ( s/^h// ) {
	    print "[146]";  ## [h]
	} elsif ( s/^i\_// ) {
	    print "[304][319]";  ## [aI]
	} elsif ( s/^i// ) {
	    print "[319]";  ## [I]
	} elsif ( s/^j// ) {
	    print "[214]";  ## [dZ] digraph
	} elsif ( s/^k// ) {
	    print "[109]";  ## [k]
	} elsif ( s/^l\`// ) {
	    print "[155][431]";  ## [l-]
	} elsif ( s/^l// ) {
	    print "[155]";  ## [l]
	} elsif ( s/^m// ) {
	    print "[114]";  ## [m]
	} elsif ( s/^nG// ) {
	    print "[119]";  ## [N]
	} elsif ( s/^n\`// ) {
	    print "[116][431]";  ## [n-]
	} elsif ( s/^n// ) {
	    print "[116]";  ## [n]
	} elsif ( s/^o\^\+// ) {
	    if ( $am ) {
		print "[306][503][151]";  ## [O:r]
	    } else {
		print "[306][503]";  ## [O:]
	    }
	} elsif ( s/^o\^// ) {
	    print "[306][503]";  ## [O:]
	} elsif ( s/^oI// ) {
	    print "[306][319]";  ## [OI]
	} elsif ( s/^oO\_// ) {
	    print "[308][503]";  ## [u:]
	} elsif ( s/^oO\.\+// ) {
	    if ( $am ) {
		print "[321][151]";  ## [Ur]
	    } else {
		print "[321][322]";  ## [U@]
	    }
	} elsif ( s/^oO\.// ) {
	    if ( $am ) {
		print "[321]";  ## [U]
	    } else {
		print "[321][322]";  ## [U@]
	    }
	} elsif ( s/^oO// ) {
	    print "[321]";  ## [U]
	} elsif ( s/^oU// ) {
	    print "[304][321]";  ## [aU]
	} elsif ( s/^o\_// ) {
	    if ( $am ) {
		print "[307][503]";  ## [o:]
	    } else {
		print "[322][321]";  ## [@U]
	    }
	} elsif ( s/^o// ) {
	    if ( $am ) {
		print "[305][503]";  ## [A:]
	    } else {
		print "[313]";  ## [A.]
	    }
	} elsif ( s/^p// ) {
	    print "[101]";  ## [p]
	} elsif ( s/^r// ) {
	    print "[151]";  ## [r]
	} elsif ( s/^sH// ) {
	    print "[134]";  ## [S]
	} elsif ( s/^s// ) {
	    print "[132]";  ## [s]
	} elsif ( s/^tH// ) {
	    print "[130]";  ## [T]
	} elsif ( s/^t\?// ) {
	    if ( $am ) {
		print "[124]";  ## [*]
	    } else {
		print "[103]";  ## [t]
	    }
	} elsif ( s/^t// ) {
	    print "[103]";  ## [t]
	} elsif ( s/^u\^\+// ) {
	    if ( $am ) {
		print "[326][419]";  ## [V"<r>] (note: single Unicode char)
	    } else {
		print "[326][503]";  ## [V":]
	    }
	} elsif ( s/^u// ) {
	    print "[314]";  ## [V]
	} elsif ( s/^v// ) {
	    print "[129]";  ## [v]
	} elsif ( s/^w\?// ) {
	    if ( $am ) {
		print "[169]";  ## [w<vls>]
	    } else {
		print "[170]";  ## [w]
	    }
	} elsif ( s/^w// ) {
	    print "[170]";  ## [w]
	} elsif ( s/^y// ) {
	    print "[153]";  ## [j]
	} elsif ( s/^zH// ) {
	    print "[135]";  ## [Z]
	} elsif ( s/^z// ) {
	    print "[133]";  ## [z]
	} elsif ( s/^(\s+)// ) {
	    print $1;
	} elsif ( s/^(.)// ) {
	    print STDERR "Line remainder is $_";
	    die "Unrecognized character ($1)";
	}
    }
}
