--Based on ] by Rex Aurorum (see module history for attribution).
--Rewritten and improved by TagaSanPedroAko
local export = {}
local pron_utilities_module = "Module:pron utilities"
local lang = require("Module:languages").getByCode("ms")
local u = mw.ustring.char
local rfind = mw.ustring.find
local rsubn = mw.ustring.gsub
local rsplit = mw.text.split
local ulower = mw.ustring.lower
local AC = u(0x0301) -- acute = ́
local CFLEX = u(0x0302) -- circumflex = ̂
local GR = u(0x0300) -- grave = ̀
local TILDE = u(0x0303) -- tilde = ̃
local MAC = u(0x0304) -- macron = ̄
local BR = u(0x0306) -- breve = ˘
local DIA = u(0x0308) -- diaeresis = ̈
local BCFLEX = u(0x032D) -- circumflex below = ̭
local vowel = "aeéèëẽiouəɛâêîôûḙ" -- vowel
local V = ""
local W = "" -- glide
local accent = AC .. MAC .. BR
local accent_c = ""
local stress_c = ""
local ipa_stress = "ˈ"
local ipa_stress_c = ""
local separator = "# ."
local separator_c = ""
local C = "" -- consonant
local unstressed_words = require("Module:table").listToSet({ --feel free to add more unstressed words
"di", "ké", -- prepositions
"dan", -- conjunctions
"dia", "-ku", "-mu", "-nya", "-nye", -- pronouns. "dia" should be respelled to "di.a" for Bahasa Baku pronunciation. "nye" is for Johor pronunciation.
})
-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
local retval = rsubn(term, foo, bar)
return retval
end
-- version of rsubn() that returns a 2nd argument boolean indicating whether
-- a substitution was made.
local function rsubb(term, foo, bar)
local retval, nsubs = rsubn(term, foo, bar)
return retval, nsubs > 0
end
-- apply rsub() repeatedly until no change
local function rsub_repeatedly(term, foo, bar)
while true do
local new_term = rsub(term, foo, bar)
if new_term == term then
return term
end
term = new_term
end
end
-- ĵ, ɟ and ć are used internally to represent , and
--
function export.IPA(text, phonetic)
local debug = {}
text = ulower(text or mw.title.getCurrentTitle().text)
-- decompose everything but é and circumflexed letters (used in Johor-Riau pronunciations to indicate length due to elision of final R)
text = mw.ustring.toNFD(text)
text = rsub(text, "." .. "", {
= "ḱ",
= "é", -- e taling
= "è", -- used for long /e/
= "ë", -- pseudo-letter for Kelantan
= "ẽ", -- pseudo-letter for Kelantan reflex of /ia/
= "â", -- use for Kelantan reflex of standard /a/ + nasal
= "ê",
= "î",
= "ô",
= "û",
= "ḙ",
})
-- convert commas and en/en dashes to IPA foot boundaries
text = rsub(text, "%s*%s*", " | ")
-- question mark or exclamation point in the middle of a sentence -> IPA foot boundary
text = rsub(text, "()%s*%s*()", "%1 | %2")
-- canonicalize multiple spaces and remove leading and trailing spaces
local function canon_spaces(text)
text = rsub(text, "%s+", " ")
text = rsub(text, "^ ", "")
text = rsub(text, " $", "")
return text
end
text = canon_spaces(text)
-- Make prefixes unstressed unless they have an explicit stress marker; also make certain
-- monosyllabic words (e.g. ], ], ], etc.) without stress marks be
-- unstressed.
local words = rsplit(text, " ")
for i, word in ipairs(words) do
if rfind(word, "%-$") and not rfind(word, accent_c) or unstressed_words then
-- add BR to the last vowel not the first one
-- adding the BR after the 'u'
words = rsub(word, "^(.*" .. V .. ")", "%1" .. BR)
end
end
text = table.concat(words, " ")
-- Convert hyphens to spaces
text = rsub(text, "%-", " ")
-- canonicalize multiple spaces again, which may have been introduced by hyphens
text = canon_spaces(text)
-- now eliminate punctuation
text = rsub(text, "", "")
-- put # at word beginning and end and double ## at text/foot boundary beginning/end
text = rsub(text, " | ", "# | #")
text = "##" .. rsub(text, " ", "# #") .. "##"
table.insert(debug, text)
--"i" or "u" to glide (in between vowels or is part of a diphthong)
text = rsub(text, "(" .. V ..")i()", "%1ɟ%2")
text = rsub(text, "("..V..")u()", "%1w%2")
-- syllable-initial X (e.g. in ], ], ])
text = rsub(text, "x("..V..")", "z%1")
-- handle certain combinations; gh, kh, ng, ny and sy handling needs to go first
text = rsub(text, "gh", "ɣ")
text = rsub(text, "kh", "x")
text = rsub(text, "ng", "ŋ")
text = rsub(text, "ny", "ɲ")
text = rsub(text, "sy", "ʃ")
table.insert(debug, text)
--alphabet-to-phoneme
text = rsub(text, "",
--="ɡ": U+0067 LATIN SMALL LETTER G → U+0261 LATIN SMALL LETTER SCRIPT G
{ = "ć", = "ə", = "e", = "ɛ", = "ɡ", = "ĵ", = "j" })
-- glottal stop.
text = rsub(text, "7", "ʔ")
table.insert(debug, text)
--syllable division
local vowel_to_glide = { = "j", = "w" }
-- i, o and u between vowels -> j and e.g. ])
text = rsub_repeatedly(text, "(" .. V .. "*)()(" .. V .. ")",
function(v1, iu, v2)
return v1 .. vowel_to_glide .. v2
end
)
text = rsub_repeatedly(text, "(" .. V .. accent_c .."*)(" .. C .. W .. "?" .. V .. ")", "%1.%2")
text = rsub_repeatedly(text, "(" .. V .. accent_c .."*" .. C .. ")(" .. C .. V .. ")", "%1.%2")
text = rsub_repeatedly(text, "(" .. V .. accent_c .."*" .. C .. "+)(" .. C .. C .. V .. ")", "%1.%2")
text = rsub_repeatedly(text, "(" .. C .. ")%.s(" .. C .. ")", "%1s.%2")
text = rsub_repeatedly(text, "(" .. accent_c .. "*)()", "%1.%2")
table.insert(debug, text)
--diphthongs
text = rsub(text, "i()", "j%1")
text = rsub(text, "u()", "w%1")
table.insert(debug, text)
local accent_to_stress_mark = { = "ˈ", = "" }
local function accent_word(word, syllables)
-- Now stress the word. If any accent exists in the word (including macron indicating an unaccented word),
-- put the stress mark(s) at the beginning of the indicated syllable(s). Otherwise, apply the default
-- stress rule.
if rfind(word, accent_c) then
for i = 1, #syllables do
syllables = rsub(syllables, "^(.*)(" .. accent_c .. ")(.*)$",
function(pre, accent, post)
return accent_to_stress_mark .. pre .. post
end
)
end
else
-- Default stress rule. Words without vowels (e.g. IPA foot boundaries) don't get stress.
if #syllables == 1 and rfind(word, V) then
syllables = "ˈ" ..syllables -- Monosyllabic words wity schwa don't get stress
elseif #syllables == 2 and rfind(word, "()(".. C .."?)()") then
syllables = "ˈ" .. syllables
elseif #syllables == 2 and rfind(word, "()(".. C .."?)()") then
syllables = "ˈ" .. syllables
elseif #syllables >= 3 and rfind(word, "()(".. C .."?)()(".. C .."?)()(".. C .. C .."?)(".. V .."?)(".. C .."?)()") then
syllables = "ˈ" .. syllables
elseif #syllables > 1 then
syllables = "ˈ" .. syllables
end
end
end
local words = rsplit(text, " ")
for j, word in ipairs(words) do
local syllables = rsplit(word, "%.")
accent_word(word, syllables)
-- Reconstruct the word.
words = table.concat(syllables, phonetic and "." or "")
end
text = table.concat(words, " ")
-- suppress syllable mark before IPA stress indicator
text = rsub(text, "%.(" .. ipa_stress_c .. ")", "%1")
table.insert(debug,text)
--vowels with circumflex to vowel+(r)
text = rsub(text,"â","a(r)")
text = rsub(text,"ê","ə(r)")
text = rsub(text,"ḙ","e(r)")
text = rsub(text,"î","i(r)")
text = rsub(text,"ô","o(r)")
text = rsub(text,"û","u(r)")
table.insert(debug, text)
--phonetic transcription
if phonetic then
table.insert(debug, text)
--phonemic diphthongs
text = rsub(text, "()()()", "%1i̯%3")
text = rsub(text, "()w()", "%1u̯%2")
table.insert(debug, text)
--devoice B and D as coda
text = rsub(text, "b()","p%1")
text = rsub(text, "d()","t%1")
--/n/ sandhi
text = rsub(text,"()(*)", "m%2")
text = rsub(text,"()(*)","ɲ%2")
text = rsub(text,"()(*)", "n̪%2")
--final K to glottal stop (native) or unreleased stop (loanword)
text = rsub(text, "k()","ʔ%1")
text = rsub(text, "ḱ()","k̚%1")
--dental T
text = rsub(text, "t","t̪")
mw.log(text)
end
table.insert(debug, text)
-- convert fake symbols to real ones
local final_conversions = {
= "t͡ʃ", -- fake "c" to real "c"
= "d͡ʒ", -- fake "j" to real "j"
= "j" -- fake "y" to real "y"
}
local final_conversions_phonetic = {
= "t͡ʃ", -- fake "c" to real "c"
= "d͡ʒ" -- fake "j" to real "j"
}
if phonetic then
text = rsub(text, "p()","p̚%1")
text = rsub(text, "t̪()","t̪̚%1")
text = rsub(text, "", final_conversions_phonetic)
end
text = rsub(text, "", final_conversions)
if not phonetic then
text = rsub(text, "ḱ","k")
text = rsub(text, "", "")
end
-- remove # symbols at word and text boundaries
text = rsub(text, "#", "")
return mw.ustring.toNFC(text)
end
local function respelling_to_IPA(data)
return ("/%s/ "):format(export.IPA(data.respelling, false), export.IPA(data.respelling, true))
end
function export.show(frame)
local parent_args = frame:getParent().args
return require(pron_utilities_module).format_prons {
lang = lang,
respelling_to_IPA = respelling_to_IPA,
raw_args = parent_args,
track_module = "ms-pron",
include_bullet = true, -- FIXME: temporary
}
end
return export