Module:User:Nyarukoseijin/ja-see

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


local export = {}

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

local m_ja = require('Module:ja')

local function gmatch_array(s, pattern) local result = {} for e in gmatch(s, pattern) do table.insert(result, e) end return result end
local function map(arr, f) local result = {} for _, e in ipairs(arr) do local fe = f(e) if fe ~= nil then table.insert(result, fe) end end return result end
local function filter(arr, f) local result = {} for _, e in ipairs(arr) do if f(e) then table.insert(result, e) end end return result end
local function contains(arr, item) for _, e in ipairs(arr) do if e == item then return true end end return false end
local function flatten(arrs) local result = {} for _, arr in ipairs(arrs) do for _, e in ipairs(arr) do table.insert(result, e) end end return result end
-- f should be str->str in the following functions
local function memoize(f) local results = {} return function(s) if not results then results = f(s) end return results end end
local getContent_memo = memoize(function(title) return mw.title.new(title):getContent() or '' end)
local function group(arr, f) local r = {} for _, e in ipairs(arr) do local fe = f(e) if r and r.key == fe then table.insert(r, e) else table.insert(r, { e, key = fe }) end end return r end

--[[ returns an array of definitions, each having the format
	{ def = <definition>,
	  kanji_spellings = <array of alternative kanji spellings listed in {{ja-kanjitab|alt=...}}, can be overrided with {{ja-def|...}}>,
	  kana_spellings = <array of alternative kana spellings listed in the headword template>,
	  historical_kana_spellings = <array of historical kana spellings listed in the headword template>,
	  header = <name of PoS header>,
	  headword_line = <wikicode of headword line> }
]]
local function get_definitions_from_wikicode(wikicode)
	local current_kanji_spellings = {}
	local current_kana_spellings = {}
	local current_historical_kana_spellings = {}
	local current_header
	local current_headword_line
	local currently_under_headword_line = false
	
	local result = {}
	for line in gmatch(match(wikicode, '==Japanese==\n(.*)') or '', '+') do
		-- the following branches are ordered by frequency; read backwards
		if currently_under_headword_line and find(line, '^#+') then
			table.insert(result, { def = line,
				kanji_spellings = find(line, '{{ja%-def|') and split(match(line, '{{ja%-def|(+)'), '|')
									or find(line, '<!%-%- kana only %-%->') and {}
									or current_kanji_spellings,
				kana_spellings = current_kana_spellings,
				historical_kana_spellings = current_historical_kana_spellings,
				header = current_header,
				headword_line = current_headword_line })
		elseif find(line, '^{{ja%-noun')
			or find(line, '^{{ja%-adj')
			or find(line, '^{{ja%-pos')
			or find(line, '^{{ja%-phrase')
			or find(line, '^{{ja%-verb')
			or find(line, '^{{ja%-verb form')
			or find(line, '^{{ja%-verb%-suru') then
				local escaped_line = gsub(gsub(line, '%|]-)|(|]-)%]%]', ']'), '|hkata=', '|hhira=')
				current_kana_spellings = map(gmatch_array(escaped_line, '|(+)'), m_ja.remove_ruby_markup)
				current_historical_kana_spellings = gmatch_array(escaped_line, '|hhira=(+)')
				current_headword_line = line
				currently_under_headword_line = true
		elseif find(line, '^===++===+$') then
			current_header = match(line, '^===+(+)===+$')
			currently_under_headword_line = false
		elseif find(line, '^{{ja%-kanjitab') then
			local alt_argument = match(line, '|alt=(*)')
			current_kanji_spellings = alt_argument and split(gsub(alt_argument, ':*', ''), ',') or {}
		elseif line == '----' then
			break
		end
	end
	return result
end

-- ditto, except that each definition also contains the title of the page it is from
local function get_definitions_from_entry(title)
	local wikicode = getContent_memo(title)
	local defs = get_definitions_from_wikicode(wikicode)
	map(defs, function(def) def.title = title end)
	return defs
end

local function get_definitions_from_entries(titles)
	return flatten(map(titles, get_definitions_from_entry))
end

local function format_definitions(defs, frame)
	local function ja(text) return '<span lang="ja" class="Jpan">' .. text .. '</span>' end
	local function link(lemma, display) return ja(']') end
	local function link_bracket(lemma, display) return ja('【]】') end
	local kanji_grade_labels = {
		'<span class="explain" title="Grade 1 kanji" style="vertical-align: top;">1</span>',
		'<span class="explain" title="Grade 2 kanji" style="vertical-align: top;">2</span>',
		'<span class="explain" title="Grade 3 kanji" style="vertical-align: top;">3</span>',
		'<span class="explain" title="Grade 4 kanji" style="vertical-align: top;">4</span>',
		'<span class="explain" title="Grade 5 kanji" style="vertical-align: top;">5</span>',
		'<span class="explain" title="Grade 6 kanji" style="vertical-align: top;">6</span>',
		'<span class="explain" title="Jōyō kanji" style="vertical-align: top;">S</span>',
		'<span class="explain" title="Jinmeiyō kanji" style="vertical-align: top;">J</span>',
		'<span class="explain" title="Hyōgaiji kanji" style="vertical-align: top;">H</span>' }
	
	local function ruby(kanji, kana) -- this function ought to be in ]
		local kanji_segments = gsub(kanji, "(+)", "`%1`")
		
		-- returns possible matches between kanji and kana
		-- for example, match('`物`の`哀`れ', 'もののあわれ') returns { '(も)の(のあわ)れ', '(もの)の(あわ)れ' }
		local function match(kanji_segments, kana)
			if kanji_segments:find('`') then
				local kana_portion, kanji_portion, rest = mw.ustring.match(kanji_segments, '(.-)`(.-)`(.*)')
				_, _, kana = mw.ustring.find(kana, '^' .. kana_portion .. '(.*)')
				if not kana then return {} end
				local candidates = {}
				for i = 1, mw.ustring.len(kana) do
					for _, candidate in ipairs(match(rest, mw.ustring.sub(kana, i + 1))) do
						table.insert(candidates, kana_portion .. '(' .. mw.ustring.sub(kana, 1, i) .. ')' .. candidate)
					end
				end
				return candidates
			else
				return (kanji_segments == kana) and { kana } or {}
			end
		end
		
		local matches = match(kanji_segments, kana)
		local result = #matches == 1 and matches or ('(' .. kana .. ')')
		return gsub(result, "%]+)%]%((+)%)", "<ruby><rb>%1</rb><rt>%2</rt></ruby>")
	end
	
	local function format_headword(defs)
		local title = defs.title
		local kana = defs.kana_spellings
		local headword = link_bracket(title, mw.title.getCurrentTitle().text == kana and title or ruby(title, kana))
		local kanji_grade = len(title) == 1 and m_ja.kanji_grade(title)
		return '<span style="font-size:x-large">' .. headword .. '</span>' .. (kanji_grade and kanji_grade_labels or '')
	end
	
	local preprocess_memo = memoize(function (s) return frame:preprocess(s) end)
	local function format_definitions(defs)
		local headword_line_categories = {}
		local function format_definition(def)
			local def_text = find(def.def, '{{rfdef') and "''This term needs a translation to English.''" or preprocess_memo(gsub(def.def, '^#+ *', ''))
			local def_prefix = gsub(match(def.def, '^#+'), '#', ':')
			local def_pos_label = ' <span style="padding-right:.6em;color:#5A5C5A;font-size:80%"></span> '
			local headword_line = def.headword_line
			if ({ Hira = true, Kana = true,  = true }) then
				headword_line = gsub(headword_line, '}}$', '|hira=' .. def.title .. '}}')
			end
			table.concat(headword_line_categories, table.concat(gmatch_array(preprocess_memo(headword_line), '%%]')))
			return def_prefix .. def_pos_label .. def_text
		end
		return table.concat(headword_line_categories) .. '\n' .. table.concat(map(defs, format_definition), '\n')
	end
	
	local is_first_row = true
	local function format_row(defs)
		local result = '|-\n| style="white-space:nowrap;vertical-align:top;' .. (is_first_row and '' or 'border-top:1px solid lightgray;') .. '" | ' .. format_headword(defs)
			.. '\n| style="' .. (is_first_row and '' or 'border-top:1px solid lightgray;') .. '" |\n' .. format_definitions(defs) .. '\n'
		is_first_row = false
		return result
	end
	
	local def_groups = group(defs, function(def) return def.title .. ',' .. def.kana_spellings end)
	local rows = map(def_groups, format_row)
	
	return '{| style="width: 100%"\n' .. table.concat(rows) .. '|}'
end

function export.show(frame)
	local params = {
		 = { list = true },
		 = {},
	}
	local args, unrecognized_args = require("Module:parameters").process(frame:getParent().args, params, true)
	for key, value in pairs(unrecognized_args) do error("“" .. key .. "” is not a recognized parameter.") end
	
	local title = mw.title.getCurrentTitle().text
	local key = args.key or title
	local defs = get_definitions_from_entries(args)
	local matching_defs = filter(defs, function(def)
		return contains(def.kanji_spellings, key) or contains(def.kana_spellings, key) or contains(def.historical_kana_spellings, key)
		end)
	return format_definitions(defs, frame)
end

return export