Modul:IPA
Erscheinungsbild
This module returns X-SAMPA pronunciation when given IPA or the IPA pronunciation when given X-SAMPA.
Data is in Modul:IPA/data, template-callable functions in Modul:IPA/templates.
Unit tests
[ännern]- See also: Modul:IPA/testcases
IPA to X-SAMPA back to IPA
[ännern]Term | IPA | Generated X-SAMPA | Regenerated IPA | Matched? |
---|
local export = {}
local m_data = mw.loadData('Modul:IPA/data')
function export.format_IPA_full(lang, pronunciations, notes)
local prefix = nil
local pronpage = mw.title.new("Anhang:" .. lang:getCanonicalName() .. " Utspraak")
if pronpage.exists then
prefix = pronpage.prefixedText
else
prefix = "wikipedia:" .. lang:getCanonicalName() .. " Phonetik"
end
prefix = "[[Wiktionary:Internatschonale phoneetsche Alphabet|IPA]]<sup>([[" .. prefix .. "|key]])</sup>: "
-- Format
return prefix .. export.format_IPA_multiple(lang, pronunciations, notes)
end
function export.format_IPA_multiple(lang, pronunciations, notes)
notes = notes or {}
local categories = {}
-- Format
if #pronunciations == 0 then
if mw.title.getCurrentTitle().nsText == "Vörlaag" then
pronunciations = {"/aɪ piː ˈeɪ/"}
else
table.insert(categories, "[[Kategorie:Utspraokvörlagen zonder utspraok]]")
end
end
for i, pron in ipairs(pronunciations) do
pron = export.format_IPA(lang, pron)
if notes[i] then
pron = pron .. "<ref>" .. notes[i] .. "</ref>"
end
pronunciations[i] = pron
end
return table.concat(pronunciations, ", ") .. table.concat(categories)
end
-- TODO: Use data module for this
local valid_symbols = ' %(%)%%{%|%}%-~.!abcdefhijklmnopqrstuvwxyz¡àáâãäæçèéêëìíîïðòóôõöøùúûüýÿāăēĕěħĩīĭŋōŏőœũūŭűŷǀǁǂǃǎǐǒǔǖǘǚǜǟǣǽǿȁȅȉȍȕȫȭȳɐɑɒɓɔɕɖɗɘəɚɛɜɝɞɟɠɡɢɣɤɥɦɧɨɪɫɬɭɮɯɰɱɲɳɴɵɶɸɹɺɻɽɾʀʁʂʃʄʈʉʊʋṽʌʍʎʏʐʑʒʔʕʘʙʛʜʝʟʡʢʬʭ⁻¹²³⁴⁵ᵝʰʱʲʳʴʵʶʷʸʼˀˁˈˌːˑ˞ˠˡˢˣ˥˦˧˨˩ˬ˭̘̙̜̝̞̟̠̣̤̥̩̪̬̯̰̹̺̻̼͇͈͉͍͎͔͕̀́̂̃̄̆̈̋̌̏̽͆͊͋͌̊̌̚͢͡β͜θχᴙᵊᵐᵑᶑᶣᶬᶮᶯᶰᶹ᷽᷄᷅᷆᷇᷈᷉ḁḛḭḯṍṏṳṵṹṻạẹẽịọụỳỵỹ‖․‥…‼‿ⁿ↑↓↗↘ⱱꜛꜜꟸꟹ𝆏𝆑'
-- Takes an IPA pronunciation and formats it and adds cleanup categories.
function export.format_IPA(lang, pron)
local categories = {}
-- Detect whether this is a phonemic or phonetic transcription
local repr_mark = {}
repr_mark.i, repr_mark.f, repr_mark.left, repr_mark.right = mw.ustring.find(pron, '^(.).-(.)$')
local repr = nil
-- If valid, strip the representation marks
if repr_mark.left == '/' and repr_mark.right == '/' then
repr = "phonemic"
pron = mw.ustring.sub(pron, 2, -2)
elseif repr_mark.left == '[' and repr_mark.right == ']' then
repr = "phonetic"
pron = mw.ustring.sub(pron, 2, -2)
else
table.insert(categories, "[[Kategorie:IPA zonder tekens um fonemies/foneties an te duden]]")
end
-- Check for obsolete and nonstandard symbols
for i, symbol in ipairs(m_data.nonstandard) do
local result = mw.ustring.find(pron, symbol)
if result then
table.insert(categories, "[[Kategorie:IPA met verolderde of neet-standard tekens|" .. result .. "]]")
break
end
end
-- Check for invalid symbols
local result = mw.ustring.gsub(pron, '[' .. valid_symbols .. ']', '')
if result ~= '' then
table.insert(categories, "[[Kategorie:IPA met ongeldige IPA-tekens|" .. result .. "]]")
end
-- Check for double character
if mw.ustring.match(pron, '([^˥˦˧˨˩])%1') then
table.insert(categories, "[[Kategorie:IPA met dubbele karakters]]")
end
-- Reference inside IPA template usage
if mw.ustring.find(pron, '</ref>') then
table.insert(categories, "[[Kategorie:IPA met referentsies der in]]")
end
if repr == "phonemic" then
if lang and m_data.phonemes[lang:getCode()] then
local valid_phonemes = m_data.phonemes[lang:getCode()]
local rest = pron
local phonemes = {}
while mw.ustring.len(rest) > 0 do
local longestmatch = ""
if mw.ustring.sub(rest, 1, 1) == "(" or mw.ustring.sub(rest, 1, 1) == ")" then
longestmatch = mw.ustring.sub(rest, 1, 1)
else
for _, phoneme in ipairs(valid_phonemes) do
if mw.ustring.len(phoneme) > mw.ustring.len(longestmatch) and mw.ustring.sub(rest, 1, mw.ustring.len(phoneme)) == phoneme then
longestmatch = phoneme
end
end
end
if mw.ustring.len(longestmatch) > 0 then
table.insert(phonemes, longestmatch)
rest = mw.ustring.sub(rest, mw.ustring.len(longestmatch) + 1)
else
local phoneme = mw.ustring.sub(rest, 1, 1)
table.insert(phonemes, "<span style=\"color: red\">" .. phoneme .. "</span>")
rest = mw.ustring.sub(rest, 2)
table.insert(categories, "[[Kategorie:IPA met ongeldige fonemen/" .. lang:getCode() .. "]]")
require("Module:debug").track("IPA/invalid phonemes/" .. phoneme)
end
end
pron = table.concat(phonemes)
end
pron = "/" .. pron .. "/"
elseif repr == "phonetic" then
pron = "[" .. pron .. "]"
end
return '<span class="IPA" lang="">' .. pron .. '</span>' .. table.concat(categories)
end
-- IPA <-> XSAMPA lookup tables
local i2x_lookup, x2i_lookup = {}, {}
for ipa_sym, data in pairs(m_data.symbols[1]) do
if type(data.XSAMPA) == "table" then
i2x_lookup[ipa_sym] = data.XSAMPA[1]
for _, xsampa_sym in ipairs(data.XSAMPA) do
x2i_lookup[xsampa_sym] = ipa_sym
end
else
i2x_lookup[ipa_sym] = data.XSAMPA
x2i_lookup[data.XSAMPA] = ipa_sym
end
end
--exception cases where two IPA characters map to one XSAMPA character
x2i_lookup["_T"]="˥"
x2i_lookup["_H"]="˦"
x2i_lookup["_M"]="˧"
x2i_lookup["_L"]="˨"
x2i_lookup["_B"]="˩"
function export.IPA_to_XSAMPA(text)
local escape = false
if type(text) == 'table' then -- a frame, extract args
text = text.args[1]
text = text:gsub('{{=}}','='):gsub('{{!}}','|')
text = mw.text.decode(text) -- XXX
escape = true
end
text = mw.ustring.gsub(text, 'ːː', ':') -- this basically sums up m_data.symbols[2].XSAMPA
text = mw.ustring.gsub(text, '.', i2x_lookup)
if escape then
text = mw.text.nowiki(text)
end
return text
end
function export.XSAMPA_to_IPA(text)
local escape = false
if type(text) == 'table' then -- a frame, extract args
text = text.args[1]
text = mw.text.decode(text) -- XXX
escape = true
end
-- XXX: may not be the most efficient, but at least correct.
local output = {}
while #text > 0 do
local a1, a2, a3, a4 = mw.ustring.sub(text, 1, 1), mw.ustring.sub(text, 1, 2), mw.ustring.sub(text, 1, 3), mw.ustring.sub(text, 1, 4)
if x2i_lookup[a4] then
table.insert(output, x2i_lookup[a4])
text = mw.ustring.sub(text, 5)
elseif x2i_lookup[a3] then
table.insert(output, x2i_lookup[a3])
text = mw.ustring.sub(text, 4)
elseif x2i_lookup[a2] then
table.insert(output, x2i_lookup[a2])
text = mw.ustring.sub(text, 3)
elseif x2i_lookup[a1] then
table.insert(output, x2i_lookup[a1])
text = mw.ustring.sub(text, 2)
else -- no match
table.insert(output, a1)
text = mw.ustring.sub(text, 2)
end
end
output = table.concat(output)
if escape then
-- output = mw.text.nowiki(output)
end
return output
end
return export