This implements Japanese headword line templates and all of the associated templates that they called to do categorization and error checking.


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 lang = require("Module:languages").getByCode("ja")
local sc = require("Module:scripts").getByCode("Jpan")
local Latn = require("Module:scripts").getByCode("Latn")

local Japanese_symbols = '%ー・=?!。、'
local katakana_range = 'ァ-ヺーヽヾ'
local hiragana_range = 'ぁ-ゖーゞゝ'
local kana_range = katakana_range .. hiragana_range .. Japanese_symbols
local Japanese_scripts_range = kana_range .. '一-鿌・々'

local katakana_pattern = '^[' .. katakana_range .. Japanese_symbols .. ']*$'
local hiragana_pattern = '^[' .. hiragana_range .. Japanese_symbols .. ']*$'
local kana_pattern = '^[' .. kana_range .. ']*$'
local kana_pattern_full = '^[、' .. kana_range .. '%s%.%-%^%%]*$'

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

local detect_kana_script = require("Module:fun").memoize(function(kana)
	if find(kana, katakana_pattern) then
		return 'kata'
	elseif find(kana, hiragana_pattern) then
		return 'hira'
	elseif find(kana, kana_pattern) then
		return 'both'
	else
		return nil
	end
end)

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 == "igék") or (data.pos_category == "adjectives" and (args["infl"] == "i" or args["infl"] == "い" or args["infl"] == "is")) then
		kana = mw.ustring.gsub(kana,'([うい])$','.%1')
	end
	local romaji = m_ja.kana_to_romaji(kana)

	-- init caps for proper nouns
	if data.pos_category == "tulajdonnevek" then
		romaji = mw.ustring.gsub(romaji, "^%l", mw.ustring.upper)
		romaji = mw.ustring.gsub(romaji, " %l", mw.ustring.upper)
		romaji = mw.ustring.gsub(romaji, "-%l", mw.ustring.upper)
	end

	-- hyphens for prefixes, suffixes, and counters (classifiers)
	if data.pos_category == "prefixes" then
		return romaji .. "-"
	elseif data.pos_category == "suffixes" or data.pos_category == "counters" or data.pos_category == "classifiers" then
		return "-" .. romaji
	else
		return romaji
	end
end

local function ends_in_iru_eru(kana)
	if mw.ustring.sub(kana, -1) ~= "る" then
		return false
	end

	local wanted = {
		["い"]=1, ["き"]=1, ["し"]=1, ["ち"]=1, ["に"]=1, ["ひ"]=1, ["み"]=1, ["り"]=1, ["ゐ"]=1,
		["ぃ"]=1, ["ぎ"]=1, ["じ"]=1, ["ぢ"]=1, ["び"]=1, ["ぴ"]=1,
		["え"]=1, ["け"]=1, ["せ"]=1, ["て"]=1, ["ね"]=1, ["へ"]=1, ["め"]=1, ["れ"]=1, ["ゑ"]=1,
		["ぇ"]=1, ["げ"]=1, ["ぜ"]=1, ["で"]=1, ["べ"]=1, ["ぺ"]=1,

		["イ"]=1, ["キ"]=1, ["シ"]=1, ["チ"]=1, ["ニ"]=1, ["ヒ"]=1, ["ミ"]=1, ["リ"]=1, ["ヰ"]=1,
		["ィ"]=1, ["ギ"]=1, ["ジ"]=1, ["ヂ"]=1, ["ビ"]=1, ["ピ"]=1,
		["エ"]=1, ["ケ"]=1, ["セ"]=1, ["テ"]=1, ["ネ"]=1, ["ヘ"]=1, ["メ"]=1, ["レ"]=1, ["ヱ"]=1,
		["ェ"]=1, ["ゲ"]=1, ["ゼ"]=1, ["デ"]=1, ["ベ"]=1, ["ペ"]=1,
	}

	return wanted[mw.ustring.sub(kana, -2, -2)]
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"
}

-- 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, PAGENAME)
	-- remove non-kanji characters
	local onlykanji = mw.ustring.gsub(PAGENAME, '[^一-鿌]', '')

	local number_of_kanji = mw.ustring.len(onlykanji)
	if number_of_kanji > 0 then
		for i=1,mw.ustring.len(onlykanji) do
			--table.insert(data.categories, ("japán terms spelled with %s kanji"):format(en_grades[m_ja.kanji_grade(mw.ustring.sub(onlykanji,i,i))]))
		end

		-- categorize by number of kanji
		if number_of_kanji == 1 then
			--table.insert(data.categories, "japán terms written with one Han script character")
		elseif en_numerals[number_of_kanji] then
			--table.insert(data.categories, ("japán terms written with %s Han script characters"):format(en_numerals[number_of_kanji]))
		end
	end
end

-- if this term is composed of only a single kanji, it does not have kanjitab/kanji reading tab
-- which generate "japán terms spelled with .. " categories, and since it is only one kanji
-- we know the kanji reading
-- (this category is for maintenance because many of these need attention)
local function singlekanji_term(data, PAGENAME)
	if mw.ustring.len(PAGENAME) == 1 and mw.ustring.match(PAGENAME, '[一-鿌]') then
		--table.insert(data.categories, "japán terms spelled with " .. PAGENAME)
		--table.insert(data.categories, "japán single-kanji terms")
	end
end

-- get a kana form to use, in order of preference: unnamed, hira, kana, pagename
local function find_kana(args, PAGENAME)
	for i,arg in ipairs(args) do
		if args[i] and find(args[i], kana_pattern_full) then return args[i] end
	end
	if find(PAGENAME, kana_pattern_full) then return PAGENAME end
	local hira = args["hira"] or ""; if hira ~= "" then return hira end
	local kata = args["kata"] or ""; if kata ~= "" then return kata end
	error("No kana detected in the unnamed parameters or |hira= and |kata= parameters. See template documentation for details.")
end

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

	local allkana, romajis = {}, {}
	local kana_rep, rom_rep = {}, {}
	local _insert_kana = detect_result and function(k)
		if k == '' then return end
		local rom = kana_to_romaji(remove_links(k), data, args)
		local key = remove_links(m_ja.remove_ruby_markup(k))
		if not kana_rep[key] then
			kana_rep[key] = true
			table.insert(allkana, k)
		end
		if not rom_rep[rom] then
			rom_rep[rom] = true
			table.insert(romajis, rom)
		end
	end or function(k)
		if k == '' then return end
		local rom = kana_to_romaji(remove_links(k), data, args)
		local key = mw.ustring.lower(rom:gsub('[ %-]', ''))
		if not kana_rep[key] then
			kana_rep[key] = true
			table.insert(allkana, k)
		end
		if not rom_rep[rom] then
			rom_rep[rom] = true
			table.insert(romajis, rom)
		end
	end

	if detect_result then
		_insert_kana((remove_links(head)))
		allkana[1] = head
	end

	for i,arg in ipairs(args) do
		if arg and arg ~= "" and find(arg, kana_pattern_full) then _insert_kana(arg) end
	end
	-- accept "hira" and "kata" but let Lua decide if they are really hiragana or katakana
	if args["hira"] and args["hira"] ~= "" and find(args["hira"], kana_pattern_full) then _insert_kana(args["hira"]) end
	if args["kata"] and args["kata"] ~= "" and find(args["kata"], kana_pattern_full) then _insert_kana(args["kata"]) end
	if args["rom"] then romajis[1] = args["rom"] end

	if #allkana == 0 then require("Module:debug").track('ja-headword/no kana form is available') end
	if #romajis == 0 then require("Module:debug").track('ja-headword/no romaji is available') end

	local suru_ending = data.pos_category == "suru igék" and '[[する]]' or ''
	for _, kana in ipairs(allkana) do
		-- add everything to inflections, except historical hiragana which is next
		-- local format_result = detect_result and allkana[i] or format_ruby(PAGENAME, allkana[i], data)
		local format_result, format_result_preserved --<ruby> form, []() form

		if detect_result then
			format_result = m_ja.remove_ruby_markup(kana)
			format_result_preserved = remove_links(format_result) .. suru_ending
			format_result = format_result .. suru_ending
		else
			local format_table = m_ja_ruby.parse_text(head, kana, {
				try = 'force',
				try_force_limit = 10000
			})
			format_result = m_ja_ruby.to_wiki(format_table, {
				break_link = true,
			}):gsub('<rt>(..-)</rt>', "<rt>[[" .. remove_links(m_ja.remove_ruby_markup(kana)) .."|%1]]</rt>") .. suru_ending
			format_result_preserved = remove_links(m_ja_ruby.to_markup(format_table)) .. suru_ending
		end

		table.insert(data.heads, format_result)
		data.heads_preserved[#data.heads] = format_result_preserved
	end

	suru_ending = data.pos_category == "suru igék" and ' suru' or ''
	for _, rom in ipairs(romajis) do
		table.insert(data.translits, rom .. suru_ending)
	end

	if not detect_result and #allkana > 1 then
		--table.insert(data.categories, "japán words with multiple readings")
	end
end

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

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

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

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

	local function replace_suffix(lemma_from, lemma_to, romaji_from, romaji_to)
		-- e.g. 持って来る, lemma = "[持](も)って来(く)る"
		-- lemma_from = "くる", lemma_to = {"き","きた"}
		local p_kr = katakana_range .. hiragana_range
		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 "[持](も)って来(く)る"
		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 - 1 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
			if len_lemma_from > 0 then
				table.insert(result_single, mw.ustring.sub(v, len_lemma_from, len_lemma_from))
				table.insert(result_single, mw.ustring.sub(lemma, key_pos[len_lemma_from][2]))
			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 form_kanji = remove_links(mw.ustring.gsub(v.lemma, "%[([^%[%]]+)%]%(([^%(%)]+)%)", "%1"))
			local form_term = mw.ustring.gsub(v.lemma, "%[([^%[%]]+)%]%(([^%(%)]+)%)", "<ruby>%1<rp>(</rp><rt>%2</rt><rp>)</rp></ruby>")
			if not form_term:find('%[%[') then form_term = '[[' .. form_kanji .. '#Japanese|' .. 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' 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'}) or
			require("Module:debug").track("ja-headword/godan conjugation failed")
		if inflected_forms then
			insert_form('szótő', inflected_forms[1])
			insert_form('múlt', inflected_forms[2])
			return
		end
	elseif inflection_type == '1s' then
		inflected_forms = replace_suffix('る', {'り', 'い', 'った'}, 'ru', {'ri', 'i', 'tta'}) --くださる
		if inflected_forms then
			insert_form('szótő', inflected_forms[1], inflected_forms[2])
			insert_form('múlt', inflected_forms[3])
			return
		end
		inflected_forms =
			replace_suffix('いく', {'いき', 'いった'}, 'iku', {'iki', 'itta'}) or --行く
			replace_suffix('う', {'い', 'うた'}, 'ou', {'oi', 'ōta'}) or --問う
			require("Module:debug").track("ja-headword/godan conjugation special failed")
		if inflected_forms then
			insert_form('szótő', inflected_forms[1])
			insert_form('múlt', inflected_forms[2])
			return
		end
	elseif inflection_type == '2' then
		inflected_forms =
			replace_suffix('る', {'', 'た'}, 'ru', {'', 'ta'}) or
			require("Module:debug").track("ja-headword/ichidan conjugation failed")
		if inflected_forms then
			insert_form('szótő', inflected_forms[1])
			insert_form('múlt', inflected_forms[2])
			return
		end
	elseif inflection_type == '3' or inflection_type == 'kuru' or inflection_type == 'suru' then
		inflected_forms =
			replace_suffix('くる', {'き', 'きた'}, 'kuru', {'ki', 'kita'}) or
			replace_suffix('する', {'し', 'した'}, 'suru', {'shi', 'shita'}) or
			replace_suffix('ずる', {'じ', 'じた'}, 'zuru', {'ji', 'jita'}) or
			require("Module:debug").track("ja-headword/kuru or suru conjugation failed")
		if inflected_forms then
			insert_form('szótő', inflected_forms[1])
			insert_form('múlt', inflected_forms[2])
			return
		end
	elseif inflection_type == 'i' or inflection_type == 'い' then
		inflected_forms =
			replace_suffix('い', {'く'}, 'i', {'ku'}) or
			require("Module:debug").track("ja-headword/-i inflection failed")
		if inflected_forms then
			insert_form('határozói', inflected_forms[1])
			return
		end
	elseif inflection_type == 'is' then
		inflected_forms =
			replace_suffix('いい', {'よく'}, 'ii', {'yoku'}) or
			require("Module:debug").track("ja-headword/slightly irregular -i inflection failed")
		if inflected_forms then
			insert_form('határozói', inflected_forms[1])
			return
		end
	elseif inflection_type == 'na' or inflection_type == 'な' then
		inflected_forms = replace_suffix('', {'[[な]]', '[[に]]'}, '', {' na', ' ni'})
		insert_form('adnominal', inflected_forms[1])
		insert_form('határozói', inflected_forms[2])
	elseif inflection_type == 'tari' or inflection_type == 'たり' then
		inflected_forms = replace_suffix('', {'[[たる]]', '[[と]]', '[[として]]'}, '', {' taru', ' to', ' toshite'})
		insert_form('adnominal', inflected_forms[1])
		insert_form('határozói', inflected_forms[2], inflected_forms[3])
	end
end

pos_functions["igék"] = function(args, data)
	-- transitivity
	local tr = args["tr"] or ""
	tr = aliases[tr] or tr
	if tr ~= "" then
		if tr == "tr" then table.insert(data.inflections, {label = "tárgyas"})
		--table.insert(data.categories, "japán transitive igék") 
		end
		if tr == "in" then table.insert(data.inflections, {label = "tárgyatlan"})
		--table.insert(data.categories, "japán intransitive igék") 
		end
		if tr == "both" then table.insert(data.inflections, {label = "tárgyas és tárgyatlan"})
		--table.insert(data.categories, "japán transitive igék")
		--table.insert(data.categories, "japán intransitive igék") 
		end
	else
		--table.insert(data.categories, "japán verbs without transitivity")
	end

	-- conjugation type
	local conjugation = args["type"] or ""
	conjugation = aliases[conjugation] or conjugation

	if conjugation == "1" or conjugation == "1s" then
		table.insert(data.inflections, {label = "godan ragozás"})
		--table.insert(data.categories, "japán type 1 igék")
		if ends_in_iru_eru(args[1] or PAGENAME) then
			--table.insert(data.categories, "japán type 1 verbs that end in -iru or -eru")
		end
	elseif conjugation == "2" then
		table.insert(data.inflections, {label = "ichidan ragozás"})
		--table.insert(data.categories, "japán type 2 igék")
	elseif conjugation == "suru" then
		table.insert(data.inflections, {label = "suru ragozás"})
		--table.insert(data.categories, "japán suru igék")
	elseif conjugation == "kuru" then
		table.insert(data.inflections, {label = "kuru ragozás"})
		--table.insert(data.categories, "japán kuru igék")
	elseif conjugation == "3" then
		-- hidden temporary maintenance category
		-- (Multi-kanji suru verbs should use ja-verb-suru.
		-- Single-kanji suru verbs use ja-verb with type=suru instead.)
		table.insert(data.inflections, {label = "3. típusú ragozás"})
		--table.insert(data.categories, "japán type 3 igék")
	elseif conjugation == "irr" then
		table.insert(data.inflections, {label = "renhagyó ragozás"})
		--table.insert(data.categories, "japán irregular igék")

		if mw.ustring.match(PAGENAME,'する$') then
			--table.insert(data.categories, "japán terms using ja-verb with type 3")
		end
	elseif conjugation == "yo" then
		table.insert(data.inflections, {label = "yodan ragozás"})
		--table.insert(data.categories, "japán yodan igék")
	elseif conjugation == "ni" then
		table.insert(data.inflections, {label = "nidan ragozás"})
		--table.insert(data.categories, "japán nidan igék")
	elseif conjugation == "kami ni" then
		table.insert(data.inflections, {label = "kami nidan ragozás"})
		--table.insert(data.categories, "japán kami nidan igék")
	elseif conjugation == "shimo ni" then
		table.insert(data.inflections, {label = "shimo nidan ragozás"})
		--table.insert(data.categories, "japán shimo nidan igék")
	end

	add_inflections(data, conjugation)

	-- >> maintenance category <<
	-- check if this ends in something other than acceptable kana in a modern verb (and isn't already categorised as yodan or nidan)
	if not mw.ustring.match(PAGENAME, '[うくぐすつぬぶむゆる]$') and conjugation ~= "yo" and conjugation ~= "ni" and conjugation ~= "kami ni" and conjugation ~= "shimo ni" then
		--table.insert(data.categories, "japán verbs without modern conjugations")
	end
end

pos_functions["szuffixumok"] = function(args, data)
	-- conjugation type
	local conjugation = args["type"] or ""
	conjugation = aliases[conjugation] or conjugation

	if conjugation == "1" or conjugation == "1s" then
		table.insert(data.inflections, {label = "godan ragozás"})
	elseif conjugation == "2" then
		table.insert(data.inflections, {label = "ichidan ragozás"})
	elseif conjugation == "3" then
		table.insert(data.inflections, {label = "irregular ragozás"})
	elseif conjugation == "yo" then
		table.insert(data.inflections, {label = "yodan ragozás"})
	elseif conjugation == "ni" then
		table.insert(data.inflections, {label = "nidan ragozás"})
	elseif conjugation == "kami ni" then
		table.insert(data.inflections, {label = "kami nidan ragozás"})
	elseif conjugation == "shimo ni" then
		table.insert(data.inflections, {label = "shimo nidan ragozás"})
	elseif conjugation == "rahen" then
		table.insert(data.inflections, {label = "r-irregular ragozás"})
	elseif conjugation == "sahen" then
		table.insert(data.inflections, {label = "s-irregular ragozás"})
	elseif conjugation == "kahen" then
		table.insert(data.inflections, {label = "k-irregular ragozás"})
	elseif conjugation == "nahen" then
		table.insert(data.inflections, {label = "n-irregular ragozás"})
	elseif conjugation == "i" or conjugation == "い" or conjugation == "is" then
		table.insert(data.inflections, {label = "-i ragozás"})
	elseif conjugation == "na" or conjugation == "な" then
		table.insert(data.inflections, {label = "-na ragozás"})
	elseif conjugation == "nari" or conjugation == "なり" then
		table.insert(data.inflections, {label = "-nari ragozás"})
	elseif conjugation == "tari" or conjugation == "たり" then
		table.insert(data.inflections, {label = "-tari ragozás"})
	elseif conjugation == "ka" or conjugation == "か" then
		table.insert(data.inflections, {label = "-ka ragozás"})
	end
	add_inflections(data, conjugation)
end

pos_functions["segédigék"] = function(args, data)
	data.pos_category = "igék"
	--table.insert(data.categories, "japán segédigék")
end

pos_functions["suru igék"] = function(args, data)
	--table.insert(data.categories, "japán suru igék")

	-- transitivity
	local tr = args["tr"] or ""
	tr = aliases[tr] or tr

	if tr == "tr" then
		table.insert(data.inflections, {label = "tárgyas"})
	elseif tr == "in" then
		table.insert(data.inflections, {label = "tárgyatlan"})
	elseif tr == "both" then
		table.insert(data.inflections, {label = "tárgyas és tárgyatlan"})
	elseif tr == "" then
		--table.insert(data.categories, "japán verbs without transitivity")
	end

	table.insert(data.inflections, {label = "suru ragozás"})
	add_inflections(data, '3')
end

pos_functions["melléknevek"] = function(args, data)
	-- categorize by inflection type
	local infl = args["infl"] or ""

	if infl == "i" or infl == "い" or infl == "is" then
		table.insert(data.inflections, {label = "-i ragozás"})
		--table.insert(data.categories, "japán い-i adjectives")
	elseif infl == "na" or infl == "な" then
		table.insert(data.inflections, {label = "-na ragozás"})
		--table.insert(data.categories, "japán な-na adjectives")
	elseif infl == "nari" or infl == "なり" then
		table.insert(data.inflections, {label = "-nari ragozás"})
		--table.insert(data.categories, "japán なり-nari adjectives")
	elseif infl == "tari" or infl == "たり" then
		table.insert(data.inflections, {label = "-tari ragozás"})
		--table.insert(data.categories, "japán たり-tari adjectives")
	elseif infl == "ka" or infl == "か" then
		table.insert(data.inflections, {label = "-ka ragozás"})
		--table.insert(data.categories, "japán か-ka adjectives")
	end
	add_inflections(data, infl)
end

pos_functions["főnevek"] = 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 = "számláló", counter})
	end
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local args = frame:getParent().args
	PAGENAME = args["pagename"] or mw.title.getCurrentTitle().text
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")

	local head = args["head"] or PAGENAME

	if args["decl"] and (not args["infl"] or args["infl"] == "") then
		args["infl"] = args["decl"]
	end

	local data = {
		lang = lang,
		sc = sc,
		pos_category = poscat,
		categories = {},
		translits = {},
		heads = {},
		inflections = {},
		heads_preserved = {}
	}
	local katakana_category = {}

	local kana = find_kana(args, PAGENAME)

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

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

	-- romaji links
	if data.pos_category == "suru igék" then
		for i, v in ipairs(data.translits) do
			v = v:sub(1, -6)
			data.translits[i] = '[[' .. v .. '#Japanese|' .. v .. ']] suru'
		end
	else
		for i, v in ipairs(data.translits) do
			data.translits[i] = '[[' .. v .. '#Japanese|' .. v .. ']]'
		end
	end

	-- the presence of kyūjitai param indicates that this is shinjitai kanji entry and vice versa
	local kyu = args["kyu"] or ""
	local shin = args["shin"] or ""

	if kyu == "" then
		kyu = nil
	else
		table.insert(data.inflections, {label = "[[shinjitai]] kanji"})
		if data.pos_category == "suru igék" then
			table.insert(data.inflections, {label = "[[kyūjitai]] kanji", "[[" .. kyu .. "]][[する]]"})
		else
			table.insert(data.inflections, {label = "[[kyūjitai]] kanji", kyu})
		end
	end

	if shin ~= "" then
		table.insert(data.inflections, {label = "[[kyūjitai]] kanji"})
		if data.pos_category == "suru igék" then
			table.insert(data.inflections, {label = "[[shinjitai]] kanji", "[[" .. shin .. "]][[する]]"})
		else
			table.insert(data.inflections, {label = "[[shinjitai]] kanji", shin})
		end
	end

	local hhira = args["hhira"] or ""
	if hhira ~= "" then
		if data.pos_category == "suru igék" then
			table.insert(data.inflections, {label = "régi hiragana", "[[" .. hhira .. "]][[する]]"})
		else
			table.insert(data.inflections, {label = "régi hiragana", hhira})
		end

		if string.match(hhira, "ゐ") ~= nil then
			--table.insert(data.categories, "japán terms historically spelled with ゐ")
		end
		if string.match(hhira, "ゑ") ~= nil then
			--table.insert(data.categories, "japán terms historically spelled with ゑ")
		end
		if string.match(hhira, "を") ~= nil then
			--table.insert(data.categories, "japán terms historically spelled with を")
		end
	end

	local hkata = args["hkata"] or ""
	if hkata ~= "" then
		if data.pos_category == "suru igék" then
			table.insert(data.inflections, {label = "régi katakana", "[[" .. hkata .. "]][[する]]"})
		else
			table.insert(data.inflections, {label = "régi katakana", hkata})
		end
	end

	if data.pos_category == "suru igék" then
		data.pos_category = "igék"
	end

	-- categorize by joyo kanji and number of kanji
	categorize_by_kanji(data, PAGENAME)
	-- generate "japán terms spelled with ... read as ..." for single-kanji terms
	singlekanji_term(data, PAGENAME)
	-- add categories for terms with iteration marks (which are not kanji and hence are not categorized by ja-kanjitab)
	extra_categorization(data, PAGENAME, katakana_category)

	if find(PAGENAME, "[ァ-ヺヽヾ]") and find(PAGENAME, "[ぁ-ゖゞゝ]") and data.pos_category ~= "közmondások" and data.pos_category ~= "kifejezések" then
		--table.insert(data.categories, "japán terms spelled with mixed kana")
	end

	-- will only use sortkey if sortkey is different from PAGENAME
	-- when katakana in PAGENAME is converted to hiragana
	local sort_key = m_ja.jsort(kana)

	if sort_key == m_ja.kata_to_hira(PAGENAME) then
		return
			require("Module:headword").full_headword(data) ..
			require("Module:utilities").format_categories(katakana_category, lang)
	else
		-- convert sortkey to katakana version for katakana terms category (should sort by katakana)
		data.sort_key = sort_key
		return
			require("Module:headword").full_headword(data) ..
			require("Module:utilities").format_categories(katakana_category, lang, m_ja.hira_to_kata(sort_key))
	end
end

return export