0

I am trying to translate Morse code into English with a dictionary I used. I am trying to have it where a user inputs a message and it will print his/her message into Morse code.

I found it easy to translate into Morse code, however Morse code to English gives me some issues.

First, if I type '.-' for 'A' I actually get 'E' instead as it's reading the first '.' key and translating it into 'E' instead of taking the whole string.

This is what I have tried so far :)

#If user types 'N' or 'n' it proceeds this loop
if morse_message == 'N' or morse_message == 'n':    #Loops looks to see if morse_message was 'N' or 'n'
    nomorse = input("Enter your Morse code here:")
    nomorse_list = (nomorse.join('')
    for letter in nomorse_list:
        not_morse = (morse_eng_dict[letter.upper()])
        print(not_morse)

And this is my dictionary I have

morse_eng_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"}

I would like to have Python take my Morse code and print the message back into English. Such as something like .. . .-.. .-.. --- (Which is HELLO)

This is for a project, so I can't use any fancy modules or anything. Any ideas or input? Thank you!

akshat
  • 1,219
  • 1
  • 8
  • 24
PKPython
  • 85
  • 1
  • 7

3 Answers3

0

morse code is built with ambiguity. as you pointed out .- can be treated as e t, or a. there's no way to distinguish them easily without resort to a big dictionary or some fuzzy logic.

operator tends to use spaces to separate the letter and word however: https://en.wikipedia.org/wiki/Morse_code

"The letters of a word are separated by a space equal to three dots (one dash), and the words are separated by a space equal to seven dots." if you insert spaces, eg: 1 space between the letters and 2 spaces between the words, into your morse code string that will make decoding a piece of cake (split then map)

eniacz
  • 127
  • 3
0

Something like this should work: Basically, it splits the string when ever there's a space, making a list in which each item is a morse code letter. It then checks each letter against the dictionary and takes the english counterpart. Lastly, it puts all these into a list, turns it into a string again and prints it. Hope it helps!

morse_eng_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"}

nomorse = input("Enter your Morse code here:")
nomorse_list = nomorse.split() #this splits the string up wherever there is a space
not_morse = []
morse = True    #The code is morse (so far)
for letter in nomorse_list:
    eng_letter = False
    for key in morse_eng_dict.keys():   #for each of the morse code letters in the dictionary
        if letter == key:
            eng_letter = morse_eng_dict[key]
    if eng_letter: #if a letter was found that corresponds
        not_morse.append(eng_letter)
    else:
        print("Input is not valid morse code.")
        morse = False
if morse == True:
    string = "".join(not_morse) #joining the string together (without spaces in between)
    print(string)
rarie
  • 16
  • 4
0

for any dictionary = {'.-': 'a'} dictionary['.-'] will give you 'a' also dictionary.get('.-') will also get you an 'a' indexes for dictionaries are strings, any strings and '.' is different from '.-'

'.','.-','...','-.--'

btw morse is a binary tree that starts with E = left.node and T = right.node ...