Module:Sandbox/AbstractWikipedia/Functions/en

Module documentation

English specific functions of the Abstract Wikipedia template-renderer prototype.


local p = {}

local f = require("Module:Sandbox/AbstractWikipedia/Functions")
local l = require("Module:Sandbox/AbstractWikipedia/Lexemes")
local wd = require("Module:Sandbox/AbstractWikipedia/Wikidata")

-- The following is a list of English-specific functions implementations for 
-- the template language

-- Ideally, this should be replaced by fetching a lexeme from Wikidata.
function p.SimpleNoun ( base_form, plural_form )
	local result = l.newLexeme ( base_form, "noun" )
	result.addForm(base_form, { number = "singular" })
	plural_form = plural_form or base_form.."s"
	result.addForm(plural_form, { number = "plural"} )
	return result
end

local month_name = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }

function p.Date ( date )
	local result = ''
	if date.day and tonumber(date.day) > 0 then
		result = result .. date.day .. ' '
	end
	if date.month and tonumber(date.month) > 0 and tonumber(date.month) <=12 then
		result = result .. month_name[tonumber(date.month)] .. ' '
	end
	if date.year and tonumber(date.year) ~= 0 then
		result = result .. date.year
	end
	return l.newLexeme ( result, "noun" )
end

-- Generates a pronoun lexeme; currently only third person.
-- If a q_id is given, the generated pronoun will correspond to that item,
-- otherwise a gender-variable pronoun ("he/she") is provided.
function p.Pronoun ( q_id )
	if q_id then 
		local gender = wd.getHumanGender(q_id)
		local lexeme 
		if gender == "masculine" then
			lexeme = f.Lexeme("L485")  -- he
		elseif gender == "feminine" then
			lexeme = f.Lexeme("L484") -- she
		elseif gender == "other" then
			lexeme = f.Lexeme("L371") -- they
		else
			lexeme = f.Lexeme("L507")
		end
		if (wd.isDead(q_id)) then
			-- We add a past tense feature for lexeme of dead people, as they are
			-- normally spoken about in the past tense. This can exposed to the
			-- verb by using the "tsubj relation".
			lexeme.addFeature("nominal_tense", "past")
		end
		lexeme.log()
		return lexeme
	else
		return f.GenderedLexeme("L485", "L484")  -- he / she
	end
end

local ordinals = {"first", "second", "third"}

-- Generates a lexeme corresponding to an ordinal number
function p.Ordinal ( number )
	number = tonumber(number) -- allow string input as well
	-- For small ordinals return spelledout form
	if number>0 and number<=#ordinals then
		return l.newLexeme(ordinals[number], "adjective")
	end
	-- Otherwise construct ordinal using number + ordinal suffix
	local suffix = 'th'
	local last_two_digits = math.abs(number) % 100
	if last_two_digits <= 3 or last_two_digits >= 21 then
		local last_digit = last_two_digits % 10
		if last_digit == 1 then
			suffix = 'st'
		elseif last_digit == 2 then
			suffix = 'nd'
		elseif last_digit == 3 then
			suffix = 'rd'
		end
	end
	return l.newLexeme(tostring(number)..suffix, "adjective")
end

return p