Module:qhv-pron: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
No edit summary
Line 39: Line 39:
syllables = split(term, "·")
syllables = split(term, "·")
for i, syllable in ipairs(syllables) do
for i, syllable in ipairs(syllables) do
if match(syllable, "'") then
syllable = gsub(syllable, "'", "ˈ")
return table.concat(syllable)
end
weight[i] = match(syllable, "[aeiouy" .. NONSYLLABIC .. "]$") and "l" or "h"
weight[i] = match(syllable, "[aeiouy" .. NONSYLLABIC .. "]$") and "l" or "h"
end
end
Line 49: Line 53:
else table.insert(syllables, #syllables-1, "ˈ") end
else table.insert(syllables, #syllables-1, "ˈ") end
return table.concat(weight)
return table.concat(syllable)
end
end



Revision as of 20:08, 7 February 2021


This module generates IPA pronunciation for High Valyrian words. Backend to {{qhv-IPA}}.
local sub = mw.ustring.sub
local find = mw.ustring.find
local gmatch = mw.ustring.gmatch
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local u = mw.ustring.char
local split = mw.text.split
local gsplit = mw.text.gsplit

local export = {}

local velar = "kɡxɣ"
local uvular = "q"
local consonants = "[mpbvwθntdszlrɾɲŋɴʎhɥjɟṛ" .. velar .. uvular .. "]"
local NONSYLLABIC = u(0x032F) -- non-syllabic, combining inverted breve below
local DIPHTHONG = u(0x035C) -- double articulation, combining double breve below
local vowels = "[aeiouyː" .. NONSYLLABIC .. DIPHTHONG .. "]"

local rules = {
	{"rh", "ṛ"}, {"th", "θ"}, {"lj", "ʎ"}, {"ñ", "ɲ"}, {"kh", "x"}, {"[gɡ]h", "ɣ"}, {"g", "ɡ"},
	
	{"([aā][eo])", "%1" .. NONSYLLABIC}, {"([iu])([aāeēoō])", "%1" .. DIPHTHONG .. "%2"}, 
	{"ā", "aː"}, {"ē", "eː"}, {"ī", "iː"}, {"ō", "oː"}, {"ū", "uː"}, {"ȳ", "yː"},
	
	{"n([" .. velar .. "])", "ŋ%1"}, {"n([" .. uvular .. "])", "ɴ%1"}, {"(" .. vowels .. ")r(" .. vowels .. ")", "%1ɾ%2"}, 
}

local function write_stress(term)
	local pattern = "(" .. consonants .. "*".. vowels .. "*" .. consonants .. "-)"
	local syllables, weight = {}, {}
	term = gsub(term, pattern, "·%1")
	term = gsub(term, "^·", "")
	term = gsub(term, "·(" .. consonants .. ")·", "·%1")
	term = gsub(term, "·(" .. consonants .. ")$", "%1")
	term = gsub(term, "·(" .. consonants .. ")(" .. consonants .. ")", "%1·%2")  
	term = gsub(term, "([ptkbdɡ])·([rlṛsz])", "·%1%2")
	term = gsub(term, "·$", "")
	
	syllables = split(term, "·")
	for i, syllable in ipairs(syllables) do
		if match(syllable, "'") then
			syllable = gsub(syllable, "'", "ˈ")
			return table.concat(syllable)
		end	
		weight[i] = match(syllable, "[aeiouy" .. NONSYLLABIC .. "]$") and "l" or "h"
	end
	
	local a, p = weight[#weight-2], weight[#weight-1]
	
	if p == "l" and a == "h" then table.insert(syllables, #syllables-2, "ˈ")
	elseif (p == "l" and a == "l") or (p == "h") or (a == nil) then table.insert(syllables, #syllables-1, "ˈ")
	elseif p == nil then table.insert(syllables, #syllables, "ˈ")
	else table.insert(syllables, #syllables-1, "ˈ") end
	
	return table.concat(syllable)
end

function export.crux(term)
	term = mw.ustring.lower(term)
	
	for _, rule in ipairs(rules) do
		term = gsub(term, rule[1], rule[2])
	end
	
	return write_stress(term)
end	

function export.antique_crux(term)
	local oldrules = {
	{"v([ou])", "w%1"}, {"v([ay])", "ɥ%1"}, {"(" .. vowels .. ")v(" .. consonants .. ")", "u"},
	{"j([iy])", "ɟ%1"},
	}
	
	term = export.crux(term)
	
	for _, oldrule in ipairs(oldrules) do
		term = gsub(term, oldrule[1], oldrule[2])	
	end
	
	return term
end

local function IPA_span(items)
	local bits = {}
	for _, item in ipairs(items) do
		local bit = "<span style=\"font-size:110%;font-family:Gentium,'DejaVu Sans','Segoe UI',sans-serif>" .. item.pron .. "</span>"
		table.insert(bits, bit)
	end
	return table.concat(bits)
end

local function format_IPA(items)
	return "[[w:IPA chart|IPA]]<sup>([[IPA for High Valyrian|key]])</sup>:&#32;" .. IPA_span(items)
end

function line_format(pronunciation, register)
	local full_pronunciations = {}
	local IPA_args = {{pron = '[' .. pronunciation .. ']'}}
	table.insert(full_pronunciations, format_IPA(IPA_args))
	return "(''" .. table.concat(register, ", ") .. "'')" .. ' ' .. table.concat(full_pronunciations, ' or ')
end

function separate_word(term, b)
	local result = {}
	
	for word in gsplit(term, " ") do
		if b then table.insert(result, export.antique_crux(word))
		else table.insert(result, export.crux(word)) end
	end
	
	return table.concat(result, " ")
end

function export.show(frame)
	local params = {
		[1] = { default = mw.title.getCurrentTitle().nsText == 'Template' and "drakarys" or mw.title.getCurrentTitle().text },
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local term = args[1]
	
	local ipa = "* "
	ipa = ipa .. line_format(separate_word(term, false), {'Modern'})
	if export.crux(term) ~= export.antique_crux(term) then
		ipa = ipa .. "\n* " .. line_format(separate_word(term, true), {'Antique'})
	end	
	
	ipa = gsub(ipa, "ṛ", "r̥")
	
	return ipa
end	

return export