How to Create a Morse Code Translator using Python

Morse code is a technique of encoding a message utilizing dots, dashesAnd areas. It’s broadly used to transmit messages secretly.

You will have seen the usage of Morse code to convey messages in naval scenes of many motion pictures. We’re speaking about the identical Morse code right here, however the one distinction is that we’re going to write a Python program to translate from English to Morse code and vice versa.

About Morse code

Morse code has totally different patterns for every English alphabet, quantity, punctuation, and non-Latin characters. As soon as you understand the Morse code patterns for various characters, encoding and decoding them is simple. You may test the Morse Code Wikipedia web page for extra particulars and patterns.

Morse code

On this tutorial, we’ll learn to encode plain English textual content into Morse code and vice versa. We are going to use English alphabets, numbers and punctuation whereas encoding decoding. If you wish to add extra kinds of characters, you are able to do it simply as soon as you understand the fundamentals of encoding and decoding.

One factor to recollect is that each uppercase and lowercase letters have the identical Morse code sample. It’s because Morse code is especially used for communication that does not trouble with alphabetic letters as in on a regular basis dialog.

Let’s go to the encryption half for encrypting and decrypting.

English to Morse code

The algorithm to transform plain English textual content to Morse code is straightforward. Let’s test the algorithm.

  1. Create a dictionary of the Morse code patterns with English alphabets, numbers, and punctuation marks.
  2. Iterate over the textual content and add the Morse code sample of every textual content character to the end result.
    • incorporates Morse code an area after every character and a double house after every phrase.
    • So if we come throughout an area within the textual content, which is the phrase separator, we have to add double house to the end result.
  3. The ensuing string would be the Morse code we wanted.
  4. Lastly, return the end result.

Strive writing the code in Python. Don’t fret even if you cannot fairly write it.

Let’s test the code for encoding plain English textual content in Morse code.

# dictionary for mapping characters to morse code
CHARS_TO_MORSE_CODE_MAPPING = {
    'A': '.-',
    'B': '-...',
    'C': '-.-.',
    'D': '-..',
    'E': '.',
    'F': '..-.',
    'G': '--.',
    'H': '....',
    'I': '..',
    'J': '.---',
    'Okay': '-.-',
    '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': '-----',
    '.': '.-.-.-',
    ',': '--..--',
    '?': '..--..',
    ''': '· − − − − ·',
    '!': '− · − · − −',
    '/': '− · · − ·',
    '(': '− · − − ·',
    ')': '− · − − · −',
    '&': '· − · · ·',
    ':': '− − − · · ·',
    ';': '− · − · − ·',
    '=': '− · · · −',
    '+': '· − · − ·',
    '-': '− · · · · −',
    '_': '· · − − · −',
    '"': '· − · · − ·',
    '$': '· · · − · · −',
    '@': '· − − · − ·',
}

# perform to encode plain English textual content to morse code
def to_morse_code(english_plain_text):
    morse_code = ''
    for char in english_plain_text:
        # checking for house
        # so as to add single house after each character and double house after each phrase
        if char == ' ':
            morse_code += '  '
        else:
            # including encoded morse code to the end result
            morse_code += CHARS_TO_MORSE_CODE_MAPPING[char.upper()] + ' '
    return morse_code

morse_code = to_morse_code(
    'Geekflare produces high-quality expertise & finance articles, makes instruments, and APIs to assist companies and folks develop.'
)
print(morse_code)

Under you possibly can see the output of the Morse code. If you have not modified the message, you also needs to see related Morse code in your terminal.

--. . . -.- ..-. .-.. .- .-. .   .--. .-. --- -.. ..- -.-. . ...   .... .. --. .... − · · · · − --.- ..- .- .-.. .. - -.--   - . -.-. .... -. --- .-.. --- --. -.--   · − · · ·   ..-. .. -. .- -. -.-. .   .- .-. - .. -.-. .-.. . ... --..--   -- .- -.- . ...   - --- --- .-.. ... --..--   .- -. -..   .- .--. .. ...   - ---   .... . .-.. .--.   -... ..- ... .. -. . ... ... . ...   .- -. -..   .--. . --- .--. .-.. .   --. .-. --- .-- .-.-.-

Hurrah! We now have the Morse code. You understand what comes subsequent.

Earlier than diving into the decryption program, let’s cease for a second and take into consideration how we will write code to decrypt it.

It’s best to have considered reversing the scenario CHARS_TO_MORSE_CODE_MAPPING dictionary as one of many steps. Doing it manually is hectic and must be up to date each time the unique task modifications. Let’s write code to reverse the dictionary.

def reverse_mapping(mapping):
    reversed = {}
    for key, worth in mapping.gadgets():
        reversed[value] = key
    return reversed

We solely reverse the key-value pairs of the given dictionary with the above code. The ensuing dictionary incorporates values ​​as new keys and keys as new values.

We now have all of the components to decode the Morse code into plain English textual content. With out additional ado, let’s decode the Morse code.

Morse code to English

We will reverse the method of Morse code encryption to get the decryption algorithm. Let’s check out the algorithm for decoding the Morse code in plain English textual content.

  1. Circled CHARS_TO_MORSE_CODE_MAPPING dictionary utilizing the util perform we wrote.
  2. Repeat the Morse code and preserve monitor of the present Morse code character.
    • If we come throughout an area, it means now we have to decode a whole Morse code character.
      • If the present Morse code character is empty and now we have two consecutive areas, add a phrase separator. It is a single house in plain English textual content.
      • If the above situation will not be true, get the decoded character from the dictionary and add it to the end result. Reset the present Morse code character.
    • If we do not come throughout an area, add it to the present morse character.
  3. If there may be the final character, add it to the end result after decoding utilizing the dictionary.
  4. Return the end result on the finish.

Let’s test the code for the above algorithm.

def reverse_mapping(mapping):
    # add perform code from the earlier snippet...

CHARS_TO_MORSE_CODE_MAPPING = {} # add dictionary values 
MORSE_CODE_TO_CHARS_MAPPING = reverse_mapping(CHARS_TO_MORSE_CODE_MAPPING)

def to_english_plain_text(morse_code):
    english_plain_text = ''

    current_char_morse_code = ''
    i = 0
    whereas i < len(morse_code) - 1:
        # checking for every character
        if morse_code[i] == ' ':
            # checking for phrase
            if len(current_char_morse_code) == 0 and morse_code[i + 1] == ' ':
                english_plain_text += ' '
                i += 1
            else:
                # including decoded character to the end result
                english_plain_text += MORSE_CODE_TO_CHARS_MAPPING[
                    current_char_morse_code]
                current_char_morse_code = ''
        else:
            # including morse code char to the present character
            current_char_morse_code += morse_code[i]
        i += 1

    # including final character to the end result
    if len(current_char_morse_code) > 0:
        english_plain_text += MORSE_CODE_TO_CHARS_MAPPING[
            current_char_morse_code]

    return english_plain_text

english_plain_text = to_english_plain_text(
    '--. . . -.- ..-. .-.. .- .-. .   .--. .-. --- -.. ..- -.-. . ...   .... .. --. .... − · · · · − --.- ..- .- .-.. .. - -.--   - . -.-. .... -. --- .-.. --- --. -.--   · − · · ·   ..-. .. -. .- -. -.-. .   .- .-. - .. -.-. .-.. . ... --..--   -- .- -.- . ...   - --- --- .-.. ... --..--   .- -. -..   .- .--. .. ...   - ---   .... . .-.. .--.   -... ..- ... .. -. . ... ... . ...   .- -. -..   .--. . --- .--. .-.. .   --. .-. --- .-- .-.-.- '
)
print(english_plain_text)

I’ve offered the morse code generated by the encryption perform. We get the next output after we run the above program.

GEEKFLARE PRODUCES HIGH-QUALITY TECHNOLOGY & FINANCE ARTICLES, MAKES TOOLS, AND APIS TO HELP BUSINESSES AND PEOPLE GROW.

Comment: the output is in higher case English alphabet as a result of we used higher case alphabet for task within the dictionary.

Final phrases

We now have seen that the output of the decoder perform is uppercase. You may enhance this system by making the output as it’s within the given time by retaining monitor of the lowercase and uppercase letters of the English alphabet. This has nothing to do with Morse code, as each uppercase and lowercase letters have the identical sample. Strive it, as a result of it is extra enjoyable to code.

That is it for this tutorial. Use the packages we have written the subsequent time you come throughout Morse code.

Glad coding 👨‍💻

Now you can take a look at how you can create a random password in Python.

Additionally take a look at these English to Morse code translators.

Leave a Comment

porno izle altyazılı porno porno