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...")
 
No edit summary
Line 3: Line 3:
local p = {}
local p = {}


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


  -- Define the range of reading speeds
    -- Ensure that content is a string
  local minSpeed = 183
    if type(content) == "string" then
  local maxSpeed = 250
        -- 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


  -- Calculate reading times for both minimum and maximum speeds
        -- Define the range of reading speeds
  local minReadingTime = math.ceil(wordCount / maxSpeed)
        local minSpeed = 183
  local maxReadingTime = math.ceil(wordCount / minSpeed)
        local maxSpeed = 250


  -- Create the reading time string
        -- Calculate reading times for both minimum and maximum speeds
  local readingTime = "Between " .. minReadingTime .. " and " .. maxReadingTime .. " minutes"
        local minReadingTime = math.ceil(wordCount / maxSpeed)
        local maxReadingTime = math.ceil(wordCount / minSpeed)


  return readingTime
        -- Create the reading time string
        local readingTime = "Between " .. minReadingTime .. " and " .. maxReadingTime .. " minutes"
 
        return readingTime
    else
        return "Error: Invalid content"
    end
end
end


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

Revision as of 14:54, 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
        -- 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 / 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