Morse code for english python3

I want to convert morse code to english using python 3+ i managed to convert english to morse code using this http://code.activestate.com/recipes/578407-simple-morse-code-translator-in-python/

But I want to convert Morse code to English

I tried to do this one charecter at a time, but the problem is that the Morse code letters are not 1 charecter long, like English letters, so E is "." . and S is "...", the problem is that in the loop of the word there is "." . and match it with E, so instead of getting S, I get "EEE", I tried to fix it by finding spaces and making that word at a time, but instead of looking for letters in the word, it searches for the whole word against the dictionary I'm new to Python and dictionaries, and I don’t know how to distinguish between E "." and S "..." when searching in my dictionary

Here is my code

# defines the dictionary to convert morse to english CODE_reversed = {'..-.': 'F', '-..-': 'X', '.--.': 'P', '-': 'T', '..---': '2', '....-': '4', '-----': '0', '--...': '7', '...-': 'V', '-.-.': 'C', '.': 'E', '.---': 'J', '---': 'O', '-.-': 'K', '----.': '9', '..': 'I', '.-..': 'L', '.....': '5', '...--': '3', '-.--': 'Y', '-....': '6', '.--': 'W', '....': 'H', '-.': 'N', '.-.': 'R', '-...': 'B', '---..': '8', '--..': 'Z', '-..': 'D', '--.-': 'Q', '--.': 'G', '--': 'M', '..-': 'U', '.-': 'A', '...': 'S', '.----': '1'} def main(): #takes user message and makes it upper case msg = input('MESSAGE: ') msg = msg.upper() addtolist = "" # creates blank string message = [] # creates blank list for i in msg: addtolist = addtolist + i # adds every letter in MSG into a string until it finds a space addtolist.upper() # makes every letter uppercase if i == " ": # if it detects a space message.extend(addtolist) # adds the previously created addtolist to a list, this makes one word addtolist = "" # clears previous variable for i in message: # for every word in the list str(i) # make it into a string print(CODE_reversed[i()]) # search the dictionary for that word if __name__ == "__main__": main() 

This code takes the word and tries to find it against the dictionary, but it doesn’t work. I also tried to find every letter of the Morse codes against the dictionary, but the problem is that you cannot say when the letter starts and ends, so β€œ...” appears like "EEE" instead of "S" and I don’t know how to fix it.

I also tried to find solutions, but found them only in Java, and I don't know Java

+6
source share
1 answer

Once you define a mapping in one direction, you can use the dict comprehension to map it in another way.

 CODE = {'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': '----.' } CODE_REVERSED = {value:key for key,value in CODE.items()} 

You can then use join with a generator expression to perform translations.

 def to_morse(s): return ' '.join(CODE.get(i.upper()) for i in s) def from_morse(s): return ''.join(CODE_REVERSED.get(i) for i in s.split()) >>> to_morse('hello') '.... . .-.. .-.. ---' >>> from_morse('.... . .-.. .-.. ---') 'HELLO' 
+14
source

All Articles