Last active
June 6, 2025 20:40
-
-
Save Timo614/cabb101688b04532c1e571f08932dc90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.