More languages
More actions
(tweaked estimations just a little bit) |
(adding a function for reading length, editors can pick between reading time and reading length) |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local mw = require("mw") | local mw = require("mw") | ||
function p.estimateReadingTime(frame) | function p.estimateReadingTime(frame) | ||
local pageTitle = frame.args[1] | local pageTitle = frame.args[1] | ||
if type(pageTitle) == "string" then | if type(pageTitle) == "string" then | ||
local content = mw.title.new(pageTitle):getContent() | local content = mw.title.new(pageTitle):getContent() | ||
if type(content) == "string" then | if type(content) == "string" then | ||
local wordCount = 0 | local wordCount = 0 | ||
for word in content:gmatch("%S+") do | for word in content:gmatch("%S+") do | ||
Line 22: | Line 14: | ||
end | end | ||
local minSpeed = 160 | local minSpeed = 160 | ||
local maxSpeed = 238 | local maxSpeed = 238 | ||
local minReadingTime = math.floor(wordCount / maxSpeed) | local minReadingTime = math.floor(wordCount / maxSpeed) | ||
local maxReadingTime = math.ceil(wordCount / minSpeed) | local maxReadingTime = math.ceil(wordCount / minSpeed) | ||
minReadingTime = math.floor(minReadingTime / 5) * 5 | minReadingTime = math.floor(minReadingTime / 5) * 5 | ||
maxReadingTime = math.ceil(maxReadingTime / 5) * 5 | maxReadingTime = math.ceil(maxReadingTime / 5) * 5 | ||
if minReadingTime == 0 then | if minReadingTime == 0 then | ||
minReadingTime = 1 | |||
end | end | ||
local readingTime = minReadingTime .. "-" .. maxReadingTime .. " minutes" | local readingTime = minReadingTime .. "-" .. maxReadingTime .. " minutes" | ||
Line 48: | Line 35: | ||
else | else | ||
return "Error: Invalid page title" | return "Error: Invalid page title" | ||
end | |||
end | |||
function p.estimateReadingLength(frame) | |||
local readingTime = p.estimateReadingTime(frame) | |||
local minReadingTime, maxReadingTime = readingTime:match("(%d+)%-(%d+)") | |||
local averageReadingTime = (tonumber(minReadingTime) + tonumber(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 | ||
end | end | ||
return p | return p |
Revision as of 19:16, 10 March 2024
local p = {}
local mw = require("mw")
function p.estimateReadingTime(frame)
local pageTitle = frame.args[1]
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
local minReadingTime = math.floor(wordCount / maxSpeed)
local maxReadingTime = math.ceil(wordCount / minSpeed)
minReadingTime = math.floor(minReadingTime / 5) * 5
maxReadingTime = math.ceil(maxReadingTime / 5) * 5
if minReadingTime == 0 then
minReadingTime = 1
end
local readingTime = minReadingTime .. "-" .. maxReadingTime .. " minutes"
return readingTime
else
return "Error: Invalid content"
end
else
return "Error: Invalid page title"
end
end
function p.estimateReadingLength(frame)
local readingTime = p.estimateReadingTime(frame)
local minReadingTime, maxReadingTime = readingTime:match("(%d+)%-(%d+)")
local averageReadingTime = (tonumber(minReadingTime) + tonumber(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