A modult a Modul:User:Erutuon/ja/doc lapon tudod dokumentálni
local export = {}
local find = mw.ustring.find
local replace = mw.ustring.gsub
local gmatch = mw.ustring.gmatch
local gsplit = mw.text.gsplit
local kataPattern = require("Module:scripts").getByCode("Kana"):getCharacters()
local hiraPattern = require("Module:scripts").getByCode("Hira"):getCharacters()
local kanaPattern = kataPattern .. hiraPattern
local namespace = mw.title.getCurrentTitle().nsText
local hiraToVowel = {
= "u",
= "e",
= "a",
= "a",
= "i",
= "a",
= "u",
= "i",
= "e",
= "a",
= "u",
= "u",
= "u",
= "i",
= "e",
= "o",
= "a",
= "a",
= "i",
= "o",
= "a",
= "a",
= "e",
= "e",
= "o",
= "o",
= "a",
= "e",
= "i",
= "i",
= "u",
= "o",
= "e",
= "u",
= "e",
= "u",
= "i",
= "a",
= "e",
= "a",
= "i",
= "o",
= "o",
= "o",
= "a",
= "u",
= "u",
= "a",
= "u",
= "e",
= "a",
= "o",
= "a",
= "o",
= "u",
= "i",
= "u",
= "o",
= "e",
= "a",
= "o",
= "u",
= "o",
= "e",
= "o",
= "a",
= "u",
= "e",
= "e",
= "e",
= "e",
= "i",
= "o",
= "e",
= "a",
= "i",
= "u",
= "o",
= "e",
= "o",
= "i",
= "e",
= "e",
= "o",
= "i",
= "u",
= "e",
= "o",
= "u",
= "a",
= "e",
= "a",
= "i",
= "o",
= "u",
= "u",
= "o",
= "a",
= "i",
= "o",
= "u",
= "u",
= "a",
= "u",
= "o",
= "e",
= "u",
= "a",
= "e",
= "o",
= "i",
= "o",
= "i",
= "a",
= "i",
= "o",
= "u",
= "e",
= "o",
= "a",
= "u",
= "a",
= "a",
= "u",
= "a",
= "i",
= "u",
= "o",
= "e",
= "e",
= "o",
= "i",
= "u",
= "a",
= "e",
= "o",
= "u",
= "u",
= "e",
= "o",
= "o",
= "i",
= "u",
= "i",
= "e",
= "o",
= "i",
= "o",
= "i",
= "a",
= "a",
= "a",
= "i",
= "u",
= "a",
= "u",
= "e",
= "a",
= "i",
= "a",
= "o",
= "e",
= "e",
= "u",
= "e",
= "u",
= "i",
= "u",
= "o",
= "i",
= "e",
= "u",
= "e",
= "a",
= "a",
= "a",
= "a",
= "o",
}
local function ifNotEmpty(var)
if var == "" then
return nil
else
return var
end
end
local function logOrThrowError(message)
if namespace == "Module" or namespace == "User" then
error(message)
else
mw.log(message)
end
end
local function getNonKana(kana)
-- Remove non-word characters.
local kana = replace(kana, "%W", "")
-- Remove kana.
local nonKana = replace(kana, "+", "")
return nonKana
end
local function isKana(kana)
return mw.ustring.len(getNonKana(kana)) == 0
end
local function addAtIndex(list, index, item)
if list then
list = list .. item
else
list = item
end
end
local function formsLongVowel(kana1, kana2)
if not (kana1 and kana2) then
return nil
end
local vowel = hiraToVowel
local isLengthenerFor = {
= { = true, },
= { = true, = true, },
= { = true, = true, },
= { = true, },
= { = true, },
}
if vowel and isLengthenerFor then
return true
else
return false
end
end
local function tag(hira)
if type(hira) == "string" then
return '<span class="Hira">' .. hira .. '</span>'
end
end
local function makeRow(list, length)
local row = {}
if type(list) ~= "table" then
mw.log("first argument to makeRow isn't table")
return nil
end
if type(length) == "number" then
for i = #list, length - 1 do
table.insert(list, "")
end
end
for i, item in pairs(list) do
table.insert(row, "| " .. tag(item))
end
return table.concat(row, "\n")
end
function export.getUnits(kana)
local wordChars
if not isKana(kana) then
logOrThrowError("Argument 1 to getUnits, " .. kana .. ", contains the non-kana word characters " .. getNonKana(kana) .. ".")
return nil
end
if find(kana, "") then
kana = require("Module:ja").kata_to_hira(kana)
end
local isSyllabic = {
= true,
= true,
}
local isMoraic = {
= true,
= true,
= true,
}
local units = {}
local index = 0
for char in gmatch(kana, ".") do
local addToLast = isMoraic or isSyllabic
if addToLast and index == 0 then
require("Module:debug").track("ruby/kana beginning with syllabic or moraic char")
logOrThrowError("The symbol " .. char .. " cannot occur at the beginning of a string of kanji: " .. kana .. ".")
return nil
end
if not addToLast then
index = index + 1
end
addAtIndex(units, index, char)
end
return units
end
function export.divideKana(kanji, kana)
if not (kanji and kana and type(kanji) == "string" and type(kana) == "string") then
mw.log("divideKana received improper arguments.")
return nil
end
if not isKana(kana) then
logOrThrowError("Argument 1 to getUnits, " .. kana .. ", contains the non-kana word characters " .. getNonKana(kana) .. ".")
return nil
end
kana = replace(kana, "", "")
local kanjiTable = require("Module:string").matchToArray(kanji, ".")
local kanaUnits = export.getUnits(kana)
if not kanaUnits then
return nil
end
local str = kanji .. ", " .. kana
if #kanaUnits == #kanjiTable then
-- mw.log("Success with " .. str .. ".")
else
-- mw.log("Initial failure with " .. str .. ".")
if #kanaUnits > #kanjiTable then
local i = 1
while i <= #kanaUnits do
-- mw.log(kanaUnits, kanaUnits, formsLongVowel(kanaUnits, kanaUnits))
if formsLongVowel(kanaUnits, kanaUnits) then
addAtIndex(kanaUnits, i, kanaUnits)
table.remove(kanaUnits, i + 1)
end
i = i + 1
if #kanaUnits == #kanjiTable then
-- mw.log("Eventual success with " .. str .. ".")
break
end
end
end
end
return kanjiTable, kanaUnits
end
function export.printUnits(frame)
local params = {
= { list = true },
}
local args = require("Module:parameters").process(frame.args, params)
local listOfUnits = {}
for i, kana in pairs(args) do
local units = export.getUnits(kana)
table.insert(listOfUnits, units)
end
local output = {}
for i, units in pairs(listOfUnits) do
local printout = table.concat(units, "、")
table.insert(output, "* " .. tag(printout) .. "<br>" .. require("Module:ja").kana_to_romaji(args))
end
return table.concat(output, "\n")
end
function export.showKanaDivision(frame)
local params = {
= { list = true },
}
local args = require("Module:parameters").process(frame.args, params)
local tableWidth = 0
local kanjiWords, kanaWords = {}, {}
for i, arg in pairs(args) do
local kanjiWord, kanaWord = unpack(mw.text.split(arg, ":"))
kanjiWord, kanaWord = export.divideKana(kanjiWord, kanaWord)
if tableWidth < #kanjiWord then
tableWidth = #kanjiWord
end
if tableWidth < #kanaWord then
tableWidth = #kanaWord
end
table.insert(kanjiWords, kanjiWord)
table.insert(kanaWords, kanaWord)
end
local output = { '{| class="wikitable"\n' }
local rows = {}
for i, kanaWord in ipairs(kanaWords) do
table.insert(rows, makeRow(kanjiWords, tableWidth))
table.insert(rows, makeRow(kanaWord, tableWidth))
end
rows = table.concat(rows, "\n|-\n")
table.insert(output, rows .. "\n|}")
output = table.concat(output)
return output
end
-- Originally from ]
function export.r(frame)
local one = frame.args or ''
local two = frame.args or ''
local three = frame.args or ''
local four = frame.args or ''
local jp = ''
local tr = ''
local gloss = frame.args or ''
local choice = ''
if find(one, '') then
choice = one
jp = two
linktitle = three
gloss = (gloss ~= '' and gloss or four)
elseif one == 'ja' then
choice = ''
jp = two
linktitle = three
gloss = (gloss ~= '' and gloss or four)
else
choice = ''
jp = one
linktitle = two
gloss = (gloss ~= '' and gloss or three)
end
if mw.ustring.match(jp, ".%]+%]%]") then
error("Cannot process Japanese text with embedded wikilinks.")
end
local content = mw.title.new(jp):getContent()
if not content then
return "{{ja-l|" .. jp .. "}}"
end
local readings = {}
local function process(text)
text = replace(text, 'hhira=+', '')
text = replace(text, 'decl=+', '')
text = replace(text, 'infl=+', '')
text = replace(text, 'kyu=+', '')
text = replace(text, 'head=+', '')
text = replace(text, 'hira=', '')
if find(text, 'proper') then
text = '^' .. replace(text, '()', '%1^')
end
if find(content, 'infl=い') then
text = replace(text, 'しい', 'し.い')
end
if find(content, 'ja%-verb') then
text = replace(text, 'おう', 'お.う')
end
for parameter in gsplit(text, '|') do
if find(parameter, '') then
table.insert(readings, parameter)
end
end
end
for parameters in gmatch(content, '{{ja%-adj|(+)}}') do
process(parameters)
end
for parameters in gmatch(content, '{{ja%-noun|(+)}}') do
process(parameters)
end
for parameters in gmatch(content, '{{ja%-verb|(+)}}') do
process(parameters)
end
for parameters in gmatch(content, '{{ja%-verb%-suru|(+)}}') do
process(parameters)
end
for parameters in gmatch(content, '{{ja%-phrase|(+)}}') do
process(parameters)
end
for parameters in gmatch(content, '{{ja%-pos|(+)}}') do
process(parameters)
end
for parameters in gmatch(content, '{{ja%-altread|(+)}}') do
process(parameters)
end
readings = require("Module:table").removeDuplicates(readings)
if #readings > 1 then
if choice ~= '' then
tr = readings
else
return '{{ja-r|' .. jp .. '|ーーーーー}}\n' .. require("Module:debug").highlight_dump(readings)
end
else
tr = readings or ''
end
-- if term is pure kana and kana is identical
if replace(jp, '', '') == '' and tr == jp then
tr = ''
end
if gloss ~= '' then
gloss = ': ' .. gloss
end
if tr ~= '' then
tr = '|' .. tr
end
if linktitle ~= '' then
jp = 'linkto=' .. jp .. '|' .. linktitle
end
if tr ~= '' then
return '{{ja-r|' .. jp .. tr .. '}}' .. gloss
else
return '{{ja-l|' .. jp .. '}}' .. gloss
end
--[[
変換済みの言葉を再変換
・選択してスペースキーを押す
・選択してWin+Cを押す
]]
end
return export