Introduction:
Morse code is method of encoding text characters as a series of on off tones, lights, or clicks that can be easily transmitted over long distances. In this article, we will be building a Morse code translator using Python that can encode and decode Morse code messages.
Step by Step Procedure to Create a Morse Code Translator in Python:
- First we need to create dictionary that maps each character to its Morse code representation. We can use Python dictionary to accomplish this task.
- Next we need to create two functions: one to encode text into Morse code and another to decode Morse code back into text.
- Encode text into Morse code we can loop through each character in the input text and use our dictionary to look up its Morse code representation. We can then concatenate the Morse code representations of each character into a single string.
- To decode Morse code back into text we need to do reverse process. We can split the Morse code string into individual codes using the space delimiter and then use our dictionary to look up the corresponding character. We can then concatenate the characters into a single string.
Define a dictionary that maps each letter and number to its corresponding Morse code symbol. For example:
morse_code = {'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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.'}
Define a function that takes a string as input and returns encoded Morse code message. The function should iterate over each character in the string and look up its Morse code symbol in the dictionary. The function should also add a space between each symbol and a slash between each word. For example:
def encode(text):
# Convert text to uppercase
text = text.upper()
# Initialize an empty string for the encoded message
encoded_message = ""
# Iterate over each character in the text
for char in text:
# If the character is a letter or number, look up its Morse code symbol
if char in morse_code:
encoded_message += morse_code[char] + " "
# If the character is a space, add a slash
elif char == " ":
encoded_message += "/ "
# Otherwise, ignore the character
else:
pass
# Return the encoded message
return encoded_message
Test your functions with some examples. For example:
# Example 1: Encode "Hello World"
text = "Hello World"
message = encode(text)
print(message)
# Output: .... . .-.. .-.. --- / .-- --- .-. .-.. -..
# Example 2: Decode "- .... .. ... / .. ... / ..-. ..- -."
message = "- .... .. ... / .. ... / ..-. ..- -."
text = decode(message)
print(text)
# Output: THIS IS FUN
Complete Code
# Define a dictionary that maps letters and numbers to Morse code symbols
morse_code = {'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...',
# Add the remaining letters and numbers
# ...
}
# Define a function that encodes a text to Morse code
def encode(text):
# Convert text to uppercase
text = text.upper()
# Initialize an empty string for the encoded message
encoded_message = ""
# Iterate over each character in the text
for char in text:
# If the character is a letter or number, look up its Morse code symbol
if char in morse_code:
encoded_message += morse_code[char] + " "
# If the character is a space, add a slash
elif char == " ":
encoded_message += "/ "
# Otherwise, ignore the character
else:
pass
# Return the encoded message
return encoded_message
# Define another function that decodes a Morse code message to text
def decode(message):
# Initialize an empty string for the decoded text
decoded_text = ""
# Split the message by spaces and slashes
symbols = message.split()
words = message.split(" / ")
# Iterate over each word in the message
for word in words:
# Split the word by spaces
letters = word.split()
# Iterate over each symbol in the word
for symbol in letters:
# If the symbol is valid, look up its corresponding letter or number
if symbol in morse_code.values():
decoded_text += list(morse_code.keys())[list(morse_code.values()).index(symbol)]
# Otherwise, ignore the symbol
else:
pass
# Add a space between each word
decoded_text += " "
# Return the decoded text
return decoded_text
# Test your functions with some examples
# Example 1: Encode "Hello World"
text = "Hello World"
message = encode(text)
print(message)
# Output: .... . .-.. .-.. --- / .-- --- .-. .-.. -..
# Example 2: Decode "- .... .. ... / .. ... / ..-. ..- -."
message = "- .... .. ... / .. ... / ..-. ..- -."
text = decode(message)
print(text)
# Output: THIS IS FUN
You may also read:
Recipe Recommender in Python – Python Project
Automatic Email Sender in Python – Python Code to Send Email