Module:fa-IPA: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Tag: Reverted
m Reverted edits by 2A01:E0A:B69:5160:78DB:6C29:7436:115C. If you think this rollback is in error, please leave a message on my talk page.
Tag: Rollback
Line 1: Line 1:
local export = {}
local export = {}

local rsubn = mw.ustring.gsub
local rsubn = mw.ustring.gsub
local log = mw.log

local m_a = require("Module:accent_qualifier")
local m_a = require("Module:accent_qualifier")
local m_IPA = require("Module:IPA")
local m_IPA = require("Module:IPA")


local sc_fa = require("Module:scripts").getByCode("fa-Arab")
local sc_tg = require("Module:scripts").getByCode("Cyrl")
local lang_fa = require("Module:languages").getByCode("fa")
local lang_fa = require("Module:languages").getByCode("fa")
local lang_tg = require("Module:languages").getByCode("tg")


function export.show(frame)
function export.show(frame)
Line 49: Line 51:
m_a.show({'Tajik'}) .. " " ..
m_a.show({'Tajik'}) .. " " ..
m_IPA.format_IPA_full(lang_fa, results_tg)
m_IPA.format_IPA_full(lang_fa, results_tg)
end

function export.show_tg(frame)
local args = frame:getParent().args

local p, results = {}, {}

if args[1] then
for index, item in ipairs(args) do
table.insert(p, (item ~= "") and item or nil)
end
else
if mw.title.getCurrentTitle().nsText == "Template" then
p = {"wīkīlu`ğat"}
else
error(
'Please provide a Classical Persian romanisation in the first parameter of {{[[Template:tg-IPA/new|tg-IPA]]}}.')
end
end

for _, word in ipairs(p) do
table.insert(results, {pron = "/" .. export.tg_IPA(word) .. "/"})
end

local final_output = ""

if mw.title.getCurrentTitle().nsText == "Template" then
final_output = "* "
end

return final_output .. m_IPA.format_IPA_full(lang_tg, results)
end
end


Line 83: Line 116:
}
}


local iranian_persian_consonants = {['q'] = 'ɣ', ['w'] = 'v', ['v'] = 'v'}
local iranian_persian_consonants = {['q'] = 'ɣ', ['w'] = 'w', ['v'] = 'v'}


local dari_persian_short_vowels = {['a'] = 'a', ['i'] = 'ɪ', ['u'] = 'ʊ'}
local dari_persian_short_vowels = {['a'] = 'a', ['i'] = 'ɪ', ['u'] = 'ʊ'}
Line 124: Line 157:
local consonants = "[^" .. vowels .. "]"
local consonants = "[^" .. vowels .. "]"


function export.fa_IPA(text)
function export.fa_IPA(text, do_debug)
text = rsubn(text, "[-.]", " ")
text = rsubn(text, "[-.]", " ")

text = rsubn(text, " | ", "# | #")
text = "##" .. rsubn(text, " ", "# #") .. "##"

text = rsubn(text, "v", "w")
text = rsubn(text, "v", "w")
-- Replace diphthong
-- Replace diphthong
Line 143: Line 172:
end)
end)
-- Replace xwa with xu
-- Replace xwa with xu
text = rsubn(text, "#xwa", "#xu")
text = rsubn(text, "^xwa", "xu")
-- Replace xwā with xā
-- Replace xwā with xā
text = rsubn(text, "#xwā", "#xā")
text = rsubn(text, "^xwā", "xā")
-- Replace short vowels
-- Replace short vowels
text = rsubn(text, ".", iranian_persian_short_vowels)
text = rsubn(text, ".", iranian_persian_short_vowels)
Line 158: Line 187:
text = rsubn(text, ".", iranian_persian_consonants)
text = rsubn(text, ".", iranian_persian_consonants)
-- Replace final v with w
-- Replace final v with w
text = rsubn(text, "ov#", "ow")
text = rsubn(text, "ov()", function(position)
local consonant = mw.ustring.sub(text, position, position)
if consonant == "" or consonant == " " then return "ow" end
end)
-- Replace final æ with e
-- Replace final æ with e
text = rsubn(text, "æ#", "e#")
text = rsubn(text, "æ()", function(position)
local consonant = mw.ustring.sub(text, position, position)

if consonant == "" or consonant == " " then return "e" end
text = rsubn(text, "#", "")
end)


return text
return text
end
end


function export.prs_IPA(text)
function export.prs_IPA(text, do_debug)
text = rsubn(text, "[-.]", " ")
text = rsubn(text, "[-.]", " ")

text = rsubn(text, " | ", "# | #")
text = "##" .. rsubn(text, " ", "# #") .. "##"

text = rsubn(text, "v", "w")
text = rsubn(text, "v", "w")
-- Replace xwa with xu
-- Replace xwa with xu
text = rsubn(text, "#xwa", "#xu")
text = rsubn(text, "^xwa", "xu")
-- Replace xwā with xā
-- Replace xwā with xā
text = rsubn(text, "#xwā", "#xā")
text = rsubn(text, "^xwā", "xā")
-- Replace short vowels
-- Replace short vowels
text = rsubn(text, ".", dari_persian_short_vowels)
text = rsubn(text, ".", dari_persian_short_vowels)
Line 189: Line 218:
text = rsubn(text, ".", common_consonants)
text = rsubn(text, ".", common_consonants)
text = rsubn(text, ".", dari_persian_consonants)
text = rsubn(text, ".", dari_persian_consonants)

text = rsubn(text, "#", "")


return text
return text
end
end


function export.tg_IPA(text)
function export.tg_IPA(text, do_debug)
text = rsubn(text, "[-.]", " ")
text = rsubn(text, "[-.]", " ")

text = rsubn(text, " | ", "# | #")
text = "##" .. rsubn(text, " ", "# #") .. "##"

text = rsubn(text, "v", "w")
text = rsubn(text, "v", "w")
-- Replace xwa with xu
-- Replace xwa with xu
text = rsubn(text, "#xwa", "#xu")
text = rsubn(text, "^xwa", "xu")
-- Replace xwā with xā
-- Replace xwā with xā
text = rsubn(text, "#xwā", "#xā")
text = rsubn(text, "^xwā", "xā")
-- Replace short vowels
-- Replace short vowels
text = rsubn(text, ".", tajik_short_vowels)
text = rsubn(text, ".", tajik_short_vowels)
Line 217: Line 240:
text = rsubn(text, ".", common_consonants)
text = rsubn(text, ".", common_consonants)
text = rsubn(text, ".", tajik_consonants)
text = rsubn(text, ".", tajik_consonants)
-- Replace ih, īh, i\', ī\' by ēh, ē\'
-- Replace ih, īh, i\', ī\' by ēh, ē\'
text = rsubn(text, "ih#", "eh#")
text = rsubn(text, "ih()", function(position)
text = rsubn(text, "īh#", "eh#")
local consonant = mw.ustring.sub(text, position, position)
if consonant == "" or consonant == " " then return "eh" end
text = rsubn(text, "īʔ#", "eʔ#")
end)
text = rsubn(text, "iʔ#", "eʔ#")
text = rsubn(text, "i([hʔ])#", "e#")
text = rsubn(text, "īh()", function(position)
text = rsubn(text, "ī([hʔ])#", "e#")
local consonant = mw.ustring.sub(text, position, position)
if consonant == "" or consonant == " " then return "eh" end

end)
text = rsubn(text, "#", "")
text = rsubn(text, "ī\'()", function(position)
local consonant = mw.ustring.sub(text, position, position)
if consonant == "" or consonant == " " then return "eʔ" end
end)
text = rsubn(text, "i\'()", function(position)
local consonant = mw.ustring.sub(text, position, position)
if consonant == "" or consonant == " " then return "eʔ" end
end)
text = rsubn(text, "i([h\'])()", function(semivowel, position)
local consonant = mw.ustring.sub(text, position, position)
if consonant == "" or consonant:find(consonants) then return "e" end
end)
text = rsubn(text, "ī([h'\''])()", function(semivowel, position)
local consonant = mw.ustring.sub(text, position, position)
if consonant == "" or consonant:find(consonants) then return "e" end
end)


return text
return text
end
end


function export.fa_cls_IPA(text)
function export.fa_cls_IPA(text, do_debug)
text = rsubn(text, "[-.]", " ")
text = rsubn(text, "[-.]", " ")

text = rsubn(text, " | ", "# | #")
text = "##" .. rsubn(text, " ", "# #") .. "##"

text = rsubn(text, "v", "w")
text = rsubn(text, "v", "w")
-- Replace xwa with xʷa
-- Replace xwa with xʷa
text = rsubn(text, "#xwa", "#xʷa")
text = rsubn(text, "^xwa", "xʷa")
-- Replace xwā with xā
-- Replace xwā with xā
text = rsubn(text, "#xwā", "#xʷā")
text = rsubn(text, "^xwā", "xʷā")
-- Replace short vowels
-- Replace short vowels
text = rsubn(text, ".", classical_persian_short_vowels)
text = rsubn(text, ".", classical_persian_short_vowels)
Line 252: Line 287:
text = rsubn(text, ".", common_consonants)
text = rsubn(text, ".", common_consonants)
text = rsubn(text, ".", classical_persian_consonants)
text = rsubn(text, ".", classical_persian_consonants)

text = rsubn(text, "#", "")


return text
return text

Revision as of 11:28, 25 January 2022

Pronunciation module for Persian. See {{fa-IPA}} for usage information and examples.

Testcases

See Module:fa-IPA/testcases.


local export = {}
local rsubn = mw.ustring.gsub
local log = mw.log
local m_a = require("Module:accent_qualifier")
local m_IPA = require("Module:IPA")

local sc_fa = require("Module:scripts").getByCode("fa-Arab")
local sc_tg = require("Module:scripts").getByCode("Cyrl")
local lang_fa = require("Module:languages").getByCode("fa")
local lang_tg = require("Module:languages").getByCode("tg")

function export.show(frame)
    local args = frame:getParent().args

    local p, results_fa_cls, results_prs, results_fa, results_tg = {}, {}, {},
                                                                   {}, {}

    if args[1] then
        for index, item in ipairs(args) do
            table.insert(p, (item ~= "") and item or nil)
        end
    else
        if mw.title.getCurrentTitle().nsText == "Template" then
            p = {"wīkīwā`ža"}
        else
            error(
                'Please provide a Classical Persian romanisation in the first parameter of {{[[Template:fa-IPA/new|fa-IPA]]}}.')
        end
    end

    for _, word in ipairs(p) do
        table.insert(results_fa_cls,
                     {pron = "/" .. export.fa_cls_IPA(word) .. "/"})
        table.insert(results_prs, {pron = "/" .. export.prs_IPA(word) .. "/"})
        table.insert(results_fa, {pron = "/" .. export.fa_IPA(word) .. "/"})
        table.insert(results_tg, {pron = "/" .. export.tg_IPA(word) .. "/"})
    end

    local final_output = ""

    if mw.title.getCurrentTitle().nsText == "Template" then
        final_output = "* "
    end

    return final_output .. m_a.show({'Classical Persian'}) .. " " ..
               m_IPA.format_IPA_full(lang_fa, results_fa_cls) .. "\n* " ..
               m_a.show({'Dari Persian'}) .. " " ..
               m_IPA.format_IPA_full(lang_fa, results_prs) .. "\n* " ..
               m_a.show({'Iranian Persian'}) .. " " ..
               m_IPA.format_IPA_full(lang_fa, results_fa) .. "\n* " ..
               m_a.show({'Tajik'}) .. " " ..
               m_IPA.format_IPA_full(lang_fa, results_tg)
end

function export.show_tg(frame)
    local args = frame:getParent().args

    local p, results = {}, {}

    if args[1] then
        for index, item in ipairs(args) do
            table.insert(p, (item ~= "") and item or nil)
        end
    else
        if mw.title.getCurrentTitle().nsText == "Template" then
            p = {"wīkīlu`ğat"}
        else
            error(
                'Please provide a Classical Persian romanisation in the first parameter of {{[[Template:tg-IPA/new|tg-IPA]]}}.')
        end
    end

    for _, word in ipairs(p) do
        table.insert(results, {pron = "/" .. export.tg_IPA(word) .. "/"})
    end

    local final_output = ""

    if mw.title.getCurrentTitle().nsText == "Template" then
        final_output = "* "
    end

    return final_output .. m_IPA.format_IPA_full(lang_tg, results)
end

local common_consonants = {
    ['j'] = 'd͡ʒ',
    ['\''] = 'ʔ',
    ['ḏ'] = 'z',
    ['ḍ'] = 'z',
    ['ğ'] = 'ɣ',
    ['ḥ'] = 'h',
    ['r'] = 'ɾ',
    ['ṣ'] = 's',
    ['š'] = 'ʃ',
    ['ṯ'] = 's',
    ['ṭ'] = 't',
    ['y'] = 'j',
    ['ž'] = 'ʒ',
    ['ẓ'] = 'z',
    ['č'] = 't͡ʃ',
    ['g'] = 'ɡ',
    ['`'] = 'ˈ'
}
-- Apostrophe is added to mark stress, as it distinctive in Persian, or, can
-- someone replace it with acute accent?

local iranian_persian_short_vowels = {['a'] = 'æ', ['i'] = 'e', ['u'] = 'o'}

local iranian_persian_long_vowels = {
    ['ā'] = 'ɒː',
    ['ī'] = 'iː',
    ['ū'] = 'uː',
    ['ō'] = 'uː',
    ['ē'] = 'iː'
}

local iranian_persian_consonants = {['q'] = 'ɣ', ['w'] = 'w', ['v'] = 'v'}

local dari_persian_short_vowels = {['a'] = 'a', ['i'] = 'ɪ', ['u'] = 'ʊ'}

local dari_persian_long_vowels = {
    ['ā'] = 'ɒː',
    ['ī'] = 'iː',
    ['ū'] = 'uː',
    ['ō'] = 'oː',
    ['ē'] = 'eː'
}

local dari_persian_consonants = {['v'] = 'w'}

local tajik_short_vowels = {['a'] = 'a', ['i'] = 'i', ['u'] = 'u'}

local tajik_long_vowels = {
    ['ā'] = 'ɔ',
    ['ī'] = 'i',
    ['ū'] = 'u',
    ['ō'] = 'ɵ',
    ['ē'] = 'e'
}

local tajik_consonants = {['w'] = 'v'}

local classical_persian_short_vowels = {['a'] = 'a', ['i'] = 'i', ['u'] = 'u'}

local classical_persian_long_vowels = {
    ['ā'] = 'ɑː',
    ['ī'] = 'iː',
    ['ū'] = 'uː',
    ['ō'] = 'oː',
    ['ē'] = 'eː'
}

local classical_persian_consonants = {['v'] = 'w'}

local vowels = "aiuāīūēō"
local consonants = "[^" .. vowels .. "]"

function export.fa_IPA(text, do_debug)
    text = rsubn(text, "[-.]", " ")
    text = rsubn(text, "v", "w")
    -- Replace diphthong
    text = rsubn(text, "a([wy])()", function(semivowel, position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant:find(consonants) then
            if semivowel == "w" then
                return "uw"
            else
                return "ey"
            end
        end
    end)
    -- Replace xwa with xu
    text = rsubn(text, "^xwa", "xu")
    -- Replace xwā with xā
    text = rsubn(text, "^xwā", "xā")
    -- Replace short vowels
    text = rsubn(text, ".", iranian_persian_short_vowels)
    -- Replace long vowels
    text = rsubn(text, ".", iranian_persian_long_vowels)
    -- Replace jj with dj
    text = rsubn(text, "jj", "dj")
    -- Replace čč with tč
    text = rsubn(text, "čč", "tč")
    -- Replace consonants
    text = rsubn(text, ".", common_consonants)
    text = rsubn(text, ".", iranian_persian_consonants)
    -- Replace final v with w
    text = rsubn(text, "ov()", function(position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant == " " then return "ow" end
    end)
    -- Replace final æ with e
    text = rsubn(text, "æ()", function(position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant == " " then return "e" end
    end)

    return text
end

function export.prs_IPA(text, do_debug)
    text = rsubn(text, "[-.]", " ")
    text = rsubn(text, "v", "w")
    -- Replace xwa with xu
    text = rsubn(text, "^xwa", "xu")
    -- Replace xwā with xā
    text = rsubn(text, "^xwā", "xā")
    -- Replace short vowels
    text = rsubn(text, ".", dari_persian_short_vowels)
    -- Replace long vowels
    text = rsubn(text, ".", dari_persian_long_vowels)
    -- Replace jj with dj
    text = rsubn(text, "jj", "dj")
    -- Replace čč with tč
    text = rsubn(text, "čč", "tč")
    -- Replace consonants
    text = rsubn(text, ".", common_consonants)
    text = rsubn(text, ".", dari_persian_consonants)

    return text
end

function export.tg_IPA(text, do_debug)
    text = rsubn(text, "[-.]", " ")
    text = rsubn(text, "v", "w")
    -- Replace xwa with xu
    text = rsubn(text, "^xwa", "xu")
    -- Replace xwā with xā
    text = rsubn(text, "^xwā", "xā")
    -- Replace short vowels
    text = rsubn(text, ".", tajik_short_vowels)
    -- Replace long vowels
    text = rsubn(text, ".", tajik_long_vowels)
    -- Replace jj with dj
    text = rsubn(text, "jj", "dj")
    -- Replace čč with tč
    text = rsubn(text, "čč", "tč")
    -- Replace consonants
    text = rsubn(text, ".", common_consonants)
    text = rsubn(text, ".", tajik_consonants)
   -- Replace ih, īh, i\', ī\' by ēh, ē\'
   text = rsubn(text, "ih()", function(position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant == " " then return "eh" end
    end)
   text = rsubn(text, "īh()", function(position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant == " " then return "eh" end
    end)
text = rsubn(text, \'()", function(position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant == " " then return "eʔ" end
    end)
text = rsubn(text, "i\'()", function(position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant == " " then return "eʔ" end
    end)
text = rsubn(text, "i([h\'])()", function(semivowel, position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant:find(consonants) then return "e" end
    end)
text = rsubn(text, "ī([h'\''])()", function(semivowel, position)
        local consonant = mw.ustring.sub(text, position, position)
        if consonant == "" or consonant:find(consonants) then return "e" end
    end)

    return text
end

function export.fa_cls_IPA(text, do_debug)
    text = rsubn(text, "[-.]", " ")
    text = rsubn(text, "v", "w")
    -- Replace xwa with xʷa
    text = rsubn(text, "^xwa", "xʷa")
    -- Replace xwā with xā
    text = rsubn(text, "^xwā", "xʷā")
    -- Replace short vowels
    text = rsubn(text, ".", classical_persian_short_vowels)
    -- Replace long vowels
    text = rsubn(text, ".", classical_persian_long_vowels)
    -- Replace jj with dj
    text = rsubn(text, "jj", "dj")
    -- Replace čč with tč
    text = rsubn(text, "čč", "tč")
    -- Replace consonants
    text = rsubn(text, ".", common_consonants)
    text = rsubn(text, ".", classical_persian_consonants)

    return text
end

return export