Morse code has been around since the early 1800s and found its way into the digital age. From its role as an important medium of communication during World War II to the transmission of encrypted messages on air and ships, Morse code has taken hold.
If you don’t know this fascinating language, get ready to discover Morse code as well as build your own translator using Python.
What is Morse code
Morse code is a communication method that encodes text characters into a standard sequence of two signals of different duration, represented by dots and dashes. Morse code is named after one of the inventors of the telegraph, Samuel Morse. You can memorize it and transmit it via sound waves or visible light that can be perceived by the human senses.
The length of Morse code is inversely proportional to the frequency of use of that alphabet. In Morse code, we see that the most common letter in English, E, is assigned to just a dot.
You can use these free Morse code software and apps to send coded messages to each other. Morse code, on the other hand, is fairly easy to learn and can be learned in less than a month with a fair amount of practice. Here are 9 sites where you can learn Morse code for free.
How to build a Morse code translator using Python
Start by defining a Python dictionary named MORSE_CODE_DICT Save the Morse code value. Keys are letters of the English alphabet, with appropriate dot or dash sequences as corresponding values. Dictionaries let you quickly look up the value corresponding to any key.
MORSE_CODE_DICT = { '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':'-----', "https://news.google.com/__i/rss/rd/articles/,":'--..--', '.':'.-.-.-',
'?':'..--..', "https://news.google.com/":'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-' }
Declare a function named encrypt that accepts message as an input parameter.Inside the function, initialize a variable named cipher with an empty string. Use it to create and store encrypted messages. Next, declare a for loop that iterates through each character in the message.
If the character is not blank, pass it to the dictionary for lookup. The dictionary will return the corresponding More code value based on the key.Add spaces to separate morse code characters and use abbreviations += Use the operator to concatenate with the code retrieved from the dictionary. If the character is blank, add an extra space to the cipher. Morse code separates words with two consecutive spaces.
def encrypt(message):
cipher = ""
for letter in message:
if letter != " ":
cipher += MORSE_CODE_DICT[letter] + " "
else:
cipher += " "
return cipher
Declare a function named decrypt that accepts message as an input parameter. To access the last character in Morse code, use the shorthand operator and add a space at the end. Initialize two variables, decrypt, quote Holds an empty string. The decipher variable holds the decoded sentence consisting of the English alphabet while using citext to store each letter of Morse code.
Iterate through a for loop that runs through each character in Morse code. If the character is not whitespace, it initializes to zero a counter variable i that tracks the number of whitespaces, and stores the 1-character Morse code in citext. Otherwise, increment the counter because the character is a space.
If the counter is 2, it means we need to add blanks to the decoded word variable decipher. Otherwise, use the value to access the key. To do this, iterate through the key-value pairs in the dictionary using the items function. If citext equals a value, use the shorthand operator to add the corresponding key to the decipher variable.
Finally, we clear the citext to get the next character and return the obtained decrypted text to the function call.
def decrypt(message):
message += " "
decipher = ""
citext = "" for letter in message:
if letter != " ":
i = 0
citext += letter
else:
i += 1
if i == 2:
decipher += " "
else:
for key, value in MORSE_CODE_DICT.items():
if citext == value:
decipher += key
citext = ""
return decipher
Test your function with a sample input.Start by initializing a variable named message in the word or sentence to be encrypted.use upper function to convert all characters to uppercase and pass them as arguments to the encrypt function. Morse code contains only uppercase letters, which are keys in the dictionary. This also helps avoid unnecessary runtime checks for valid case.
Print the resulting value to display the Morse code equivalent of the original sentence.
Then store the Morse code in a message variable and pass it to the decoding function. Print the result to make sure it is correct.
You can accept input from the user instead of hardcoding the input input() function.
message = "Make Use Of"
result = encrypt(message.upper())
print(result)message = "-- .- -.- . ..- ... . --- ..-."
result = decrypt(message)
print(result)
Output of Morse code translation in Python
The Python program converts each letter of the English alphabet to its corresponding Morse code and displays it on the terminal output screen as shown below. If you copy the output you get and pass it to decryption, you will receive the original text you passed earlier. This confirms that the translation worked perfectly.
Morse code mobile application
Morse Code Translator is now available for free. Hundreds of applications such as Morse Mania, Morse Trainer, Morse Code Reader, Morse Code Keyboard, Morse Code Translator are available for various platforms. Surprisingly, it is also supported by GBoard (Google Keyboard).
To access Morse code in Gboard, go to Gboard[設定]open the icon,[言語]to select[英語 (米国)]Tap. Swipe right on the option and tap[モールス符号]Choose. Your keyboard now supports Morse code insertion along with accessibility services such as TalkBack, Switch Access, and Select to Speak.