Jump to content

Module:zh/data/yue-word

From Wiktionary, the free dictionary
local export = {}

function export.get_all()
	local ret = {}
	for i=0,196 do --196 is the limit
		success, data = pcall(mw.loadData,('Module:zh/data/yue-word/%03d'):format(i))
		if success then
			for key,val in pairs(data) do
				ret[key] = {}
				val = mw.ustring.gsub(val,", ",",")
				for pron in mw.text.gsplit(val,",",true) do
					pron = mw.ustring.gsub(pron,",",", ")
					table.insert(ret[key],pron)
				end
			end
		end
	end
	return ret
end

function export.get_all_reverse()
	local ret = {}
	for i=0,196 do --196 is the limit
		success, data = pcall(mw.loadData,('Module:zh/data/Jyutping_word/%03d'):format(i))
		if success then
			for key,val in pairs(data) do
				val = mw.ustring.gsub(val,", ",",")
				for pron in mw.text.gsplit(val,",",true) do
					pron = mw.ustring.gsub(pron,",",", ")
					if ret[pron] then
						table.insert(ret[pron],key)
					else
						ret[pron] = {key}
					end
				end
			end
		end
	end
	return ret
end

function export.get_homophones()
	local ret = export.get_all_reverse()
	for key,val in pairs(ret) do
		if #val == 1 then
			ret[key] = nil
		end
	end
	return ret
end

return export