Modul:ja-kanji-readings

Daripada Wiktionary

Pendokumenan untuk modul ini boleh diciptakan di Modul:ja-kanji-readings/doc

local export = {}

local titleObj = mw.title.getCurrentTitle()
local fullpagename = titleObj.fullText
local pagename = titleObj.text
local namespace = titleObj.nsText

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

local find = mw.ustring.find
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local gsub = mw.ustring.gsub
local split = mw.text.split

local data = mw.loadData("Module:ja/data")
	
local labels = {
	goon = {
		index = 1,
		entry = "呉音",
		text = "Goon",
		text2 = "goon",
		classification = "on",
	},
	kanon = {
		index = 2,
		entry = "漢音",
		text = "Kan’on",
		text2 = "kan'on",
		classification = "on",
	},
	toon = {
		index = 3,
		entry = "唐音",
		text = "Tōon",
		text2 = "tōon",
		classification = "on",
	},
	kanyoon = {
		index = 4,
		entry = "慣用音",
		text = "Kan’yōon",
		text2 = "kan'yōon",
		classification = "on",
	},
	soon = {
		index = 5,
		entry = "宋音",
		text = "Sōon",
		text2 = "sōon",
		classification = "on",
	},
	on = {
		index = 6,
		entry = "on'yomi",
		text = "On",
		text2 = "on",
		classification = "on",
		unclassified = " (tidak dikelaskan)",
	},
	kun = {
		index = 7,
		entry = "kun'yomi",
		text = "Kun",
		text2 = "kun",
		classification = "kun",
	},
	nanori = {
		index = 8,
		entry = "nanori",
		text = "Nanori",
		text2 = "nanori",
		classification = "nanori",
	},
}

local function if_not_empty(var)
	if var == "" then
		return nil
	else
		return var
	end
end

local function plain_link(data)
	data.term = gsub(data.term, '[%.%- ]', '') -- 「かな-し.い」→「かなしい」, 「も-しく は」→「もしくは」
	if data.tr then
		data.tr = gsub(data.tr, '[%.%-]', '')
	end
	data.lang = ja
	data.sc = Jpan
	data.pos = if_not_empty(data.pos)
	data.gloss = if_not_empty(data.gloss)
	return require("Module:links").full_link(data)
end

local function process_okurigana(reading, kanji)
	if not (reading and kanji) then
		return nil
	end
	
	 -- 「むす-ぶ」→「結ぶ」
	return gsub(reading, '^(.+)(%-)', kanji)
end

local function make_romaji(rom, options)
	if not rom then
		return nil
	end
	
	 -- 「むす-ぶ」→「<u>むす</u>ぶ」
	rom = gsub(rom, '^(.+)(%-)', '<u>%1</u>')
	
	return require("Module:ja").kana_to_romaji(rom, options)
end

local function format_historical_reading(reading, romanization, pos)
	if not reading then
		return ""
	end
	return '<sup>←' .. plain_link{ term = reading, tr = romanization, pos = table.concat(pos, CONCAT_SEP) } .. '</sup>'
end

local function check(categories, reading_mod, reading_hist, reading_oldest)
	-- test if reading contains katakana
	if match(reading_mod .. (reading_hist or "") .. (reading_oldest or ""), '[ァ-ヺ]') then
		table.insert(categories, '[[Kategori:Permintaan untuk perhatian mengenai bahasa Jepune|1]]') -- sometimes legit, like 「頁(ページ)」
	end

	if reading_hist or reading_oldest then
		-- test if historical readings contain small kana (anachronistic)
		if match(reading_hist .. (reading_oldest or ""), '[ぁぃぅぇぉゃゅょ]') then
			table.insert(categories, '[[Kategori:Permintaan untuk perhatian mengenai bahasa Jepun|2]]') -- 
		end
		
		-- test if reading contains kun'yomi delimiter thing but historical readings don't
		if match(reading_mod, '%-') and
				(reading_hist and not match(reading_hist, '%-')		or
				reading_oldest and not match(reading_oldest, '%-'))	then
			table.insert(categories, '[[Kategori:Permintaan untuk perhatian mengenai bahasa Jepun|3]]')
		end
	end
end

--[=[
		Copied from [[Module:ja]] 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.org/w/index.php?diff=46057625
]=]
function export.show(frame)
	local params = {
		["goon"] = {},
		["kanon"] = {},
		["toon"] = {},
		["soon"] = {},
		["on"] = {},
		["kanyoon"] = {},
		["kun"] = {},
		["nanori"] = {},
		["pagename"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	if args.pagename then
		if namespace == "" then
			error("Parameter pagename tidak boleh digunakan dalam penyertaan, kerana hanya untuk ujian.")
		end
		pagename = args.pagename
	end

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

	local items = {
		goon = args["goon"],
		kanon = args["kanon"],
		toon = args["toon"],
		soon = args["soon"],
		on = args["on"],
		kanyoon = args["kanyoon"],
		kun = args["kun"],
		nanori = args["nanori"],
	}

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

	local is_old_format = false
	
	-- We need a separate kanji sortkey module.
	local sortkey = require("Module:zh-sortkey").makeSortKey(pagename, "ja")
	
	local unclassified_on = {}
	local classified_on = {}
	
	for class, readings in pairs(items) do
		if readings then
			local label = labels[class]
			
			local unclassified = ""

			if label.unclassified then
				if not (items.goon or items.kanon or items.toon or items.soon or items.kanyoon) then
					unclassified = label.unclassified
				end
			end
			
			local kana = "[ぁ-ー]"
			
			if match(readings, '%[%[' .. kana) then
				is_old_format = true

				if label.classification == 'on' then
					for reading in gmatch(readings, kana .. '+') do
						table.insert(categories, '[[Kategori:Kanji bahasa Jepun dengan bacaan ' .. reading .. '|' .. sortkey .. ']]')
					end
				end

				readings = gsub(
					readings,
					"%[%[([^%]|]+)%]%]",
					function(entry)
						if find(entry, "^[" .. Jpan:getCharacters() .. "]+$") then
							return plain_link{ term = entry  }
						else
							return "[[" .. entry .. "]]"
						end
					end
				)
			else
				local glosses = {} -- hold glosses for entire line, in case a gloss contains a comma
				for i, wholematch, gloss in require("Module:string").imatch(readings, '( "([^"]+)")') do
					glosses[i] = gloss
					wholematch = require('Module:string').pattern_escape(wholematch)
					readings = gsub(readings, wholematch, '##' .. i)
				end

				readings = split(readings, ',%s*')

				for i, reading in ipairs(readings) do
					local CONCAT_SEP = ', '

					local is_jouyou = false

					local reading_hist, reading_oldest

					local gloss = ''

					local pos, pos_hist, pos_oldest = { }, { '[[w:Ortografi kana bersejarah|bersejarah]]' }, { 'purba' }

					-- Get gloss; remove marker from text.
					local unchanged_reading = reading
					reading, number_of_glosses = gsub(
						reading,
						'##(%d+)',
						function(gloss_id)
							gloss = glosses[tonumber(gloss_id)]
							return ""
						end
					)
					
					if number_of_glosses > 1 then
						error("Terlalu banyak glosses dalam bacaan " .. unchanged_reading .. ".")
					end
					
					if match(reading, '##') then
						local gloss_id = match(reading, '##(%d+)') -- extract gloss id
						reading = gsub(reading, '##' .. gloss_id, '') -- remove marker from text
						gloss = glosses[tonumber(gloss_id)] -- save gloss
					end

					-- check for formatting indicating presence of historical kana spelling
					local hist_readings = mw.text.split(reading, "<")
					
					if #hist_readings <= 3 then
						reading_mod, reading_hist, reading_oldest = unpack(hist_readings)
					else
						error("Bacaan " .. reading .. " mengandungi terlalau banyak bacaan bersejarah: " .. #hist_readings .. ". Maksimum hanya 3: moden, bersejarah, purba.")
					end
					
					if class == "on" then
						unclassified_on[reading_mod] = true
					elseif label.classification == "on" then
						classified_on[reading_mod] = true
					end
					
					check(categories, reading_mod, reading_hist, reading_oldest)

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

						if type then
							is_jouyou = true

							if type == 1 or type == 2 then
								table.insert(pos, '[[w:Jōyō kanji|Jōyō]]')
							elseif type == 3 or type == 4 then
								table.insert(pos, '[[w:kanji Jōyō|Jōyō]], jarang')
							end
						end
					end

					if reading_mod then
						table.insert(categories, '[[Kategori:Kanji bahasa Jepun dengan bacaan ' .. label.text2 .. ' ' .. gsub(reading_mod, "[%. ]", "") .. '|' .. sortkey .. ']]')
					end
					if reading_hist then
						table.insert(categories, '[[Kategori:Kanji bahasa Jepun dengan bacaan bersejarah ' .. label.text2 .. ' ' .. gsub(reading_hist, "[%. ]", "") .. '|' .. sortkey .. ']]')
					end
					if reading_oldest then
						table.insert(categories, '[[Kategori:Kanji bahasa Jepun dengan bacaan purba ' .. label.text2 .. ' ' .. gsub(reading_oldest, "[%. ]", "") .. '|' .. sortkey .. ']]')
					end
					
					local kanji, kanji_hist, kanji_oldest
					-- process kun readings with okurigana, create kanji-okurigana links
					if match(reading, '%-') then
						kanji = process_okurigana(reading_mod, pagename)
						table.insert(pos, 1, plain_link{ term = kanji })
						
						if kanji_hist then
							kanji_hist = process_okurigana(reading_hist, pagename)
							table.insert(pos_hist, 1, plain_link{ term = kanji_hist })
						end
						
						if kanji_oldest then
							kanji_oldest = process_okurigana(reading_oldest, pagename)
							table.insert(pos_oldest, 1, plain_link{ term = kanji_oldest })
						end
					elseif label.classification == 'kun' then
						table.insert(categories, '[[Kategori:Kanji Jepun dengan bacaan kun hilang penamaan okurigana|' .. sortkey .. ']]')
					end

					local rom = make_romaji(reading_mod)
					local rom_hist = make_romaji(reading_hist, {hist=true})
					local rom_oldest = make_romaji(reading_oldest, {hist=true})
					
					local mod_link = plain_link{ term = reading_mod, tr = rom, gloss = gloss, pos = table.concat(pos, CONCAT_SEP) }
					if is_jouyou then
						mod_link = '<mark class="jouyou-reading" style="background:rgba(224, 255, 255, 0.5);">' .. mod_link .. '</mark>'
					end
					
					readings[i] =
						mod_link
						..
						format_historical_reading(reading_hist, rom_hist, pos_hist)
						..
						format_historical_reading(reading_oldest, rom_oldest, pos_oldest)
				end

				readings = table.concat(readings, '; ')
			end
			
			links[label.index] = "* '''[[" .. label.entry .. "|".. label.text .. "]]'''" .. unclassified .. ": " .. readings
		end
	end
	
	for i, reading in pairs(unclassified_on) do
		-- [[Special:WhatLinksHere/Template:tracking/ja-kanji-readings/duplicate reading]]
		if classified_on[reading] then
			require("Module:debug").track("ja-kanji-readings/duplicate reading")
		end
	end

	links = require("Module:table").compressSparseArray(links)
	
	--[==[
	-- determine if this is joyo kanji (常用) or jinmeiyo kanji (人名用) or neither (表外)
	local joyo_kanji_pattern = ('[' .. data.joyo_kanji .. ']')
	local jinmeiyo_kanji_pattern = ('[' .. data.jinmeiyo_kanji .. ']')
	local sortkey = ""
	if match(pagename, joyo_kanji_pattern) then
		sortkey = "Common"
	elseif match(pagename, jinmeiyo_kanji_pattern) then
		sortkey = "Names"
	else
		sortkey = "Uncommon"
	end
	]==]
	-- NOTE: with the introduction of the new {{ja-readings}} formatting the above block of code currently does nothing...

	if is_old_format then
		table.insert(links, '[[Kategori:Kanji Jepun menggunakan format lama ja-bacaan]]')
	end
	
	local links = table.concat(links, "\n")
	if namespace == "" then
		categories = table.concat(categories, "\n")
	else
		categories = ""
	end

	local output = links .. categories
	
	-- mw.log(output)
	
	return output
end

function export.catboiler(frame)
	local output = {}
	local categories = {}
	local catfix = ""
	
	local title = mw.title.getCurrentTitle()
	local pagename = title.text
	local namespace = title.nsText
	
	local mode
	
	if frame.args[1] then
		pagename = frame.args[1]
		pagename = gsub(pagename, "^Kategori:", "")
		namespace = "Kategori"
		mode = "testing"
	end
	
	if namespace ~= "Kategori" then
		error("This template should only be used in the Category namespace.")
	end
	
	local kanaRegex = "[-ぁ-ー]+"
	
	if not (	find(pagename, "^Kanji bahasa Jepun dengan bacaan .+ " .. kanaRegex .. "$") or
				find(pagename, "^Kanji bahasa Jepun dibaca sebagai " .. kanaRegex .. "$") or
				find(pagename, "^Kanji bahasa Jepun mengikut bacaan .+$")				)
		then
		
		error('The category name "' .. pagename .. '" is not recognized.')
	end
		
	local periods = {
		historical = true,
		ancient = true,
	}
	
	local onTypes = {
		["goon"] = true,
		["kan'on"] = true,
		["tōon"] = true,
		["kan'yōon"] = true,
		["sōon"] = true,
		["on"] = true,
	}
	
	local cat1, cat2, cat3
	local sortkey1, sortkey2, sortkey3
	local hira_sortkey
	
	-- If the pagename contains kana, it is a "with x reading" category; otherwise, it's a "by x reading" category.
	if find(pagename, "[ぁ-ー]") then
		-- Japanese kanji with on reading あつ, Japanese kanji with historical on reading あつ
		local period, readingType, reading = match(pagename, "^Kanji bahasa Jepun dengan ([a-z]-) ?([%a']+) reading (" .. kanaRegex .. ")$")
		
		if not readingType then
			reading = match(pagename, "^Kanji bahasa Jepun dibaca sebagai (" .. kanaRegex .. ")$")
		end
		
		if period == "" then
			period = nil
		end
		
		if period or reading then
			catfix = require("Module:utilities").catfix(ja, Jpan)
		end
		
		if reading then
			hira_sortkey = require("Module:ja").jsort(reading)
			frame:callParserFunction("DISPLAYTITLE", gsub(fullpagename, reading, '<span class="Jpan" lang="ja">%0</span>'))
		end
		
		if readingType then
			if period then
				if not periods[period] then
					error('The period name "' .. period .. '" is not valid.')
				end
				
				cat1 = "mengikut bacaan " .. readingType .. " " .. period
				sortkey1 = hira_sortkey
				
				if find(readingType, ".on$") then
					if onTypes[readingType] then
						cat2 = "dengan bacaan " .. reading .. " on " .. period
						sortkey2 = readingType
					else
						error('"' .. readingType .. '" is not a valid type of on\'yomi.')
					end
				else
					cat2 = "with " .. period .. " reading " .. reading
					sortkey2 = readingType
				end
			else
				local label = labels[readingType]
				if label and mode ~= "testing" then
					table.insert(
						output,
						"Kategori ini mempunyai [[kanji]] dengan bacaan [[" .. label.entry .. "|".. mw.ustring.lower(label.text) .. "]]" ..
							plain_link{ term = reading, tr = require("Module:ja").kana_to_romaji(reading) } ..
							"."
					)
				end
				
				cat1 = "mengikut bacaan " .. readingType
				sortkey1 = hira_sortkey
				
				if find(readingType, "on$") then
					cat2 = "dengan bacaan on " .. reading
					sortkey2 = readingType
				end
				
				cat3 = "dibaca sebagai " .. reading
				sortkey3 = readingType
			end

			if readingType == "kun" and not find(reading, "%-") then
				table.insert(categories, "[[Kategori:Kanji bahasa Jepun dengan bacaan kun hilang penamaan okurigana|" .. sortkey1 .. "]]")
			end
		else
			cat1 = "mengikut bacaan"
			sortkey1 = hira_sortkey
		end
	else
		local period, readingType = match(pagename, "^Kanji bahasa Jepun mengikut bacaan ([a-z]-) ?([%a']+)$")
		
		if period == "" then
			period = nil
		end
		
		if period then
			if find(readingType, ".on$") then
				if onTypes[readingType] then
					cat1 = "mengikut bacaan on " .. period
					sortkey1 = readingType
				else
					error('"' .. readingType .. '" is not a valid type of on\'yomi.')
				end
			else
				cat1 = "mengikut bacaan " .. readingType
				sortkey1 = period
			end
			
			cat2 = "mengikut bacaan " .. period
			sortkey2 = readingType
		else
			if find(readingType, ".on$") then
				if onTypes[readingType] then
					sortkey1 = readingType
					cat1 = "mengikut bacaan on"
				else
					error('"' .. readingType .. '" is not a valid type of on\'yomi.')
				end
			else
				cat1 = "mengikut bacaan"
				sortkey1 = readingType
			end
		end
	end
	
	if cat1 then
		table.insert(categories, "[[Kategori:Kanji " .. cat1 .. " bahasa Jepun|" .. sortkey1 .. "]]")
	end
	if cat2 then
		table.insert(categories, "[[Kategori:Kanji " .. cat2 .. " bahasa Jepun|" .. sortkey2 .. "]]")
	end
	if cat3 then
		table.insert(categories, "[[Kategori:Kanji " .. cat3 .. " bahasa Jepun|" .. sortkey3 .. "]]")
	end
	
	local forceOutput
	if mode == "testing" then
		forceOutput = true
	end
	
	categories = table.concat(categories)
	
	if mode == "testing" then
		table.insert(output, pagename)
		
		categories = gsub(categories, "%]%]%[%[", "]]\n[[")
		categories = frame:extensionTag{ name = "syntaxhighlight", content = categories }
		if categories == "" then
			categories = '<span class="error">Gagal menjana kategori untuk ' .. pagename .. '</span>'
		end
	end
	
	local output = table.concat(output) .. categories .. ( mode ~= "texting" and catfix or "" )
	
	return output
end

return export