Module:hi-translit
Appearance
Documentation for this module may be created at Module:hi-translit/doc
-- Transliteration for Hindi (possibly other languages using Devanagari script, except for Sanskrit)
local export = {}
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local conv = {
-- consonants
['क'] = 'க', ['ख'] = 'க²', ['ग'] = 'க³', ['घ'] = 'க⁴', ['ङ'] = 'ங',
['च'] = 'ச', ['छ'] = 'ச²', ['ज'] = 'ஜ', ['झ'] = 'ஜ²', ['ञ'] = 'ஞ',
['ट'] = 'ட', ['ठ'] = 'ட²', ['ड'] = 'ட³', ['ढ'] = 'ட⁴', ['ण'] = 'ண',
['त'] = 'த', ['थ'] = 'த²', ['द'] = 'த³', ['ध'] = 'த⁴', ['न'] = 'ந',
['प'] = 'ப', ['फ'] = 'ப²', ['ब'] = 'ப³', ['भ'] = 'ப⁴', ['म'] = 'ம',
['य'] = 'ய', ['र'] = 'ர', ['ल'] = 'ல', ['व'] = 'வ', ['ळ'] = 'ள',
['श'] = 'ஶ', ['ष'] = 'ஷ', ['स'] = 'ஸ', ['ह'] = 'ஹ',
['क़'] = 'ஃʼக', ['ख़'] = 'ஃக²', ['ग़'] = 'ஃக³', ['ऴ'] = 'ழ',
['ज़'] = 'ஃஜ', ['ष़'] = 'ஷ', ['झ़'] = 'ஜ²', ['ड़'] = 'ஃட²', ['ढ़'] = 'ஃட³',
['फ़'] = 'ஃப', ['थ़'] = 'θ', ['ऩ'] = 'ன', ['ऱ'] = 'ற',
-- ['ज्ञ'] = 'ஜ்ஞ',
-- vowel diacritics
['ि'] = 'ி', ['ु'] = 'ு', ['े'] = 'ெ', ['ो'] = 'ொ',
['ॊ'] = 'ோ', ['ॆ'] = 'ெ',
['ा'] = 'ா', ['ी'] = 'ீ', ['ू'] = 'ூ',
['ृ'] = '◌்ருʼ',
['ै'] = 'ை', ['ौ'] = 'ௌ',
['ॉ'] = 'ாʼ',
['ॅ'] = 'ெʼ',
-- vowel signs
['अ'] = 'அ', ['इ'] = 'இ', ['उ'] = 'உ', ['ए'] = 'ஏ', ['ओ'] = 'ஓ',
['आ'] = 'ஆ', ['ई'] = 'ஈ', ['ऊ'] = 'ஊ', ['ऎ'] = 'ஐ', ['ऒ'] = 'ஒ',
['ऋ'] = 'ருʼ',
['ऐ'] = 'ஐ', ['औ'] = 'ஔ',
['ऑ'] = 'ஆʼ',
['ऍ'] = 'எʼ',
['ॐ'] = 'ஒம்',
-- chandrabindu
['ँ'] = 'ம்',
-- anusvara
['ं'] = 'ம்ʼ',
-- visarga
['ः'] = 'ஃ',
-- virama
['्'] = '்',
-- numerals
['०'] = '0', ['१'] = '1', ['२'] = '2', ['३'] = '3', ['४'] = '4',
['५'] = '5', ['६'] = '6', ['७'] = '7', ['८'] = '8', ['९'] = '9',
-- punctuation
['।'] = '.', -- danda
['॥'] = '.', -- double danda
['+'] = '', -- compound separator
-- abbreviation sign
['॰'] = '.',
}
local nasal_assim = {
['क'] = 'ङ', ['ख'] = 'ङ', ['ग'] = 'ङ', ['घ'] = 'ङ',
['च'] = 'ञ', ['छ'] = 'ञ', ['ज'] = 'ञ', ['झ'] = 'ञ',
['ट'] = 'ण', ['ठ'] = 'ण', ['ड'] = 'ण', ['ढ'] = 'ण',
['प'] = 'म', ['फ'] = 'म', ['ब'] = 'म', ['भ'] = 'म', ['म'] = 'म',
['व'] = 'ँ', ['य'] = 'ँ', ['ष'] = 'न', ['श'] = 'न', ['स'] = 'न'
}
local perm_cl = {
['म्ल'] = true, ['व्ल'] = true, ['न्ल'] = true,
}
local all_cons, special_cons = 'कखगघङचछजझञटठडढतथदधपफबभशषसयरलवहणनम', 'यरलवहनम'
local vowel, vowel_sign = 'aिुृेोाीूैौॉॅॆॊ', 'अइउएओआईऊऋऐऔऑऍ'
local syncope_pattern = '([' .. vowel .. vowel_sign .. '])(़?[' .. all_cons .. '])a(़?[' .. gsub(all_cons, "य", "") .. '])([ंँ]?[' .. vowel .. vowel_sign .. '])'
local function rev_string(text)
local result, length = {}, mw.ustring.len(text)
for i = length, 1, -1 do
table.insert(result, mw.ustring.sub(text, i, i))
end
return table.concat(result)
end
function export.tr(text, lang, sc)
text = gsub(text, '([' .. all_cons .. ']़?)([' .. vowel .. '्]?)', function(c, d)
return c .. (d == "" and 'a' or d) end)
for word in mw.ustring.gmatch(text, "[ऀ-ॿa]+") do
local orig_word = word
word = rev_string(word)
word = gsub(word, '^a(़?)([' .. all_cons .. '])(.)(.?)', function(opt, first, second, third)
return (((match(first, '[' .. special_cons .. ']') and match(second, '्') and not perm_cl[first..second..third])
or match(first .. second, 'य[ीेै]'))
and 'a' or "") .. opt .. first .. second .. third end)
while match(word, syncope_pattern) do
word = gsub(word, syncope_pattern, '%1%2%3%4')
end
word = gsub(word, '(.?)ं(.)', function(succ, prev)
return succ .. (succ..prev == "a" and "्म" or
(succ == "" and match(prev, '[' .. vowel .. ']') and "̃" or nasal_assim[succ] or "n")) .. prev end)
text = gsub(text, orig_word, rev_string(word))
end
text = gsub(text, '.़?', conv)
text = gsub(text, 'a([iu])̃', 'a͠%1')
text = gsub(text, 'jñ', 'gy')
text = gsub(text, 'ñz', 'nz')
return mw.ustring.toNFC(text)
end
return export