Jump to content

Module:ja-acc-multi

From Wiktionary, the free dictionary


local export = {}

local find = mw.ustring.find
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local match = mw.ustring.match

local m_ja = require("Module:ja")
local m_ja_pron = require("Module:ja-pron")
local m_ja_pron_accent = m_ja_pron.accent

function export.show(frame)
	local params = {
		["kana\1"] = {list = true, allow_holes = true},
		["kana\1_accent"] = {list = true, allow_holes = true},
		["kana\1_dev"] = {list = true, allow_holes = true},
		["kana\1_acc"] = {alias_of = "kana\1_accent", list = true, allow_holes = true},
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local kana, kana_acc, kana_dev = args.kana, args.kana_accent, args.kana_dev
	local acckanaoutputtable = {}
	local accromajioutputtable = {}
	local accnamesoutputtable = {}
	local accnumbersoutputtable = {}
	for i, val in ipairs(kana) do
		local accval = kana_acc[i]
		if not accval then
			error("Missing accent value for kana" .. i)
		end
		
		local acc_none = false
		if accval == "none" then
			accval = "0" -- TODO give proper accent value for "none"
			acc_none = true
		end
		
		local pronoutput = m_ja_pron_accent(val, accval, kana_dev[i])
		local acckanaoutput_start = '<span lang="ja" class="Jpan">'
		local acckanaoutput_end = '</span>'
		local accromajioutput_start = '<span class="Latn"><samp>'
		local accromajioutput_end = '</samp></span>'
		local acckanaoutput, accromajioutput, accnameoutput =
			match(pronoutput, "(" .. acckanaoutput_start .. ".-" .. acckanaoutput_end ..
				") (" .. accromajioutput_start .. ".-" .. accromajioutput_end .. ") (%(.-%))")
		
		accromajioutput = string.sub(accromajioutput,
			string.len(accromajioutput_start) + 1,
			string.len(accromajioutput) - string.len(accromajioutput_end))
		
		local errmsg = "Failed to analyze the output by ja-pron"
		
		if string.sub(accromajioutput, 1, 1) == "[" then
			accromajioutput = string.sub(accromajioutput, 2, string.len(accromajioutput))
		else
			error(errmsg)
		end
		if string.sub(accromajioutput, string.len(accromajioutput), string.len(accromajioutput)) == "]" then
			accromajioutput = string.sub(accromajioutput, 1, string.len(accromajioutput) - 1)
		else
			error(errmsg)
		end
		
		local heibansubstr = "[[平板型|Heiban]]"
		local atamadakasubstr = "[[頭高型|Atamadaka]]"
		local odakasubstr = "[[尾高型|Odaka]]"
		local nakadakasubstr = "[[中高型|Nakadaka]]"
		local accname = nil
		if string.find(accnameoutput, heibansubstr, 1, true) then
			accname = heibansubstr
		elseif string.find(accnameoutput, atamadakasubstr, 1, true) then
			accname = atamadakasubstr
		elseif string.find(accnameoutput, odakasubstr, 1, true) then
			accname = odakasubstr
		elseif string.find(accnameoutput, nakadakasubstr, 1, true) then
			accname = nakadakasubstr
		else
			error(errmsg)
		end
		
		local numberpart = string.sub(accnameoutput,
			string.find(accnameoutput, accname, 1, true) + string.len(accname),
			string.len(accnameoutput))
		if not string.find(numberpart, " – [", 1, true) then
			error(errmsg)
		end
		numberpart = string.sub(numberpart, 6, string.len(numberpart))
		if not (string.sub(numberpart, string.len(numberpart) - 1, string.len(numberpart)) == "])") then
			error(errmsg)
		end
		numberpart = string.sub(numberpart, 1, string.len(numberpart) - 1)
		
		table.insert(acckanaoutputtable, acckanaoutput)
		table.insert(accromajioutputtable, accromajioutput)
		if not acc_none then
			table.insert(accnamesoutputtable, accname)
			table.insert(accnumbersoutputtable, numberpart)
		end
	end
	
	return table.concat(acckanaoutputtable)
		.. " " ..
		'<span class="Latn"><samp>[' .. table.concat(accromajioutputtable, " ") .. ']</samp></span>'
		.. " (" .. table.concat(accnamesoutputtable, " + ") .. " – "
		.. table.concat(accnumbersoutputtable, "-") .. ")"
end

return export