Text To Morse Dynamic

Second “Helpful” post of the day, Here we go, Alright, so I made a module that transforms text into morse, look

-- MorseCodeModule

local MorseCodeModule = {}

-- Morse code dictionary
local morseCode = {
    ["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"] = "--..",
    ["1"] = ".----",   ["2"] = "..---",   ["3"] = "...--",   ["4"] = "....-", 
    ["5"] = ".....",   ["6"] = "-....",   ["7"] = "--...",   ["8"] = "---..", 
    ["9"] = "----.",   ["0"] = "-----",
    [" "] = " "
}

function MorseCodeModule.textToMorse(text)
      local morseCode = ""
    local uppercaseText = string.upper(text)

    for char in uppercaseText:gmatch("[%a%s]") do
        morseCode = morseCode .. (morseCodeTable[char] or "")
        if char ~= " " then
            morseCode = morseCode .. " "
        end
    end

    return string.match(morseCode, "^%s*(.-)%s*$")
end

function MorseCodeModule.morseToText(morseCode)
local decodedText = ""
    local words = {}

    for word in morseCode:gmatch("[^/]+") do
        local decodedWord = ""
        for char in word:gmatch("[^%s]+") do
            for key, value in pairs(morseCodeTable) do
                if value == char then
                    decodedWord = decodedWord .. key
                    break
                end
            end
        end
        table.insert(words, decodedWord)
    end

    decodedText = table.concat(words, " ")

    return decodedText
end

return MorseCodeModule

Example :

local MorseCodeModule = require(game:GetService("ServerScriptService").MorseCodeModule)

local plainText = "Hello World!"
local encodedMorseCode = encodeMorseCode(plainText)
print(encodedMorseCode)
local decodedText = decodeMorseCode(encodedMorseCode)
print(decodedText)

Thanks for the amazing contribution! @vxsqi

3 Likes

Mine it’s a bit better

no cap

1 Like

you’re reminding me of KrimsonWoIf. but with that resource I linked, you can read morse code by audio

1 Like

Yeah, I agree on that, while this module i made it’s more focused on text to morse only

Will there be a Decode function?

Totally, I am trying to improve the module a lot, Maybe I will add even more functions, sooo yeah

You can use string.split(text, '') to get a table with letters, it should be more optimized than performing a sub operation for each one. string.split(text, ' ') also makes it insanely easy to make a decode function.

1 Like

Why not just match the text using string patterns instead?

local morseCodeTable = {
    ["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"] = "--..",
    [" "] = "/"
}

function encodeMorseCode(text)
    local morseCode = ""
    local uppercaseText = string.upper(text)

    for char in uppercaseText:gmatch("[%a%s]") do
        morseCode = morseCode .. (morseCodeTable[char] or "")
        if char ~= " " then
            morseCode = morseCode .. " "
        end
    end

    return string.match(morseCode, "^%s*(.-)%s*$")
end

function decodeMorseCode(morseCode)
    local decodedText = ""
    local words = {}

    for word in morseCode:gmatch("[^/]+") do
        local decodedWord = ""
        for char in word:gmatch("[^%s]+") do
            for key, value in pairs(morseCodeTable) do
                if value == char then
                    decodedWord = decodedWord .. key
                    break
                end
            end
        end
        table.insert(words, decodedWord)
    end

    decodedText = table.concat(words, " ")

    return decodedText
end

local plainText = "Hello World!"
local encodedMorseCode = encodeMorseCode(plainText)
print(encodedMorseCode)
local decodedText = decodeMorseCode(encodedMorseCode)
print(decodedText)
1 Like

thank you, you both are absolutely right!, i’ll have this changed right now, thanks for the contribution !!, very appreciated

No worries. I noticed yours didnt handle spaces too so I wanted to show you how customizable string pattern usage was.

1 Like