Module:Pronunciation

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

Documentation for this module may be created at Module:Pronunciation/doc

-- <nowiki>
local p = {}

local arr = require("Module:Array")
local data = mw.loadData("Module:Pronunciation/data")

function p.pronounce_word(word)
	local ipa = data[string.lower(word)]
	if ipa ~= nil then
		return ipa[1]
	else
		return nil
	end
end

function p.respell_word(word)
	local respell = data[string.lower(word)]
	if respell ~= nil then
		return respell[2]
	else
		return nil
	end
end

function p.delimiter(_type, where)
	where = where or "start"
	_type = _type or "broad"
	return ({
		broad = { start = "/", ["end"] = "/" },
		narrow = { start = "[", ["end"] = "]" }
	})[_type][where]
end

function p.pronounce_main(words, _type)
	_type = _type or "broad"
	local pn = {}
	for s in string.gmatch(words, "%S+") do
		local ipa = p.pronounce_word(s)
		if ipa then
			table.insert(pn, ipa)
		end
	end
	if #pn == 0 then
		return nil
	end
	return p.delimiter(_type, "start") .. 
		table.concat(pn, " ") .. 
		p.delimiter(_type, "end")
end

function p.pronounce(frame)
	local args = frame:getParent().args
--	return table.concat(frame, "<br>")
	local _type = "broad"
	local r = arr.find_index(args, function(elem) return elem == "narrow" end, nil)
	if r ~= nil then
		_type = "narrow"
		table.remove(args, r)
	end
	return p.pronounce_main(args[1], _type)
end

function p.respell_main(words)
	local pn = {}
	for s in string.gmatch(words, "%S+") do
		local ipa = p.respell_word(s)
		if ipa then
			table.insert(pn, ipa)
		end
	end
	if #pn > 0 then
		return table.concat(pn, " ")
	end
end

function p.respell(frame)
	local args = frame:getParent().args
--	return table.concat(frame, "<br>")
	return p.respell_main(args[1])
end

return p
-- </nowiki>