ellem has asked for the wisdom of the Perl Monks concerning the following question:

The idea is that a user is prompted to enter some letters and numbers like:
123 Anywhere Street
and the script spits back:
one two tree alpha november yankee whiskey hotel... etc.
So the questions are:

How do I read each letter and number with the correct white space?

Should I use tr/// or s/// or my $A eq "alpha"; (or something) to do the translation?
There's more than one way to do it, but only some of them actually work.

Replies are listed 'Best First'.
Re: Phonetic Translation
by Paladin (Vicar) on Aug 27, 2003 at 17:35 UTC
    I'd use split to break it into characters, and a hash to map characters to their names.
    my %trans = ( 1 => "one", 2 => "two", ... z => "zulu", ); foreach (split //, $line) { if (exists $trans{$_}) { print $trans{$_}, " "; } else { print $_, " "; } }
    Update: Fixed missing ; in hash assignment
      Oh, I have used split a zillion times and because I didn't have a character (like , ) to split on I didn't realize I could use it. (No I don't know what I am doing.)

      I am sure everyone knows, but in case there is someone who does not there needs to be semi-colon after the hash for the code to compile:
      my %trans = ( 1 => "one", 2 => "two", ... z => "zulu", ) ; #<---note semi-colon foreach (split //, $line) { ....
      Oh and to deal with the case thing I did this:
      print "Enter some letters and numbers: " ; my $line = <STDIN> ; chomp $line ; $print = lc $line ; ... foreach (split //, $print) { ...

      There's more than one way to do it, but only some of them actually work.
Re: Phonetic Translation
by benn (Vicar) on Aug 27, 2003 at 18:13 UTC
    Further to Paladin's fine answer, I'd just add a couple of points...

    You'll probably want to lc either $line or each split character, unless you want to distinguish between upper and lower, in which case (forgive the pun) you'll need something like 'a'=>alpha, 'A'=>ALPHA in your lookup hash.

    Whitespace may also be confusing to the user - certain rendering systems (HTML for instance) ignore multiple spaces, so you may want to add something like ' '=>'space'.

    Cheers, Ben.

      If the case matters, you could use this
      my %trans = ( a => "alpha", b => "beta", ... z => "zulu" ); my $line = 'AbZaBz'; foreach (split //, $line) { if (exists $trans{lc($_)}) { print (($_ ne lc($_)) ? uc($trans{lc($_)}) : $trans{$_}); } else { print $_; } print ' '; } __OUTPUT__ ALPHA beta ZULU alpha BETA zulu
Re: Phonetic Translation
by johndageek (Hermit) on Aug 28, 2003 at 19:25 UTC
    Just some fun with the proposed problem. Phonetics gone awry!

    Obviously not complete or most efficient form, but just a few things to think about.


    #!/usr/bin/perl -w use strict; my $str = 'One day my 2 sisters gave me 100% of thier work. "How could + they?" I asked at 8:00 P.M.. "Easily" replied my subconcious, "Those + two want to go too.'; print "original string:\n$str\n\n"; ## do all your special replacements in the order that works for most s +ituations. $str =~ s/100/ one hundred /g; $str =~ s/00/ double ought /g; $str =~ s/0/ zero /g; $str =~ s/1/ one /g; $str =~ s/2/ two /g; $str =~ s/8/ eight /g; $str =~ s/%/ percent /g; $str =~ s/\?/ question mark /g; $str =~ s/\./ period /g; $str =~ s/\:/ colon /g; $str =~ s/\"/ quotation mark /g; ## lower case all letters/ or if you like pain, ## prepend all upper case letters with the words "upper case" $str=~s/([A-Z])/upper case $1/g; $str=lc($str); ## make multiple spaces equal to one space $str=~s/\s+/ /g; ## now Phoneticize to your heart's content my %tran = ( a => " alpha ", b=>" bravo ", c=>" charlie ", d=>" delta ", e=>" echo ", f=>" foxtrot ", g=>" golf ", h=>" hotel ", i=>" indian ", j=>" juliet ", k=>" kilo ", l=>" lima ", m=>" mike ", n=>" november ", o=>" oscar ", p=>" papa ", q=>" quebec ", r=>" romeo ", s=>" siera ", t=>" tango ", u=>" uniform ", v=>" victor ", w=>" whiskey ", x=>" xray ", y=>" yankee ", z=>" zulu " ); print "$str\n\n"; my $endstr = ""; ## now create the phoneticized sentence foreach (split //, $str) { if (exists $tran{$_}) { $endstr = $endstr . "$tran{$_}"; } else { ## or add $_ as another answer suggested $endstr = $endstr . "/"; } } ## get rid of extra spaces $endstr=~ s/\s+/ /g; ## show em your stuff print "$endstr\n";
Re: Phonetic Translation
by davido (Cardinal) on Aug 28, 2003 at 07:09 UTC
    This discussion of the Phonetic Alphabet inspired me to finally get around to writing that JAPH I've been considering for some time: A JAPH that implements a Phonetic Alphabet translator.

    You can see the outcome at this link. It is a full implementation of a phonetic alphabet translator, using an anonymous hash as the lookup table.

    Have fun with it. If you want a de-obfu version for clarity let me know and I'll post it. But in reality the one I posted isn't all that obscure.


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Phonetic Translation
by Anonymous Monk on Aug 28, 2003 at 11:10 UTC
    1. create hashes for letter-word pairs 2. concatenate the input string 3. Compare the string's(aka. vector's) each letter as an array's elements to the hashes. Do this step throgh a loop (eg.: for, untill) 4. Enjoy! :-) "Nobody expects the 5p4n15h 1nqu151710n!" M0n7y "l337" Py70n andras.simon@sanyo.hu