Translating Morse Code-Encoder and Decoder-Cool python project

 

Morse translator

A brief introduction of morse code

Morse code is a character encoding method that transmits (sending and receiving) messages or data or a series of bits or pulses using an electrical or binary transmission process. The data in morse code can be represented as a short and long electrical pulse or dots and dashes, or one and zero binary. The developer and inventor of morse code are "Samuel Morse".


Morse translator

Fig.: Telegraph key and sounder for making signal.

Image from Wikipedia: src.


Some Facts About Morse Code

1. The length of a dot(.) in morse code is one unit.

2. A dash(-) in morse code is three units.

3. A dash is three-time more extensive than a dot. (3- = .)

4. In a morse code, the distance between parts of the same letter is one unit.

5. In a morse code, the distance between letters is three units.

6. In a morse code, the distance between words is seven units.

7. Morse code is specially used in war to encrypt a message to hide it from the enemy. Also used in computer science for data encryption. Other uses are Aviation (radion navigating for the pilot) and Amateur radio.


Morse code for alphabets
Fig.: Morse code for alphabets


Morse code for digits
Fig.: Morse code for digits

Morse code for punctuation
Fig.: Morse code for punctuation


Does a programmer learn the morse code translating method?

Yes. A programmer should learn all encryption and decryption algorithm techniques to protect the data. With the help of morse code, a programmer can translate and encrypt its data as the programmer wants. You can change the dictionary of morse code. Making it more complex will make the encryption method more secure.


Algorithm for morse code


The algorithm of morse code is divided into two parts, the encoder, and the decoder. The encoder encodes or encrypts the plain text message in the morse dictionary message (Morse encoded data). The decoder same the opposite. The decoder decodes or decrypts the morse encoded data in plain text (human-readable format).


Algorithm for Morse code dictionary

1. We have to maintain the data structure method to arrange the morse code with the language symbol (Alphabets, Digits, Punctuations).

2. Symbol will be considered as 'key' in a dictionary variable.

3. Morse code of the symbol will be considered as the 'value' of its symbol key in the dictionary variable.


Algorithm for Encoder: Morse code translator

1. Load morse code dictionary.

2. Initialize the required variables.

3. Find the length of the message to encode.

4. Run a for loop to the end of the message length.

5. Match each loop's message character with the morse dictionary keys.

6. If it matches, then adds the values of the matched keys in the 'output' string from the morse dictionary.

7. Detect the first character in the 'output' string, after that add space for each morse code to the 'output' string.


Algorithm for Decoder: Morse code translator

1. As usual, load morse code dictionary.

2. Initialize the required variables.

3. Now, we want to decode the morse code message. So, the process is reverse of the Morse encoder. We will reverse the dictionary by exchanging the position of keys and values of the morse dictionary.

4. Run a for loop to the end of the message length by splitting the message when it finds a space between the morse codes.

5. Match each loop's message morse with the morse reverse dictionary keys.

6. If it matches, then add the values of the matched keys in the 'output' string from the reverse morse dictionary.

7. Now, the final 'output' string will be merged as one string excluding space. As there is no morse code for 'space', the space has been excluded. It refers that, "use '-' dash or '_' underscore instead of 'space' in the message for morse encoder and decoder".


Code for "Translating Morse Code-Encoder and Decoder-Cool python project"


#Morse dictionary in python dict
#"key": "value"
def LoadMorseDictionary():
Morse_dictionary = {
"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": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----",
",": "--..--",
".": ".-.-.-",
"?": "..--..",
"''": ".-..-.",
":": "---...",
";": "-.-.-.",
"'": ".----.",
"-": "-....-",
"/": "-..-.",
"(": "-.--.",
"&": ".-...",
"!": "-.-.--",
"_": "..--.-",
"=": "-...-",
"+": ".-.-.",
"$": "...-..-",
"@": ".--.-.",
}
return Morse_dictionary

#Implementation for Encoder: Morse code translator
def codeMorse(morseCodemsg, Morse_dictionary):
output = ""
spaceDetect=0
for i in range(len(morseCodemsg)):
if morseCodemsg[i] in Morse_dictionary.keys():
if spaceDetect>0:
output = output + " " + Morse_dictionary.get(morseCodemsg[i])
elif spaceDetect==0:
spaceDetect+=1
output = output + Morse_dictionary.get(morseCodemsg[i])
return output

#Implementation for Decoder: Morse code translator
def decodeMorse(morseCodemsg, Morse_dictionary):
#exchanging the position of "key" and "value" in the morse dictionary
Reverse_Morse_dictionary = {value: key for (key, value) in Morse_dictionary.items()}
output = []
for item in morseCodemsg.split(' '):
output.append(Reverse_Morse_dictionary.get(item))
return "".join([str(item) for var in output for item in var])

if __name__=='__main__':
#running a continuous loop until the input get '0' to terminate the program
while(1):
print("press 1 for encode \npress 2 for decode\npress 0 for exit")
n=int(input()) #input for options choice
s=LoadMorseDictionary()
if n==1:
message = input("type plain text message:")
print("Encoder output:",codeMorse(message.lower(), s))
elif n==2:
message = input("type morse message:")
print("Decoder output:",decodeMorse(message, s))
elif n==0: #if the input is '0', then the program will end
exit()



Sample input and output


press 1 for encode 
press 2 for decode
press 0 for exit
1
type plain text message:morse
Encoder output: -- --- .-. ... .
press 1 for encode 
press 2 for decode
press 0 for exit
1
type plain text message:school-of-beginners
Encoder output: ... -.-. .... --- --- .-.. -....- --- ..-. -....- -... . --. .. -. -. . .-. ...
press 1 for encode 
press 2 for decode
press 0 for exit
2
type morse message:... -.-. .... --- --- .-.. -....- --- ..-. -....- -... . --. .. -. -. . .-. ...
Decoder output: school-of-beginners
press 1 for encode 
press 2 for decode
press 0 for exit
0

Process finished with exit code 0


Gather more knowledge from the below references:

Morse code

Morse code translator and questions about morse code


Post a Comment

Previous Post Next Post