မဝ်ဂျူ:mdh-pron
မံက်ပြာကတ်
Pronunciation module that generates IPA for Maguindanao. Powers Template:mdh-IPA, See there for usage.
-- Based on [[Module:es-pronunc]] by Benwing2.
local export = {}
local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("mdh")
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 GR = u(0x0300) -- grave = ̀
local CFLEX = u(0x0302) -- circumflex = ̂
local TILDE = u(0x0303) -- tilde = ̃
local DIA = u(0x0308) -- diaeresis = ̈
local vowel = "aəeëiou" -- vowel
local V = "[" .. vowel .. "]"
local W = "[jw]" -- glide
local accent = AC .. GR .. CFLEX
local accent_c = "[" .. accent .. "]"
local stress_c = "[" .. AC .. GR .. "]"
local ipa_stress = "ˈˌ"
local ipa_stress_c = "[" .. ipa_stress .. "]"
local separator = accent .. ipa_stress .. "# ."
local separator_c = "[" .. separator .. "]"
local C = "[^" .. vowel .. separator .. "]" -- consonant
local unstressed_words = require("Module:table").listToSet({ --feel free to add more unstressed words
"su", "nu", "u", "sa" -- case markers
})
-- 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͡ʒ] and [j]
--
function export.IPA(text, phonetic)
local debug = {}
text = ulower(text or mw.title.getCurrentTitle().text)
-- decompose everything but ë, ñ and ü
text = mw.ustring.toNFD(text)
text = rsub(text, "." .. "[" .. TILDE .. DIA .. "]", {
["e" .. DIA] = "ë",
["n" .. TILDE] = "ñ",
["u" .. TILDE] = "ü",
})
-- 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. [[say]], [[ed]], [[si]], [[na]], 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 CFLEX to the last vowel not the first one
-- adding the CFLEX after the 'u'
words[i] = rsub(word, "^(.*" .. V .. ")", "%1" .. CFLEX)
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)
--determining whether "y" is a consonant or a vowel
text = rsub(text, "y(" .. V .. ")", "ɟ%1") -- not the real sound
text = rsub(text, "y#", "i")
text = rsub(text, "w(" .. V .. ")","w%1")
text = rsub(text, "w#","u")
-- handle certain combinations; ch ng and sh handling needs to go first
text = rsub(text, "ch", "ts")
text = rsub(text, "ng", "ŋ")
text = rsub(text, "sh", "ʃ") -- primarily for unassimilated forms of loanwords and names from Arabic (e.g. [[sharif]], [[sheriat]])
--x
text = rsub(text, "x", "ks")
--c, g, q (for compatibility with Spanish-based forms, usually proper nouns, e.g. [[Guiaman]])
text = rsub(text, "c([ie])", "s%1")
text = rsub(text, "gu(" .. V .. ")", "ɡ%1")
text = rsub(text, "gü(" .. V .. ")", "ɡw%1")
text = rsub(text, "qu([ieë])", "k%1")
text = rsub(text, "ü", "u")
table.insert(debug, text)
--alphabet-to-phoneme
text = rsub(text, "[cëfgjñrvy]",
--["g"]="ɡ": U+0067 LATIN SMALL LETTER G → U+0261 LATIN SMALL LETTER SCRIPT G. Ë is for schwa
{ ["c"] = "k", ["ë"] = "ə", ["f"] = "p", ["g"] = "ɡ", ["j"] = "ĵ", ["ñ"] = "nj", ["q"] = "k", ["r"] = "ɾ", ["v"] = "b", ["y"] = "j"})
-- trill in rr
text = rsub(text, "ɾɾ", "r")
text = rsub(text, "n([# .]*[bpm])", "m%1")
-- reduce any remaining double consonants (e.g. [[Kammis]], [[Sawwal]]);
text = rsub(text, "(" .. C .. ")%1", "%1")
table.insert(debug, text)
--syllable division
local vowel_to_glide = { ["i"] = "j", ["o"] = "w", ["u"] = "w" }
-- i, o and u between vowels -> j and w. Usually in proper nouns
text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*)([iou])(" .. V .. ")",
function(v1, iou, v2)
return v1 .. vowel_to_glide[iou] .. 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")
-- Any aeo, or stressed iu, should be syllabically divided from a following aeo or stressed iu.
text = rsub_repeatedly(text, "([aeo]" .. accent_c .. "*)([aeo])", "%1.%2")
text = rsub_repeatedly(text, "([aeo]" .. accent_c .. "*)(" .. V .. stress_c .. ")", "%1.%2")
text = rsub(text, "([iu]" .. stress_c .. ")([aeo])", "%1.%2")
text = rsub_repeatedly(text, "([iu]" .. stress_c .. ")(" .. V .. stress_c .. ")", "%1.%2")
text = rsub_repeatedly(text, "i(" .. accent_c .. "*)i", "i%1.i")
text = rsub_repeatedly(text, "u(" .. accent_c .. "*)u", "u%1.u")
table.insert(debug, text)
--diphthongs
text = rsub(text, "i([aəeou])", "j%1")
text = rsub(text, "u([aəeio])", "w%1")
table.insert(debug, text)
local accent_to_stress_mark = { [AC] = "ˈ", [GR] = "ˌ", [CFLEX] = "" }
local function accent_word(word, syllables)
-- Now stress the word. If any accent exists in the word (including ^ 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[i] = rsub(syllables[i], "^(.*)(" .. accent_c .. ")(.*)$",
function(pre, accent, post)
return accent_to_stress_mark[accent] .. 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, "[^aəeioubcdfhĵklmnŋpqɾrstvwxzɡɟ#]#") or #syllables == 1 and rfind(word, "[aəeiou]") then
syllables[#syllables] = "ˈ" .. syllables[#syllables]
elseif #syllables > 1 then
syllables[#syllables - 1] = "ˈ" .. syllables[#syllables - 1]
end
end
end
local words = rsplit(text, " ")
for j, word in ipairs(words) do
-- accentuation
local syllables = rsplit(word, "%.")
accent_word(word, syllables)
-- Reconstruct the word.
words[j] = 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")
--make all primary stresses but the last one be secondary
text = rsub_repeatedly(text, "ˈ(.+)ˈ", "ˌ%1ˈ")
table.insert(debug, text)
--phonemic diphthongs
text = rsub(text,"([aəeou])i","%1j")
text = rsub(text,"([aəeio])u","%1w")
table.insert(debug, text)
--phonetic transcription
if phonetic then
--phonemic diphthongs
text = rsub(text, "([aəeou])j", "%1i̯")
text = rsub(text, "([aəeio])w", "%1u̯")
--i or u, and /e/ and /o/
text = rsub(text,"([e])","i")
text = rsub(text,"([o])","u")
table.insert(debug, text)
text = rsub(text,"d([ˈˌ]?)j","%1d͡ʒ") --/d/ before /j/
text = rsub(text,"d([ˈˌ]?)s","%1d͡ʒ") --/d/ before /s/
text = rsub(text,"n([ˈˌ]?)k","ŋ%1k") -- /n/ before /k/ (some proper nouns)
text = rsub(text,"n([ˈˌ]?)ɡ","ŋ%1ɡ") -- /n/ before /ɡ/ (some proper nouns and loanwords)
text = rsub(text,"n([ˈˌ]?)h","ŋ%1h") -- /n/ before /h/ (some proper nouns)
text = rsub(text,"n([ˈˌ]?)m","m%1m") -- /n/ before /m/
text = rsub(text,"s([ˈˌ]?)j","%1ʃ") -- /s/ before /j/
text = rsub(text,"t([ˈˌ]?)j","%1ć") -- /t/ before /j/
text = rsub(text,"([ˈˌ]?)d([j])([aiu])","%1ĵ%3") -- /dj/ before any vowel following stress
text = rsub(text,"([u])([m])([ˈ]?)([pb])","u%2%3%4") -- /o/ and /ʊ/ before /mb/ or /mp/
table.insert(debug, text)
end
table.insert(debug, text)
-- convert fake symbols to real ones
local final_conversions = {
["ɟ"] = "j", -- fake "y" to real "y"
["ĵ"] = "d͡ʒ" -- fake "j" to real "j"
}
local final_conversions_phonetic = {
["ɟ"] = "j", -- fake "y" to real "y"
["ĵ"] = "d͡ʒ" -- fake "j" to real "j"
}
if phonetic then
text = rsub(text, "[ɟĵ]", final_conversions_phonetic)
end
text = rsub(text, "[ɟĵ]", final_conversions)
-- remove # symbols at word and text boundaries
text = rsub(text, "#", "")
return mw.ustring.toNFC(text)
end
function export.show(frame)
local params = {
[1] = {},
["pre"] = {},
}
local parargs = frame:getParent().args
local args = require("Module:parameters").process(parargs, params)
local results = {}
local text = args[1] or mw.title.getCurrentTitle().text
table.insert(results, { pron = "/" .. export.IPA(text, false) .. "/" })
table.insert(results, { pron = "[" .. export.IPA(text, true) .. "]" })
local pre = args.pre and args.pre .. " " or ""
return "* " .. pre .. m_IPA.format_IPA_full(lang, results)
end
return export