Skip to content

Instantly share code, notes, and snippets.

@Timo614
Last active June 6, 2025 20:40
Show Gist options
  • Save Timo614/cabb101688b04532c1e571f08932dc90 to your computer and use it in GitHub Desktop.
Save Timo614/cabb101688b04532c1e571f08932dc90 to your computer and use it in GitHub Desktop.
import sys
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",
"-----": "0", ".----": "1", "..---": "2", "...--": "3", "....-": "4",
".....": "5", "-....": "6", "--...": "7", "---..": "8", "----.": "9",
".-.-.-": ".", "--..--": ",", "..--..": "?", ".----.": "'",
"-.-.--": "!", "-..-.": "/", "-.--.": "(", "-.--.-": ")",
".-...": "&", "---...": ":", "-.-.-.": ";", "-...-": "=",
".-.-.": "+", "-....-": "-", "..--.-": "_", ".-..-.": "\"",
"...-..-": "$", ".--.-.": "@",
"...---...": "SOS"
}
def normalize_morse_string(raw_string):
unicode_dots = ["\u00B7", "\u22C5", "\u2022", "\u2219"]
unicode_dashes = ["\u2013", "\u2014", "\u2212"]
normalized_chars = []
for ch in raw_string:
if ch in unicode_dots:
normalized_chars.append(".")
elif ch in unicode_dashes:
normalized_chars.append("-")
else:
normalized_chars.append(ch)
return "".join(normalized_chars)
def decode_morse_letter(pattern, morse_dict, unknown_patterns):
if pattern in morse_dict:
return morse_dict[pattern]
else:
unknown_patterns.append(pattern)
return "?"
def decode_full_morse_string(morse_string, morse_dict):
unknown_patterns = []
decoded_lines = []
for line in morse_string.split("\n"):
words = line.split("\t")
decoded_words = []
for word_chunk in words:
word_chunk = word_chunk.strip()
if not word_chunk:
continue
letter_patterns = word_chunk.split(" ")
decoded_letters = []
for pat in letter_patterns:
pat = pat.strip()
if not pat:
continue
decoded_char = decode_morse_letter(pat, morse_dict, unknown_patterns)
decoded_letters.append(decoded_char)
decoded_words.append("".join(decoded_letters))
decoded_lines.append(" ".join(decoded_words))
decoded_text = "\n".join(decoded_lines)
return decoded_text, unknown_patterns
if __name__ == "__main__":
RAW_MORSE_SEGMENTS = [
"· · ···- ·· ·-· – ··- ·- ·-·· ·-·· -.-- · \t · · · · · · ·· -· ···- ·· ··· ·· -··· ·-·· ·",
". -.. .. --. . - .- .-.. . . . \t .. -. - . .-. .--. .-. . - .- - .. -",
". . ... .... .- -.. --- .-- . . \t ..-. --- .-. -.-. . ... . . . . .",
".-.. ..- -.-. .. -.. . . . \t -- . -- --- .-. -.-- .",
"- .. ... -.-- --- ..- .-. \t .--. --- ... .. - .. --- -. .",
".-. --.-",
"...---..."
]
RAW_MORSE_INPUT = "\t\n".join(RAW_MORSE_SEGMENTS)
normalized = normalize_morse_string(RAW_MORSE_INPUT)
decoded_text, unknowns = decode_full_morse_string(normalized, MORSE_CODE_DICT)
print(normalized)
print(decoded_text)
@Timo614
Copy link
Author

Timo614 commented Jun 6, 2025

. . ...- .. .-. - ..- .- .-.. .-.. -.-- . 	 . . . . . . .. -. ...- .. ... .. -... .-.. .	
. -.. .. --. . - .- .-.. . . . 	 .. -. - . .-. .--. .-. . - .- - .. -	
. . ... .... .- -.. --- .-- . . 	 ..-. --- .-. -.-. . ... . . . . .	
.-.. ..- -.-. .. -.. . . . 	 -- . -- --- .-. -.-- .	
- .. ... -.-- --- ..- .-. 	 .--. --- ... .. - .. --- -. .	
.-. --.-	
...---...
EEVIRTUALLYE EEEEEEINVISIBLE
EDIGETALEEE INTERPRETATIT
EESHADOWEE FORCESEEEEE
LUCIDEEE MEMORYE
TISYOUR POSITIONE
RQ
SOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment