Dokumentation för denna modul finns på /dok (redigera), /test


Användning redigera

Enbart via {{ipa}}.

Parametrar redigera

  • (namngiven och frivillig) word= behövs enbart för dokumentation och testande (modulen behöver aldrig "hjälp")

Självtest redigera

ĉiuĵaŭde -> [t͡ʃiuˈʒawde]
ĈIUĴAŬDE -> [t͡ʃiuˈʒawde]
Rio-de-Ĵanejro -> [ˈrio de ʒaˈnejro]
Mi estas finvenkisto! -> [mi ˈestas finvenˈkisto]

en.wiktionary redigera

-- based on https://en.wiktionary.org/wiki/Module:eo-pron
-- this module must NOT be "updated" by blindly copying from that page
-- authors: "DTLHS" et al
-- modification for non-en wiktionaries: "Taylor 49"

local export = {}

local has_vowel, term_to_words, string_to_letters, string_to_syllables,
  string_to_ipa, letters_to_syllables, strip_initial_consonants, term_to_IPA

local consonants = {
  ["b"] = "b",
  ["c"] = "t͡s",
  ["ĉ"] = "t͡ʃ",
  ["d"] = "d",
  ["f"] = "f",
  ["g"] = "ɡ",
  ["ĝ"] = "d͡ʒ",
  ["h"] = "h",
  ["ĥ"] = "x",
  ["j"] = "j",
  ["ĵ"] = "ʒ",
  ["k"] = "k",
  ["l"] = "l",
  ["m"] = "m",
  ["n"] = "n",
  ["p"] = "p",
  ["r"] = "r",
  ["s"] = "s",
  ["ŝ"] = "ʃ",
  ["t"] = "t",
  ["v"] = "v",
  ["z"] = "z",
  ['ŭ'] = "w"}

local vowels = {
  ["a"] = "a",
  ["e"] = "e",
  ["i"] = "i",
  ["o"] = "o",
  ["u"] = "u",

}

local letters_phonemes = {}

-- combine into single table
for k, v in pairs(vowels) do letters_phonemes[k] = v end
for k, v in pairs(consonants) do letters_phonemes[k] = v end

function term_to_words(term)
  -- split by spaces, hyphens, or periods
  return mw.text.split(term, '[%s%-%.]')
end
function string_to_letters(term)
  return mw.text.split(term, "")
end

function letters_to_syllables(letters)

  if not letters[2] then
    return {[1] = letters[1]}
  end
  local l_r_exceptions = {["m"] = true, ["n"] = true, ["ŭ"] = true, ["j"] = true}

  local result = {[1] = ""}
  local j = 1
  for i = 1, #letters - 2 do
    result[j] = result[j] .. letters[i]
    local letter = mw.ustring.lower(letters[i])
    local letter1 = mw.ustring.lower(letters[i + 1])
    local letter2 = mw.ustring.lower(letters[i + 2])

    if vowels[letter] then
      if consonants[letter1] and vowels[letter2] then
        -- single consonant goes with following vowel
        if has_vowel(result[j]) and (letter1 ~= 'ŭ') then
          j = j + 1
          result[j] = ""
        end

      elseif consonants[letter1] and not l_r_exceptions[letter1] 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[j]) then
          j = j + 1
          result[j] = ""
        end

      elseif vowels[letter1] then
        -- two vowels
        if has_vowel(result[j]) then
          j = j + 1
          result[j] = ""
        end
      end
    elseif consonants[letter] then
      if consonants[letter1] and vowels[letter2] then
        if (mw.ustring.len(result[j]) ~= 1) then
          -- single consonant goes with following vowel
          if has_vowel(result[j]) then
            j = j + 1
            result[j] = ""
          end
        end
      elseif consonants[letter1] and not l_r_exceptions[letter1] 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[j]) then
          j = j + 1
          result[j] = ""
        end

      elseif vowels[letter1] then
        -- two vowels
        if has_vowel(result[j]) then
          j = j + 1
          result[j] = ""
        end
      end
    end
  end

  -- add last two letters
  if letters[2] then
    local c1 = letters[#letters - 1]
    local c2 = letters[#letters]

    if c1 ~= 'ŭ' then
            if vowels[c1] and vowels[c2] then
                result[j] = result[j] .. c1
                j = j + 1
                result[j] = c2
            elseif has_vowel(result[j]) and has_vowel(c1 .. c2) then
              j = j + 1
              result[j] = c1 .. c2
          else
            result[j] = result[j] .. c1 .. c2
        end

        else
            if vowels[letters[#letters - 2]] and vowels[c2] then
              result[j] = result[j] .. c1
                j = j + 1
                result[j] = c2
            elseif has_vowel(result[j]) and has_vowel(c1 .. c2) then
              j = j + 1
              result[j] = c1 .. c2
          else
            result[j] = result[j] .. c1 .. c2
        end
        end
    end


  local result2 = {}
  for i, j in pairs(result) do
    if j and j ~= "" then
      table.insert(result2, j)
    end
  end
  return result2
end

function string_to_syllables(term)
  -- split if given artificial syllable breaks
  local split_input = mw.text.split(term, '‧', true)
  local result = {}
  for _, split in pairs(split_input) do
    for j, syllable in pairs(letters_to_syllables(string_to_letters(split))) do
      table.insert(result, syllable)
    end
  end

  return result

end

function string_to_ipa(syllable)

  local syllable_letters = string_to_letters(syllable)
  local syllable_ipa = ""

  for k, letter in pairs(syllable_letters) do
    if letters_phonemes[mw.ustring.lower(letter)] then
      syllable_ipa = syllable_ipa .. letters_phonemes[mw.ustring.lower(letter)]
    end

  end
  return syllable_ipa
end

function has_vowel(term)
  return mw.ustring.lower(term):find("[aeiou]") ~= nil
end

function strip_initial_consonants(term)
  local letters = string_to_letters(term)
  local result = {}

  local gate = false
  for i, j in pairs(letters) do
    if vowels[j] then
      gate = true
    end

    if gate then
      table.insert(result, j)
    end
  end

  return table.concat(result)
end


function term_to_IPA(term)

  local words = term_to_words(term)
  local result = {}

  for i, word in pairs(words) do
    if word ~= "" then
      -- add /o/ if word is a single character and a consonant
      if mw.ustring.len(word) == 1 then
        if consonants[word] then
          word = word .. 'o'
        end
      end

      -- break into syllables and make each into IPA
      local hyphenated = string_to_syllables(word)
      local word_result = {}
      for j, syllable in pairs(hyphenated) do
        local syllable_ipa = string_to_ipa(syllable)
        word_result[j] = syllable_ipa
      end

      -- add stress to penultimate syllable, and set rhyme to last two syllables
      if word_result[2] then
        word_result[#word_result - 1] = "ˈ" .. word_result[#word_result - 1]
      end

      result[i] = table.concat(word_result)
    end
  end

  return result

end

function export.IPA (arxframent)
  local pron = ''
  local IPA_input = mw.title.getCurrentTitle().text -- {{PAGENAME}}
  arxourown = arxframent.args
  local strover = arxourown["word"] -- use only if needed
  if (type(strover)=="string") then
    if (string.len(strover)~=0) then
      IPA_input = strover -- override only if parameter is non-empty (due to forwarding)
    end--if
  end--if
  IPA_input = mw.ustring.lower(IPA_input)
  pron = "[" .. table.concat(term_to_IPA(IPA_input), " ") .. "]"
  return pron
end

return export