Jump to content

Module:Book Marks2

From Wikipedia, the free encyclopedia
local p = {}

-- A helper function to map text to number
local num_words = {
    ["zero"] = 0, ["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4,
    ["five"] = 5, ["six"] = 6, ["seven"] = 7, ["eight"] = 8, ["nine"] = 9,
    ["ten"] = 10, ["eleven"] = 11, ["twelve"] = 12, ["thirteen"] = 13, 
    ["fourteen"] = 14, ["fifteen"] = 15, ["sixteen"] = 16, ["seventeen"] = 17,
    ["eighteen"] = 18, ["nineteen"] = 19, ["twenty"] = 20, ["twenty-one"] = 21,
    ["twenty-two"] = 22, ["twenty-three"] = 23, ["twenty-four"] = 24,
    ["twenty-five"] = 25, ["twenty-six"] = 26, ["twenty-seven"] = 27,
    ["twenty-eight"] = 28, ["twenty-nine"] = 29, ["thirty"] = 30
}

-- Function to convert text to number
local function text_to_number(text)
    text = text:lower()  -- Convert text to lowercase to handle cases like "One" or "one"
    return num_words[text] or nil  -- Return the mapped number or nil if not found
end

-- ensure no floats
local function is_integer(num)
    return num == math.floor(num)  -- Ensure the number has no fractional part
end

function p.generateSummary(frame)
    -- Retrieve input parameters
    local total_reviews = tonumber(frame.args['total_reviews']) or text_to_number(frame.args['total_reviews']) or 0
    local rave = tonumber(frame.args['rave']) or text_to_number(frame.args['rave']) or 0
    local positive = tonumber(frame.args['positive']) or text_to_number(frame.args['positive']) or 0
    local m = tonumber(frame.args['m']) or text_to_number(frame.args['m']) or 0
    local pan = tonumber(frame.args['pan']) or text_to_number(frame.args['pan']) or 0
    local consensus = frame.args['consensus'] or "Unknown"

    -- Check if review counts sum up to total_reviews
    if total_reviews ~= (rave + positive + m + pan) then
        return "Error: Review counts do not sum to total."
    end

	-- Check for reasonable total 
	if total_reviews < 5 then
		return "Error: There must be at least 5 tot rev"
	end

    -- Check for non-negative integers
    if rave < 0 or positive < 0 or m < 0 or pan < 0 then
        return "Error: All inputs must be non-negative integers."
    end

    -- Build the sentence
    local sentence = "Per [[Literary_Hub#Book_Marks|Book Marks]], a website that [[Review_aggregation|aggregates]] critical reviews for literature from mainstream critics, the book received an overall \"" .. consensus .. "\" consensus rating based on " .. total_reviews .. " independent third-party assessments, including "

	rave = rave or 0
	positive = positive or 0
	m = m or 0
	pan = pan or 0

    local reviews = {}
    if rave > 0 then
        table.insert(reviews, rave .. " \"rave\"")
    end
    if positive > 0 then
        table.insert(reviews, positive .. " \"positive\"")
    end
    if m > 0 then
        table.insert(reviews, m .. " \"mixed\"")
    end
    if pan > 0 then
        table.insert(reviews, pan .. " \"pan\"")
    end

	-- Handle the cases based on the number of reviews
	if #reviews == 4 then
	    sentence = sentence .. table.concat(reviews, ", ", 1, 3) .. ", and " .. reviews[4] .. " reviews."
	elseif #reviews == 3 then
	    sentence = sentence .. table.concat(reviews, ", ", 1, 2) .. ", and " .. reviews[3] .. " reviews."
	elseif #reviews == 2 then
	    sentence = sentence .. reviews[1] .. " and " .. reviews[2] .. " reviews."
	elseif #reviews == 1 then
	    sentence = sentence .. reviews[1] .. " reviews."
	else
	    sentence = sentence .. "no reviews."
	end
	
    return sentence
end

return p