Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Reading time: Difference between revisions

From ProleWiki, the proletarian encyclopedia
(rolling back tests)
Tag: Manual revert
(quick test)
Tag: Manual revert
Line 1: Line 1:
-- Calculate estimated reading time for any page rounded to multiple of 5. Outputs "X-Y minutes".
-- invoke on any page or template with {{#invoke:Reading time|estimateReadingTime|YourPageHere}} (change YourPageHere)
local p = {}
local p = {}
local mw = require("mw")
local mw = require("mw")


function p.estimateReadingTime(frame)
-- Global variables
     -- Get the page title from the frame parameter
local minReadingTime
     local pageTitle = frame.args[1]
local maxReadingTime
local pageTitle
local pageNamespace
 
function p.calculateReadingTime()
     -- Fetching the current page title and namespace
     pageTitle = mw.title.getCurrentTitle().text
    pageNamespace = mw.title.getCurrentTitle().namespace


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


        -- Ensure that content is a string
         if type(content) == "string" then
         if type(content) == "string" then
            -- Count words using a more robust pattern
             local wordCount = 0
             local wordCount = 0
             for word in content:gmatch("%S+") do
             for word in content:gmatch("%S+") do
Line 22: Line 22:
             end
             end


             -- Define the range of reading speeds
             -- Defining the range of reading speeds
             local minSpeed = 160
             local minSpeed = 160
             local maxSpeed = 238
             local maxSpeed = 238


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


            -- Round the reading times to the nearest multiple of 5
             minReadingTime = math.floor(minReadingTime / 5) * 5
             minReadingTime = math.floor(minReadingTime / 5) * 5
             maxReadingTime = math.ceil(maxReadingTime / 5) * 5
             maxReadingTime = math.ceil(maxReadingTime / 5) * 5
           
 
             -- Ensure that minReadingTime is at least 1
             -- ensure that minReadingTime is at least 1
             if minReadingTime == 0 then
             if minReadingTime == 0 then
              minReadingTime = 1
                minReadingTime = 1
             end
             end
        end
    end
end
function p.estimateReadingTime(frame)
    p.calculateReadingTime()
    return minReadingTime .. "-" .. maxReadingTime .. " minutes"
end


            -- Create the reading time string
function p.estimateReadingLength(frame)
            local readingTime = minReadingTime .. "-" .. maxReadingTime .. " minutes"
    p.calculateReadingTime()
    local averageReadingTime = (minReadingTime + maxReadingTime) / 2


            return readingTime
    if averageReadingTime < 120 then
         else
        return "Short"
            return "Error: Invalid content"
    elseif averageReadingTime >= 120 and averageReadingTime < 360 then
         end
         return "Medium"
    elseif averageReadingTime >= 360 and averageReadingTime < 720 then
         return "Long"
     else
     else
         return "Error: Invalid page title"
         return "Very Long"
     end
     end
end
end


return p
return p

Revision as of 20:13, 10 March 2024

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

-- Global variables
local minReadingTime
local maxReadingTime
local pageTitle
local pageNamespace

function p.calculateReadingTime()
    -- Fetching the current page title and namespace
    pageTitle = mw.title.getCurrentTitle().text
    pageNamespace = mw.title.getCurrentTitle().namespace

    if type(pageTitle) == "string" then
        local content = mw.title.new(pageTitle):getContent()

        if type(content) == "string" then
            local wordCount = 0
            for word in content:gmatch("%S+") do
                wordCount = wordCount + 1
            end

            -- Defining the range of reading speeds
            local minSpeed = 160
            local maxSpeed = 238

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

            minReadingTime = math.floor(minReadingTime / 5) * 5
            maxReadingTime = math.ceil(maxReadingTime / 5) * 5

            -- ensure that minReadingTime is at least 1
            if minReadingTime == 0 then
                minReadingTime = 1
            end
        end
    end
end

function p.estimateReadingTime(frame)
    p.calculateReadingTime()
    return minReadingTime .. "-" .. maxReadingTime .. " minutes"
end

function p.estimateReadingLength(frame)
    p.calculateReadingTime()
    local averageReadingTime = (minReadingTime + maxReadingTime) / 2

    if averageReadingTime < 120 then
        return "Short"
    elseif averageReadingTime >= 120 and averageReadingTime < 360 then
        return "Medium"
    elseif averageReadingTime >= 360 and averageReadingTime < 720 then
        return "Long"
    else
        return "Very Long"
    end
end

return p