=for comment

utfize.pl
Converts output from "derive" into Unicode UTF-8.
Brett Kessler, Washington University in St. Louis
http://spell.psychology.wustl.edu/~bkessler

=cut

use warnings;
use strict;

our $utf = {
  q{.a} => q{ə},
  q{bh} => q{bʱ},
  q{ch} => q{cʰ},
  q{.d} => q{ɖ},
  q{dh} => q{dʱ},
  q{dl} => q{dˡ},
  q{g} => q{ɡ},
  q{gh} => q{ɡʱ},
  q{^h} => q{ɦ},
  q{^j} => q{ɟ},
  q{kh} => q{kʰ},
  q{~l} => q{l̃},
  q{^n} => q{ŋ},
  q{.n} => q{ɳ},
  q{~n} => q{ɲ},
  q{.r} => q{r̩},
  q{^s} => q{ʃ},
  q{.s} => q{ʂ},
  q{.t} => q{ʈ},
  q{th} => q{tʰ},
  q{v} => q{ʋ},
  q{.z} => q{ʐ},
  q{<z} => q{z̞},
};

sub TranslateChar($) {
  my $in = shift(@_);
  if ($in =~ m{\[.+?\]}) {return $in;}
  if ($in =~ m{^[abcdehijklmnNoprstuwxz -]$}) {return $in;}
  my $out = $utf->{$in};
  if (!defined($out)) {
    die(qq{Don't know how to translate {$in}});
  }
  return $out;
}

sub TranslateString($) {
  my $string = shift(@_);
  $string =~ s{(\.a|bh|ch|dh|\.dh?|dl|gh|\^h|\^jh?|kh|\.l|~l|~n|\.n|\^n|ph|\.r|\^s|\.s|th|\.th?|<z|\.z|\[.+?\]|.)}{TranslateChar($1);}eg;
  return $string;
}

sub main() {
  while (defined(my $line = <>)) {
    $line =~ s{#(.+?)#}{q{#} . TranslateString($1) . q{#};}e;
    print($line);
  }
}

main();
