Called by {{eo-pron}}
. Do not use directly.
local export = {}
local concat = table.concat
local insert = table.insert
local max = math.max
local remove = table.remove
local ulen = mw.ustring.len
local ulower = mw.ustring.lower
local usub = mw.ustring.sub
local lang = require("Module:languages").getByCode("eo")
local dz = mw.ustring.char(0xF000)
local consonants = {
= "b",
= "t͡s",
= "t͡ʃ",
= "d",
= "d͡z",
= "f",
= "ɡ",
= "d͡ʒ",
= "h",
= "x",
= "j",
= "ʒ",
= "k",
= "l",
= "m",
= "n",
= "p",
= "r",
= "s",
= "ʃ",
= "t",
= "v",
= "z",
= "w"}
local vowels = {
= "a",
= "e",
= "i",
= "o",
= "u",
}
local phonemes = {}
-- combine into single table
for k, v in pairs(vowels) do phonemes = v end
for k, v in pairs(consonants) do phonemes = v end
local function has_vowel(term)
return ulower(term):find("") ~= nil
end
local function letters_to_syllables(letters)
if not letters then
return { = letters}
end
local l_r_exceptions = { = true, = true, = true, = true}
local i = 1
while true do
local letter = letters
if not letter then
break
elseif ulower(letter) == "d" then
local letter1 = letters
if not letter1 then
break
end
if ulower(letter1) == "z" then
letter = letter .. letter1
letters = letter
remove(letters, i + 1)
end
end
i = i + 1
end
local result = { = ""}
local j = 1
for i = 1, #letters - 2 do
result = result .. letters
local letter = ulower(letters)
local letter1 = ulower(letters)
local letter2 = ulower(letters)
if phonemes then
if consonants and vowels then
if vowels or ulen(result) ~= 1 then
-- single consonant goes with following vowel
if has_vowel(result) and (letter1 ~= 'ŭ') then
j = j + 1
result = ""
end
end
elseif consonants and not l_r_exceptions and (letter2 == 'l' or letter2 == 'r') and (letter1 ~= 'l' and letter1 ~= 'r') then
-- consonant followed by l or r goes with l or r
if has_vowel(result) then
j = j + 1
result = ""
end
elseif vowels then
-- two vowels
if has_vowel(result) then
j = j + 1
result = ""
end
end
end
end
-- add last two letters
if letters then
local c1 = letters
local c2 = letters
if c1 ~= 'ŭ' then
if vowels and vowels then
result = result .. c1
j = j + 1
result = c2
elseif has_vowel(result) and has_vowel(c1 .. c2) then
j = j + 1
result = c1 .. c2
else
result = result .. c1 .. c2
end
else
if vowels] and vowels then
result = result .. c1
j = j + 1
result = c2
elseif has_vowel(result) and has_vowel(c1 .. c2) then
j = j + 1
result = c1 .. c2
else
result = result .. c1 .. c2
end
end
end
local result2 = {}
for i, j in ipairs(result) do
if j and j ~= "" then
insert(result2, j)
end
end
return result2
end
local function string_to_letters(term)
return mw.text.split(term, "")
end
local function string_to_syllables(term)
-- split if given artificial syllable breaks
local split_input = mw.text.split(term, '‧', true)
local result = {}
for _, split in ipairs(split_input) do
for j, syllable in ipairs(letters_to_syllables(string_to_letters(split))) do
insert(result, syllable)
end
end
return result
end
local function letter_to_ipa(letter)
return letter == dz and phonemes.dz or phonemes or ""
end
local function string_to_ipa(syllable)
return (syllable:gsub("", dz)
:gsub(".*", letter_to_ipa)
:gsub("()j", "%1i̯")
:gsub("()w", "%1u̯")
)
end
local function strip_initial_consonants(term)
for i, letter in term:gmatch("()(.*)") do
if vowels then
return term:sub(i)
end
end
return term
end
local function term_to_words(term)
-- split by spaces, hyphens, or periods
return mw.text.split(term, '')
end
local function term_to_IPA_and_rhyme(term)
local words = term_to_words(term)
local result, rhyme = {}
for i, word in ipairs(words) do
if word ~= "" then
-- Add /o/ if the word is a single character and a consonant.
if ulen(word) == 1 then
if consonants then
word = word .. "o"
end
end
-- Break into syllables, and make each into IPA.
local hyphenated, word_result = string_to_syllables(word), {}
for j, syllable in ipairs(hyphenated) do
word_result = string_to_ipa(syllable)
end
-- Add stress to the penultimate syllable.
if word_result then
insert(word_result, #word_result - 1, "ˈ")
end
-- Set rhyme to last two syllables, or the last syllable if this is
-- a one-syllable word.
rhyme = concat(hyphenated, nil, max(#hyphenated - 1, 1))
rhyme = string_to_syllables(strip_initial_consonants(rhyme))
result = concat(word_result)
end
end
-- Rhyme to IPA.
if rhyme then
for i, syllable in ipairs(rhyme) do
rhyme = string_to_ipa(syllable)
end
rhyme = concat(rhyme)
end
return { = result, = rhyme}
end
local function parse_input(input)
-- no input -> use page title
return input or mw.title.getCurrentTitle().text
end
function export.IPA(IPA_input)
IPA_input = ulower(parse_input(IPA_input))
return "/" .. concat(term_to_IPA_and_rhyme(IPA_input).IPA, " ") .. "/"
end
function export.rhyme(rhyme_input)
rhyme_input = parse_input(rhyme_input)
return term_to_IPA_and_rhyme(rhyme_input).rhyme
end
function export.hyphenation(hyphenation_input)
hyphenation_input = parse_input(hyphenation_input)
local words = term_to_words(hyphenation_input)
local result = {}
local hyphenated
for i, word in ipairs(words) do
hyphenated = string_to_syllables(word)
insert(result, concat(hyphenated, "‧"))
end
return concat(result, ' ')
end
function export.letters(letters_input)
letters_input = parse_input(letters_input)
return concat(string_to_letters(letters_input), '-')
end
local function get_artificial_breaks(frame)
-- override for syllable breaks
local args = frame:getParent().args
if not args then
return nil
end
local param = 1
local result = {}
while true do
if not args then
if not result then
return nil
end
return concat(result, "‧")
end
insert(result, args)
param = param + 1
end
end
function export.pronunciation_section(frame)
local args = frame:getParent().args
local artificial_breaks = get_artificial_breaks(frame)
local IPA_override = args
if not IPA_override then
IPA_override = export.IPA(artificial_breaks)
end
local hyphenation_override = args
if not hyphenation_override then
hyphenation_override = export.hyphenation(artificial_breaks)
end
local rhyme_override = args
if not rhyme_override then
rhyme_override = export.rhyme(artificial_breaks)
end
local audio = args
if rhyme_override then
rhyme_override = require("Module:rhymes").format_rhymes { lang = lang, rhymes = {{ rhyme = rhyme_override }} }
end
IPA_override = require("Module:IPA").format_IPA_full { lang = lang, items = {{pron = IPA_override }} }
local result = "<ul>"
result = result .. "<li>" .. IPA_override .. "</li>"
if audio then
audio = require("Module:audio").format_audio { lang = lang, file = audio }
result = result .. "<li>" .. audio .. "</li>"
end
if rhyme_override then
result = result .. "<li>" .. rhyme_override .. "</li>"
end
result = result .. "<li>Hyphenation: " .. hyphenation_override .. "</li>"
result = result .. "</ul>"
return result
end
return export