Tuesday, September 11, 2012

Convert words and numbers to the NATO Phonetic Alphabet

By Vasudev Ram


In my previous post, AboutMyBrowser from SupportBee, I had mentioned the NATO Phonetic Alphabet, which I saw via this HN thread.

That gave me the idea to write a function in Python to let you convert alphabets to the NATO Phonetic Alphabet code. It's a simple one, but it can be used to print out your name, or your city, etc., in that code, to use while talking to someone on the phone, radio, etc.

Here is the Python program, nato_phonetic_alphabet.py:

# nato_phonetic_alphabet.py
# Convert alphabets to the NATO Phonetic Alphabet code
# See: http://en.wikipedia.org/wiki/NATO_phonetic_alphabet
# Author: Vasudev Ram - http://www.dancingbison.com
# Version: 0.1

a_to_n = {
 "A": "ALFA", "B": "BRAVO", "C": "CHARLIE", "D": "DELTA",
 "E": "ECHO", "F": "FOXTROT", "G": "GOLF", "H": "HOTEL",
 "I": "INDIA", "J": "JULIET", "K": "KILO", "L": "LIMA",
 "M": "MIKE", "N": "NOVEMBER", "O": "OSCAR", "P": "PAPA",
 "Q": "QUEBEC", "R": "ROMEO", "S": "SIERRA", "T": "TANGO",
 "U": "UNIFORM", "V": "VICTOR", "W": "WHISKEY", "X": "XRAY",
 "Y": "YANKEE", "Z": "ZULU",
 "1": "ONE", "2": "TWO", "3": "THREE", "4": "FOUR",
 "5": "FIVE", "6": "SIX", "7": "SEVEN", "8": "EIGHT",
 "9": "NINE", "0": "ZERO",
}

def alphabet_to_nato(alphabet):
 """
 """
 global a_to_n
 assert len(alphabet) == 1
 assert type(alphabet) == type("A")
 if a_to_n.has_key(alphabet):
  return a_to_n[alphabet]
 else:
  return " "

def main():
 # Test alphabet_to_nato() with a few alphabets.
 for alphabet in ("A", "D", "I", "P", "X", "1", "4", "7", "0"):
  print alphabet, ": ", alphabet_to_nato(alphabet)
 # Test alphabet_to_nato() with a few person names.
 names = ("VASUDEV RAM", "JOHN DOE", "J RANDOM HACKER")
 for name in names:
  print name, ": ",
  for alphabet in name:
   print alphabet_to_nato(alphabet),
  print

if __name__ == "__main__":
 main()

# EOF: nato_phonetic_alphabet.py

And here is the test output from the program - it converts a few alphabets and digits to the code, and also a few person names, including that of yours truly :)

A :  ALFA
D :  DELTA
I :  INDIA
P :  PAPA
X :  XRAY
1 :  ONE
4 :  FOUR
7 :  SEVEN
0 :  ZERO
VASUDEV RAM :  VICTOR ALFA SIERRA UNIFORM DELTA ECHO VICTOR   ROMEO ALFA MIKE
JOHN DOE :  JULIET OSCAR HOTEL NOVEMBER   DELTA OSCAR ECHO
J RANDOM HACKER :  JULIET   ROMEO ALFA NOVEMBER DELTA OSCAR MIKE   HOTEL ALFA CH
ARLIE KILO ECHO ROMEO

I may use this function in the next release of PipeController, just as a fun example, after adding a few more features to it (PipeController, that is).

Enjoy.

- Vasudev Ram - Dancing Bison Enterprises

No comments: