Monday, January 26, 2009

Morse code encoder/decoder

This is a Morse code encoder/decoder program written in python. Simply pass a string that contains Morse code supported characters and you'll see a binary format of the string, encoded as Morse code as specified @ http://en.wikipedia.org/wiki/Morse_code.

  1. short mark, dot or 'dit' (·) — 1
  2. longer mark, dash or 'dah' (–) — 111
  3. intra-character gap (between the dots and dashes within a character) — 0
  4. short gap (between letters) — 000
  5. medium gap (between words) — 0000000


#!/usr/bin/python
import sys
__author__="Aanand Natarajan"

#morse code dictionary
codes = {'1':".----",'2':"..---",'3':"...--",'4':"....-",'5':".....",'6':"-....",'7':"--...",'8':"---..",
'9':"----.",'0':"-----",'A':".-",'B':"-...",'C':"-.-.",'D':"-..",'E':".",'F':"..-.",'G':"--.",
'H':"....",'I':"..",'J':".---",'K':"-.-",'L':".-..",'M':"--",'N':"-.",'O':"---",'P':".--.",
'Q':"--.-",'R':".-.",'S':"...",'T':"-",'U':"..-",'V':"...-",'W':".--",'X':"-..-",'Y':"-.--",
'Z':"--..",
#punctuations
',':"--..--",'.':".-.-.-",'?':"..--..",';':"-.-.-",':':"---...",'/':"-..-.",
'-':"-....-","'":".----.",'(':"-.--.",')':"-.--.-",'!':"-.-.--",'&':".-...",
'=':"-...-",'+':".-.-.",'_':"..--.-",'"':".-..-.",'$':"...-..-",'@':".--.-.",
#space
' ':"|"}

binary = {'.':'10','-':'1110',',':'000','|':'0000000'}


def encode(value):
""" encodes the value into morse code """
morse_value=""
value.replace('*', 'X')
value.replace('^', 'XX')
for c in value:
try :
morse_value += codes[c.upper()]+','
except :
print "Unintended character " + c + " omitted"
return _get_binary(morse_value)

def decode(morse_code_value):
""" decodes the morse bytes """
decoded_value = _decode_binary(morse_code_value)
ascii_value=""
for v in decoded_value.split(","):
ascii_value += _get_key(v)
return ascii_value

def _get_binary(value):
binary_value = ""
for c in value:
binary_value += binary[c]
return binary_value

def _get_key(value):
""" returns the key for the given value """
for k,v in codes.items():
if v == value:
return k
return ''

def _decode_binary(binary):
dah_replaced = binary.replace('1110', '-')
dit_replaced = dah_replaced.replace('10', '.')
comma_replaced = dit_replaced.replace('000', ',')
zero_replaced = comma_replaced.replace('0', '|,')
return zero_replaced

def _do_decode(value):
print "Decoded : "+decode(value)

def _do_encode(value):
print "Encoded : "+encode(value)

if __name__ == "__main__":
if len(sys.argv) > 2:
if sys.argv[1] == 'd' :
print "decoding"
_do_decode(sys.argv[2])
else:
print "encoding"
_do_encode(sys.argv[2])
elif len(sys.argv) > 1:
print "encoding"
_do_encode(sys.argv[1])
else:
print "Usage : "+sys.argv[0]+" [d (decode) |e (encode)] [input string]"


Usage:
aanand@AanandDL:~$ ./MorseEnDecode.py "aanand aka tuxaanand"
encoding
Encoded : 101110000101110000111010000101110000111010000111010
1000000000000001011100001110101110000101110000000000000011100
0010101110000111010101110000101110000101110000111010000101110
00011101000011101010000
aanand@AanandDL:~$ ./MorseEnDecode.py d "1011100001011100001110
100001011100001110100001110101000000000000001011100001110101110
000101110000000000000011100001010111000011101010111000010111000
010111000011101000010111000011101000011101010000"
decoding
Decoded : AANAND AKA TUXAANAND
aanand@AanandDL:~$

No comments: