יחידה:ja-headword

מתוך ויקימילון, מיזם רב לשוני ליצירת מילון חופשי שיתופי.

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:ja-headword/תיעוד

local m_ja = require("Module:ja")
local m_ja_ruby = require('Module:ja-ruby')

local find = mw.ustring.find

local export = {}
local pos_functions = {}

local sc = require("Module:scripts").getByCode("Jpan")

local data_range = mw.loadData'Module:ja/data/range'
local range_rubymarkup = ' ^%-%.%%'
local range_katakana =  range_rubymarkup .. data_range.katakana .. data_range.punctuation
local range_hiragana = range_rubymarkup .. data_range.hiragana .. data_range.punctuation
local range_kana = range_rubymarkup .. data_range.katakana .. data_range.hiragana .. data_range.hentaigana .. data_range.punctuation
local range_purekana = data_range.hiragana .. data_range.katakana .. data_range.hentaigana
local range_jp = range_kana .. data_range.kanji .. data_range.ideograph .. data_range.kana_graph

local function remove_links(text)
	return (text:gsub("%[%[[^|%]]-|", ""):gsub("%[%[", ""):gsub("%]%]", ""))
end

local detect_kana_script = require("Module:fun").memoize(function(kana)
	if find(kana, '^[' .. range_katakana .. ']+$') then
		return 'kata'
	elseif find(kana, '^[' .. range_hiragana .. ']+$') then
		return 'hira'
	elseif find(kana, '^[' .. range_kana .. ']+$') then
		return 'both'
	else
		return nil
	end
end)

local en_numerals = {
	"one", "two", "three", "four", "five",
	"six", "seven", "eight", "nine", "ten",
	"eleven", "twelve", "thirteen", "fourteen", "fifteen"
}

local en_grades = {
	"first grade", "second grade", "third grade",
	"fourth grade", "fifth grade", "sixth grade",
	"secondary school", "jinmeiyō", "hyōgaiji"
}

local aliases = {
	['transitive']='tr', ['trans']='tr',
	['intransitive']='in', ['intrans']='in', ['intr']='in',
	['godan']='1', ['ichidan']='2', ['irregular']='irr'
}

local function kana_to_romaji(kana, data, args)
	-- make adjustments for -u verbs and -i adjectives by placing a period before the last character
	-- to prevent romanizing long vowels with macrons
	if (data.pos_category == "verbs") or (data.pos_category == "adjectives" and (args["infl"] == "i" or args["infl"] == "い" or args["infl"] == "is")) then
		kana = mw.ustring.gsub(kana,'([うい])$','.%1')
	end
	-- hyphens for prefixes, suffixes, and counters (classifiers)
	if data.pos_category == "prefixes" then
		kana = kana:gsub('%-?$', '-')
	elseif data.pos_category == "suffixes" or data.pos_category == "counters" or data.pos_category == "classifiers" then
		kana = kana:gsub('^%-?', '-')
	elseif data.pos_category == "proper nouns" then -- automatic caps for proper nouns, if not already specified
		if not kana:match'%^' then
			kana = mw.ustring.gsub(kana, '%f[%a](%a)', '^%1')
		end
	end
	kana = m_ja.kana_to_romaji(kana)
	return kana
end

local function historical_kana(data, hist_kana, modern_kana)
	if hist_kana:match'ゐ' then
		table.insert(data.categories, data.lang_name .. " terms historically spelled with ゐ")
	end
	if hist_kana:match'ゑ' then
		table.insert(data.categories, data.lang_name .. " terms historically spelled with ゑ")
	end
	if hist_kana:match'を' and not (modern_kana and modern_kana:match'を') then
		table.insert(data.categories, data.lang_name .. " terms historically spelled with を")
	end
	if hist_kana:match'ぢ' and not (modern_kana and modern_kana:match'ぢ') then
		table.insert(data.categories, data.lang_name .. " terms historically spelled with ぢ")
	end
	if hist_kana:match'づ' and not (modern_kana and modern_kana:match'づ') then
		table.insert(data.categories, data.lang_name .. " terms historically spelled with づ")
	end
	table.insert(data.info_hist, require('Module:ja-link').link({
		lang = data.lang,
		lemma = hist_kana,
	}, {
		hist = true,
		face = 'head',
		disableSelfLink = true,
	}))
end

local function assign_kana_to_kanji(head, kana, pagename, template_name)
	local m_tu = require'Module:template utilities'

	local kanji_pos = {[0] = { nil, 0}}
	local head_nolink = {}
	local link_border = 0
	local function insert_kanji_pos(substr)
		table.insert(head_nolink, substr)
		for p1, w1 in mw.ustring.gmatch(substr, '()([々' .. data_range.kanji .. '])') do
			p1 = mw.ustring.byteoffset(substr, p1) + link_border
			table.insert(kanji_pos, { p1, p1 + w1:len() - 1 })
		end
	end
	for p1, p2, w1 in  m_tu.gfind_bracket(head, {['%[%['] = ']]'}) do
		insert_kanji_pos(head:sub(link_border + 1, p1 - 1))
		
		local p_pipe = w1:find'|' or 2
		link_border = p1 + p_pipe - 1
		insert_kanji_pos(w1:sub(p_pipe + 1, -3))
		
		link_border = p2
	end
	insert_kanji_pos(head:sub(link_border + 1))
	head_nolink = table.concat(head_nolink)

	local pagetext = mw.title.new(pagename):getContent()
	if not pagetext then return head, kana end

	local non_kanji = {}
	local last_kanji = 1
	for p1 in mw.ustring.gmatch(head_nolink, '[々' .. data_range.kanji .. ']()') do
		table.insert(non_kanji, mw.ustring.sub(head_nolink, last_kanji, p1 - 2))
		last_kanji = p1
	end
	table.insert(non_kanji, mw.ustring.sub(head_nolink, last_kanji))

	for kanjitab in pagetext:gmatch('(){{%s*' .. template_name) do
		kanjitab = select(3, m_tu.find_bracket(pagetext, m_tu.brackets_temp, kanjitab))
		if not kanjitab then error('ill-formed [[t:' .. template_name:gsub('%%', '') .. ']] syntax') end
		kanjitab = m_tu.parse_temp(kanjitab)
		
		local readings = {}
		local readings_len = {}
		
		for i = 1, table.maxn(kanjitab.args) do
			local r_i = kanjitab.args[i] or ''
			local r_o = kanjitab.args['o' .. i] or ''
			if kanjitab.args['k' .. i] then
				readings[i] = kanjitab.args['k' .. i] .. r_o
				readings_len[i] = tonumber(r_i:match'^%s*%D*(%d*)%s*$') or 1
			else
				local r_kana, r_len = r_i:match'^%s*(%D*)(%d*)%s*$'
				readings[i] = r_kana .. r_o
				readings_len[i] = tonumber(r_len) or 1
			end
		end

		local kana_decom = {}
		local reading_id = 1
		local reading_len = 1
		for i = 1, #non_kanji - 1 do
			if reading_len <= 1 then
				reading_len = readings_len[reading_id] or 1

				table.insert(kana_decom, non_kanji[i])
				table.insert(kana_decom, readings[reading_id])

				reading_id = reading_id + 1
			else
				reading_len = reading_len - 1
			end
		end
		table.insert(kana_decom, non_kanji[#non_kanji])
		
		local function strip_nonkana(str, repl)
			return mw.ustring.gsub(str, '[^' .. range_purekana .. ']+', repl) or nil
		end
		local xeno_reading = {strip_nonkana(kana, ''):match('^' .. strip_nonkana(table.concat(kana_decom), '(.-)') .. '$')}
		if #xeno_reading > 0 then
			local head_decom = {}
			reading_id = 1
			reading_len = 1
			for i = 1, #non_kanji - 1 do
				if reading_len <= 1 then
					reading_len = readings_len[reading_id] or 1

					table.insert(head_decom, head:sub(kanji_pos[i - 1][2] + 1, kanji_pos[i][1] - 1))
					table.insert(head_decom, head:sub(kanji_pos[i][1], kanji_pos[i + reading_len - 1][2]))

					reading_id = reading_id + 1
				else
					reading_len = reading_len - 1
				end
			end
			table.insert(head_decom, head:sub(kanji_pos[#non_kanji - 1][2] + 1))
			
			if #head_decom ~= #kana_decom then error('number of parameters in [[t:' .. template_name:gsub('%%', '') .. ']] is incorrect') end
			
			local n_xeno_reading = 0
			for i = 1, #kana_decom, 2 do
				kana_decom[i] = mw.ustring.gsub(kana_decom[i], '[^' .. range_purekana .. ']+', function()
					n_xeno_reading = n_xeno_reading + 1
					if xeno_reading[n_xeno_reading] == '' then return nil
					else return xeno_reading[n_xeno_reading] end
				end)
			end
			
			return table.concat(head_decom, '%'), table.concat(kana_decom, '%')
		end
	end

	return head, kana
end

-- adds category Japanese terms spelled with jōyō kanji or Japanese terms spelled with non-jōyō kanji
-- (if it contains any kanji)
local function categorize_by_kanji(data)
	local number_of_kanji = 0
	for c in mw.ustring.gmatch(data.pagename, '[々' .. data_range.kanji .. ']') do
		number_of_kanji = number_of_kanji + 1
		table.insert(data.categories, (data.lang_name .. " terms spelled with %s kanji"):format(en_grades[m_ja.kanji_grade(c)]))
	end

	-- categorize by number of kanji
	if number_of_kanji == 1 then
		table.insert(data.categories, data.lang_name .. " terms written with one Han script character")
		-- single-kanji terms
		if mw.ustring.len(data.pagename) == 1 then
			table.insert(data.categories, data.lang_name .. " terms spelled with " .. data.pagename)
			table.insert(data.categories, data.lang_name .. " single-kanji terms")
		end
	elseif en_numerals[number_of_kanji] then
		table.insert(data.categories, (data.lang_name .. " terms written with %s Han script characters"):format(en_numerals[number_of_kanji]))
	end
end

-- categorize by the script of the pagename or specific characters contained in it
local function extra_categorization(data)
	-- if data.pagename is hiragana, put in that category, same for katakana (but do it at the end)
	if detect_kana_script(data.pagename) == 'hira' then table.insert(data.categories, data.lang_name .. " hiragana") end
	if detect_kana_script(data.pagename) == 'kata' then table.insert(data.katakana_category, data.lang_name .. " katakana") end
	if find(data.pagename, "[^" .. range_jp .. "]") and find(data.pagename, '[' .. range_jp .. ']') then
		table.insert(data.categories, data.lang_name .. " terms written in multiple scripts") end

	for _,character in ipairs({'々','〆','ヶ','ゝ','ゞ','ヽ','ヾ','ゐ','ヰ','ゑ','ヱ','ゔ','ヷ','ヸ','ヹ','ヺ','・','=','゠'}) do
		if data.pagename:match(character) then
			table.insert(data.categories, (data.lang_name .. " terms spelled with %s"):format(character))
		end
	end

	if find(data.pagename, "[" .. data_range.hiragana .. "]") and find(data.pagename, "[" .. data_range.katakana .. "]") and data.pos_category ~= "proverbs" and data.pos_category ~= "phrases" then
		table.insert(data.categories, data.lang_name .. " terms spelled with mixed kana")
	end
end

-- go through args and build inflections by finding whatever kanas were given to us
local function format_headword(args, data, head)
	local headword_kana_type = detect_kana_script(data.pagename)

	local allkana, romajis = {}, {}
	local rep = {}
	local _insert_kana = headword_kana_type and function(k) -- pure-kana-title entry
		if k == '' then return end
		local key = remove_links(k:gsub("[%^%-%. %%]", ""))
		if not rep[key] then
			table.insert(allkana, k)
			rep[key] = true
		end
		romajis[1] = kana_to_romaji(remove_links(k), data, args)
	end or function(k) -- non-pure-kana-title entry
		if k == '' then return end
		local key = m_ja.kana_to_romaji(remove_links(k:gsub("[%^%-%. %%]", "")))
		if not rep[key] then
			table.insert(romajis, kana_to_romaji(remove_links(k), data, args))
			table.insert(allkana, k)
			rep[key] = true
		end
	end

	if headword_kana_type then
		_insert_kana(data.pagename)
	end

	for i, arg in ipairs(args[1]) do
		-- test for kana: filter out POS designations
		if find(arg, '^[' .. range_kana .. ']+$') then
			_insert_kana(arg)
		end
	end

	if args["rom"] then romajis[1] = args["rom"] end

	if #allkana == 0 then error('Kana form is required') end
	if #romajis == 0 then error('Romaji is required') end

	local suru_ending = data.pos_category == "suru verbs" and '[[する]]' or ''
	if headword_kana_type then
		if #args.head > 0 then
			table.insert(data.categories, data.lang_name .. " terms with redundant head parameter")
		end
		
		for _, kana in ipairs(allkana) do
			table.insert(data.heads, kana:gsub("[%^%-%. %%]", "") .. suru_ending)
		end
		if args.hkana[1] then
			historical_kana(data, args.hkana[1], allkana[1])
		end
		
		data.heads_preserved = remove_links(allkana[1]:gsub("[%^%-%. %%]", "")) .. suru_ending
	else
		for i_kana, kana in ipairs(allkana) do
			local head = args.head[i_kana] or args.head[1] or data.pagename
			if args.head[i_kana] == data.pagename then
				table.insert(data.categories, data.lang_name .. " terms with redundant head parameter")
			end
			
			local head_for_ruby, kana_for_ruby
			if mw.ustring.len(head) > 1 and head:match'%%' == nil and kana:match'%%' == nil then
				head_for_ruby, kana_for_ruby = assign_kana_to_kanji(head, kana, data.pagename, data.lang_code .. '%-kanjitab')
			else
				head_for_ruby, kana_for_ruby = head, kana
			end
			local format_table = m_ja_ruby.parse_text(head_for_ruby, kana_for_ruby, {
				try = 'force',
				try_force_limit = 10000
			})
			
			table.insert(data.heads, m_ja_ruby.to_wiki(format_table, {
				break_link = true,
			}):gsub('<rt>(..-)</rt>', "<rt>[[" .. remove_links(kana:gsub("[%^%-%. %%]", "")) .."|%1]]</rt>") .. suru_ending)
			if args.hkana[i_kana] then
				historical_kana(data, args.hkana[i_kana], kana)
			end
			
			if not data.heads_preserved then
				data.heads_preserved = remove_links(m_ja_ruby.to_markup(format_table)) .. suru_ending
			end
		end
	end
	
	suru_ending = data.pos_category == "suru verbs" and ' suru' or ''
	for _, rom in ipairs(romajis) do
		table.insert(data.translits, '[[' .. rom .. '#' .. data.lang_name .. '|' .. rom .. ']]' .. suru_ending)
	end


	if #romajis > 1 then
		table.insert(data.categories, data.lang_name .. " words with multiple readings")
	end

	data.id = allkana[1] and remove_links(allkana[1]:gsub("[%^%-%. %%]", ""))
end

local function add_transitivity(data, tr)
	tr = aliases[tr] or tr
	if tr == "tr" then
		table.insert(data.info_mid, 'transitive')
		table.insert(data.categories, data.lang_name .. " transitive verbs")
	elseif tr == "in" then
		table.insert(data.info_mid, 'intransitive')
		table.insert(data.categories, data.lang_name .. " intransitive verbs")
	elseif tr == "both" then
		table.insert(data.info_mid, 'transitive or intransitive')
		table.insert(data.categories, data.lang_name .. " transitive verbs")
		table.insert(data.categories, data.lang_name .. " intransitive verbs")
	else
		table.insert(data.categories, data.lang_name .. " verbs without transitivity")
	end
end

local function add_inflections(data, inflection_type, cat_suffix)
	local lemma = data.heads_preserved
	local romaji = remove_links(data.translits[1])
	inflection_type = aliases[inflection_type] or inflection_type

	local function replace_suffix(lemma_from, lemma_to, romaji_from, romaji_to)
		-- e.g. 持って来る, lemma = "[持](も)って来(く)る"
		-- lemma_from = "くる", lemma_to = {"き","きた"}
		local p_kr = range_katakana .. range_hiragana
		local lemma_sub
		local romaji_sub
		local key_pos = {}
		local i1, i2

		romaji_from = romaji_from or m_ja.kana_to_romaji(lemma_from)
		if type(lemma_to) ~= 'table' then lemma_to = {lemma_to} end
		if type(romaji_to) ~= 'table' then romaji_to = {romaji_to} end
		for i, v in ipairs(lemma_to) do
			romaji_to[i] = romaji_to[i] or m_ja.kana_to_romaji(v)
		end

		lemma_sub = lemma
		lemma_from = lemma_from ~= '' and mw.text.split(lemma_from, '') or {} -- lemma_from = {"く","る"}
		local len_lemma_from = #lemma_from -- find the last two kana in "[持](も)って来(く)る"
		key_pos[len_lemma_from + 1] = {-1}
		for i = len_lemma_from, 1, -1 do
			i1, _, i2 = mw.ustring.find(lemma_sub, '[' .. m_ja.kata_to_hira(lemma_from[i]) .. m_ja.hira_to_kata(lemma_from[i]) .. ']()[^' .. p_kr .. ']-$')
			if not i1 then return nil end
			i1 = i1 - 1
			key_pos[i] = {i1, i2}
			lemma_sub = mw.ustring.sub(lemma_sub, 1, i1)
		end
		romaji_sub, i1 = romaji:gsub(romaji_from .. '%s*$', '')
		if i1 ~= 1 then return nil end

		local result = {}
		for i, v in ipairs(lemma_to) do
			local result_single = {lemma_sub}
			for j = 1, len_lemma_from do
				table.insert(result_single, mw.ustring.sub(v, j, j))
				table.insert(result_single, mw.ustring.sub(lemma, key_pos[j][2], key_pos[j + 1][1]))
			end
			table.insert(result_single, mw.ustring.sub(v, len_lemma_from + 1))
			result[i] = {lemma = table.concat(result_single), romaji = romaji_sub .. romaji_to[i]}
			-- "[持](も)って来(" .. "き" .. ")" .. "" .. "" and "[持](も)って来(" .. "き" .. ")" .. "た" .. ""
		end
		return result -- {{lemma="[持](も)って来(き)",romaji="motteki"},{lemma="[持](も)って来(き)た",romaji="mottekita"}}
	end

	local function insert_form(label, ...)
		-- label = "stem" or "past" etc.
		-- ... = {lemma=...,romaji=...},{lemma=...,romaji=...}
		local labeled_forms = {label = label}
		for _, v in ipairs{...} do
			local table_form = m_ja_ruby.parse_markup(v.lemma)
			local form_term = m_ja_ruby.to_wiki(table_form)
			if not form_term:find'%[%[.+%]%]' then
				form_term = '[[' .. m_ja_ruby.to_text(table_form) .. '#' .. data.lang_name .. '|' .. form_term .. ']]'
			end
			table.insert(labeled_forms, {
				term = form_term,
				translit = v.romaji,
			})
		end
		table.insert(data.inflections, labeled_forms)
	end

	local inflected_forms
	if inflection_type == '1' or inflection_type == '1s' then
		table.insert(data.info_mid, '<abbr title="godan (type I) conjugation">godan</abbr>')
		if cat_suffix then
			table.insert(data.categories, "Japanese type 1 " .. cat_suffix)
			if cat_suffix == 'verbs' and data.translits[1] and mw.ustring.find(remove_links(data.translits[1]), '[ieIEīēĪĒ]ru$') then
				table.insert(data.categories, "Japanese  type 1 verbs that end in -iru or -eru")
			end
		end
		if inflection_type == '1' then
			inflected_forms =
				replace_suffix('く', {'き', 'いた'}, 'ku', {'ki', 'ita'}) or
				replace_suffix('ぐ', {'ぎ', 'いだ'}, 'gu', {'gi', 'ida'}) or
				replace_suffix('す', {'し', 'した'}, 'su', {'shi', 'shita'}) or
				replace_suffix('つ', {'ち', 'った'}, 'tsu', {'chi', 'tta'}) or
				replace_suffix('ぬ', {'に', 'んだ'}, 'nu', {'ni', 'nda'}) or
				replace_suffix('ぶ', {'び', 'んだ'}, 'bu', {'bi', 'nda'}) or
				replace_suffix('む', {'み', 'んだ'}, 'mu', {'mi', 'nda'}) or
				replace_suffix('る', {'り', 'った'}, 'ru', {'ri', 'tta'}) or
				replace_suffix('う', {'い', 'った'}, 'u', {'i', 'tta'})
			if inflected_forms then
				insert_form('stem', inflected_forms[1])
				insert_form('past', inflected_forms[2])
			else
				require("Module:debug").track("ja-headword/godan conjugation failed")
			end
		else
			inflected_forms =
				replace_suffix('る', {'り', 'った', 'い'}, 'ru', {'ri', 'tta', 'i'}) or --くださる
				replace_suffix('いく', {'いき', 'いった'}, 'iku', {'iki', 'itta'}) or --行く
				replace_suffix('う', {'い', 'うた'}, 'ou', {'oi', 'ōta'}) --問う
			if inflected_forms then
				insert_form('stem', inflected_forms[1], inflected_forms[3])
				insert_form('past', inflected_forms[2])
			else
				require("Module:debug").track("ja-headword/godan conjugation special failed")
			end
		end
	elseif inflection_type == '2' then
		table.insert(data.info_mid, '<abbr title="ichidan (type II) conjugation">ichidan</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese type 2 " .. cat_suffix) end
		inflected_forms = replace_suffix('る', {'', 'た'}, 'ru', {'', 'ta'})
		if inflected_forms then
			insert_form('stem', inflected_forms[1])
			insert_form('past', inflected_forms[2])
		else
			require("Module:debug").track("ja-headword/ichidan conjugation failed")
		end
	elseif inflection_type == 'suru' then
		table.insert(data.info_mid, '<abbr title="suru (type III) conjugation">suru</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese suru " .. cat_suffix) end
		inflected_forms =
			replace_suffix('する', {'し', 'した'}, 'suru', {'shi', 'shita'}) or
			replace_suffix('ずる', {'じ', 'じた'}, 'zuru', {'ji', 'jita'})
		if inflected_forms then
			insert_form('stem', inflected_forms[1])
			insert_form('past', inflected_forms[2])
		else
			require("Module:debug").track("ja-headword/suru conjugation failed")
		end
	elseif inflection_type == 'kuru' then
		table.insert(data.info_mid, '<abbr title="kuru (type III) conjugation">kuru</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese kuru " .. cat_suffix) end
		inflected_forms = replace_suffix('くる', {'き', 'きた'}, 'kuru', {'ki', 'kita'})
		if inflected_forms then
			insert_form('stem', inflected_forms[1])
			insert_form('past', inflected_forms[2])
		else
			require("Module:debug").track("ja-headword/kuru conjugation failed")
		end
	elseif inflection_type == 'i' or inflection_type == 'い' then
		table.insert(data.info_mid, '<abbr title="-i (type I) inflection">-i</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese い-i " .. cat_suffix) end
		inflected_forms = replace_suffix('い', {'く'}, 'i', {'ku'})
		if inflected_forms then
			insert_form('adverbial', inflected_forms[1])
		else
			require("Module:debug").track("ja-headword/-i inflection failed")
		end
	elseif inflection_type == 'is' then
		table.insert(data.info_mid, '<abbr title="-i (type I) inflection">-i</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese い-i " .. cat_suffix) end
		inflected_forms = replace_suffix('いい', {'よく'}, 'ii', {'yoku'})
		if inflected_forms then
			insert_form('adverbial', inflected_forms[1])
		else
			require("Module:debug").track("ja-headword/slightly irregular -i inflection failed")
		end
	elseif inflection_type == 'na' or inflection_type == 'な' then
		table.insert(data.info_mid, '<abbr title="-na (type II) inflection">-na</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese な-na " .. cat_suffix) end
		inflected_forms = replace_suffix('', {'[[な]]', '[[に]]'}, '', {' na', ' ni'})
		insert_form('adnominal', inflected_forms[1])
		insert_form('adverbial', inflected_forms[2])

	elseif inflection_type == "yo" then
		table.insert(data.info_mid, '<abbr title="yodan conjugation (classical)"><sup><small>†</small></sup>yodan</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese yodan " .. cat_suffix) end
	elseif inflection_type == "kami ni" then
		table.insert(data.info_mid, '<abbr title="kami nidan conjugation (classical)"><sup><small>†</small></sup>nidan</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese kami nidan " .. cat_suffix) end
	elseif inflection_type == "shimo ni" then
		table.insert(data.info_mid, '<abbr title="shimo nidan conjugation (classical)"><sup><small>†</small></sup>nidan</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese shimo nidan " .. cat_suffix) end
	elseif inflection_type == "rahen" then
		table.insert(data.info_mid, '<abbr title="r-special conjugation (classical)"><sup><small>†</small></sup>-ri</abbr>')
	elseif inflection_type == "sahen" then
		table.insert(data.info_mid, '<abbr title="s-special conjugation (classical)"><sup><small>†</small></sup>-se</abbr>')
	elseif inflection_type == "kahen" then
		table.insert(data.info_mid, '<abbr title="k-special conjugation (classical)"><sup><small>†</small></sup>-ko</abbr>')
	elseif inflection_type == "nahen" then
		table.insert(data.info_mid, '<abbr title="n-special conjugation (classical)"><sup><small>†</small></sup>-n</abbr>')
	elseif inflection_type == "nari" or inflection_type == "なり" then
		table.insert(data.info_mid, '<abbr title="-nari inflection (classical)"><sup><small>†</small></sup>-nari</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese なり-nari " .. cat_suffix) end
	elseif inflection_type == 'tari' or inflection_type == 'たり' then
		table.insert(data.info_mid, '<abbr title="-tari inflection (classical)"><sup><small>†</small></sup>-tari</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese たり-tari " .. cat_suffix) end
		inflected_forms = replace_suffix('', {'[[たる]]', '[[と]]', '[[として]]'}, '', {' taru', ' to', ' to shite'})
		insert_form('adnominal', inflected_forms[1])
		insert_form('adverbial', inflected_forms[2], inflected_forms[3])
	elseif inflection_type == "ku" or inflection_type == "く" then
		table.insert(data.info_mid, '<abbr title="-ku inflection (classical)"><sup><small>†</small></sup>-ku</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese く-ku " .. cat_suffix) end
	elseif inflection_type == "shiku" or inflection_type == "しく" then
		table.insert(data.info_mid, '<abbr title="-shiku inflection (classical)"><sup><small>†</small></sup>-shiku</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese しく-shiku " .. cat_suffix) end
	elseif inflection_type == "ka" or inflection_type == "か" then
		table.insert(data.info_mid, '<abbr title="-ka inflection (dialectal)"><sup><small>†</small></sup>-ka</abbr>')
		if cat_suffix then table.insert(data.categories, "Japanese か-ka " .. cat_suffix) end

	elseif inflection_type == 'irr' then
		table.insert(data.info_mid, 'irregular')
		if cat_suffix then table.insert(data.categories, "Japanese irregular " .. cat_suffix) end
	elseif inflection_type == '-' or inflection_type == 'un' then
		table.insert(data.info_mid, 'uninflectable')
	end
end

pos_functions["verbs"] = function(args, data)
	add_transitivity(data, args["tr"])
	add_inflections(data, args["infl"], 'verbs')
end

pos_functions["suffixes"] = function(args, data)
	add_inflections(data, args["infl"])
end

pos_functions["auxiliary verbs"] = function(args, data)
	table.insert(data.categories, data.lang_name .. " auxiliary verbs")
	add_inflections(data, args["infl"])
	data.pos_category = "verbs"
end

pos_functions["suru verbs"] = function(args, data)
	add_transitivity(data, args["tr"])
	add_inflections(data, 'suru', 'verbs')
	data.pos_category = "verbs"
end

pos_functions["adjectives"] = function(args, data)
	add_inflections(data, args["infl"], 'adjectives')
end

pos_functions["nouns"] = function(args, data)
	-- the counter (classifier) parameter, only relevant for nouns
	local counter = args["count"] or ""

	if counter == "-" then
		table.insert(data.inflections, {label = "uncountable"})
	elseif counter ~= "" then
		table.insert(data.inflections, {label = "counter", counter})
	end
end

--[==[
For use in soft redirect pages. Sortkey is not provided.
data = {
	pagename = ..., -- (require) pagename of the redirect page
	lang = ..., -- (required) language object
	categories = {}, -- (required) receive categories
	katakana_category = {}, -- (required) receive katakana-sorted categories
	pos = ..., "noun", "verb", etc. no POS categories if not given
}
]==]
function export.cat(data)
	if type(data) ~= 'table' then return end
	
	data.lang_name = data.lang:getCanonicalName()
	
	categorize_by_kanji(data)
	extra_categorization(data)
	
	if data.pos then
		local pos = (({
			['adj'] = 'adjective', ['verb-suru'] = 'verb',
			['adv'] = 'adverb', ['aux'] = 'verb', ['proper'] = 'proper noun',
		})[data.pos] or data.pos)
		pos = pos .. (pos:sub(-1) == 'x' and 'es' or 's')
		table.insert(data.categories, data.lang_name .. ' ' .. pos)
		table.insert(data.categories, data.lang_name .. ' ' .. require'Module:headword'.pos_lemma_or_nonlemma(pos, true) .. 's')
	end
end

--[==[
The main entry point.
This is the only function that can be invoked from a template.
]==]
function export.show(frame)
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	local args = require('Module:parameters').process(frame:getParent().args, {
		[1] = {list = true},
		['rom'] = {},
		['tr'] = {},
		['infl'] = {}, ['type'] = {alias_of = 'infl'}, ['decl'] = {alias_of = 'infl'},
		['count'] = {},
		['hkana'] = {list = true}, ['hhira'] = {alias_of = 'hkana'}, ['hkata'] = {alias_of = 'hkana'},
		['head'] = {list = true},
		['sort'] = {},
		['pagename'] = {},
	})

	local data = {
		lang = nil,
		sc = sc,
		pos_category = poscat,
		categories = {},
		heads = {},
		no_redundant_head_cat = true,
		translits = {},
		inflections = {},
		genders = {'m'}, -- placeholder
		nogendercat = true,
		sort_key = nil,
		id = nil,
		--custom info
		pagename = args['pagename'] or mw.title.getCurrentTitle().text,
		lang_code = frame.args['lang'] or 'ja',
		lang_name = nil,
		katakana_category = {},
		info_mid = {},
		info_hist = {},
		heads_preserved = nil,
	}
	data.lang = require("Module:languages").getByCode(data.lang_code)
	data.lang_name = data.lang:getCanonicalName()

	-- sort out all the kanas and do the romanization business
	format_headword(args, data)

	-- add certain "inflections" and categories for adjectives, verbs, or nouns
	if pos_functions[poscat] then
		pos_functions[poscat](args, data)
	end

	-- categorize by joyo kanji and number of kanji
	categorize_by_kanji(data)
	-- categorize by the script of the pagename or specific characters contained in it
	extra_categorization(data)

	data.sort_key = args['sort'] or data.id and (data.lang:makeSortKey(data.id)) or nil

	return
		require('Module:utilities').format_categories(data.katakana_category, data.lang, data.sort_key and m_ja.hira_to_kata(data.sort_key)) ..
		require('Module:headword').full_headword(data):gsub('<span class="gender">.-</span>', 
			(#data.info_hist > 0 and '<sup>←' .. table.concat(data.info_hist, ' or ') .. '<sup>[[w:Historical kana orthography|?]]</sup></sup>' or '') ..
			('<i>' .. table.concat(data.info_mid, '&nbsp;') .. '</i>'))
end

return export