摩尔斯电码(Ohaver Scheme)Python

时间:2017-12-27 15:42:51

标签: python

在这里做一个摩尔斯电码的项目,需要一些帮助。

它是如何工作的?

  1. 输入消息:PUEL

  2. 摩尔斯电码:。 - ... - ..- .. 4314(数字是指基于输入消息中每个字母的点和短划线数量的代码长度(由&#34分隔; &#34)

  3. 反向访问莫尔斯电码字符串进行编码。 (4314< - 在这里访问字符串)

  4. 使用反向数字串将点和短划线转换回文本(参见3)

  5. 结果:PERL

  6. 我制作的主要代码(重要代码):

    ## this is my morseDictionary 
    # convert from text to morsecode.
    morseDictionary = {
        "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" : "--..",
        "_" : "..--",
        "," : ".-.-",
        "." : "---.",
        "?" : "----"
    
    }
    
     # reverse dictionary of morseDictionary
     # convert from morse to text.
     morseReverse = dict((value,key) for (key,value) in morseDictionary.items())
    
    
    # convert the message from string to morse code. Ex : .--...--- 2322
    def convertMorseM(string):
      string1 = ""
      string2 = ""
      codecombine = ""
      code_length = ""
      final_string = ""
      for letter in string:
          string1 = morseDictionary[letter]
          string2 = str(len(morseDictionary[letter]))
          code_length = code_length + string2
          codecombine = codecombine + string1
          final_string = codecombine + " " + code_length
      return final_string
    
    # where the encoding of morse applies (problem)
    def encodeMessage(message):
      final_string = " "
      index = 0
      string = " "
    
      for items in message[::-1]:
          if items.isspace():
              return final_string
          string = morseReverse[(message[:index + int(items)])]
          final_string = final_string + string
          index = index + int(items)
    
      # main function
      def main():
        print("Welcome to morse code converter !")
        print("Enter your message to see your awesome message translated to a morse code !")
        message = input("Enter a message : ")
        message = message.upper()
    
        # function calling for all necessary items
        # numbers of characters in the message.
        codenum = str(len(message))
    
        # converts into morse code with code length etc : .--...--- 2322
        convMessage = convertMorseM(message)
    
        ## gets the code length of the string
        codLength = codeLength(message)
    
        ## encodeds the convMessage starting from behind <--
        encodeM = encodeMessage(convMessage)
    
    
        ## display all data here
        print("Your message : " + message)
        print("Morse code convert : " + convMessage)
        print("Word length : " + codenum)
        print("Code Length for each letter : " + codLength)
        print(" ")
        print("Encoded Message : " + encodeMessage)
    
        print(" ")
    

    尝试此代码并发生错误:

       File "MorseCode.py", line 41, in encodeMessage
       string = morseRev[message[:index + int(items)]]
       KeyError : '.--...-'
    

    我需要有关此问题的帮助。谢谢

1 个答案:

答案 0 :(得分:-1)

您编写的程序过于复杂,因此我很难为您调试它。我更容易重写它,这样你就可以看到如何以更简单的方式做你想做的事情。从输入开始,我认为你真的只需要三件事:

  1. 作为一系列dits和dahs的输入(你不需要这里的空格)
  2. 反转长度
  3. 倒莫尔斯字典(从dit-dahs到字符)
  4. 得到(1):

    input = 'PUEL'
    input_morse_str = ''.join([morse_dictionary[c] for c in input])
    

    得到(2):

    此处不需要字符串(您在示例程序中执行str然后int,您也可以将它们保留为整数):

    reversed_lengths = list(reversed([len(morse_dictionary[c]) for c in input]))
    

    最后,您还需要用于摩尔斯电码的倒置字典(3):

    inverse_dictionary = {value: key for (key, value) in morse_dictionary.items()}
    

    使用这三种数据结构,您可以继续解码:

    output = ''; i = 0
    for l in reversed_lengths:
        output += inverse_dictionary[input_morse_str[i:i+l]]
        i += l
    print output
    

    这里的所有内容放在一个工作脚本中(输入硬编码):

    $ cat morse_reverse.py 
    #!/usr/bin/env python
    
    morse_dictionary = {
        "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" : "--..", "_" : "..--", "," : ".-.-", "." : "---.", "?" : "----"
    }
    
    inverse_dictionary = {value: key for (key, value) in morse_dictionary.items()}
    
    
    input = 'PUEL'
    input_morse = [morse_dictionary[c] for c in input]
    input_morse_str = ''.join(input_morse)
    reversed_lengths = list(reversed([len(c) for c in input_morse]))
    
    output = ''
    i = 0
    for l in reversed_lengths:
        output += inverse_dictionary[input_morse_str[i:i+l]]
        i += l
    print output
    $ python morse_reverse.py
    PERL