Module:Deranize: Difference between revisions

rest of the owl
m (grammar)
(rest of the owl)
Line 1: Line 1:
-- neither mw.ustring.toNFD nor mw.ustring.match works properly, so resorting to this for the time being (lol)
-- Converts Latin text to Derani.
local detone = {
-- Supports: syllable splitting, tone, prefixes, hiatus/diphthong marks, punctuation.
  ["a"] = {"a", 1},
-- For cartouches and null-raı, try: deranize("Ruaq sá [poq] sá* da.")
  ["u"] = {"u", 1},
 
  ["ı"] = {"ı", 1},
u = mw.ustring
  ["o"] = {"o", 1},
 
  ["e"] = {"e", 1},
function get_tone(word)
  ["á"] = {"a", 2},
   local nfd = u.toNFD(word)
   ["ú"] = {"u", 2},
   if u.find(nfd, "́") then return 2 end
   ["í"] = {"ı", 2},
   if u.find(nfd, "̈") then return 3 end
  ["ó"] = {"o", 2},
   if u.find(nfd, "̂") then return 4 end
   ["é"] = {"e", 2},
   return 1
  ["ä"] = {"a", 3},
end
   ["ü"] = {"u", 3},
 
  ["ï"] = {"ı", 3},
function strip_tone(word)
   ["ö"] = {"o", 3},
   return u.gsub(u.gsub(u.toNFD(word), "[́̈̂]", ""), "i", "ı")
  ["ë"] = {"e", 3},
end
  ["â"] = {"a", 4},
   ["û"] = {"u", 4},
  ["î"] = {"ı", 4},
  ["ô"] = {"o", 4},
  ["ê"] = {"e", 4},
}


local deranimap = {
local deranimap = {
Line 55: Line 49:
   ["e"]  = "󱚴", -- DERANI LETTER FOFUAQ
   ["e"]  = "󱚴", -- DERANI LETTER FOFUAQ
   -- tone marks
   -- tone marks
  [1]    = "",
   [2]    = "󱛊", -- DERANI COMBINING RISING TONE
   [2]    = "󱛊", -- DERANI COMBINING RISING TONE
   [3]    = "󱛋", -- DERANI COMBINING LOW GLOTTAL TONE
   [3]    = "󱛋", -- DERANI COMBINING LOW GLOTTAL TONE
   [4]    = "󱛌", -- DERANI COMBINING RISING-FALLING TONE
   [4]    = "󱛌", -- DERANI COMBINING RISING-FALLING TONE
   -- TODO: the rest of the owl
   -- prefix
  ["-"] = "󱛒",
  ["hiatus"] = "󱛍",
  ["diphthong"] = "󱛎",
  [""] = "",
}
 
local derani_punctuation = {
  ["\""] = "󱛓",
  [","] = " 󱛔",
  ["."] = " 󱛕",
  ["!"] = " 󱛖",
  ["?"] = " 󱛗",
  ["["] = "󱛘",
  ["]"] = "󱛙",
  ["*"] = " 󱛚",
  ["_"] = "󱛛",
}
}


function deranize(frame)
function deranize_word(word)
  assert(frame.args[1] ~= nil and frame.args[2] == nil, "This function requires exactly one argument")
  local normalized = mw.ustring.gsub(mw.ustring.lower(frame.args[1]), "i", "ı")
  mw.log(normalized)
   local res = {}
   local res = {}
   local ix = 1
   local ix = 1
   local len = mw.ustring.len(normalized)
  local tone = get_tone(word)
   local len = mw.ustring.len(word)
   -- NB. this is not PCRE regex, see https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
   -- NB. this is not PCRE regex, see https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
  --              ↓ initpos (these ones eval to an index)                    precoda ↓        ↓ postcoda
   local toaqre = "()([mbpfndtzcsrljꝡgk']?h?)([auıoe])([̣]?)([́̈̂̀]?)([auıoe]?[auıoe]?)()([qm]?)([-·]?)()([auıoe]?)"
  --                ↓ initial              ↓ medial                ↓ final            ↓ coda  ↓ lingeringvowel
  local is_first_syllable = true
   local toaqre = "()([mbpfndtzcsrljꝡgk']?h?)([auıoeáúíóéäüïöëâûîôê])([auıoe]?[auıoe]?)()([qm]?)()([auıoe]?)"
   while ix <= len do
   while ix < len do
     local shouldbreak = false
     local shouldbreak = false
     local initpos, initial, medial, final, precoda, coda, postcoda, lingeringvowel = mw.ustring.match(normalized, toaqre, ix)
     local pos_init, initial, medial, underdot, diacritic, final, pos_precoda, coda, hyphen, pos_postcoda, lingering_vowel = mw.ustring.match(word, toaqre, ix)
     if initpos == nil then
     if pos_init == nil then
       shouldbreak = true
       break
      initpos = nil
     end
     end
    mw.log(table.concat({ix, initpos, initial, medial, final, precoda, coda, postcoda, lingeringvowel}, ", "))
     res[#res+1] = u.toNFC(u.sub(word, ix, pos_init - 1))
     res[#res+1] = mw.ustring.toNFC(mw.ustring.sub(normalized, ix, initpos - 1))
     if coda == "m" and lingering_vowel ~= "" then
    if shouldbreak then break end
     if coda == "m" and lingeringvowel ~= "" then
       coda = ""
       coda = ""
       ix = precoda
       ix = pos_precoda
     else
     else
       ix = postcoda
       ix = pos_postcoda
    end
 
    local glyphs = {initial, tone, medial}
    if initial == "" and is_first_syllable then glyphs[1] = "'" end
    if not is_first_syllable then glyphs[2] = "" end
    if glyphs[1] == "'" or glyphs[1] == "ꝡ" then
      -- Move tone onto the first vowel
      glyphs[2], glyphs[3] = glyphs[3], glyphs[2]
    end
    local v = medial..final
    if v == "aı" or v == "ao" or v == "eı" or v == "oı" then
      glyphs[#glyphs+1] = "diphthong"
    elseif final ~= "" then
      glyphs[#glyphs+1] = "hiatus"
    end
    for _, fin in ipairs(mw.text.split(final, "")) do
      glyphs[#glyphs+1] = fin
     end
     end
    local nucleus, tone = unpack(detone[medial])
    local glyphs = {initial, nucleus, tone}
     if coda ~= "" then
     if coda ~= "" then
       glyphs[#glyphs+1] = coda .. "_"
       glyphs[#glyphs+1] = coda .. "_"
     end
     end
     for _, fin in ipairs(mw.text.split(final, "")) do
     if underdot ~= "" or hyphen ~= "" then
       glyphs[#glyphs+1] = fin
       glyphs[#glyphs+1] = "-"
     end
     end
     for _, glyph in ipairs(glyphs) do
     for _, glyph in ipairs(glyphs) do
       local mapped = deranimap[glyph]
       res[#res+1] = deranimap[glyph]
      if mapped then
      res[#res+1] = mapped
      end
     end
     end
     -- TODO: actually implement derani hiatus symbol, o'aomo, etc. insertion logic here 🤪
     is_first_syllable = false
   end
   end
  res[#res+1] = u.toNFC(u.sub(word, ix, len))
   return table.concat(res)
   return table.concat(res)
end
function deranize(frame)
  assert(frame.args[1] ~= nil and frame.args[2] == nil, "This function requires exactly one argument")
  local text = u.gsub(u.lower(u.toNFD(frame.args[1])), "i", "ı")
  local converted = u.gsub(text, "(%S+)", deranize_word)
  for k, v in pairs(derani_punctuation) do
    converted = u.gsub(converted, "%" .. k, v)
  end
  return converted
end
end


return {deranize = deranize}
return {deranize = deranize}