Module:Reading time: Difference between revisions

From ProleWiki, the proletarian encyclopedia
(Undo revision 55946 by CriticalResist (talk))
Tag: Undo
No edit summary
Line 2: Line 2:


local p = {}
local p = {}
local mw = require("mw")


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


     -- Ensure that content is a string
     -- Ensure that pageTitle is a string
     if type(content) == "string" then
     if type(pageTitle) == "string" then
         -- Split the content into words using spaces as a delimiter
         -- Use MediaWiki's built-in function to get the page content
         local words = {}
         local content = mw.title.new(pageTitle):getContent()
        for word in content:gmatch("%S+") do
            table.insert(words, word)
        end


         -- Calculate the reading time based on the number of words
         -- Ensure that content is a string
        local wordCount = #words
        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
            -- Define the range of reading speeds
        local minSpeed = 183
            local minSpeed = 183
        local maxSpeed = 250
            local maxSpeed = 250


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


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


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


return p
return p

Revision as of 15:03, 17 September 2023

-- Module:ReadingTime

local p = {}
local mw = require("mw")

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

    -- Ensure that pageTitle is a string
    if type(pageTitle) == "string" then
        -- Use MediaWiki's built-in function to get the page content
        local content = mw.title.new(pageTitle):getContent()

        -- 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
    else
        return "Error: Invalid page title"
    end
end

return p