local export = {}
local m_str_utils = require("Module:string utilities")
local gmatch = m_str_utils.gmatch
local gsub = m_str_utils.gsub
local match = m_str_utils.match
local plain_gsub = m_str_utils.plain_gsub
local reverse = m_str_utils.reverse
local toNFC = mw.ustring.toNFC
local conv = {
-- consonants
['क'] = 'k', ['ख'] = 'kh', ['ग'] = 'g', ['घ'] = 'gh', ['ङ'] = 'ṅ',
['च'] = 'c', ['छ'] = 'ch', ['ज'] = 'j', ['झ'] = 'jh', ['ञ'] = 'ñ',
['ट'] = 'ṭ', ['ठ'] = 'ṭh', ['ड'] = 'ḍ', ['ढ'] = 'ḍh', ['ण'] = 'ṇ',
['त'] = 't', ['थ'] = 'th', ['द'] = 'd', ['ध'] = 'dh', ['न'] = 'n',
['प'] = 'p', ['फ'] = 'ph', ['ब'] = 'b', ['भ'] = 'bh', ['म'] = 'm',
['य'] = 'y', ['र'] = 'r', ['ल'] = 'l', ['व'] = 'v', ['ळ'] = 'ḷ',
['श'] = 'ś', ['ष'] = 'ṣ', ['स'] = 's', ['ह'] = 'h',
['क़'] = 'q', ['ख़'] = 'x', ['ग़'] = 'ġ', ['ऴ'] = 'ḻ',
['ज़'] = 'z', ['ष़'] = 'ẓ', ['झ़'] = 'ź', ['ड़'] = 'ṛ', ['ढ़'] = 'ṛh',
['फ़'] = 'f', ['ऩ'] = 'ṉ', ['ऱ'] = 'ṟ', ['य़'] = 'ẏ', ['व़'] = 'w',
['ज्ञ'] = 'jñ', -- commonly used in Marathi
-- vowel diacritics
['ि'] = 'i', ['ु'] = 'u', ['े'] = 'e', ['ो'] = 'o',
['ॊ'] = 'ǒ', ['ॆ'] = 'ĕ', -- uncommon but added for completeness
['ा'] = 'ā', ['ी'] = 'ī', ['ू'] = 'ū',
['ृ'] = 'ṛ',
['ै'] = 'ai', ['ौ'] = 'au',
['ॉ'] = 'ŏ',
['ॅ'] = 'ĕ',
-- vowel signs
['अ'] = 'a', ['इ'] = 'i', ['उ'] = 'u', ['ए'] = 'e', ['ओ'] = 'o',
['आ'] = 'ā', ['ई'] = 'ī', ['ऊ'] = 'ū', ['ऎ'] = 'ĕ', ['ऒ'] = 'ǒ',
['ऋ'] = 'ṛ',
['ऐ'] = 'ai', ['औ'] = 'au',
['ऑ'] = 'ŏ',
['ऍ'] = 'ĕ',
['ॐ'] = 'om',
-- 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_short = {
['क'] = 'ङ', ['ख'] = 'ङ', ['ग'] = 'ङ', ['घ'] = 'ङ', ['ङ'] = 'ङ',
['च'] = 'ञ', ['छ'] = 'ञ', ['ज'] = 'ञ', ['झ'] = 'ञ', ['ञ'] = 'ञ',
['ट'] = 'ण', ['ठ'] = 'ण', ['ड'] = 'ण', ['ढ'] = 'ण', ['ण'] = 'ण',
['त'] = 'न', ['थ'] = 'न', ['द'] = 'न', ['ध'] = 'न', ['न'] = 'न',
['प'] = 'म', ['फ'] = 'म', ['ब'] = 'म', ['भ'] = 'म', ['म'] = 'म',
['य'] = 'ञ', ['र'] = 'न', ['ल'] = 'न', ['व'] = 'म',
['श'] = 'ञ', ['ष'] = 'ण', ['स'] = 'न', ['ह'] = 'ँ',
['ज़'] = 'न', ['फ़'] = 'म', ['क़'] = 'ङ', ['ख़'] = 'ङ', ['ग़'] = 'ङ',
['ड़'] = 'ँ', ['ढ़'] = 'ँ'
}
local nasal_assim_long = {
['क'] = 'ँ', ['ख'] = 'ँ', ['ग'] = 'ङ', ['घ'] = 'ङ', ['ङ'] = 'ँ',
['च'] = 'ँ', ['छ'] = 'ँ', ['ज'] = 'ञ', ['झ'] = 'ञ', ['ञ'] = 'ँ',
['ट'] = 'ँ', ['ठ'] = 'ँ', ['ड'] = 'ण', ['ढ'] = 'ण', ['ण'] = 'ँ',
['त'] = 'ँ', ['थ'] = 'ँ', ['द'] = 'न', ['ध'] = 'न', ['न'] = 'ँ',
['प'] = 'ँ', ['फ'] = 'ँ', ['ब'] = 'म', ['भ'] = 'म', ['म'] = 'ँ',
['ह'] = 'ँ',
['ज़'] = 'न', ['फ़'] = 'म', ['क़'] = 'ङ', ['ख़'] = 'ङ', ['ग़'] = 'ङ',
['ड़'] = 'ँ', ['ढ़'] = 'ँ'
}
local perm_cl = {
['म्ल'] = true, ['व्ल'] = true, ['न्ल'] = true,
['म्र'] = true, ['व्र'] = true, ['न्र'] = true, ['ण्र'] = true,
['न्न'] = true, ['म्म'] = true, ['ण्ण'] = true, ['ल्ल'] = true, ['र्र'] = true
}
local all_cons, special_cons = 'कखगघङचछजझञटठडढणतथदधनपफबभमयरलवषशसह', 'यरलवहनमञण'
local vowel, vowel_sign = '*aिुृेोाीूैौॉॅॆॊ\'', 'अइउएओआईऊऋऐऔऑऍ\''
local long_vowel, short_vowel = 'ाीूेैोौआईऊएऐओऔ', '*aिुृॆॊॅॉअइउऋऍऑऎऒ\''
local syncope_pattern = '([' .. vowel .. vowel_sign .. '])(़?[' .. all_cons .. '])a(़?[' .. all_cons .. '])([ंँ]?[' .. vowel .. vowel_sign .. '])'
function export.tr(text, lang, sc)
-- treat anusvara + nasal as geminate nasal after short vowels
text = gsub(text, '([' .. short_vowel .. all_cons .. '])ं([नम])', '%1%2्%2')
-- word-final apostrophe (e.g. from bold formatting) does not delete schwa
text = gsub(text, '([' .. all_cons .. ']़?)(\'%A)', '%1a%2')
text = gsub(text, '([' .. all_cons .. ']़?)(\')$', '%1a%2')
text = gsub(text, '([' .. all_cons .. ']़?)([' .. vowel .. '्]?)', function(c, d)
return c .. (d == "" and 'a' or d) end)
for word in gmatch(text, "[ऀ-ॣॱ-ॿa*]+") do
local orig_word = word
word = reverse(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 = reverse(word)
local special_vowel, normal_vowel = 'ीेैोौईऐओऔ', '*aिुाूृॆॅॊॉअइउआऊऋऎऍऒऑए\''
word = gsub(word, '([' .. special_vowel .. '])ँ(.़?)', function(prev, succ)
return prev .. "̃" .. succ
end)
word = gsub(word, '([' .. normal_vowel .. '])ं([सशषवयकखटतथदडपचछ]़?)', function(prev, succ)
return prev .. (nasal_assim_short[succ] or "̃") .. succ
end)
word = gsub(word, '([' .. normal_vowel .. '])ँ([सशषवयकखटतथदडपचछ]़?)', function(prev, succ)
return prev .. "̃" .. succ
end)
word = gsub(word, 'ँ', 'ं')
word = gsub(word, '([' .. short_vowel .. '])ं(.़?)', function(prev, succ)
return prev .. (nasal_assim_short[succ] or "̃") .. succ
end)
word = gsub(word, '([' .. long_vowel .. '])ं(.़?)', function(prev, succ)
return prev .. (nasal_assim_long[succ] or "̃") .. succ
end)
text = plain_gsub(text, orig_word, word)
end
text = gsub(text, '.़?', conv)
text = gsub(text, 'a([iu])̃', 'a͠%1')
text = gsub(text, 'ñz', 'nz')
text = gsub(text, '%*', 'a')
return toNFC(text)
end
return export