python - morse code to english python3 -


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

but want convert morse code english

i have attempted 1 charecter @ time, problem morse code letters not 1 charecter long english letters, e "." , s "...", problem have dictionary loop find "." , match e, instead of getting s "e e e" tried fix detecting spaces , doing word @ time, instead of looking letters in word searches entire word against dictionary i'm new python , dictionaries , don't know how differeniate between e "." , s "..." when searching dictionary

here code

# defines dictionary convert morse 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 , makes upper case         msg = input('message: ')         msg = msg.upper()      addtolist = "" # creates blank string     message = [] # creates blank list     in msg:         addtolist = addtolist + # adds every letter in msg string until finds space         addtolist.upper() # makes every letter uppercase          if == " ": # if detects space             message.extend(addtolist)             # adds created addtolist list, makes 1 word             addtolist = "" # clears previous variable      in message:         # every word in list         str(i) # make string     print(code_reversed[i()]) # search dictionary word   if __name__ == "__main__":     main() 

this code takes word, , tries against dictionary, doesn't work have tried searching each individual morse code letter against dictionary problem can't tell when letter starts , ends, "..." comes out "eee" instead of "s" , don't know how fix this

i have tried searching solutions have found them in java , not know java

once define mapping in 1 direction, can use dict comprehension map other 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 key,value in code.items()} 

then can use join generator expression perform translations.

def to_morse(s):     return ' '.join(code.get(i.upper()) in s)  def from_morse(s):     return ''.join(code_reversed.get(i) in s.split())  >>> to_morse('hello') '.... . .-.. .-.. ---' >>> from_morse('.... . .-.. .-.. ---') 'hello' 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -