Module:Pronunciation

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Pronunciation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Pronunciation/doc. [edit]
Module:Pronunciation requires Module:Array.
Module:Pronunciation loads data from Module:Pronunciation/data.
Function list
L 7 — p.pronounce_word
L 16 — p.respell_word
L 25 — p.delimiter
L 34 — p.pronounce_main
L 51 — p.pronounce
L 63 — p.respell_main
L 76 — p.respell

-- <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>