Module:games: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
m copying not needed
add l with stroke
Line 22: Line 22:
local non_diacritic_equivalencies = {
local non_diacritic_equivalencies = {
["ð"] = "d", ["ø"] = "o", ["þ"] = "???", ["đ"] = "d", ["ħ"] = "h", ["ı"] = "i",
["ð"] = "d", ["ø"] = "o", ["þ"] = "???", ["đ"] = "d", ["ħ"] = "h", ["ı"] = "i",
["ŋ"] = "n", ["œ"] = "???", ["ŧ"] = "t", ["ſ"] = "s", ["ß"] = "???", -- ...
["ŋ"] = "n", ["œ"] = "???", ["ŧ"] = "t", ["ſ"] = "s", ["ß"] = "???", ["ł"] = "l", -- ...
}
}



Revision as of 22:27, 18 December 2017

Created for Wiktionary:Christmas Competition 2017. One function powers {{game entry}}, and other functions use data generated by Module:games/data to count points and generate other statistics about the game (see Wiktionary talk:Christmas Competition 2017/game data).


local export = {}

local fun = require "Module:fun"
local m_table = require "Module:table"

local namespace = mw.title.getCurrentTitle().nsText

local function quote(word)
	return "“" .. word .. "”"
end

local function link(word)
	return "[[" .. word .. "]]"
end

local function italicize(word)
	return "''" .. word .. "''"
end

local is_combining = require "Module:Unicode data".is_combining

local non_diacritic_equivalencies = {
	["ð"] = "d", ["ø"] = "o", ["þ"] = "???", ["đ"] = "d", ["ħ"] = "h", ["ı"] = "i",
	["ŋ"] = "n", ["œ"] = "???", ["ŧ"] = "t", ["ſ"] = "s", ["ß"] = "???", ["ł"] = "l", -- ...
}

-- Remove all diacritics. Make use of the replacements shown above.
-- Remove spaces and hyphens.
local function regularize(word)
	return (mw.ustring.lower(mw.ustring.toNFD(word))
		:gsub(
			"[\194-\244][\128-\191]+",
			function (non_ASCII_character)
				if is_combining(mw.ustring.codepoint(non_ASCII_character)) then
					return ""
				end
				return non_diacritic_equivalencies[non_ASCII_character]
			end))
		:gsub(
			"[ -]",
			"")
end

function export.Christmas_Competition_entry(frame)
	local words = m_table.shallowcopy(frame:getParent().args)
	
	if namespace == "" or namespace == "Reconstruction" or namespace == "Appendix" then
		error("This template is only supposed to be used in the Christmas Competition!")
	end
	
	if #words < 3 then
		if namespace == "Template" then
			words = { "Shèngdànjié", "jiellat", "llatino" }
		else
			error("This template wants at least three words.")
		end
	end
	
	local regularized_words = fun.map(
		regularize,
		words)
	
	local regularized_previous_word = table.remove(regularized_words, 1)
	local regularized_last_three = regularized_previous_word:sub(-3)
	local regularized_following_word = table.remove(regularized_words, #regularized_words)
	
	local previous_word = link(table.remove(words, 1))
	local last_three = mw.ustring.sub(previous_word, -3)
	local following_word = link(table.remove(words, #words))
	
	-- Check interposing words.
	local messages, message_number, message
	for _, word in ipairs(regularized_words) do
		local ending = word:match("^" .. regularized_last_three .. "(.+)$")
		
		-- Rules:
		--	-- Interposing words must start with last three letters of previous word.
		--	-- After these three letters must be three or more additional letters.
		--	-- These additional letters must begin the following word.
		if not ending then
			message = "The interposing word "
					.. quote(word) .. " must start with the last three letters of "
					.. quote(previous_word) .. ", " .. quote(regularized_last_three) .. "."
		elseif #ending < 3 then
			local difference = 3 - #ending
			local agreement = difference == 1 and "" or "s"
			message = "The interposing word "
					.. quote(word) .. " needs " .. difference .. " more letter" .. agreement .. "."
		elseif not regularized_following_word:find("^" .. ending) then
			message = "The last word " .. quote(following_word) .. " must start with " .. quote(ending) .. ", the final letters of "
					.. quote(word) .. " after the last three letters of "
					.. quote(previous_word) .. ", " .. quote(last_three) .. ", are removed."
		end
		
		if message then
			messages = messages or {}
			message_number = message_number or 1
			messages[message_number] = message
			message_number = message_number + 1
		end
		message = nil
	end
	
	if messages then
		error(table.concat(messages, " "))
	end
	
	return previous_word .. " ("
		.. table.concat(
			fun.map(
				function(word)
					return italicize(link(word))
				end,
				words),
			", ")
		.. ") "
		.. following_word
end

return export