This module deploys a formatted table for Japanese counter words/suffixes.
Parameter lemma
is the counter, for example, 個. And parameter kana
is the kana reading, こ.
Parameter 1
must be a single hyphen "-". A column looks like this:
|-|number|kanji 1|kanji 2|kanji 3...|-|reading 1|reading 2|reading 3...
Column's number can be any number, or question mark ? for 何. Column's kanji, if unspecified, is default number+suffix.
Short example table for 個:
{{#invoke:ja-counter-table|show |lemma=個|kana=こ |-|1|-|いっこ |-|2|-|にこ |-|3|-|さんこ |-|?|-|なんこ }}
Japanese counter/suffix: 個 (こ, ko) | |||
---|---|---|---|
Number | Kanji | Kana | Romaji |
1 | 一個 | いっこ | ikko |
2 | 二個 | にこ | niko |
3 | 三個 | さんこ | sanko |
? | 何個 | なんこ | nanko |
local export = {}
local janumeral = require("Module:ja-numeral").convert
local kanatoromaji = require("Module:Hrkt-translit").tr
local tagtext = require("Module:script utilities").tag_text
local embeddedlanguagelinks = require("Module:links").embedded_language_links
local lang = require("Module:languages").getByCode("en")
local sc = require("Module:scripts").getByCode("Latn")
local function taglinkwikitext(text)
return tagtext(embeddedlanguagelinks{term = text, lang = lang, sc = sc}, lang, sc)
end
local function formatnumber(number)
local str = tostring(number)
if str == nil or str == "" or not string.match(str, "^%d+$") then
return str
end
local result = ""
local strlen = string.len(str)
for i = 0, math.floor(strlen / 3) - 1 do
result = "," .. string.sub(str, strlen - (i * 3) - 2, strlen - (i * 3)) .. result
end
if strlen % 3 == 0 then
result = string.sub(result, 2, string.len(result))
else
result = string.sub(str, 1, strlen % 3) .. result
end
return result
end
function export.show(frame)
local params = {
= { required=true, allow_empty=true, list=true },
= { },
= { },
= { },
= { },
= { }
}
local args = require("Module:parameters").process(frame.args, params)
local columnsargs = args
if columnsargs == nil then
error("Zero columns")
end
local counterlemma = args
local counterkana = args
local countertr = args
local navbar = args
local title = args
local output = mw.html.create("table")
:addClass("wikitable")
:attr("style", "border: 1px "
.. "solid #AAAAAA; border-collapse:collapse;")
:attr("cellpadding", "3")
:attr("rules", "all")
local headertr = mw.html.create("tr")
output:node(headertr)
local headerth = mw.html.create("th")
:attr("align", "center")
:attr("colspan", "4")
headertr:node(headerth)
local headertitlediv = mw.html.create("div")
if title ~= nil then
if title ~= "" then
headertitlediv:wikitext(title)
end
else
headertitlediv:wikitext("Japanese counter/suffix")
if counterlemma ~= nil and counterlemma ~= "" then
headertitlediv:wikitext(": ")
local link = frame:expandTemplate{
title="m", args={"ja", counterlemma}}
headertitlediv:wikitext(link)
local list = {}
if counterkana ~= nil and counterkana ~= "" then
if counterlemma ~= counterkana then
table.insert(list, counterkana)
end
local romaji = kanatoromaji(counterkana)
table.insert(list, "''" .. romaji .. "''")
end
if countertr ~= nil and countertr ~= "" then
local text = taglinkwikitext(countertr)
table.insert(list, '"' .. text .. '"')
end
if #list > 0 then
headertitlediv:wikitext(" (" .. table.concat(list, ", ") .. ")")
end
end
end
if navbar ~= nil and navbar ~= "" then
local headernavbardiv = mw.html.create("div")
headernavbardiv:attr("style", 'float:left; width:6em;text-align:left;')
headernavbardiv:wikitext(navbar)
headerth:node(headernavbardiv)
end
headerth:node(headertitlediv)
if columnsargs ~= nil then
if columnsargs ~= "-" then
error('Argument 1 must be single hyphen "-"')
end
local subheadertr = mw.html.create("tr")
local numberth = mw.html.create("th")
numberth:wikitext(taglinkwikitext("]"))
subheadertr:node(numberth)
local kanjith = mw.html.create("th")
kanjith:wikitext(taglinkwikitext("]"))
subheadertr:node(kanjith)
local kanath = mw.html.create("th")
kanath:wikitext(taglinkwikitext("]"))
subheadertr:node(kanath)
local romajith = mw.html.create("th")
romajith:wikitext(taglinkwikitext("]"))
subheadertr:node(romajith)
output:node(subheadertr)
end
if counterlemma == nil then
counterlemma = ""
end
if counterkana == nil then
counterkana = ""
end
local ncolumns = 0
local i = 1
while true do
if columnsargs == nil then
break
end
local columntr = mw.html.create("tr")
local number = columnsargs
local lemmasbegin = i + 2
local lemmasend = i + 2
while true do
local lemma = columnsargs
if lemma == nil or lemma == "-" then
break
end
lemmasend = lemmasend + 1
end
local nlemmas = lemmasend - lemmasbegin
local readingsbegin = lemmasend + 1
local readingsend = lemmasend + 1
while true do
local reading = columnsargs
if reading == nil or reading == "-" then
break
end
readingsend = readingsend + 1
end
local nreadings = readingsend - readingsbegin
local numbertd = mw.html.create("td")
numbertd:attr("align", "right")
if number ~= nil and number ~= "" then
local formattednumber = formatnumber(number)
numbertd:wikitext(formattednumber)
end
columntr:node(numbertd)
local kanjitd = mw.html.create("td")
kanjitd:attr("align", "center")
if nlemmas == 0 and number == "?" then
local lemma = "何" .. counterlemma
local link = frame:expandTemplate{
title="m", args={"ja", lemma}}
kanjitd:wikitext(link)
elseif nlemmas == 0 and string.match(number, "^%d+$") then
if tonumber(number) == 0 then
local lemma1 = "0" .. counterlemma
local lemma2 = "零" .. counterlemma
local link1 = frame:expandTemplate{
title="m", args={"ja", lemma1}}
local link2 = frame:expandTemplate{
title="m", args={"ja", lemma2}}
kanjitd:wikitext(link1)
kanjitd:wikitext("、")
kanjitd:wikitext(link2)
else
local numeral = janumeral(number)
local lemma = numeral .. counterlemma
local link = frame:expandTemplate{
title="m", args={"ja", lemma}}
kanjitd:wikitext(link)
end
elseif nlemmas > 0 then
for lemmai = lemmasbegin, lemmasend - 1 do
local lemma = columnsargs
if lemma ~= "" then
local link = frame:expandTemplate{
title="m", args={"ja", lemma}}
kanjitd:wikitext(link)
end
if lemmai ~= lemmasend - 1 then
kanjitd:wikitext("、")
end
end
end
columntr:node(kanjitd)
local kanatd = mw.html.create("td")
kanatd:attr("align", "center")
kanatd:wikitext('<span lang="ja" class="Jpan">')
for readingi = readingsbegin, readingsend - 1 do
local reading = columnsargs
kanatd:wikitext(reading)
if readingi ~= readingsend - 1 then
kanatd:wikitext("、")
end
end
kanatd:wikitext('</span>')
columntr:node(kanatd)
local romajitd = mw.html.create("td")
romajitd:attr("align", "center")
for readingi = readingsbegin, readingsend - 1 do
local reading = columnsargs
local romaji = kanatoromaji(reading)
romajitd:wikitext(romaji)
if readingi ~= readingsend - 1 then
romajitd:wikitext(", ")
end
end
columntr:node(romajitd)
output:node(columntr)
i = readingsend
ncolumns = ncolumns + 1
end
if ncolumns > 14 then
output = mw.html.create("div")
:node(output)
:addClass("list-switcher")
:attr("style", "min-height:18em;")
output = mw.html.create("div")
:addClass("list-switcher-wrapper")
:attr("style", "width: fit-content;")
:node(output)
end
return output
end
return export