How to resolve the algorithm Textonyms step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Textonyms step by step in the Lua programming language

Table of Contents

Problem Statement

When entering text on a phone's digital pad it is possible that a particular combination of digits corresponds to more than one word. Such are called textonyms. Assuming the digit keys are mapped to letters as follows:

Write a program that finds textonyms in a list of words such as   Textonyms/wordlist   or   unixdict.txt. The task should produce a report: Where: At your discretion show a couple of examples of your solution displaying Textonyms. E.G.:

Use a word list and keypad mapping other than English.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Textonyms step by step in the Lua programming language

Source code in the lua programming language

-- Global variables
http = require("socket.http")
keys = {"VOICEMAIL", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
dictFile = "http://www.puzzlers.org/pub/wordlists/unixdict.txt"

-- Return the sequence of keys required to type a given word
function keySequence (str)
    local sequence, noMatch, letter = ""
    for pos = 1, #str do
        letter = str:sub(pos, pos)
        for i, chars in pairs(keys) do
            noMatch = true
            if chars:match(letter) then
                sequence = sequence .. tostring(i)
                noMatch = false
                break
            end
        end
        if noMatch then return nil end
    end
    return tonumber(sequence)
end

-- Generate table of words grouped by key sequence
function textonyms (dict)
    local combTable, keySeq = {}
    for word in dict:gmatch("%S+") do
        keySeq = keySequence(word)
        if keySeq then
            if combTable[keySeq] then
                table.insert(combTable[keySeq], word)
            else
                combTable[keySeq] = {word}
            end
        end
    end
    return combTable
end

-- Analyse sequence table and print details
function showReport (keySeqs)
    local wordCount, seqCount, tCount = 0, 0, 0
    for seq, wordList in pairs(keySeqs) do
        wordCount = wordCount + #wordList
        seqCount = seqCount + 1
        if #wordList > 1 then tCount = tCount + 1 end
    end
    print("There are " .. wordCount .. " words in " .. dictFile)
    print("which can be represented by the digit key mapping.")
    print("They require " .. seqCount .. " digit combinations to represent them.")
    print(tCount .. " digit combinations represent Textonyms.")
end

-- Main procedure
showReport(textonyms(http.request(dictFile)))


  

You may also check:How to resolve the algorithm Palindrome detection step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the RPL programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the OASYS Assembler programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the C# programming language
You may also check:How to resolve the algorithm Discordian date step by step in the MAD programming language