MORSE2ASCII Tutorial: Build a Simple Morse-to-ASCII Converter
This tutorial walks you through building a simple Morse-to-ASCII converter: a program that reads Morse code (dots, dashes, and spaces) and outputs corresponding ASCII text. We’ll cover the Morse alphabet, parsing rules, a clear implementation in Python, and simple tests you can run and expand.
What you’ll build
- A function that converts a Morse string into human-readable ASCII.
- Support for letters A–Z, digits 0–9, basic punctuation (.,?‘/@&:;=+-_“!()$), and word separation.
- Robust handling of extra spaces and invalid sequences.
Morse basics (rules)
- Dot = .
- Dash = –
- Letters are sequences of dots/dashes separated by a single space.
- Words are separated by a slash (/) or by two or more spaces (we’ll treat “/” and double spaces as word separators).
- Unknown sequences map to a placeholder like “?” so you can spot decoding errors.
Morse table (subset)
- A: .-N: -. 0: —– . (period): .-.-.-
- B: -… O: — 1: .—- , (comma): –..–
- C: -.-. P: .–. 2: ..— ? (question): ..–..
- D: -.. Q: –.- 3: …– ’ (apostrophe): .—-.
- E: . R: .-. 4: ….- / (slash): -..-.
- F: ..-. S: … 5: ….. @: .–.-.
- G: –. T: – 6: -…. &: .-…
- H: …. U: ..- 7: –… : (colon): —…
- I: .. V: …- 8: —.. ; (semicolon): -.-.-.
- J: .— W: .– 9: —-. = (equals): -…-
- K: -.- X: -..- +: .-.-.
- L: .-.. Y: -.– -: -….- -M: – Z: –.. _: _ (not standard; map as needed)
(Use the full mapping in the code below.)
Python implementation
python
# morse2ascii.py MORSE_TO_ASCII = { ”.-”:“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”, ”.-.-.-”:”.”,”–..–”:”,”,”..–..”:”?”,”.—-.”:”‘”,”-..-.”:”/”, ”.–.-.”:”@”,”.-…”:”&”,”—…”:”:”,”-.-.-.”:”;”,”-…-”:”=”, ”.-.-.”:”+”,”-….-”:”-”,”.-..-.”:’“’,”-.–.-”:”(”, ”-.–.-”:”)” } def morse_to_ascii(morse_str, unknown=”?”): # Normalize separators: treat ‘/’ as word separator, compress multiple spaces morse_str = morse_str.strip() # Replace slashes with triple-space marker to ensure separation morse_str = morse_str.replace(”/”, ” “) # Split into words on 3+ spaces words = [w for w in import(“re”).split(r”\s{3,}”, morse_str) if w != ””] decoded_words = [] for w in words: letters = [l for l in w.split() if l != ””] decoded = [] for sym in letters: decoded.append(MORSE_TO_ASCII.get(sym, unknown)) decoded_words.append(””.join(decoded)) return ” “.join(decoded_words) if name == ”main“: examples = [ ”… — …”, # SOS ”… — … .- .-.”, # “SOS AR” ”.- / -… / -.-.”, # “A B C” using slashes ”… — … –..– .-.-.-” # “SOS,.” ] for e in examples: print(e, ”=>”, morse_to_ascii(e))
How it works (brief)
- Normalize separators: slashes replaced with triple spaces so both “/” and multi-space separate words.
- Split on 3+ spaces to get words, split words on single spaces for letters.
- Lookup each Morse symbol in the dictionary; unknown symbols become “?”.
Testing
- Run the script with the examples included.
- Try edge cases: leading/trailing spaces, mixed separators, invalid symbols like “…-.-” (should return “?”).
Extensions you can add
- ASCII-to-Morse encoding function.
- Tolerant input parsing for noisy signals (e.g., accept “|” or “,” as separators).
- GUI or web UI, or a command-line flag for case-insensitive output.
- Support prosigns or procedural signals (e.g., SK, SOS special handling).
That’s all — a compact, testable Morse-to-ASCII converter you can extend for more features.
Leave a Reply