跳转到内容

模組:Ja-compound

維基詞典,自由的多語言詞典


local export = {}

local link = require('Module:ja-link').link
-- [[Module:parameters]]

function export.show(frame)
	local args = require("Module:parameters").process(frame:getParent().args, {
		[1] = {list = true, allow_holes = true},
		["t"] = {list = true, allow_holes = true},
		["rom"] = {list = true, allow_holes = true},
		["pos"] = {list = true, allow_holes = true},
		["lit"] = {list = true, allow_holes = true},
		["linkto"] = {list = true, allow_holes = true},
		["s"] = {list = true, allow_holes = true},
		["sort"] = {},
		["nocat"] = {},
	})
	local len = (args[1].maxindex + 1) / 2
	local result = {}
	local categories, sort = {'日語複合詞'}, {}

	local function format_label(labels)
		local labels_f = {}
		for lb in labels:gmatch'[^,]+' do
			local rl = ({
				['rendaku'] = {'連濁', '有連濁的日語詞'},
				['renjo'] = {'連聲', '有連聲的日語詞'},
			})[lb]
			if rl then
				table.insert(labels_f, rl[1])
				table.insert(categories, rl[2])
			else
				table.insert(labels_f, lb)
			end
		end
		return table.concat(labels_f, ',') .. '來自'
	end

	for i = 1, len do
		local kana_this = args[1][i * 2] or args[1][i * 2 - 1]
		local kana_this_raw = kana_this:gsub('[ ^%.%%%-]', '')

		local b_pre, b_suf = kana_this:match'%-$', kana_this:match'^%-'
		local cat_form = args[1][i * 2 - 1] and args[1][i * 2 - 1]:gsub('[ ^%.%%%-]', '') or kana_this_raw
		if b_pre then
			if b_suf then
				table.insert(categories, '含有中缀' .. cat_form ..'的日語詞')
			else
				table.insert(categories, '含有前綴' .. cat_form..'的日語詞')
			end
		elseif b_suf then table.insert(categories, '含有後綴' .. cat_form ..'的日語詞') end

		local data_pos
		if args.s[i] then
			local sandhi_lemma, sandhi_kana, sandhi_label, _s
			_s, sandhi_label = args.s[i]:match'^(.-)%?(.-)$'
			_s = _s or args.s[i]
			sandhi_lemma, sandhi_kana = _s:match'^(.-):(.-)$'
			if sandhi_lemma == '' then
				sandhi_lemma = args[1][i * 2 - 1]
			elseif sandhi_lemma == nil then
				sandhi_lemma = _s
			end

			data_pos = table.concat{
				args['pos'][i] and args['pos'][i] .. ', ' or '',
				sandhi_label and format_label(sandhi_label) or 'as ',
				link{
					lemma = sandhi_lemma,
					kana = sandhi_kana,
					linkto = '-',
				},
			}
			table.insert(sort, ((sandhi_kana or sandhi_lemma):gsub("[%^%-%% ]", "")))
		else
			data_pos = args['pos'][i]
			table.insert(sort, (kana_this_raw:gsub('[%^%% ]', '')))
		end
		table.insert(result, link{
			lemma = args[1][i * 2 - 1],
			kana = args[1][i * 2],
			gloss = args['t'][i],
			transliteration = args['rom'][i],
			lit = args['lit'][i],
			linkto = args['linkto'][i],
			pos = data_pos,
		})
	end
	result = table.concat(result, ' + ')

	sort = args.sort or table.concat(sort)
	sort = (sort == '' and '' or '|') .. sort .. ']]'
	categories = '[[Category:' .. table.concat(categories, sort .. '[[Category:') .. sort
	if args.nocat then categories = '' end

	return result .. categories
end

return export