Education STEM

Morse code translator (Python)

In one of my earlier posts, I described how to take text and generate Morse code. For details on that, see Text to Morse: codes and sound generation (Python). In this post, I’ll do the reverse. The application will take Morse code symbols and translate into English text.

First, let’s start with a fun fact. In my Jeep, I noticed that the driver’s foot rest’s surface look like Morse code. Coincidentally, I saw some posts about it online and sure enough, I wanted to verify if this is indeed a Morse code and if so, what does it say? Below is a picture of the pedal surface.

When you read it from left to right, and top to bottom, each block of dots and dashes indeed translate to a letter in Morse code. My edit above shows how I took the left-most vertical symbols and what they translate to (“SAND”). As you move to the next column top down, you find “SNOW”. Then continuing, we find “RIVERS” and “ROCKS”. Pretty cool little “Easter Egg” if you would.

Okay, back to the topic of actually writing a program that can take any Morse code combination and will spit out the English translation.

The Design

First, I’ll reuse my approach of using dictionary (Key:Value) objects to map Morse code representations to a letter, number, or symbol. I’m using the International Morse codes (there are 3 or so different variants of Morse code). Only this time, instead of using key to match its value, I’ll be using value to look up the key…that way, all my dictionary objects from my previous apps are directly usable!

I take the input as a string (no limit) and break it up into words (separated by spaces, that is) and load them into a List object. Using each of those inputs (which are Morse codes, so they’re are dots and dashes), I find their corresponding keys…the keys are the actual letter, number, or symbol representation.

So, if the user input was “.-  -…  -.-.” my code will break it up into 3 elements: “.-“, ” -…”, and  “-.-.”. This part is converting a string into a List…an array of each element, which are loosely ‘words’.

Then it’ll search for each one in the values part of the dictionary object for an exact match, if it finds it it’ll retrieve the key names for it. In this case, it’d retrieve ‘A’, ‘B’, ‘C’ in this example. If it doesn’t find it, it’ll continue to find a match for numbers and symbols in turns. If at end, it doesn’t find it, it’s not a valid Morse code and will simply inform the user that no translation was found; if SOME were valid and SOME were not, it’ll translate the valid ones and gracefully ignore the invalid ones and continue without disruption.

As there are matches, another array keeps on loading the valid key names as it iterates through any length of input until the entire input is processed. It could also be a file with codes. This array holds the translated words, letters, symbols as individual elements.

Once the translation is complete, each of those elements are rejoined to form a readable string of a sentence in the exact order they were entered…only now in English instead of Morse code.

Let’s take a look at the session clip below:

As you can see the code in first input transates to “HIDAD! SEND $200” because there’s no designated Morse code for SPACE in-between words by International standard, so they’re supposed to appear as that. With timing delay, the word breaks are inferred. However, for ease of input and readability, I also allow a special character “*” to be entered optionally to denote the word breaks. So, in the input immediately following it, you’ll notice the asterisks in-between some words as desired. Now, my code knows exactly where to insert the spaces for prettier output: “HI DAD! SEND $200”

—to which, I could reply using my other app by typing in English and have it transmit in Morse code something like: YOU STILL OWE ME 🙂

In the clip below, I wanted to use the app (instead of manually matching each letter from a page/table) to show me what those words in Jeep would look like…

Hmmm…entering those symbols, it does check out! They are indeed the words: SAND, SNOW, RIVERS, ROCKS

The codes are entered using dash (-) or minus key and decimal (.) or period key but for the clip, I just copied them and pasted them from Notepad.

Want to get the full code or more information? Please see below.

This post is not meant to be a step-by-step, detailed tutorial, instead to serve key concepts, and approaches to problem-solving. Basic->Intermediate technical/coding knowledge is assumed.
If you like more info or source file to learn/use, or need more basic assistance, you may contact me at tanman1129 at hotmail dot com. To support this voluntary effort, you can donate via Paypal from the button in my Home page, or become a Patron via my Patreon site. A supporter/patron gets direct answers on any of my blogs including code/data/source files whenever possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top