Module:ms-pron
Itsura
- This module lacks a documentation subpage. Please create it.
- Useful links: subpage list • links • transclusions • testcases • sandbox
--Based on [[module:id-pron]] by Rex Aurorum (see module history for attribution).
--Rewritten and improved by TagaSanPedroAko
local export = {}
local m_pron_utils = require("Module:pron utilities")
local m_table = require("Module:table")
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éèëẽioòuəɛâêîôûḙɛɔ" -- vowel
local V = "[" .. vowel .. "]"
local W = "[jw]" -- glide
local accent = AC .. MAC .. BR
local accent_c = "[" .. accent .. "]"
local stress_c = "[" .. MAC .. BR .. "]"
local ipa_stress = "ˈ"
local ipa_stress_c = "[" .. ipa_stress .. "]"
local separator = "# ."
local separator_c = "[" .. separator .. "]"
local C = "[^" .. vowel .. separator .. "]" -- consonant
local unstressed_words = m_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 [d͡ʒ], [j] and [t͡ʃ]
--
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, "." .. "[" .. AC .. CFLEX .. DIA .. GR .. TILDE .. BCFLEX .. "]", {
["k" .. AC] = "ḱ",
["e" .. AC] = "é", -- e taling
["e" .. GR] = "è", -- used for long /e/
["o" .. GR] = "ò",
["e" .. DIA] = "ë", -- pseudo-letter for Kelantan [ɛ]
["a" .. CFLEX] = "â",
["e" .. CFLEX] = "ê",
["i" .. CFLEX] = "î",
["o" .. CFLEX] = "ô",
["u" .. CFLEX] = "û",
["e" .. BCFLEX] = "ḙ",
["l" .. TILDE] = "ɫ",
})
text = mw.ustring.toNFC(text)
-- 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*[!?]%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. [[di]], [[ke]], [[se-]], 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[word] then
-- add BR to the last vowel not the first one
-- adding the BR after the 'u'
words[i] = 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 [[xenofobia]], [[xenon]], [[xilofon]])
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, "[ceéègjòy]",
--["g"]="ɡ": U+0067 LATIN SMALL LETTER G → U+0261 LATIN SMALL LETTER SCRIPT G
{ ["c"] = "ć", ["e"] = "ə", ["é"] = "e", ["è"] = "ɛ", ["g"] = "ɡ", ["j"] = "ĵ", ["ò"] = "ɔ", ["y"] = "j" })
-- glottal stop.
text = rsub(text, "7", "ʔ")
table.insert(debug, text)
--syllable division
local vowel_to_glide = { ["i"] = "j", ["u"] = "w" }
-- i, o and u between vowels -> j and e.g. [[rangkaian]])
text = rsub_repeatedly(text, "(" .. V .. "*)([iu])(" .. V .. ")",
function(v1, iu, v2)
return v1 .. vowel_to_glide[iu] .. 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, "([aeiou]" .. accent_c .. "*)([aeiou])", "%1.%2")
table.insert(debug, text)
--diphthongs
text = rsub(text, "i([aeiouâêîôû])", "j%1")
text = rsub(text, "u([aeiouâêîôû])", "w%1")
table.insert(debug, text)
local accent_to_stress_mark = { [MAC] = "ˈ", [BR] = "" }
local function accent_word(word, syllables)
-- Default stress rule. Words without vowels (e.g. IPA foot boundaries) don't get stress.
if #syllables == 1 then
if rfind(syllables[1], BR) then
syllables[1] = rsub(syllables[1], "^(.*)(".. BR ..")(.*)$", "%1%3")
elseif rfind(word, V) then
syllables[#syllables] = "ˈ" ..syllables[#syllables] -- Monosyllabic words wity schwa don't get stress
end
elseif #syllables == 2 then
if rfind(word, "([ə])(".. C .."?)([.])") then
syllables[#syllables] = "ˈ" .. syllables[#syllables]
else
syllables[#syllables - 1] = "ˈ" .. syllables[#syllables - 1]
end
elseif #syllables >= 3 then -- Can be millions of syllable
local i = #syllables
while i > 0 do
if i == #syllables then
i = i - 1
end
if i > 2 then
cur = syllables[i]
prev = syllables[i - 1]
next = syllables[i + 1]
if rfind(cur, "([ə])(".. C .."?)") == nil then
syllables[i] = "ˈ" .. cur
elseif rfind(next, "([ə])(".. C .."?)") == nil then
syllables[i + 1] = "ˈ" .. next
i = i + 1
elseif rfind(prev, "([ə])(".. C .."?)") == nil then
syllables[i - 1] = "ˈ" .. prev
i = i - 1
end
elseif i <= 2 and rfind(syllables[2], "([ə])(".. C .."?)") == nil and rfind(syllables[3], "ˈ") == nil then
syllables[2] = "ˈ" .. syllables[2]
elseif i <= 2 and rfind(syllables[1], "([əê])(".. C .."?)") == nil then
syllables[1] = rsub(syllables[1], "(#+)(".. C .."?)", "%1ˈ%2")
elseif i <= 2 and rfind(syllables[3], "([ˈə])(".. C .."?)") == nil then
syllables[3] = "ˈ" .. syllables[3]
else
if #syllables == 3 then
syllables[2] = "ˈ" .. syllables[2]
end
end
i = i - 2
end
if #syllables >= 4 then
if rfind(syllables[1], "ˈ") and rfind(syllables[3], "ˈ") == nil and rfind(syllables[4], "ˈ") == nil then
syllables[3] = "ˈ" .. syllables[3]
end
elseif #syllables <= 3 and rfind(syllables[1], "ˈ") and rfind(syllables[3], "[ˈə]") == nil then
syllables[3] = "ˈ" .. syllables[3]
end
end
end
local words = rsplit(text, " ")
for j, word in ipairs(words) do
local syllables = rsplit(word, "%.")
-- Apply stress marker
accent_word(word, syllables)
-- Reconstruct the word.
words[j] = table.concat(syllables, phonetic and "." or "")
end
text = table.concat(words, " ")
text = rsub_repeatedly(text, "ˈ(.+)ˈ", "ˌ%1ˈ")
-- suppress syllable mark before IPA stress indicator
text = rsub(text, "%.([ˌˈ])", "%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, "([aeou])([ɟj])([#.ˈˌ])", "%1i̯%3")
text = rsub(text, "([a])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,"([n])([ˈˌ# .]*[bpm])", "m%2")
text = rsub(text,"([n])([ˈˌ# .]*[ćĵʃ])","ɲ%2")
text = rsub(text,"([n])([ˈˌ# .]*[t])", "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/ [%s]"):format(export.IPA(data.respelling, false), export.IPA(data.respelling, true))
end
function export.show(frame)
local parent_args = frame:getParent().args
return m_pron_utils.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