简体   繁体   English

如何让我的 Python 摩尔斯电码解码器在一个单词被翻译后分离?

[英]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?当我让我的解码器运行时,我可以将一个单词从 Morse 翻译成普通单词,但如果我使用多个单词,它不会分隔单词,我该如何分隔单词? 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: hello my name is ,是:

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

when I run the decoder it gives me hellomynameis , I would like it to give me hello my name is当我运行解码器时,它给了我hellomynameis ,我希望它给我hello my name is

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.除了输入中的space之外,您没有提供任何其他分隔符,因此您无法以任何方式划分单词。

Your solution is to give your input a word separator (for example您的解决方案是给您的输入一个单词分隔符(例如 (double space), then split with .split(" ") and loop tru words). (双空格),然后用.split(" ")和循环 tru 单词分割)。

Other solution might be nltk library, which might have some special functions for that - but here I'm just guessing.其他解决方案可能是nltk库,它可能有一些特殊功能 - 但我只是在猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM