Module:ja-kanji-readings

Hello, you have come here looking for the meaning of the word Module:ja-kanji-readings. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:ja-kanji-readings, but we will also tell you about its etymology, its characteristics and you will know how to say Module:ja-kanji-readings in singular and plural. Everything you need to know about the word Module:ja-kanji-readings you have here. The definition of the word Module:ja-kanji-readings will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:ja-kanji-readings, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.

This module contains functions used by {{ja-readings}} to format the lists of readings in entries on kanji and to create the content for some of the Japanese kanji reading categories.


local export = {}

local m_ja = require("Module:ja")
local m_str_utils = require("Module:string utilities")

local concat = table.concat
local find = m_str_utils.find
local get_script = require("Module:scripts").getByCode
local hira_to_kata = m_ja.hira_to_kata
local insert = table.insert
local kana_to_romaji = require("Module:Hrkt-translit").tr
local kata_to_hira = m_ja.kata_to_hira
local gmatch = m_str_utils.gmatch
local match = m_str_utils.match
local split = m_str_utils.split

local Jpan = get_script("Jpan")
-- local katakana_script = get_script("Kana")
local Hira = get_script("Hira")

local PAGENAME = mw.loadData("Module:headword/data").pagename
local NAMESPACE = mw.title.getCurrentTitle().nsText

-- Only used by commented-out code.
-- local data = mw.loadData("Module:ja/data")

local CONCAT_SEP = ', '
	
local labels = {
	{
		text = "Go-on",
		text2 = "goon",
		classification = "on",
	},
	{
		text = "Kan-on",
		text2 = "kan'on",
		classification = "on",
	},
	{
		text = "Sō-on",
		text2 = "sōon",
		classification = "on",
	},
	{
		text = "Tō-on",
		text2 = "tōon",
		classification = "on",
	},
	{
		text = "Kan’yō-on",
		text2 = "kan'yōon",
		classification = "on",
	},
	{
		entry = "on'yomi",
		text = "On",
		text2 = "on",
		classification = "on",
		unclassified = " (unclassified)",
	},
	{
		entry = "kun'yomi",
		text = "Kun",
		text2 = "kun",
		classification = "kun",
	},
	{
		text = "Nanori",
		text2 = "nanori",
		classification = "nanori",
	},
}

local function track(code)
	require("Module:debug").track("ja-kanji-readings/" .. code)
end

local function plain_link(data)
	data.term = data.term:gsub('', '') -- 「かな-し.い」→「かなしい」, 「も-しく は」→「もしくは」
	data.tr = data.tr and data.tr:gsub('', '') or '-'
	data.sc = match(data.term:gsub('', ''), '') and Jpan or Hira
	data.pos = data.pos ~= '' and data.pos or nil
	return require("Module:links").full_link(data, "term") --"term" makes italic
end

--[=[
		Copied from ] on 2017/6/14.
		Replaces the code in Template:ja-readings which accepted kanji readings,
		and displayed them in a consistent format.
		Substantial change in function was introduced in https://en.wiktionary.orghttps://en.wiktionary.org/w/index.php?diff=46057625
]=]
function export.show(frame)
	local args = require("Module:parameters").process(frame:getParent().args, {
		 = {},
		 = {},
		 = {},
		 = {},
		 = {},
		 = {},
		 = {},
		 = {},
		 = {},
	})
	
	local lang_code = frame.args or 'ja'
	local lang = require'Module:languages'.getByCode(lang_code)
	local lang_name = lang:getCanonicalName()
	
	if args.pagename and NAMESPACE == "" then
		error("The pagename parameter should not be used in entries, as it is only for testing.")
	end
	local pagename = args.pagename or PAGENAME

	local yomi_data = mw.loadData("Module:ja/data/jouyou-yomi").yomi

	-- this holds the finished product composed of wikilinks to be displayed
	-- in the Readings section under the Kanji section
	local links, categories = {}, {}

	local is_old_format = false
	
	-- We need a separate kanji sortkey module.
	local sortkey = (require("Module:Hani-sortkey").makeSortKey(pagename, lang_code, "Jpan"))
	local function add_reading_category(reading, subtype, period)
		reading = kata_to_hira(reading:gsub("+", ""):gsub("%-$", ""):gsub("%-", "・"))
		if subtype then
			return insert(categories, '[[Category:' .. lang_name .. ' kanji with ' ..
				(period or '') .. ' ' .. subtype .. ' reading ' .. reading ..
				'|' .. sortkey .. ']]')
		else
			return insert(categories, '[[Category:' .. lang_name .. ' kanji read as ' ..
				reading .. '|' .. sortkey .. ']]')
		end
	end
	
	local unclassified_on = {}
	local classified_on = {}
	local kun = {}
			
	local kana = ""
	
	for _, label in ipairs(labels) do
		local readings = args
		if readings then
			local unclassified = ""

			if label.unclassified then
				if not (args.goon or args.kanon or args.soon or args.toon or args.kanyoon) then
					unclassified = label.unclassified
				end
			end
			
			if find(readings, '%[%[' .. kana) then
				is_old_format = true

				if label.classification == 'on' then
					for reading in gmatch(readings, kana .. '+') do
						add_reading_category(reading)
					end
				end

				readings = readings:gsub("%|]+)%]%]", function(entry)
					if find(entry, "^+$") then
						return plain_link{
							lang = lang,
							term = entry,
						}
					else
						return "]"
					end
				end)
			else
				readings = split(readings, "%s*%s*")

				for i, reading in ipairs(readings) do
					local is_jouyou = false

					local pos, pos_hist, pos_oldest = { }, { ']' }, { 'ancient' }

					-- check for formatting indicating presence of historical kana spelling
					local reading_mod, reading_hist, reading_oldest, reading_surplus = reading:match'^(.-)%f<?(.-)%f<?(.-)%f<?(.*)$'
					
					if reading_surplus ~= '' then
						error("The reading " .. reading .. " contains too many historical readings. The maximum is 3: modern, historical, ancient.")
					end
					
					if label.text2 == "on" then
						unclassified_on = true
						insert(unclassified_on, reading_mod)
					elseif label.text2 == "kun" then
						kun = true
						insert(kun, reading_mod)
					elseif label.classification == "on" then
						classified_on = true
						insert(classified_on, reading_mod)
					end
					
					-- test if reading contains katakana
					if find(reading_mod .. reading_hist .. reading_oldest, '') then
						insert(categories, ']') -- sometimes legit, like 「頁(ページ)」
					end

					if reading_hist ~= '' or reading_oldest ~= '' then
						-- test if historical readings contain small kana (anachronistic)
						if find(reading_hist .. reading_oldest, '') then
							insert(categories, ']') -- 
						end
						
						-- test if reading contains kun'yomi delimiter thing but historical readings don't
						if reading_mod:find("-", 1, true) then
							if reading_hist ~= '' and not reading_hist:find("-", 1, true) or reading_oldest ~= '' and not reading_oldest:find("-", 1, true) then
								insert(categories, ']')
							end
						end
					end

					-- check if there is data indicating that our kanji is a jouyou kanji
					if yomi_data then
						local reading = (label.classification == 'on' and hira_to_kata(reading_mod) or reading_mod)
						reading = reading:gsub('%.', '') -- 「あたら-し.い」→「あたら-しい」
						local yomi_type = yomi_data

						if yomi_type then
							is_jouyou = true

							if yomi_type == 1 or yomi_type == 2 then
								insert(pos, ']')
							elseif yomi_type == 3 or yomi_type == 4 then
								insert(pos, ']')
							end
						end
					end
					
					local subtype = label.text2
					if reading_mod then
						add_reading_category(reading_mod, subtype)
					end
					if reading_hist ~= '' then
						add_reading_category(reading_hist, subtype, 'historical')
					end
					if reading_oldest ~= '' then
						add_reading_category(reading_oldest, subtype, 'ancient')
					end
					
					-- process kun readings with okurigana, create kanji-okurigana links
					if reading:find("-", 1, true) then
						insert(pos, 1, plain_link{
							lang = lang,
							term = reading_mod:gsub('^.+%-', pagename),
						})
						
						if reading_hist ~= '' then
							insert(pos_hist, 1, plain_link{
								lang = lang,
								term = reading_hist:gsub('^.+%-', pagename),
							})
						end
						
						if reading_oldest ~= '' then
							insert(pos_oldest, 1, plain_link{
								lang = lang,
								term = reading_oldest:gsub('^.+%-', pagename),
							})
						end
					elseif label.classification == 'kun' then
						insert(categories, ']')
					end

					local rom = kana_to_romaji((reading_mod), lang_code):gsub('^(.+)(%-)', '<u>%1</u>')
					local rom_hist = kana_to_romaji((reading_hist:gsub('^(.+)(%-)', '<u>%1</u>')), lang_code, nil, {hist = true})
					local rom_oldest = kana_to_romaji((reading_oldest:gsub('^(.+)(%-)', '<u>%1</u>')), lang_code, nil, {hist = true})
					
					local mod_link = plain_link{
						lang = lang,
						term = reading_mod,
						tr = rom,
						pos = concat(pos, CONCAT_SEP),
					}
					if is_jouyou then
						mod_link = '<mark class="jouyou-reading">' .. mod_link .. '</mark>'
					end
					
					readings = mod_link .. (reading_hist ~= '' and '<sup>←' .. plain_link{
						lang = lang,
						term = reading_hist,
						tr = rom_hist,
						pos = concat(pos_hist, CONCAT_SEP),
					} .. '</sup>' or '') .. (reading_oldest ~= '' and '<sup>←' .. plain_link{
						lang = lang,
						term = reading_oldest,
						tr = rom_oldest,
						pos = concat(pos_oldest, CONCAT_SEP),
					} .. '</sup>' or '')
				end

				readings = concat(readings, "、")
			end
			
			-- Add "on-yomi", "kun-yomi", or "nanori-yomi" class around list of
			-- readings to allow JavaScript to locate them.
			insert(links, "* ''']'''" .. unclassified .. ': <span class="' .. label.classification .. '-yomi">' .. readings .. '</span>')
		end
	end
	
	for _, reading in ipairs(unclassified_on) do
		-- ]
		if classified_on then
			track("duplicate reading")
		end
		track("unclassified reading") -- Track unclassified readings for later classification
	end
	
	if not next(classified_on) and not next(unclassified_on) then
		if next(kun) then
			-- ]
			track("kun only")
		end
	elseif not next(kun) then
		-- ]
		track("on only")
	end

	if is_old_format then
		insert(categories, ']')
	end
	
	return concat(links, '\n') .. (NAMESPACE == '' and concat(categories) or '') .. require("Module:TemplateStyles")("Template:ja-readings/style.css")
end

return export