o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)
Write Python code that translate morse code into language
- User
Write Python code that translate morse code into language
- Assistant
Certainly! Below is a Python function that translates Morse code into English text. The function uses a dictionary to map Morse code symbols to their corresponding English letters, numbers, and punctuation.
def morse_to_text(morse_code): # Dictionary representing the Morse code chart 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' } # Initialize an empty string to store the translated message message = '' # Split the Morse code string into words using ' / ' as a separator words = morse_code.strip().split(' / ') # Iterate over each word in the Morse code input for word in words: # Split the word into individual Morse code letters letters = word.strip().split() # Iterate over each letter and translate it for letter in letters: # Get the corresponding English character from the dictionary decoded_letter = MORSE_CODE_DICT.get(letter, '') # Append the character to the message message += decoded_letter # Add a space after each word message += ' ' # Return the translated message, removing any trailing spaces return message.strip() # Example usage: morse_code_input = '.... . .-.. .-.. --- / .-- --- .-. .-.. -..' translated_text = morse_to_text(morse_code_input) print(translated_text) # Output: HELLO WORLDExplanation:
- The
morse_to_textfunction takes a string of Morse code as input. - It defines a dictionary
MORSE_CODE_DICTthat maps Morse code patterns to English characters. - The input Morse code is split into words using
' / 'as a separator, which is a common convention for representing spaces in Morse code. - Each word is then split into individual Morse code letters.
- Each Morse code letter is translated to its corresponding English character using the dictionary.
- The translated letters are concatenated to form words, and words are separated by spaces.
- The function returns the translated English message.
Example Input and Output:
# Example 1 morse_code_input = '... --- ...' translated_text = morse_to_text(morse_code_input) print(translated_text) # Output: SOS # Example 2 morse_code_input = '-.-. --- -.. . / .. ... / ..-. ..- -.' translated_text = morse_to_text(morse_code_input) print(translated_text) # Output: CODE IS FUNNotes:
- Ensure that individual Morse code letters are separated by spaces.
- Use
' / 'to separate words in the Morse code input. - The function handles unknown Morse code symbols by ignoring them (they are replaced with an empty string).
Usage Tips:
- You can modify the
MORSE_CODE_DICTto include additional symbols if needed. - To translate English text to Morse code, you would need to create a reverse mapping from characters to Morse code symbols.
Complete Function Ready for Use:
Feel free to integrate this function into your projects or enhance it according to your requirements. It provides a simple way to decode Morse code messages using Python.
- The