create main decoding file
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dictionary mapping Morse code sequences to English letters
|
| 2 |
+
MORSE_TO_TEXT = {
|
| 3 |
+
'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E',
|
| 4 |
+
'..-.': 'F', '--.': 'G', '....': 'H', '..': 'I', '.---': 'J',
|
| 5 |
+
'-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N', '---': 'O',
|
| 6 |
+
'.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T',
|
| 7 |
+
'..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y',
|
| 8 |
+
'--..': 'Z', '-----': '0', '.----': '1', '..---': '2', '...--': '3',
|
| 9 |
+
'....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8',
|
| 10 |
+
'----.': '9'
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def decode_morse(morse_code: str) -> str:
|
| 14 |
+
"""
|
| 15 |
+
Decodes a Morse code string into plain text.
|
| 16 |
+
- Single spaces separate individual letters.
|
| 17 |
+
- Triple spaces (or a unique delimiter) separate entire words.
|
| 18 |
+
"""
|
| 19 |
+
decoded_message = []
|
| 20 |
+
|
| 21 |
+
# Split the message into words using three spaces as a delimiter
|
| 22 |
+
words = morse_code.split(' ')
|
| 23 |
+
|
| 24 |
+
for word in words:
|
| 25 |
+
decoded_word = ""
|
| 26 |
+
# Split each word into individual letter tokens
|
| 27 |
+
letters = word.split(' ')
|
| 28 |
+
|
| 29 |
+
for letter in letters:
|
| 30 |
+
# Map the Morse token to text, ignore if not found
|
| 31 |
+
if letter in MORSE_TO_TEXT:
|
| 32 |
+
decoded_word += MORSE_TO_TEXT[letter]
|
| 33 |
+
|
| 34 |
+
decoded_message.append(decoded_word)
|
| 35 |
+
|
| 36 |
+
# Join words with a single space
|
| 37 |
+
return ' '.join(decoded_message)
|
| 38 |
+
|
| 39 |
+
# Example execution
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
# Test Morse code for "HELLO WORLD"
|
| 42 |
+
morse = input("Please input your morse code in ...s and ---s")
|
| 43 |
+
print("Decoded data:", decode_morse(sample_morse))
|