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
No edit summary
(trying out another method)
Line 8: Line 8:
local pageNamespace
local pageNamespace


function p.estimateReadingTime(frame)
function p.calculateReadingTime()
     -- Fetching the current page title and namespace
     -- Fetching the current page title and namespace
     pageTitle = mw.title.getCurrentTitle().text
     pageTitle = mw.title.getCurrentTitle().text
Line 42: Line 42:
         maxReadingTime = nil
         maxReadingTime = nil
     end
     end
end
function p.estimateReadingTime(frame)
    if not minReadingTime or not maxReadingTime then
        p.calculateReadingTime()
    end
    return minReadingTime .. "-" .. maxReadingTime .. " minutes"
end
end


function p.estimateReadingLength(frame)
function p.estimateReadingLength(frame)
    if not minReadingTime or not maxReadingTime then
        p.calculateReadingTime()
    end
     local averageReadingTime = (minReadingTime + maxReadingTime) / 2
     local averageReadingTime = (minReadingTime + maxReadingTime) / 2



Revision as of 19:41, 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

            local minSpeed = 160
            local maxSpeed = 238

            minReadingTime = math.floor(wordCount / maxSpeed)
            maxReadingTime = math.ceil(wordCount / minSpeed)

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

            if minReadingTime == 0 then
                minReadingTime = 1
            end
        else
            minReadingTime = nil
            maxReadingTime = nil
        end
    else
        minReadingTime = nil
        maxReadingTime = nil
    end
end

function p.estimateReadingTime(frame)
    if not minReadingTime or not maxReadingTime then
        p.calculateReadingTime()
    end

    return minReadingTime .. "-" .. maxReadingTime .. " minutes"
end

function p.estimateReadingLength(frame)
    if not minReadingTime or not maxReadingTime then
        p.calculateReadingTime()
    end

    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