Difference between revisions of "Module:Kana"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (convert full-width spaces to half-width) |
m (5 revisions imported) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 26: | Line 26: | ||
local function cleanUpRomaji(s) | local function cleanUpRomaji(s) | ||
-- Format long vowels the Hepburn way | -- Format long vowels the Hepburn way | ||
| + | s = s:gsub('aa', 'ā') | ||
s = s:gsub('o[ou]', 'ō') | s = s:gsub('o[ou]', 'ō') | ||
s = s:gsub('uu', 'ū') | s = s:gsub('uu', 'ū') | ||
| Line 32: | Line 33: | ||
-- Fix small tsu before consonants that aren't "t" | -- Fix small tsu before consonants that aren't "t" | ||
s = s:gsub('t([kgsjdfhbpmnry])', '%1%1') | s = s:gsub('t([kgsjdfhbpmnry])', '%1%1') | ||
| + | s = s:gsub('ssu', 'tsu') -- Special case for "tsu" which became "ssu" on the line above | ||
-- Convert full-width spaces to half-width | -- Convert full-width spaces to half-width | ||
s = s:gsub(' ', ' ') | s = s:gsub(' ', ' ') | ||
| − | + | ||
return s | return s | ||
end | end | ||
Latest revision as of 05:49, 27 September 2020
-- This module converts between Japanese hiragana and katakana
local kanaData = mw.loadData('Module:Kana/data')
local p = {}
local function makeSingleCharConversionTable(inputKey, outputKey)
local ret = {}
for i, singleCharData in ipairs(kanaData.singleChars) do
ret[singleCharData[inputKey]] = singleCharData[outputKey]
end
return ret
end
local function convertString(s, inputKey, outputKey)
-- Convert double chars
for i, doubleCharData in ipairs(kanaData.doubleChars) do
s = mw.ustring.gsub(s, doubleCharData[inputKey], doubleCharData[outputKey])
end
-- Convert single chars
s = mw.ustring.gsub(s, '.', makeSingleCharConversionTable(inputKey, outputKey))
return s
end
local function cleanUpRomaji(s)
-- Format long vowels the Hepburn way
s = s:gsub('aa', 'ā')
s = s:gsub('o[ou]', 'ō')
s = s:gsub('uu', 'ū')
s = s:gsub('ii', 'ī')
-- Fix small tsu before consonants that aren't "t"
s = s:gsub('t([kgsjdfhbpmnry])', '%1%1')
s = s:gsub('ssu', 'tsu') -- Special case for "tsu" which became "ssu" on the line above
-- Convert full-width spaces to half-width
s = s:gsub(' ', ' ')
return s
end
function p._htok(s)
return convertString(s, 'hiragana', 'katakana')
end
function p._ktoh(s)
return convertString(s, 'katakana', 'hiragana')
end
function p._htor(s)
s = convertString(s, 'hiragana', 'romaji')
return cleanUpRomaji(s)
end
function p._ktor(s)
s = convertString(s, 'katakana', 'romaji')
return cleanUpRomaji(s)
end
return setmetatable(p, {
__index = function (t, key)
return function (frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Kana',
})
return p['_' .. key](args[1])
end
end
})