Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

How to get my Python Morse code-decoder to separate after a word has been translated?

When I get my decoder to run I can translate a word from Morse to normal but if I use more than one word it doesn't separate the words, how do I separate the words? Here is my code:

code_dict =  {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5',
     '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R',
     '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F',
     '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?',
     '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=',
     '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N',
     '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';',
     '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G',
     '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':',
     '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': '$', '.---': 'J', '-----': '0',
     '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'
     }

def decodeMorse(morseCode):
    results = []
    for item in morseCode.split(' '):
        results.append(code_dict.get(item))
    results = ''.join(results)
    return results.lower()

morseCode = input('Message: ')
print(decodeMorse(morseCode))

Edit:

hello my name is, is:

.... . .-.. .-.. ---  -- -.--  -. .- -- .  .. ...

when I run the decoder it gives me hellomynameis, I would like it to give me hello my name is

question from:https://stackoverflow.com/questions/65887688/how-to-get-my-python-morse-code-decoder-to-separate-after-a-word-has-been-transl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your example made it not possible. You are not giving any other separator than a space in the input and so you are not able to divide words in any way.

Your solution is to give your input a word separator (for example (double space), then split with .split(" ") and loop tru words).

Other solution might be nltk library, which might have some special functions for that - but here I'm just guessing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...