มอดูล:eo-headword

จาก วิกิพจนานุกรม พจนานุกรมเสรี

This module implements Esperanto headword-line templates.


local export = {}
local pos_functions = {}

local PAGENAME = mw.title.getCurrentTitle().text 
local lang = require("Module:languages").getByCode("eo")
local except = require("Module:eo-headword/exceptions")

-- Verb stem must have at least two letters.
local participle_pattern = "..([aio]n?t[aeo]j?n?)$"

local function monosyllabic(word)
	return select(2, mw.ustring.gsub(word, "[AEIOUaeiou]", "")) == 1
end

local function check(data, tracking_categories)
	local catname = "คำนามภาษาเอสเปรันโตที่ขาดรูปผัน"
	
	if data.pos_category == "คำคุณศัพท์" or data.pos_category == "คำบ่งชี้" or data.pos_category == "พาร์ทิซิเพิลเชิงคุณศัพท์" then
		catname = "คำคุณศัพท์ภาษาเอสเปรันโตที่ขาดรูปผัน"
	end
	
	for _, entry in ipairs(data.check) do
		local t = mw.title.new(entry)
		if t and not t.exists then
			table.insert(tracking_categories, catname)
		end
	end

	local content = mw.title.new(PAGENAME):getContent()
	local code = mw.ustring.match(content, "{{eo%-IPA[^}]*}}") or mw.ustring.match(content, "{{eo%-pron[^}]*}}")

	if not code then
		table.insert(tracking_categories, "ศัพท์ภาษาเอสเปรันโตที่ไม่มี eo-IPA")
	end
end

-- Determines which function in pos_functions should be used for a word in {{eo-head}}.
local function getPOS(word)
	-- deal with some exceptions
	if except[word] then
		return except[word]
	end
	
	-- delete anything after "de"
	word = mw.ustring.gsub(word, " de .+$", "")
	
	if word:find(" ") then
		if word:find("o%f[%z%s]") and word:find("a%f[%z%s]") then
			return "คำนาม"
		end
	end
	
	-- deal with letters
	if mw.ustring.match(word,"^[aeiou]$") or mw.ustring.match(word,"^[bcĉdfgĝhĥjĵklmnprsŝtŭvz]o$") then
		return "คำนาม"
	end
	
	-- deal with ordinals
	if mw.ustring.match(word, "^[0-9]+%-?a$") then
		return "คำคุณศัพท์"
	elseif mw.ustring.match(word, "^[0-9]+%-?aj?n?$") then
		return "รูปผันคำคุณศัพท์"
	end
	
	-- Words with only one vowel are always irregular
	if monosyllabic(word) then
		return nil
	elseif mw.ustring.find(word, participle_pattern) then -- detect this firstly
		return "พาร์ทิซิเพิล"
	end
	
	local ending = word:match("[aeiou][^aeiou]*$")
	
	if ending == "a" then
		return "คำคุณศัพท์"
	elseif ending == "aj" or ending == "an" or ending == "ajn" then
		return "รูปผันคำคุณศัพท์"
	elseif ending == "e" or ending == "en" then
		return "คำกริยาวิเศษณ์"
	elseif ending == "o" then
		if mw.ustring.find(word, "^%u") then
			return "คำวิสามานยนาม"
		else
			return "คำนาม"
		end
	elseif ending == "oj" or ending == "on" or ending == "ojn" then
		if mw.ustring.find(word, "^%u") then
			return "รูปผันคำวิสามานยนาม"
		else
			return "รูปผันคำนาม"
		end
	elseif ending == "i" then
		return "คำกริยา"
	elseif ending:find("^[iaou]s$") or ending == "u" then
		return "รูปผันคำกริยา"
	else
		return nil
	end
end
export.getPOS = getPOS

-- The main entry point
function export.show(frame)
	local tracking_categories = {}
	
	local parent_args = frame:getParent().args
	local poscat = parent_args["pos"] or frame.args["poscat"] or getPOS(PAGENAME)
	-- หมวดหมู่เป็นภาษาไทย
	local poscat_th = require("Module:utilities").translate_term(poscat)

	if not poscat_th then
		if mw.title.getCurrentTitle().nsText == "แม่แบบ" then
			poscat_th = "คำนาม"
		else
			error("Part of speech of \"" .. PAGENAME .. "\" cannot be automatically determined.")
		end
	--[[
	else
		if poscat == "plurale tantum" then
			poscat = "pluralia tantum"
		elseif not (poscat == "pluralia tantum" or poscat:find("s$")) then
			poscat = poscat .. "s" -- Pluralize part of speech.
		end
	--]]
	end
	
	local params = {
		["head"] = {list = true},
		["pos"] = {},
		["suff"] = {type = "boolean"},
	}
	
	if pos_functions[poscat_th] then
		for key, val in pairs(pos_functions[poscat_th].params) do
			params[key] = val
		end
	end
	
	local args = require("Module:parameters").process(parent_args, params)
	local data = {lang = lang, pos_category = poscat_th, categories = {}, heads = args["head"], genders = {}, inflections = {}, check = {}}
	
	if pos_functions[poscat_th] then
		pos_functions[poscat_th].func(args, data)
	end
	
	check(data, tracking_categories)
	
	return require("Module:headword").full_headword(data) .. require("Module:utilities").format_categories(tracking_categories, lang)
end

pos_functions["คำคุณศัพท์"] = {
	params = {
	},
	func = function(args, data)
		table.insert(data.inflections, {label = "กรรมการกเอกพจน์", accel = "accusative-form-of", PAGENAME .. "n"})
		table.insert(data.inflections, {label = "พหูพจน์", accel = "plural-form-of", PAGENAME .. "j"})
		table.insert(data.inflections, {label = "กรรมการกพหูพจน์", accel = "accusative-plural-form-of", PAGENAME .. "jn"})
		table.insert(data.check, PAGENAME .. "n")
		table.insert(data.check, PAGENAME .. "j")
		table.insert(data.check, PAGENAME .. "jn")
	end
}

pos_functions["คำบ่งชี้"] = {
	params = {
	},
	func = function(args, data)
		table.insert(data.inflections, {label = "กรรมการกเอกพจน์", accel = "accusative-form-of", PAGENAME .. "n"})
		table.insert(data.inflections, {label = "พหูพจน์", accel = "plural-form-of", PAGENAME .. "j"})
		table.insert(data.inflections, {label = "กรรมการกพหูพจน์", accel = "accusative-plural-form-of", PAGENAME .. "jn"})
		table.insert(data.check, PAGENAME .. "n")
		table.insert(data.check, PAGENAME .. "j")
		table.insert(data.check, PAGENAME .. "jn")
	end
}

pos_functions["คำนาม"] = {
	params = {
		[1] = {list = true, allow_holes = true},
	},
	func = function(args, data)
		-- Get the parameters
		local inflected_words_specified = false
		local inflected_words = {}
		
		for i = 1, args[1].maxindex do
			local word = args[1][i]
			
			if word == "+" or word == "-" then
				word = nil
			end
			
			if word then
				inflected_words[word] = true
				inflected_words_specified = true
			end
		end
		
		local pl = {}
		local acc = {}
		local acc_pl = {}
		
		-- Split multi-word terms
		for word in mw.text.gsplit(PAGENAME, " ", true) do
			local pos = getPOS(word)
			
			-- Inflect each word separately
			if (not inflected_words_specified or inflected_words[word]) and (pos == "คำคุณศัพท์" or pos == "คำนาม" or pos == "คำวิสามานยนาม") then
				local is_letter = ""
				if mw.ustring.match(word,"^[aeiou]$") or mw.ustring.match(word,"^[bcĉdfgĝhĥjĵklmnprsŝtŭvz]o$")then
					is_letter = "-o"
				end
				table.insert(acc, word .. is_letter .. "n")
				table.insert(pl, word .. is_letter .. "j")
				table.insert(acc_pl, word .. is_letter .. "jn")
			else
				table.insert(acc, word)
				table.insert(pl, word)
				table.insert(acc_pl, word)
			end
		end
		
		-- Merge back together
		acc = table.concat(acc, " ")
		pl = table.concat(pl, " ")
		acc_pl = table.concat(acc_pl, " ")
		
		local acc2, pl2, acc_pl2
		
		if PAGENAME == "sozo" then
			acc2 = "sozo-on"
			pl2 = "sozo-oj"
			acc_pl2 = "sozo-ojn"
		end
		
		if args[1][1] == "-" then
			table.insert(data.inflections, {label = "นับไม่ได้"})
			table.insert(data.inflections, {label = "กรรมการก", accel = "uncountable-accusative-form-of", acc})
			table.insert(data.categories, "คำนามนับไม่ได้" .. lang:getCategoryName())
			table.insert(data.check, acc)
		else
			table.insert(data.inflections, {label = "กรรมการกเอกพจน์", accel = "accusative-form-of", acc, acc2})
			table.insert(data.inflections, {label = "พหูพจน์", accel = "plural-form-of", pl, pl2})
			table.insert(data.inflections, {label = "กรรมการกพหูพจน์", accel = "accusative-plural-form-of", acc_pl, acc_pl2})
			table.insert(data.check, acc)
			table.insert(data.check, pl)
			table.insert(data.check, acc_pl)
		end
	end
}

pos_functions["คำวิสามานยนาม"] = {
	params = {
		[1] = {list = true, allow_holes = true},
	},
	func = function(args, data)
		-- Get the parameters
		local inflected_words_specified = false
		local inflected_words = {}
		
		for i = 1, args[1].maxindex do
			local word = args[1][i]
			
			if word == "+" or word == "-" then
				word = nil
			end
			
			if word then
				inflected_words[word] = true
				inflected_words_specified = true
			end
		end
		
		local acc = {}
		local pl = {}
		local acc_pl = {}
		
		local de = false
		
		-- Split multi-word terms
		for word in mw.text.gsplit(PAGENAME, " ", true) do
			local pos = getPOS(word)
			
			if word == "de" then de = true end
			
			-- Inflect each word separately
			if (not inflected_words_specified or inflected_words[word]) and (pos == "คำคุณศัพท์" or pos == "คำนาม" or pos == "คำวิสามานยนาม" or mw.ustring.match(word,'[ao]j$') and not monosyllabic(word)) and not de then
				table.insert(acc, word .. "n")
				table.insert(pl, word .. "j")
				table.insert(acc_pl, word .. "jn")
			else
				table.insert(acc, word)
				table.insert(pl, word)
				table.insert(acc_pl, word)
			end
		end
		
		-- Merge back together
		acc = table.concat(acc, " ")
		pl = table.concat(pl, " ")
		acc_pl = table.concat(acc_pl, " ")
		
		if args[1][1] == "+" then
			table.insert(data.inflections, {label = "กรรมการกเอกพจน์", accel = "accusative-form-of", acc})
			table.insert(data.inflections, {label = "พหูพจน์", accel = "plural-form-of", pl})
			table.insert(data.inflections, {label = "กรรมการกพหูพจน์", accel = "accusative-plural-form-of", acc_pl})
			table.insert(data.check, acc)
			table.insert(data.check, pl)
			table.insert(data.check, acc_pl)
		else
			table.insert(data.inflections, {label = "กรรมการก", accel = "uncountable-accusative-form-of", acc})
			table.insert(data.check, acc)
		end
	end
}

pos_functions["คำกริยา"] = {
	params = {
	},
	func = function(args, data)
		local stem = PAGENAME:sub(1, -2)
		
		table.insert(data.inflections, {label = "ปัจจุบันกาล", accel = "present-form-of", stem .. "as"})
		table.insert(data.inflections, {label = "อดีตกาล", accel = "past-form-of", stem .. "is"})
		table.insert(data.inflections, {label = "อนาคตกาล", accel = "future-form-of", stem .. "os"})
		table.insert(data.inflections, {label = "conditional", accel = "conditional-form-of", stem .. "us"})
		table.insert(data.inflections, {label = "volitive", accel = "volitive-form-of", stem .. "u"})
	end
}

pos_functions["รูปผันคำนาม"] = {
	params = {
		[1] = {},
	},
	func = function(args, data)
		if args[1] == "p" or args[1] == "p+" then
			table.insert(data.genders, "p")
			data.categories = {"คำนามภาษาเอสเปรันโต", "คำนามพหูพจน์เท่านั้นภาษาเอสเปรันโต"}
			table.insert(data.inflections, {label = "กรรมการก", accel = "accusative-form-of", PAGENAME .. "n"})
			table.insert(data.check, PAGENAME .. "n")
			if args[1] == "p+" then
				local singular = mw.ustring.gsub(PAGENAME, "j$", "")
				table.insert(data.inflections, {label = "เอกพจน์", singular})
				table.insert(data.check, singular)
			end
		end
	end
}

pos_functions["คำนามพหูพจน์เท่านั้น"] = {
	params = {
		[1] = {},
	},
	func = function(args, data)
		data.pos_category = "คำนาม"
		table.insert(data.categories, 1, "คำนามภาษาเอสเปรันโต")
		table.insert(data.inflections, {label = "กรรมการก", accel = "accusative-form-of", PAGENAME .. "n"})
		table.insert(data.check, PAGENAME .. "n")
		if args[1] == "+" then
			local singular = mw.ustring.gsub(PAGENAME, "j$", "")
			table.insert(data.inflections, {label = "เอกพจน์", singular})
			table.insert(data.check, singular)
		end
	end
}

pos_functions["พาร์ทิซิเพิล"] = {
	params = {
		[1] = {}, [2] = {}, --these will be phased out
	},
	func = function(args, data)
		local ending = mw.ustring.match(PAGENAME, participle_pattern)
		if ending then
			local vowel, consonant = ending:match("([aoe])(j?n?)$")
			if consonant == "" then
				if vowel == "a" or vowel == "o" then
					table.insert(data.inflections, {label = "กรรมการกเอกพจน์", accel = ending .. "n-form-of", PAGENAME .. "n"})
					table.insert(data.inflections, {label = "พหูพจน์", accel = ending .. "j-form-of", PAGENAME .. "j"})
					table.insert(data.inflections, {label = "กรรมการกพหูพจน์", accel = ending .. "jn-form-of", PAGENAME .. "jn"})
					table.insert(data.check, PAGENAME .. "n")
					table.insert(data.check, PAGENAME .. "j")
					table.insert(data.check, PAGENAME .. "jn")
				elseif vowel == "e" then
					data.categories = {"พาร์ทิซิเพิลเชิงกริยาวิเศษณ์ภาษาเอสเปรันโต"}
				end
			elseif vowel == "e" then
				error("-e" .. consonant .. " is not a valid participle ending!")
			end
			if vowel == "a" then
				data.categories = {"พาร์ทิซิเพิลเชิงคุณศัพท์ภาษาเอสเปรันโต"}
			elseif vowel == "o" then
				data.categories = {"พาร์ทิซิเพิลเชิงนามภาษาเอสเปรันโต"}
			end
		else
			error("This term is not a participle!")
		end
	end
}

return export