Dokumentationen för denna modul kan skapas på Modul:ja-headword/dok /test
local m_ja = require("Module:ja")
local export = {}
local pos_functions = {}
local lang = require("Module:languages").getByCode("ja")
local Japanese_symbols = 'ー・=?!。、'
local katakana_range = 'ァ-ヺーヽヾ'
local katakana_pattern = ''
local hiragana_range = 'ぁ-ゖーゞゝ'
local hiragana_pattern = ''
local kana_range = katakana_range .. hiragana_range .. Japanese_symbols
local kanapattern = ''
local Japanese_scripts_pattern = ''
local function detect_kana_script(kana)
if mw.ustring.gsub(kana,katakana_pattern,'') == '' then return 'kata' end
if mw.ustring.gsub(kana,hiragana_pattern,'') == '' then return 'hira' end
if mw.ustring.gsub(mw.ustring.gsub(kana,hiragana_pattern,''),katakana_pattern,'') == '' then return 'both' end
return 'none'
end
local function kana_to_romaji(kana, poscat, args)
-- make adjustments for -u verbs and -i adjectives by placing a period before the last character
-- to prevent romanizing long vowels with macrons
if poscat == "verbs" or (poscat == "adjectives" and ((args == "i" or args == "い") or (args == "i" or args == "い"))) then
kana = mw.ustring.gsub(kana,'(.)$','.%1')
end
local romaji = m_ja.kana_to_romaji(kana)
-- init caps for proper nouns
if poscat == "proper nouns" then
romaji = mw.ustring.gsub(romaji, "^%l", mw.ustring.upper)
romaji = mw.ustring.gsub(romaji, " %l", mw.ustring.upper)
romaji = mw.ustring.gsub(romaji, "-%l", mw.ustring.upper)
end
-- hyphens for prefixes, suffixes, and counters (classifiers)
if poscat == "prefixes" then return romaji .. "-" end
if poscat == "suffixes" or poscat == "counters" or poscat == "classifiers" then return "-" .. romaji end
return romaji
end
local en_numerals = {
"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen"
}
local en_grades = {
"first grade", "second grade", "third grade",
"fourth grade", "fifth grade", "sixth grade",
"secondary school", "jinmeiyō", "hyōgaiji"
}
-- adds category Japanese terms spelled with jōyō kanji or Japanese terms spelled with non-jōyō kanji
-- (if it contains any kanji)
local function categorize_by_kanji(categories, PAGENAME)
-- remove non-kanji characters
local onlykanji = mw.ustring.gsub(PAGENAME, '', '')
local number_of_kanji = mw.ustring.len(onlykanji)
if number_of_kanji > 0 then
for i=1,mw.ustring.len(onlykanji) do
table.insert(categories, ("Japanese terms spelled with %s kanji"):format(en_grades))
end
-- categorize by number of kanji
if number_of_kanji == 1 then
table.insert(categories, "Japanese terms written with one Han script character")
elseif en_numerals then
table.insert(categories, ("Japanese terms written with %s Han script characters"):format(en_numerals))
end
end
end
-- if this term is composed of only a single kanji, it does not have kanjitab/kanji reading tab
-- which generate "Japanese terms spelled with .. " categories, and since it is only one kanji
-- we know the kanji reading
-- (this category is for maintenance because many of these need attention)
local function singlekanji_term(categories, PAGENAME)
if mw.ustring.len(PAGENAME) == 1 and mw.ustring.match(PAGENAME, '') then
table.insert(categories, "Japanese terms spelled with " .. PAGENAME)
table.insert(categories, "Japanese single-kanji terms")
end
end
-- get a kana form to use, in order of preference: unnamed, hira, kana, pagename
local function find_kana(args, PAGENAME)
for i,arg in ipairs(args) do
if args and mw.ustring.gsub(args, kanapattern, '') == '' then return args end
end
if mw.ustring.gsub(PAGENAME, kanapattern, '') == '' then return PAGENAME end
local hira = args or ""; if hira ~= "" then return hira end
local kata = args or ""; if kata ~= "" then return kata end
error("No kana detected in the unnamed parameters, |hira= and |kata= parameter. See template documentation for details.")
end
-- go through args and build inflections by finding whatever kanas were given to us
local function find_inflections(args, categories, inflections, poscat, PAGENAME)
local function romanization(kana)
-- accept the automatic romanization generated in function kana_to_romaji() above
-- compare that to the manual romanization if it exists and add it to inflections
local rom = args or ""
local auto_rom = kana_to_romaji(kana, poscat, args)
if rom == "" then rom = auto_rom end
-- check auto rom against manual and put in hidden category if they differ
if rom ~= auto_rom then table.insert(categories, "Japanese terms with romaji needing attention") end
-- throw an error if there is no romanization
if rom == "" then error("Japanese terms must have a kana form.") end
-- add romaji
-- add link manually for WT:ACCEL unless headword is for suru verb
if poscat == "suru verbs" then
table.insert(inflections, {label = "romaji", "] ]"})
elseif detect_kana_script(PAGENAME) ~= 'none' then
-- only accelerate romaji creation for kana entries
table.insert(inflections, {label = "romaji", accel = "romanized-form-of", rom})
else
table.insert(inflections, {label = "romaji", rom})
end
end
local allkana,original,readings = {},{},{}
for i,arg in ipairs(args) do
if arg and arg ~= "" and mw.ustring.gsub(arg,kanapattern,'') == '' then table.insert(allkana, arg) end
end
-- accept "hira" and "kata" but let Lua decide if they are really hiragana or katakana
if args and args ~= "" and mw.ustring.gsub(args,kanapattern,'') == '' then table.insert(allkana, args) end
if args and args ~= "" and mw.ustring.gsub(args,kanapattern,'') == '' then table.insert(allkana, args) end
if mw.ustring.gsub(PAGENAME,kanapattern,'') == '' then
if #allkana == 0 then table.insert(allkana, PAGENAME) end
end
for i = 1, #allkana do
-- remove markup
table.insert(original,allkana)
allkana = mw.ustring.gsub(allkana, '', '')
end
for i = 1, #allkana do
-- if this is not kana, blank it out
if allkana and not mw.ustring.match(allkana, kanapattern) then
allkana = ""
else
-- if this is kana, count it as another effective reading (ignoring hiragana-katakana distinction)
readings)] = 1
end
-- only if this kana is different from the page name
if allkana ~= PAGENAME and allkana ~= "" then
-- find script type and put it in "label"
local labelval = ""
if detect_kana_script(allkana) == 'both' then labelval = "hiragana and katakana"
elseif detect_kana_script(allkana) == 'hira' then labelval = "hiragana"
else labelval = "katakana" end
-- add everything to inflections, except historical hiragana which is next
if poscat == "nouns" or poscat == "proper nouns"or poscat == "verbs" or poscat == "adjectives" or poscat == "adverbs" then
-- enable accelerated entry creation using hiragana links for certain parts of speech
table.insert(inflections, {label = labelval, accel = ("kana-%s-form-of"):format(poscat:sub(1,poscat:len()-1):gsub(' ','-')), allkana})
elseif poscat ~= "suru verbs" then
table.insert(inflections, {label = labelval, allkana})
else
table.insert(inflections, {label = labelval, " .. "]]]"})
end
end
-- do the romanization business if it passes through every check
local undergo_romanization = false
if allkana ~= "" then
undergo_romanization = true
if allkana == PAGENAME and mw.ustring.gsub(PAGENAME,kanapattern,'') ~= '' then
undergo_romanization = false
else
for j = 1, #allkana do
if i < j and allkana and m_ja.kana_to_romaji(allkana) == m_ja.kana_to_romaji(allkana) then
undergo_romanization = false
end
end
end
end
if undergo_romanization then romanization(original) end
end
local hhira = args or ""
if hhira ~= "" then
if poscat == "suru verbs" then
table.insert(inflections, {label = "historical hiragana", "]]"})
else
table.insert(inflections, {label = "historical hiragana", hhira})
end
end
local num_readings = 0
for _ in pairs(readings) do
num_readings = num_readings + 1
end
if num_readings > 1 then table.insert(categories, "Japanese words with multiple readings") end
end
-- categorize by the script of the pagename or specific characters contained in it
local function extra_categorization(categories, PAGENAME, katakana_category)
-- if PAGENAME is hiragana, put in that category, same for katakana (but do it at the end)
if detect_kana_script(PAGENAME) == 'hira' then table.insert(categories, "Japanese hiragana") end
if detect_kana_script(PAGENAME) == 'kata' then table.insert(katakana_category, "Japanese katakana") end
if (mw.ustring.gsub(PAGENAME, Japanese_scripts_pattern, '') ~= "") and mw.ustring.match(PAGENAME, Japanese_scripts_pattern) then
table.insert(categories, "Japanese terms written in multiple scripts") end
for _,character in ipairs({'々','ゝ','ゞ','ヽ','ヾ'}) do
if mw.ustring.match(PAGENAME,character) then table.insert(categories, ("Japanese terms spelled with %s"):format(character)) end
end
end
local aliases = {
='tr', ='tr',
='in', ='in', ='in',
='1', ='2', ='3'
}
pos_functions = function(args, inflections, categories)
table.insert(categories, "Japanese verbs")
-- transitivity
local tr = args or ""
tr = aliases or tr
if tr ~= "" then
if tr == "tr" then table.insert(inflections, {label = "transitive"}) end
if tr == "in" then table.insert(inflections, {label = "intransitive"}) end
if tr == "both" then table.insert(inflections, {label = "transitive and intransitive"}) end
else
table.insert(categories, "Japanese verbs without transitivity")
end
-- conjugation type
local conjugation = args or ""
conjugation = aliases or conjugation
if conjugation ~= "" then
if conjugation == "1" then table.insert(inflections, {label = "godan conjugation"}); table.insert(categories, "Japanese type 1 verbs") end
if conjugation == "2" then table.insert(inflections, {label = "ichidan conjugation"}); table.insert(categories, "Japanese type 2 verbs") end
if conjugation == "3" then
-- hidden temporary maintenance category
-- (suru verbs should use ja-verb-suru but sometime erroneously use ja-verb with type=3 instead)
table.insert(inflections, {label = "irregular conjugation"}); table.insert(categories, "Japanese type 3 verbs")
if mw.ustring.match(PAGENAME,'する$') then table.insert(categories, "Japanese terms using ja-verb with type 3") end
end
end
-- >> maintenance category <<
-- check if this ends in something other than acceptable kana in a modern verb
if not mw.ustring.match(PAGENAME, '$') then table.insert(categories, "Japanese verbs without modern conjugations") end
end
pos_functions = function(args, inflections, categories)
table.insert(categories, "Japanese verbs")
table.insert(categories, "Japanese auxiliary verbs")
end
pos_functions = function(args, inflections, categories)
table.insert(categories, "Japanese verbs")
table.insert(categories, "Japanese type 3 verbs")
-- transitivity
local tr = args or ""
tr = aliases or tr
if tr ~= "" then
if tr == "tr" then table.insert(inflections, {label = "transitive"}) end
if tr == "in" then table.insert(inflections, {label = "intransitive"}) end
if tr == "both" then table.insert(inflections, {label = "transitive and intransitive"}) end
else
table.insert(categories, "Japanese verbs without transitivity")
end
end
pos_functions = function(args, inflections, categories)
table.insert(categories, "Japanese adjectives")
-- categorize by inflection type
local infl = args or ""
local decl = args or ""
if infl == "" then infl = decl end
if infl ~= "" then
if infl == "i" or infl == "い" then table.insert(inflections, {label = "-i inflection"}); table.insert(categories, "Japanese い-i adjectives") end
if infl == "na" or infl == "な" then table.insert(inflections, {label = "-na inflection"}); table.insert(categories, "Japanese な-na adjectives") end
if infl == "nari" or infl == "なり" then table.insert(inflections, {label = "-nari inflection"}); table.insert(categories, "Japanese なり-nari adjectives") end
if infl == "tari" or infl == "たり" then table.insert(inflections, {label = "-tari inflection"}); table.insert(categories, "Japanese たり-tari adjectives") end
end
end
pos_functions = function(args, inflections, categories)
table.insert(categories, "Japanese nouns")
-- the counter (classifier) parameter, only relevant for nouns
local counter = args or ""
if counter ~= "" then
if counter == "-" then table.insert(inflections, {label = "uncountable"}) else table.insert(inflections, {label = "counter", counter}) end
end
end
-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
PAGENAME = mw.title.getCurrentTitle().text
local args = frame:getParent().args
local poscat = frame.args or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
local categories = {}
local katakana_category = {}
local inflections = {}
-- set to PAGENAME if left empty
local head = args or ""; if head == "" then head = PAGENAME end
-- if this is a suru verb append ]
if poscat == "suru verbs" then head = head .. " + ]" end
local kana = find_kana(args, PAGENAME)
-- the presence of kyūjitai param indicates that this is shinjitai kanji entry and vice versa
local kyu = args or ""; if kyu == "" then kyu = nil else table.insert(inflections, {label = "shinjitai kanji"}); table.insert(inflections, {label = "kyūjitai kanji", kyu}) end
local shin = args or ""; if shin ~= "" then table.insert(inflections, {label = "kyūjitai kanji"}); table.insert(inflections, {label = "shinjitai kanji", shin}) end
-- add certain "inflections" and categories for adjectives, verbs, or nouns
if pos_functions then
pos_functions(args, inflections, categories)
else
table.insert(categories, "Japanese " .. poscat)
end
-- sort out all the kanas and do the romanization business
find_inflections(args, categories, inflections, poscat, PAGENAME, kana)
-- categorize by joyo kanji and number of kanji
categorize_by_kanji(categories, PAGENAME)
-- generate "Japanese terms spelled with ... read as ..." for single-kanji terms
singlekanji_term(categories, PAGENAME)
-- add categories for terms with iteration marks (which are not kanji and hence are not categorized by ja-kanjitab)
extra_categorization(categories, PAGENAME, katakana_category)
-- will only use sortkey if sortkey is different from PAGENAME
-- when katakana in PAGENAME is converted to hiragana
sortkey = m_ja.jsort(kana)
if sortkey == m_ja.kata_to_hira(PAGENAME) then
return
require("Module:headword").full_headword(lang, nil, head, nil, nil, inflections, categories, nil) ..
require("Module:utilities").format_categories(katakana_category, lang)
else
-- convert sortkey to katakana version for katakana terms category (should sort by katakana)
return
require("Module:headword").full_headword(lang, nil, head, nil, nil, inflections, categories, sortkey) ..
require("Module:utilities").format_categories(katakana_category, lang, m_ja.hira_to_kata(sortkey))
end
end
return export