跳至內容

模組:Jpan-headword

Ùi Wiktionary

可在模組:Jpan-headword/doc建立此模組的說明文件

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

local export = {}
local pos_functions = {}

local data_range = mw.loadData'Module:ja/data/range'
local range_kana = data_range.hiragana .. data_range.katakana .. data_range.hentaigana

local function remove_links(text)
	return (text:gsub("%[%[[^|%]]-|", ""):gsub("%[%[", ""):gsub("%]%]", ""))
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_kana .. ']+', 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_kana .. ']+', 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

local zh_numerals = {
	"一", "兩", "三", "四", "五",
	"六", "七", "八", "九", "十",
	"十一", "十二", "十三", "十四", "十五"
}

local zh_grades = {
	"一年級", "二年級", "三年級",
	"四年級", "五年級", "六年級",
	"中學", "人名用", "表外"
}

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

local function kana_to_romaji_with_pos_format(kana, data, args)
	local rom = remove_links(require'Module:ja-translit'.kana_to_romaji(kana, {language_name = data.lang_code}))
	
	-- make adjustments for -u verbs and -i adjectives
	if args['infl'] == '1' or args['infl'] == '1s' or args['infl'] == 'godan' then
		rom = rom:gsub('ō$', 'ou'):gsub('ū$', 'uu')
	elseif args['infl'] == 'i' or args['infl'] == 'is' or args['infl'] == 'い' then
		rom = rom:gsub('ī$', 'ii')
	end
	
	-- hyphens for prefixes, suffixes, and counters (classifiers)
	if data.headword.pos_category == "prefixes" then
		rom = rom:gsub('%-?$', '-')
	elseif data.headword.pos_category == "suffixes" or data.headword.pos_category == "counters" or data.headword.pos_category == "classifiers" then
		rom = rom:gsub('^%-?', '-')
	elseif data.headword.pos_category == "proper nouns" or data.headword.pos_category == "專有名詞"  then -- automatic caps for proper nouns, if not already specified
		if not kana:match'%^' then
			rom = mw.ustring.gsub(rom, '%f[^%s%c%p]%l', mw.ustring.upper)
		end
	end
	
	return rom
end

local function historical_kana(data, hist_kana, modern_kana)
	if hist_kana:match'ゐ' then
		table.insert(data.headword.categories, "歷史假名遣包含「ゐ」的" .. data.lang_name .. "詞")
	end
	if hist_kana:match'ゑ' then
		table.insert(data.headword.categories, "歷史假名遣包含「ゑ」的" .. data.lang_name .. "詞")
	end
	if hist_kana:match'を' and not (modern_kana and modern_kana:match'を') then
		table.insert(data.headword.categories, "歷史假名遣包含「を」的" .. data.lang_name .. "詞")
	end
	if hist_kana:match'ぢ' and not (modern_kana and modern_kana:match'ぢ') then
		table.insert(data.headword.categories, "歷史假名遣包含「ぢ」的" .. data.lang_name .. "詞")
	end
	if hist_kana:match'づ' and not (modern_kana and modern_kana:match'づ') then
		table.insert(data.headword.categories, "歷史假名遣包含「づ」的" .. data.lang_name .. "詞")
	end
	table.insert(data.info_hist, require('Module:ja-link').link({
		lang = data.headword.lang,
		lemma = hist_kana,
	}, {
		hist = true,
		face = 'head',
		disableSelfLink = true,
	}))
end

local function detect_pagename_kana(data)
	if mw.ustring.match(data.pagename, '^[%p%s%c' .. data_range.hiragana .. ']+$') then
		data.pagename_kana = 'hira'
	elseif mw.ustring.match(data.pagename, '^[%p%s%c' .. data_range.katakana .. ']+$') then
		data.pagename_kana = 'kata'
	elseif mw.ustring.match(data.pagename, '^[%p%s%c' .. range_kana .. ']+$') then
		data.pagename_kana = 'both'
	end
end

-- go through args and build inflections by finding whatever kanas were given to us
local function format_headword(args, data, head)
	detect_pagename_kana(data)
	
	first_para = args[1][1]
	if first_para then
		first_para_script = m_ja.script(first_para)
		if not (first_para_script:find("Kana") or first_para_script:find("Hira")) then
		-- filter out POS designations
			table.remove(args[1], 1)
		end
	end

	local suru_ending = data.headword.pos_category == "suru verbs" and '[[する]]' or ''
	local rom_suru_ending = data.headword.pos_category == "suru verbs" and ' suru' or ''
			
	if args["kyu"] then
		if data.pos_category == "suru verbs" then
			table.insert(data.headword.inflections, {label = "舊字體", "[[" .. args["kyu"] .. "]][[する]]"})
		else
			table.insert(data.headword.inflections, {label = "舊字體", args["kyu"]})
		end
	end
	if args["shin"] then
		table.insert(data.headword.inflections, {label = "舊字體"})
		if data.pos_category == "suru verbs" then
			table.insert(data.headword.inflections, {label = "新字體", "[[" .. args["shin"] .. "]][[する]]"})
		else
			table.insert(data.headword.inflections, {label = "新字體", args["shin"]})
		end
	end
	
	if data.pagename_kana then -- pure-kana-title entry
		if #args.head > 0 or args.head.default then
			table.insert(data.headword.categories, data.lang_name .. " terms with redundant head parameter")
		end
		
		-- {{ja-xxx}} vs {{ja-xxx|こ.うし}} vs {{ja-xxx|コウシ}} in [[こうし]]
		if not args[1][1] then
			args[1][1] = data.pagename
		elseif remove_links(args[1][1]:gsub("[%^%-%. %%]", "")) ~= data.pagename then
			table.insert(args[1], 1, data.pagename)
		end
		
		for i, k in ipairs(args[1]) do
			table.insert(data.headword.heads, {
				term = k:gsub("[%^%-%. %%]", "") .. suru_ending,
				tr = '-',
				q = args.label[i],
			})
		end
		
		local rom = args.rom.default or kana_to_romaji_with_pos_format(args[1][1], data, args)
		data.headword.heads[1].tr = '[[' .. rom .. '#' .. data.lang_name .. '|' .. rom .. ']]' .. rom_suru_ending
		
		data.kanas[1] = data.pagename
		
		data.inflection_base.form = remove_links(args[1][1]:gsub("[%^%-%. %%]", "")) .. suru_ending
		data.inflection_base.romaji = rom .. rom_suru_ending

		
		if args.hist[1] then
			historical_kana(data, args.hist[1], args[1][1])
		end
	else -- non-pure-kana-title entry
		if #args[1] == 0 then error('Kana form is required') end
		if args.head.default == data.pagename then
			table.insert(data.headword.categories, data.lang_name .. " terms with redundant head parameter")
		end
		
		local rom_repetition_auto = {}
		local rom_repetition_final = {}
		for i, k in ipairs(args[1]) do
			local rom_auto = kana_to_romaji_with_pos_format(k, data, args)
			if rom_repetition_auto[rom_auto] then
				--e.g. [[海豚]] {{ja-noun|いるか|イルカ}}
				--leave them there? use bot to remove?
				require'Module:debug'.track'Jpan-headword/redundant kana'
			else
				rom_repetition_auto[rom_auto] = true
				local head = args.head[i] or args.head.default or data.pagename
				if args.head[i] == data.pagename then
					table.insert(data.headword.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 k:match'%%' == nil then
					head_for_ruby, kana_for_ruby = assign_kana_to_kanji(head, k, data.pagename, data.lang_code .. '%-kanjitab')
				else
					head_for_ruby, kana_for_ruby = head, k
				end
				local format_table = m_ja_ruby.parse_text(head_for_ruby, kana_for_ruby, {
					try = 'force',
					try_force_limit = 10000
				})
				local kana_bare = remove_links(k:gsub("[%^%-%. %%]", ""))
				local rom = args.rom[i] or args.rom.default or rom_auto
				
				table.insert(data.headword.heads, {
					term = m_ja_ruby.to_wiki(format_table, {
						break_link = true,
					}):gsub('<rt>(..-)</rt>', "<rt>[[" .. kana_bare .."|%1]]</rt>") .. suru_ending,
					tr = rom_repetition_final[rom] and '-' or '[[' .. rom .. '#' .. data.lang_name .. '|' .. rom .. ']]' .. rom_suru_ending,
					q = args.label[i],
				})
				rom_repetition_final[rom] = true
				table.insert(data.kanas, kana_bare)
				
				if args.hist[i] then
					historical_kana(data, args.hist[i], k)
				end
				
				if not data.inflection_base.form then
					data.inflection_base.form = remove_links(m_ja_ruby.to_markup(format_table)) .. suru_ending
					data.inflection_base.romaji = rom .. rom_suru_ending
				end
			end
		end
		
		if #data.kanas > 1 then
			table.insert(data.headword.categories, "有多個讀音的" .. data.lang_name .. "詞")
		else
			local lang_code = data.lang_code
			local lang_name = data.lang_name
			local content = mw.title.getCurrentTitle():getContent()
			local loc1, loc2 = content:find("%f[^%z%s]==%s*" .. lang_name:gsub("%-", "%%%-") .. "%s*==()")
			local loc2 = content:find("%f[^%z%s]==[^\n=]+==", loc2)
			if loc1 then
				content = content:sub(loc1, loc2)
				local reading, first_reading
				for template, args in require("Module:templateparser").findTemplates(content) do
					if (
						template == lang_code .. "-head" or
						template == lang_code .. "-pos"
					) and args[2] then
						reading = remove_links(args[2]):gsub("[ %-%.^%%]", "")
					elseif (
						template == lang_code .. "-noun" or
						template == lang_code .. "-verb" or
						template == lang_code .. "-adj" or
						template == lang_code .. "-phrase" or
						template == lang_code .. "-verb form" or
						template == lang_code .. "-verb-suru"
					) and args[1] then
						reading = remove_links(args[1]):gsub("[ %-%.^%%]", "")
					elseif template == lang_code .. "-see" and args[1] then
						if mw.ustring.find(args[1], "[^" .. data_range.hiragana .. data_range.katakana .. data_range.hentaigana .. "]") then
							-- TODO: check linked page
						else
							reading = remove_links(args[1]):gsub("[ %-%.^%%]", "")
						end
					end
					reading = reading and require'Module:ja-translit'.kana_to_romaji(reading, {language_name = lang_code})
					first_reading = first_reading or reading
					if reading ~= first_reading then
						table.insert(data.headword.categories, "有多個讀音的" .. lang_name .. "詞")
						break
					end
				end
			end
		end
	end
end

local function add_transitivity(data, tr)
	tr = aliases[tr] or tr
	if tr == "tr" then
		table.insert(data.info_mid, '他動詞')
		table.insert(data.headword.categories, data.lang_name .. '他動詞')
	elseif tr == "in" then
		table.insert(data.info_mid, '自動詞')
		table.insert(data.headword.categories, data.lang_name .. "自動詞")
	elseif tr == "both" then
		table.insert(data.info_mid, '自、他動詞')
		table.insert(data.headword.categories, data.lang_name .. "自動詞")
		table.insert(data.headword.categories, data.lang_name .. "他動詞")
	else
		table.insert(data.headword.categories, "未指定是否及物的" .. data.lang_name .. "動詞")
	end
end

local function add_inflections(data, inflection_type, cat_suffix)
	local lemma = data.inflection_base.form
	local romaji = data.inflection_base.romaji
	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 result = {}
		
		local pattern_from, n_from = lemma_from:gsub('.[\128-\191]*', function(c)
			return '[' .. c .. m_ja.hira_to_kata(c) .. ']([^' .. range_kana .. ']*)'
		end)
		pattern_from = pattern_from .. '$'
		-- "[くク]([^kana range]*)[るル]([^kana range]*)$"
		
		for i_lemma_to, s_lemma_to in ipairs(lemma_to) do
			local n_to = 0
			local pattern_to = s_lemma_to:gsub('.[\128-\191]*', function(c)
				if n_to < n_from then
					n_to = n_to + 1
					return c  .. '%' .. n_to
				else return c end
			end)
			for i = n_to + 1, n_from do
				pattern_to = pattern_to .. '%' .. i
			end
			-- "き%1%2", "き%1た%2"
			
			local lemma_inflected, success = mw.ustring.gsub(lemma, pattern_from, pattern_to)
			if success == 0 then return nil end
			local romaji_inflected, success = romaji:gsub(romaji_from .. '$', romaji_to[i_lemma_to])
			if success == 0 then return nil end
			
			table.insert(result, { lemma = lemma_inflected, romaji = romaji_inflected })
		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.headword.inflections, labeled_forms)
	end

	local inflected_forms
	if data.lang_code == 'ja' then
		if inflection_type == '1' or inflection_type == '1s' then
			table.insert(data.info_mid, '<abbr title="五段活用(第一類)">五段</abbr>')
			if cat_suffix then
				table.insert(data.headword.categories, data.lang_name .. "五段" .. cat_suffix)
				if cat_suffix == 'verbs' and mw.ustring.match(data.inflection_base.romaji, '[ieIEīēĪĒ]ru$') then
					table.insert(data.headword.categories, "詞尾是「いる」或「える」的" .. data.lang_name .. "五段動詞")
				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('連用形', inflected_forms[1])
					insert_form('過去式', inflected_forms[2])
				else
					require'Module:debug'.track'Jpan-headword/inflection failed/ja'
				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('連用形', inflected_forms[1], inflected_forms[3])
					insert_form('過去式', inflected_forms[2])
				else
					require'Module:debug'.track'Jpan-headword/inflection failed/ja'
				end
			end
		elseif inflection_type == '2' then
			table.insert(data.info_mid, '<abbr title="一段活用(第二類)">一段</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "一段" .. cat_suffix) end
			inflected_forms = replace_suffix('る', {'', 'た'}, 'ru', {'', 'ta'})
			if inflected_forms then
				insert_form('連用形', inflected_forms[1])
				insert_form('過去式', inflected_forms[2])
			else
				require'Module:debug'.track'Jpan-headword/inflection failed/ja'
			end
		elseif inflection_type == 'suru' then
			table.insert(data.info_mid, '<abbr title="サ行活用">サ行</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "サ行活用" .. cat_suffix) end
			inflected_forms =
				replace_suffix('する', {'し', 'した'}, 'suru', {'shi', 'shita'}) or
				replace_suffix('ずる', {'じ', 'じた'}, 'zuru', {'ji', 'jita'})
			if inflected_forms then
				insert_form('連用形', inflected_forms[1])
				insert_form('過去式', inflected_forms[2])
			else
				require'Module:debug'.track'Jpan-headword/inflection failed/ja'
			end
		elseif inflection_type == 'kuru' then
			table.insert(data.info_mid, '<abbr title="カ行活用(第三組)">カ行</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "カ行活用" .. cat_suffix) end
			inflected_forms = replace_suffix('くる', {'き', 'きた'}, 'kuru', {'ki', 'kita'})
			if inflected_forms then
				insert_form('連用形', inflected_forms[1])
				insert_form('過去式', inflected_forms[2])
			else
				require'Module:debug'.track'Jpan-headword/inflection failed/ja'
			end
		elseif inflection_type == 'i' or inflection_type == 'い' then
			table.insert(data.info_mid, '<abbr title="イ形容詞活用">-i</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "イ" .. cat_suffix) end
			inflected_forms = replace_suffix('い', {'く'}, 'i', {'ku'})
			if inflected_forms then
				insert_form('連用形', inflected_forms[1])
			else
				require'Module:debug'.track'Jpan-headword/inflection failed/ja'
			end
		elseif inflection_type == 'is' then
			table.insert(data.info_mid, '<abbr title="イ形容詞活用">-i</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "イ" .. cat_suffix) end
			inflected_forms = replace_suffix('いい', {'よく'}, 'ii', {'yoku'})
			if inflected_forms then
				insert_form('連用形', inflected_forms[1])
			else
				require'Module:debug'.track'Jpan-headword/inflection failed/ja'
			end
		elseif inflection_type == 'na' or inflection_type == 'な' then
			table.insert(data.info_mid, '<abbr title="ナ形容詞活用">-na</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "ナ" .. cat_suffix) end
			inflected_forms = replace_suffix('', {'[[な]]', '[[に]]'}, '', {' na', ' ni'})
			insert_form('連體形', inflected_forms[1])
			insert_form('連用形', inflected_forms[2])

		elseif inflection_type == "yo" then
			table.insert(data.info_mid, '<abbr title="四段活用(古典)"><sup><small>†</small></sup>四段</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "四段" .. cat_suffix) end
		elseif inflection_type == "kami ni" then
			table.insert(data.info_mid, '<abbr title="上二段活用(古典)"><sup><small>†</small></sup>二段</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "上二段" .. cat_suffix) end
		elseif inflection_type == "shimo ni" then
			table.insert(data.info_mid, '<abbr title="下二段活用(古典)"><sup><small>†</small></sup>二段</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "下二段" .. cat_suffix) end
		elseif inflection_type == "rahen" then
			table.insert(data.info_mid, '<abbr title="ラ行活用(古典)"><sup><small>†</small></sup>-ri</abbr>')
		elseif inflection_type == "sahen" then
			table.insert(data.info_mid, '<abbr title="サ行活用(古典)"><sup><small>†</small></sup>-se</abbr>')
		elseif inflection_type == "kahen" then
			table.insert(data.info_mid, '<abbr title="カ行活用(古典)"><sup><small>†</small></sup>-ko</abbr>')
		elseif inflection_type == "nahen" then
			table.insert(data.info_mid, '<abbr title="カ行活用(古典)"><sup><small>†</small></sup>-n</abbr>')
		elseif inflection_type == "nari" or inflection_type == "なり" then
			table.insert(data.info_mid, '<abbr title="ナリ活用(古典)"><sup><small>†</small></sup>-nari</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "ナリ" .. cat_suffix) end
		elseif inflection_type == 'tari' or inflection_type == 'たり' then
			table.insert(data.info_mid, '<abbr title="タリ活用(古典)"><sup><small>†</small></sup>-tari</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "タリ" .. cat_suffix) end
			inflected_forms = replace_suffix('', {'[[とした]]', '[[たる]]', '[[と]]', '[[として]]'}, '', {' to shita', ' taru', ' to', ' to shite'})
			insert_form('連體形', inflected_forms[1], inflected_forms[2])
			insert_form('連用形', inflected_forms[3], inflected_forms[4])
		elseif inflection_type == "ku" or inflection_type == "く" then
			table.insert(data.info_mid, '<abbr title="ク活用(古典)"><sup><small>†</small></sup>-ku</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "ク" .. cat_suffix) end
		elseif inflection_type == "shiku" or inflection_type == "しく" then
			table.insert(data.info_mid, '<abbr title="シク活用(古典)"><sup><small>†</small></sup>-shiku</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "シク" .. cat_suffix) end
		elseif inflection_type == "ka" or inflection_type == "か" then
			table.insert(data.info_mid, '<abbr title="カ活用(方言)"><sup><small>†</small></sup>-ka</abbr>')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "カ" .. cat_suffix) end

		elseif inflection_type == 'irr' then
			table.insert(data.info_mid, '不規則')
			if cat_suffix then table.insert(data.headword.categories, data.lang_name .. "不規則" .. cat_suffix) end
		elseif inflection_type == '-' or inflection_type == 'un' then
			table.insert(data.info_mid, '無活用')
		end
	--elseif data.lang_code == 'ryu' then ...
	end
end

local function add_categories(data)
	local lang_name = data.lang_name
	local tc = data.headword.categories
	
	-- adds category [langname] terms spelled with jōyō kanji or [langname] terms spelled with non-jōyō kanji
	-- (if it contains any kanji)
	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(tc, ("有%s漢字的" .. lang_name .. "詞"):format(zh_grades[m_ja.kanji_grade(c)]))
	end

	-- categorize by number of kanji
	if number_of_kanji == 1 then
		table.insert(tc, "有一個漢字的" .. lang_name .. "詞")
		-- single-kanji terms
		if mw.ustring.len(data.pagename) == 1 then
			table.insert(tc, "寫作「" .. data.pagename .. "」的" .. lang_name .. "詞" )
			table.insert(tc, lang_name .. "單漢字詞")
		end
	elseif zh_numerals[number_of_kanji] then
		table.insert(tc, ("有%s個漢字的" .. lang_name .. "詞"):format(zh_numerals[number_of_kanji]))
	end
	
	-- categorize by the script of the pagename or specific characters contained in it
	-- if data.pagename is hiragana or katakana 
	if data.pagename_kana == 'hira' then table.insert(tc, lang_name .. "平假名") end
	if data.pagename_kana == 'kata' then table.insert(data.katakana_category, lang_name .. "片假名") end
	local p, n = mw.ustring.gsub(data.pagename, '[' .. range_kana .. data_range.kanji .. data_range.ideograph .. data_range.kana_graph .. data_range.punctuation .. ']+', '')
	if p ~= '' and n > 0 then table.insert(tc, "用多種文字書寫的" .. lang_name.. "詞") end

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

	if mw.ustring.match(data.pagename, "[" .. data_range.hiragana .. "]") then
		if mw.ustring.match(data.pagename, "[" .. data_range.katakana .. "]") then
			if data.headword.pos_category ~= "諺語" and data.headword.pos_category ~= "短語" then
				table.insert(tc, "假名混用的" .. lang_name .. "詞")
			end
		end
	end
end

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

pos_functions["後綴"] = function(args, data)
	add_inflections(data, args["infl"])
end
	
pos_functions["助動詞"] = function(args, data)
	table.insert(data.headword.categories, data.lang_name .. "助動詞")
	add_inflections(data, args["infl"])
	data.headword.pos_category = "動詞"
end

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

pos_functions["形容詞"] = function(args, data)
	add_inflections(data, args["infl"], '形容詞')
end

pos_functions["形容词"] = pos_functions["形容詞"]

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

	if counter == "-" then
		table.insert(data.headword.inflections, {label = "不可數"})
	elseif counter ~= "" then
		table.insert(data.headword.inflections, {label = "量詞", counter})
	end
end

--[==[
Generate categories by pagename, also optionally by POS
Also for use in soft redirect pages ([[Module:ja-see]]).
Sortkey is not provided.
data = {
	pagename = ..., -- (required)
	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)
	data.lang_name = data.lang:getCanonicalName()
	detect_pagename_kana(data)
	
	if data.pos then
		local pos = data.pos
		table.insert(data.categories, data.lang_name .. pos)
		table.insert(data.categories, data.lang_name .. require'Module:headword'.pos_lemma_or_nonlemma(pos, true))
	end
	
	data.headword = { categories = data.categories }
	add_categories(data)
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[2] or frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	
	local params = {
		[1] = {list = true},
		['rom'] = {list = true, allow_holes = true, separate_no_index = true},
		['head'] = {list = true, allow_holes = true, separate_no_index = true},
		['label'] = {list = true, allow_holes = true},
		['hist'] = {list = true}, ['hhira'] = {alias_of = 'hist'}, ['hkata'] = {alias_of = 'hist'},
		['tr'] = {},
		['infl'] = {}, ['type'] = {alias_of = 'infl'}, ['decl'] = {alias_of = 'infl'},
		['count'] = {},
		['sort'] = {},
		['pagename'] = {},
		['kyu'] = {}, ['shin'] = {},
	}
	-- For backwards compatibility with uses of {{ja-syllable}} with the script parameter.
	if poscat == "syllables" then
		params["sc"] = {}
	end
	
	local args = require('Module:parameters').process(frame:getParent().args, params)

	local data = {
		headword = {
			lang = nil,
			pos_category = poscat,
			categories = {},
			heads = {},
			no_redundant_head_cat = true,
			inflections = {},
			genders = {'m'}, -- placeholder
			nogendercat = true,
			sort_key = nil,
		},
		--custom info
		pagename = args['pagename'] or mw.title.getCurrentTitle().subpageText,
		pagename_kana = nil, -- "hira" "kata" "both", nil
		lang_code = frame.args[1],
		lang_name = nil, -- "Japanese", "Okinawan" ...
		katakana_category = {},
		info_mid = {}, -- "godan", "intransitive" ...
		info_hist = {}, -- historical kana
		inflection_base = {}, -- base of inflections
		kanas = {}, -- kana id
	}
	data.headword.lang = require("Module:languages").getByCode(data.lang_code)
	data.lang_name = data.headword.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

	-- categories
	add_categories(data)
	data.headword.sort_key = require'Module:Jpan-sortkey'.sortkey_from_string(args.sort or data.kanas[1], data.lang_code)
	
	-- output
	local i_kanas = 0
	return require('Module:utilities').format_categories(data.katakana_category, data.headword.lang, m_ja.hira_to_kata(data.headword.sort_key)) .. require('Module:headword').full_headword(data.headword):gsub('<span class="gender">.-</span>', function()
		return (#data.info_hist > 0 and '<sup>←' .. table.concat(data.info_hist, ' 或 ') .. '<sup>[[w:歷史假名遣|?]]</sup></sup>' or '') .. ('<i>' .. table.concat(data.info_mid, '&nbsp;') .. '</i>')
	end):gsub('<strong .->.-</strong>', function(m0)
		i_kanas = i_kanas + 1
		if data.kanas[i_kanas] then
			return  '<span id="' .. data.lang_name .. ':_' .. data.kanas[i_kanas] .. '" class="senseid">' .. m0 .. '</span>'
		end
	end)
end

return export