Thursday, August 16, 2012

Phonetic Phrase Translator (Python)

I thought that I would noodle around in Python this morning, and I had a lot of fun making this dumb program, but basically it takes a phrase that you input and converts it to a fancy "military style" phonetic phrase.. It's useful when you talk to tech support and you need to give them a serial number or other long phrase. Pretty simple stuff, the cMil function takes 'a' and converts each char to uppercase and then returns 'b' The cMil function is called from a while loop, using the counter (cnt) in the array format to pass each character. The range is the "len" (lenght)of the phrase, minus 1, as the counting starts at zero. (Remember that if "test" was the phrase, 0,1,2,3 is four numbers! and test[3] is 't' and not 's')
 def cMil(a):  
      a = a.upper()  
      if a == ' ':  
           b = ''  
      if a == 'A':  
           b = 'Alpha'  
      if a == 'B':  
           b = 'Bravo'  
      if a == 'C':  
           b = 'Charlie'  
      if a == 'D':  
           b = 'Delta'  
      if a == 'E':  
           b = 'Echo'       
      if a == 'F':  
           b = 'Foxtrot'  
      if a == 'G':  
           b = 'Golf'  
      if a == 'H':  
           b = 'Hotel'  
      if a == 'I':  
           b = 'India'  
      if a == 'J':  
           b = 'Juliet'  
      if a == 'K':  
           b = 'Kilo'  
      if a == 'L':  
           b = 'Lima'  
      if a == 'M':  
           b = 'Mike'  
      if a == 'N':  
           b = 'November'  
      if a == 'O':  
           b = 'Oscar'  
      if a == 'P':  
           b = 'Papa'  
      if a == 'Q':  
           b = 'Quebec'  
      if a == 'R':  
           b = 'Romeo'  
      if a == 'S':  
           b = 'Sierra'       
      if a == 'T':  
           b = 'Tango'  
      if a == 'U':  
           b = 'Uniform'  
      if a == 'V':  
           b = 'Victor'  
      if a == 'W':  
           b = 'Whiskey'  
      if a == 'X':  
           b = 'X-Ray'  
      if a == 'Y':  
           b = 'Yankee'  
      if a == 'Z':  
           b = 'Zulu'  
      if a == '0':  
           b = 'Zero'  
      if a == '1':  
           b = 'One'  
      if a == '2':  
           b = 'Two'  
      if a == '3':  
           b = 'Three'  
      if a == '4':  
           b = 'Four'  
      if a == '5':  
           b = 'Five'  
      if a == '6':  
           b = 'Six'  
      if a == '7':  
           b = 'Seven'  
      if a == '8':  
           b = 'Eight'  
      if a == '9':  
           b = 'Nine'  
      return b  
 phrase = raw_input('What phrase do you want translated to Phonetic Speak?\n')  
 print '\n'  
 len1 = len(phrase)  
 #print('Length of phrase is', len1)  
 range = (len1 - 1)  
 cnt = 0  
 while (cnt <= range):  
      print(cMil(phrase[cnt]))  
      cnt = cnt + 1  

1 comment:

northernflights said...

Need to fix the numbers. http://en.wikipedia.org/wiki/NATO_phonetic_alphabet\

At minimum:
"Three" needs to be "Tree"
"Nine" needs to be "Niner"