Morse code is an encrypted form of communication, consisting of signals which are transmitted at intervals. Fixed combinations of these signals represent the different letters, punctuation, and numbers. The Morse code was invented in 1835 and developed by Samuel Morsewith the aim of using it for sending telegraphic messages . With a telegraph (see figure) they could only choose between two states: the key down (= current) or key upwards (= no current) and duration (short or long). Telegraphy is widely regarded as a forerunner of the later digital communication. Note that at speed races between experienced Morse code operators and experts in sending text messages, Morse code always wins1.

telegraaf

In the international Morse code short signals are noted down as one dot (.) and long signals with a dash (-). Between different characters a short pause is inserted each time which is denoted by a space (letter space), and between different words a longer pause is inserted each time (word space) which is denoted by a slash (/). For example, ... --- ... is commonle known as SOS in Morse code. The text MORSE CODE itself is written as

-- --- .-. ... .      /     -.-. --- -.. .
M  O   R   S   E  (space)  C    O   D   E

Assignment

Given is a text file morsecode.txt2. This text file contains a list of letters (first column) and the corresponding representation in Morse code (second column). The columns are separated by a tab. For example, in this file you can see that the letter K is represented in Morse code as -.- and that a comma is represented as --..--. Asked:

  1. Write a function readMorseCodes that takes the location of a text file. The content of this file must be formatted as the text file morsecode.txt3. The function must return a dictionary, in which the characters from the file are used as keys, and the Morse code notation of these characters and their corresponding values.

  2. Write a function textToMorse to which two parameters need to be passed. For the string that is passed as the first parameter, the function must return the corresponding representation in Morse code. Here the conversion of characters to their corresponding Morse code must be looked up in the dictionary that is passed to the function as a second parameter. This dictionary has the form that is returned by the function readMorseCodes. Characters that are not in the dictionary (and not in morsecode.txt4) should be ignored in the conversion. For conversion into Morse code no distinction should be made between large and small letters in the string that is passed to the function textToMorse as the first parameter.

  3. Write a function morseToText that converts morse code into readable text. The Morse code must be passed to the function as first parameter. Analogous to the textToMorse function the function morseToText must also call on the dictionary, which is passed to the function as a second parameter, for the conversion of Morse code to the corresponding character. This dictionary has the form that is returned by the function readMorseCodes.

Example

In the following interactive Python session we assume that the text file morsecode.txt5 is located in the current directory.

>>> characterToCode = readMorseCodes('morsecode.txt')
>>> characterToCode['S']
'...'
>>> characterToCode['O']
'---'

>>> characterToCode = readMorseCodes('morsecode.txt')
>>> morse = textToMorse('SOS Piet', characterToCode)
>>> morse
'... --- .../.--. .. . -'
>>> morseToText(morse, characterToCode)
'SOS PIET'

Epilogue

The blinking light atop the Capitol Records Tower in Hollywood, Los Angeles spells out the word HOLLYWOOD in Morse code. It's done so ever since the building opened in 1956.