Module:Reading time: Difference between revisions

From ProleWiki, the proletarian encyclopedia
No edit summary
Tag: Reverted
(Undo revision 55946 by CriticalResist (talk))
Tag: Undo
Line 9: Line 9:
     -- Ensure that content is a string
     -- Ensure that content is a string
     if type(content) == "string" then
     if type(content) == "string" then
         -- Count words using a more robust pattern
         -- Split the content into words using spaces as a delimiter
         local wordCount = 0
         local words = {}
         for word in content:gmatch("%S+") do
         for word in content:gmatch("%S+") do
             wordCount = wordCount + 1
             table.insert(words, word)
         end
         end
        -- Calculate the reading time based on the number of words
        local wordCount = #words


         -- Define the range of reading speeds
         -- Define the range of reading speeds

Revision as of 15:01, 17 September 2023

-- Module:ReadingTime

local p = {}

function p.estimateReadingTime(frame)
    -- Get the content from the frame parameter
    local content = frame.args[1]

    -- Ensure that content is a string
    if type(content) == "string" then
        -- Split the content into words using spaces as a delimiter
        local words = {}
        for word in content:gmatch("%S+") do
            table.insert(words, word)
        end

        -- Calculate the reading time based on the number of words
        local wordCount = #words

        -- Define the range of reading speeds
        local minSpeed = 183
        local maxSpeed = 250

        -- Calculate reading times for both minimum and maximum speeds
        local minReadingTime = math.ceil(wordCount / maxSpeed)
        local maxReadingTime = math.ceil(wordCount / minSpeed)

        -- Create the reading time string
        local readingTime = "Between " .. minReadingTime .. " and " .. maxReadingTime .. " minutes"

        return readingTime
    else
        return "Error: Invalid content"
    end
end

return p