#!/usr/bin/python -tt # Licensed under the Apache License, Version 2.0 # http://www.apache.org/licenses/LICENSE-2.0 """A Python program to output phonetic names for each letter in an input statement. Try running this program from the command line like this: python phonetic.py python phonetic.py YHF That should print: Whiskey Tango Foxtrot -or- Yankee Hotel Foxtrot """ import sys import re # Define a main() function that prints the phonetic alpha. def main(): # Get the name from the command line, using 'WTF' as a fallback. if len(sys.argv) >= 2: name = str.upper(sys.argv[1]) else: name = 'WTF' #Initialize some variables to help with the translation. i = 0 j = open('table.txt', 'rU') final = '' entries = re.split("\n",j.read()) #Replace each letter and print its phonetic alpha. while i < len(name): for line in entries: match = re.match(r'(%s)\t(\w+)' % (name[i]), line) if match: final = final + ' ' + match.group(2) i = i + 1 print str.strip(final) # This is the standard boilerplate that calls the main() function. if __name__ == '__main__': main()