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
(Created page with "-- Module:ReadingTime local p = {} function p.estimateReadingTime(content) -- Calculate the reading time based on content length local words = mw.ustring.gmatch(content, "%S+") local wordCount = 0 for _ in words 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 / m...")
 
(Undo revision 64043 by CriticalResist (talk))
Tag: Undo
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:ReadingTime
-- 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")


function p.estimateReadingTime(content)
function p.estimateReadingTime(frame)
  -- Calculate the reading time based on content length
    -- Get the page title from the frame parameter
  local words = mw.ustring.gmatch(content, "%S+")
    local pageTitle = frame.args[1]
  local wordCount = 0
  for _ in words do
      wordCount = wordCount + 1
  end


  -- Define the range of reading speeds
    -- Ensure that pageTitle is a string
  local minSpeed = 183
    if type(pageTitle) == "string" then
  local maxSpeed = 250
        -- Use MediaWiki's built-in function to get the page content
        local content = mw.title.new(pageTitle):getContent()


  -- Calculate reading times for both minimum and maximum speeds
        -- Ensure that content is a string
  local minReadingTime = math.ceil(wordCount / maxSpeed)
        if type(content) == "string" then
  local maxReadingTime = math.ceil(wordCount / minSpeed)
            -- Count words using a more robust pattern
            local wordCount = 0
            for word in content:gmatch("%S+") do
                wordCount = wordCount + 1
            end


  -- Create the reading time string
            -- Define the range of reading speeds
  local readingTime = "Between " .. minReadingTime .. " and " .. maxReadingTime .. " minutes"
            local minSpeed = 160
            local maxSpeed = 238


  return readingTime
            -- Calculate reading times for both minimum and maximum speeds
            local minReadingTime = math.floor(wordCount / maxSpeed)
            local maxReadingTime = math.ceil(wordCount / minSpeed)
 
            -- Round the reading times to the nearest multiple of 5
            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
 
            -- Create the reading time string
            local readingTime = minReadingTime .. "-" .. maxReadingTime .. " minutes"
 
            return readingTime
        else
            return "Error: Invalid content"
        end
    else
        return "Error: Invalid page title"
    end
end
end


return p
return p
-- invoke on page with {{#invoke:ReadingTime|estimateReadingTime|{{PAGESIZE:{{PAGENAME}}}}}} ?

Latest revision as of 20:15, 10 March 2024

-- 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 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 = 160
            local maxSpeed = 238

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

            -- Round the reading times to the nearest multiple of 5
            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

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

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

return p