Module:Reading time

From ProleWiki, the proletarian encyclopedia
Revision as of 14:59, 17 September 2023 by CriticalResist (talk | contribs)
-- 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
        -- Count words using a more robust pattern
        local wordCount = 0
        for word in content:gmatch("%S+") do
            wordCount = wordCount + 1
        end

        -- 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