Download app.py from kcode-15/Morse_code_decoder: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/kcode-15/Morse_code_decoder/resolve/62b040ec9e42ac7d51eb0d31637f79b4ae6ff41d/app.py
- Command line
-
hf download hf://kcode-15/Morse_code_decoder@62b040ec9e42ac7d51eb0d31637f79b4ae6ff41d/app.py
-
curl -L -o app.py https://huggingface.co/kcode-15/Morse_code_decoder/resolve/62b040ec9e42ac7d51eb0d31637f79b4ae6ff41d/app.py
1.63 kB
| # Dictionary mapping Morse code sequences to English letters | |
| MORSE_TO_TEXT = { | |
| '.-': '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 decode_morse(morse_code: str) -> str: | |
| """ | |
| Decodes a Morse code string into plain text. | |
| - Single spaces separate individual letters. | |
| - Triple spaces (or a unique delimiter) separate entire words. | |
| """ | |
| decoded_message = [] | |
| # Split the message into words using three spaces as a delimiter | |
| words = morse_code.split(' ') | |
| for word in words: | |
| decoded_word = "" | |
| # Split each word into individual letter tokens | |
| letters = word.split(' ') | |
| for letter in letters: | |
| # Map the Morse token to text, ignore if not found | |
| if letter in MORSE_TO_TEXT: | |
| decoded_word += MORSE_TO_TEXT[letter] | |
| decoded_message.append(decoded_word) | |
| # Join words with a single space | |
| return ' '.join(decoded_message) | |
| # Example execution | |
| if __name__ == "__main__": | |
| # Test Morse code for "HELLO WORLD" | |
| morse = input("Please input your morse code in ...s and ---s") | |
| print("Decoded data:", decode_morse(sample_morse)) | |