Engee documentation
Notebook

Translating text into Morse code

Morse code, also known as Morse code, is a system for transmitting text information. It is based on the use of two signals of different durations, the dot (short signal) and the dash (long signal). This system was developed by the American inventor Samuel Morse in the first half of the 19th century and was one of the first methods of telegraphic communication.

Each letter of the alphabet, digit and some punctuation marks are represented by a unique combination of dots and dashes, for example, the letter A is transmitted as a dot-dash (.-) and the letter B as a dash-dot-dot-dot-dot-dot (-...). There is a short pause between two consecutive dots or dashes within one character, and a slightly longer pause between different characters (letters or numbers). In turn, the pause between words is even longer.

Morse code was widely used in telegraphy to transmit messages over long distances. It has also found use in radio communications, especially in the maritime and aviation industries.

Today, Morse code continues to be studied and used by radio enthusiasts and retro-technology enthusiasts because signal transmission can be done with simple devices such as a telegraph key, and even in poor communication conditions signals can be recognised due to their simplicity.

Now, based on the theoretical foundations of this system described above, let us define a dictionary for Latin letters and Arabic numerals.

In [ ]:
# Словарь для преобразования символов в азбуку Морзе
morse_code = Dict(
    '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' => "----.",
    ' ' => "/" # Пробел между словами обозначается слешем
)
Out[0]:
Dict{Char, String} with 37 entries:
  '1' => ".----"
  'E' => "."
  'Z' => "--.."
  '6' => "-...."
  'X' => "-..-"
  '7' => "--..."
  'B' => "-..."
  'C' => "-.-."
  '5' => "....."
  'D' => "-.."
  'A' => ".-"
  '4' => "....-"
  'R' => ".-."
  'G' => "--."
  '8' => "---.."
  'F' => "..-."
  'N' => "-."
  'M' => "--"
  'K' => "-.-"
  'J' => ".---"
  'O' => "---"
  'I' => ".."
  'P' => ".--."
  'H' => "...."
  'Q' => "--.-"
  ⋮   => ⋮

Let's define the function of string translation in Morse code format. This function searches for a value in the dictionary. If the value is not found, it leaves it unchanged.

In [ ]:
function text_to_morse(text::String)
    morse_message = String[]  
    for char in uppercase(text)
        if haskey(morse_code, char)
            push!(morse_message, string(morse_code[char]))  # Присваиваем значение из словаря
        else
            push!(morse_message, string(char))  # Оставляем исходный символ
        end
    end
    return join(morse_message, " ")  # Объединяем элементы вектора через пробел
end
Out[0]:
text_to_morse (generic function with 1 method)
In [ ]:
# Пример использования функции
input_text = "ENGEE!"
output_morse = text_to_morse(input_text)
println(output_morse)
. -. --. . . !

Conclusion

The results of the demonstration confirm that this function works correctly. Due to the fact that the exclamation mark does not have a value in the dictionary, we get this mark in the output.