Skip to content

Instantly share code, notes, and snippets.

@alexgottscha
Created January 13, 2015 01:20
Show Gist options
  • Save alexgottscha/208225af7f41c79a7f48 to your computer and use it in GitHub Desktop.
Save alexgottscha/208225af7f41c79a7f48 to your computer and use it in GitHub Desktop.
Terrible Phonetics
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# thanks to /u/Qurtys_Lyn
'''Convert words into phonetic alphabet'''
import sys
def get_opts():
'''Tell it what to do'''
from argparse import ArgumentParser
opts = ArgumentParser()
opts.add_argument('words', nargs='*')
opts.add_argument('-t', '--terrible',
action='store_true', default=False)
return opts.parse_args()
def phonetic(words, terrible=False):
'''Input word string, return phonetic alphabet string'''
if terrible:
alphabet = {
'A': 'Aisle', 'B': 'Bdellium', 'C': 'Czar',
'D': 'Djinn', 'E': 'Eureka', 'F': 'Faze',
'G': 'Gnat', 'H': 'Hour', 'I': 'Illicit',
'J': 'Jalapeño', 'K': 'Knight', 'L': 'Fifty',
'M': 'Mnemonic', 'N': 'No', 'O': 'Ouija',
'P': 'Pneumatic', 'Q': 'Quiche', 'R': 'Rye',
'S': 'Sea', 'T': 'Tsar', 'U': 'Urn',
'V': 'Five', 'W': 'Wright', 'X': 'Xerxes',
'Y': 'Yiperite', 'Z': 'Zhivago', ' ': '-' }
else:
alphabet = {
'A': 'Alfa', 'B': 'Bravo', 'C': 'Charlie',
'D': 'Delta', 'E': 'Echo', 'F': 'Foxtrot',
'G': 'Golf', 'H': 'Hotel', 'I': 'India',
'J': 'Juliett', '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': 'X-ray',
'Y': 'Yankee', 'Z': 'Zulu', ' ': '-' }
letters = list(words.upper())
for i, letter in enumerate(letters):
if letter in alphabet:
letters[i] = alphabet[letter]
return ' '.join(letters)
def main():
'''read words from stdin or arguments'''
options = get_opts()
if len(options.words) > 1:
words = ' '.join(options.words)
print phonetic(words, options.terrible)
else:
try:
while True:
words = raw_input()
print phonetic(words, options.terrible)
except (EOFError, KeyboardInterrupt):
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment