def encode_morse(msg, sep='|', mapping=None):
 mapping = (mapping or char_morse)
 assert (sep not in mapping)
 word_sep = (2 * sep)
 mapping[' '] = word_sep
 suffix = (msg and (msg[(-1)] in whitespace))
 msg = (' ' if word_sep else '').join(msg.split())
 chars = set(''.join(msg.split()))
 ok = set(mapping.keys())
 msg = translate(msg, None, ''.join((chars - ok)))
 morsestring = []
 words = msg.split()
 for word in words:
  morseword = []
  for letter in word:
   morseletter = mapping[letter]
   morseword.append(morseletter)
  word = sep.join(morseword)
  morsestring.append(word)
 return (word_sep.join(morsestring) + (word_sep if suffix else ''))

def encode_morse ( msg , sep = '|' , mapping = None ) : mapping = ( mapping or char_morse ) assert ( sep not in mapping ) word_sep = ( 2 * sep ) mapping [ ' ' ] = word_sep suffix = ( msg and ( msg [ ( - 1 ) ] in whitespace ) ) msg = ( ' ' if word_sep else '' ) . join ( msg . split ( ) ) chars = set ( '' . join ( msg . split ( ) ) ) ok = set ( mapping . keys ( ) ) msg = translate ( msg , None , '' . join ( ( chars - ok ) ) ) morsestring = [ ] words = msg . split ( ) for word in words : morseword = [ ] for letter in word : morseletter = mapping [ letter ] morseword . append ( morseletter ) word = sep . join ( morseword ) morsestring . append ( word ) return ( word_sep . join ( morsestring ) + ( word_sep if suffix else '' ) )
encodes a plaintext into popular morse code with letters separated by sep and words by a double sep .